import type { ReactNode } from "react"; import React, { forwardRef } from "react"; import { useMediaQuery } from "usehooks-ts"; import { cn } from "@/libs/utils"; import { Button } from "../button/button"; export interface ButtonGroupProps extends React.HTMLAttributes { children: ReactNode; } const ButtonGroup = forwardRef( ({ className, children, ...props }, ref) => { const Comp = "div"; const isDesktop = useMediaQuery("(min-width: 768px)"); return ( {React.Children.map(children, (child, index) => { if (React.isValidElement(child) && child.type === Button) { let conditionalClass = ""; if (isDesktop) { if (index === 0) { conditionalClass = "!rounded-tr-none !rounded-br-none !border-0 !border-r"; } else if (index === React.Children.count(children) - 1) { conditionalClass = "!rounded-tl-none !rounded-bl-none !border-0"; } else { conditionalClass = "!rounded-none !border-y-0 !border-l-0 !border-r"; } } // I passed 'any' as type, because I don't know how to type this return React.cloneElement(child, { key: child.key, className: conditionalClass, }); } // eslint-disable-next-line no-console console.warn("ButtonGroup only accepts Button as children"); return null; })} ); }, ); ButtonGroup.displayName = "ButtonGroup"; export { ButtonGroup };