|
| 1 | +import { mergeProps } from "@base-ui/react/merge-props"; |
| 2 | +import { useRender } from "@base-ui/react/use-render"; |
| 3 | +import { cva, type VariantProps } from "class-variance-authority"; |
| 4 | +import { Separator } from "@/components/ui/separator"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | + |
| 7 | +const buttonGroupVariants = cva( |
| 8 | + "has-[>[data-slot=button-group]]:gap-2 has-[select[aria-hidden=true]:last-child]:[&>[data-slot=select-trigger]:last-of-type]:rounded-r-md flex w-fit items-stretch *:focus-visible:z-10 *:focus-visible:relative [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1", |
| 9 | + { |
| 10 | + variants: { |
| 11 | + orientation: { |
| 12 | + horizontal: |
| 13 | + "[&>[data-slot]:not(:has(~[data-slot]))]:rounded-r-md! [&>[data-slot]~[data-slot]]:rounded-l-none [&>[data-slot]~[data-slot]]:border-l-0 *:data-slot:rounded-r-none", |
| 14 | + vertical: |
| 15 | + "[&>[data-slot]:not(:has(~[data-slot]))]:rounded-b-md! flex-col [&>[data-slot]~[data-slot]]:rounded-t-none [&>[data-slot]~[data-slot]]:border-t-0 *:data-slot:rounded-b-none", |
| 16 | + }, |
| 17 | + }, |
| 18 | + defaultVariants: { |
| 19 | + orientation: "horizontal", |
| 20 | + }, |
| 21 | + }, |
| 22 | +); |
| 23 | + |
| 24 | +function ButtonGroup({ |
| 25 | + className, |
| 26 | + orientation, |
| 27 | + ...props |
| 28 | +}: React.ComponentProps<"div"> & VariantProps<typeof buttonGroupVariants>) { |
| 29 | + return ( |
| 30 | + <div |
| 31 | + role="group" |
| 32 | + data-slot="button-group" |
| 33 | + data-orientation={orientation} |
| 34 | + className={cn(buttonGroupVariants({ orientation }), className)} |
| 35 | + {...props} |
| 36 | + /> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function ButtonGroupText({ |
| 41 | + className, |
| 42 | + render, |
| 43 | + ...props |
| 44 | +}: useRender.ComponentProps<"div">) { |
| 45 | + return useRender({ |
| 46 | + defaultTagName: "div", |
| 47 | + props: mergeProps<"div">( |
| 48 | + { |
| 49 | + className: cn( |
| 50 | + "bg-muted gap-2 rounded-md border px-2.5 text-sm font-medium shadow-xs [&_svg:not([class*='size-'])]:size-4 flex items-center [&_svg]:pointer-events-none", |
| 51 | + className, |
| 52 | + ), |
| 53 | + }, |
| 54 | + props, |
| 55 | + ), |
| 56 | + render, |
| 57 | + state: { |
| 58 | + slot: "button-group-text", |
| 59 | + }, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function ButtonGroupSeparator({ |
| 64 | + className, |
| 65 | + orientation = "vertical", |
| 66 | + ...props |
| 67 | +}: React.ComponentProps<typeof Separator>) { |
| 68 | + return ( |
| 69 | + <Separator |
| 70 | + data-slot="button-group-separator" |
| 71 | + orientation={orientation} |
| 72 | + className={cn( |
| 73 | + "bg-input relative self-stretch data-horizontal:mx-px data-horizontal:w-auto data-vertical:my-px data-vertical:h-auto", |
| 74 | + className, |
| 75 | + )} |
| 76 | + {...props} |
| 77 | + /> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +export { |
| 82 | + ButtonGroup, |
| 83 | + ButtonGroupSeparator, |
| 84 | + ButtonGroupText, |
| 85 | + buttonGroupVariants, |
| 86 | +}; |
0 commit comments