Button Group

Groups a set of related buttons together with a shared border.
Installation
Expand

npx @left4code/cosmic-ui-cli@latest add button-group
 
Expand

This writes the component and everything it imports, then installs the packages it needs. Run npx @left4code/cosmic-ui-cli@latest initfirst if you haven't already.

No state machine here - it's a plain layout wrapper with a shared border and dividers between items (divide-x), not the full Button component with its own Frame border underneath, which would clash with a shared outer border. Or copy and paste the code below by hand.

components/ui/button-group.tsx
Expand

import { twMerge } from "tailwind-merge";

function ButtonGroup({
  children,
  className,
}: React.PropsWithChildren<{ className?: string }>) {
  return (
    <div
      role="group"
      className={twMerge(["inline-flex divide-x divide-primary/30 border border-primary/30", className])}
    >
      {children}
    </div>
  );
}

function ButtonGroupItem({
  children,
  className,
  ...rest
}: React.ComponentProps<"button">) {
  return (
    <button
      {...rest}
      className={twMerge([
        "px-4 py-2 hover:bg-primary/10 disabled:opacity-30 disabled:pointer-events-none",
        className,
      ])}
    >
      {children}
    </button>
  );
}

export { ButtonGroup, ButtonGroupItem };
 
Expand

Update the import paths to match your project setup.

Usage
Expand

import { ButtonGroup, ButtonGroupItem } from "@/components/ui/button-group";
 
Expand
Expand

<ButtonGroup>
  <ButtonGroupItem>Day</ButtonGroupItem>
  <ButtonGroupItem>Week</ButtonGroupItem>
  <ButtonGroupItem>Month</ButtonGroupItem>
</ButtonGroup>
 
Expand
Powered by synthetic caffeine · Deployed by Left4code · Signal traceable on GitHub.