Button Group
optics/button-groupFreeComponent
Groups buttons together with proper spacing and styling.
See it running
Open the demo on optics
optics.agusmayol.com.ar/components/button-groupInstall it
npx shadcn@latest add https://optics.agusmayol.com.ar/r/button-group.jsonSource
1import { mergeProps } from "@base-ui/react/merge-props";2import { useRender } from "@base-ui/react/use-render";3import { cva } from "class-variance-authority";4import { buttonVariants } from "@/registry/optics/button";5import { cn } from "@/lib/utils";6import { Separator } from "@/registry/optics/separator";78const buttonGroupVariants = cva(9 "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",10 {11 variants: {12 orientation: {13 horizontal:14 "[&>[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",15 vertical:16 "[&>[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",17 },18 },19 defaultVariants: {20 orientation: "horizontal",21 },22 },23);2425function ButtonGroup({ className = "", orientation = "horizontal", ...props }) {26 return (27 <div28 role="group"29 data-slot="button-group"30 data-orientation={orientation}31 className={cn(buttonGroupVariants({ orientation }), className)}32 {...props}33 />34 );35}3637function ButtonGroupText({38 className = "",39 render = undefined,40 variant,41 size,42 animation,43 ...props44}) {45 return useRender({46 defaultTagName: "div",47 props: mergeProps(48 {49 className: cn(50 "bg-muted gap-2 rounded-md border px-2.5 text-xs/relaxed font-medium [&_svg:not([class*='size-'])]:size-4 flex items-center [&_svg]:pointer-events-none",51 variant && buttonVariants({ variant, size, animation }),52 className,53 ),54 },55 props,56 ),57 render,58 state: {59 slot: "button-group-text",60 },61 });62}6364function ButtonGroupSeparator({65 className = "",66 orientation = "vertical",67 ...props68}) {69 return (70 <Separator71 data-slot="button-group-separator"72 orientation={orientation}73 className={cn(74 "bg-input relative self-stretch data-[orientation=horizontal]:mx-px data-[orientation=horizontal]:w-auto data-[orientation=vertical]:my-px data-[orientation=vertical]:h-auto",75 className,76 )}77 {...props}78 />79 );80}8182ButtonGroup.displayName = "ButtonGroup";83ButtonGroupText.displayName = "ButtonGroupText";84// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from optics
All 79 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | optics | Components | Free | 5 deps | |
| Alertalert | optics | Components | Free | 4 deps | |
| Alert Dialogalert-dialog | optics | Components | Free | 4 deps | |
| Aspect Ratioaspect-ratio | optics | Components | Free | 2 deps | |
| Auto Heightauto-height | optics | Components | Free | 3 deps | |
| Avataravatar | optics | Components | Free | 3 deps | |
| Badgebadge | optics | Components | Free | 4 deps | |
| Breadcrumbbreadcrumb | optics | Components | Free | 4 deps |
