UIPin
ComponentsIconsIllustrationsMotionTemplatesApps
Boards
UIPin

Free, open-source UI components, icons, illustrations, motion, templates and apps, in one search. Every item links to its source and licence.

UIPin is free. If it saved you time, you can keep it running:

Buy me a coffee
ComponentsIconsIllustrationsMotionTemplatesAppsLibrariesCompareNew this weekMCP for agentsBoardsPrivacy

Components, icons and artwork belong to their authors and are shown under their open-source licences.

HextaUI/Button group

Buttons
1 of 23
Compiling

Theme it

Every preview, live

Brand colour

Corner radius

Font

Recolours shadcn’s tokens, Tailwind’s accent colours and colours set in the code. Red, amber and green classes stay, since they usually mean error, warning or success.

23 versions·7 librariesCompare all button groups →

  • Button group
  • 8-bit Button Group
  • Button Group
  • 8-bit Button
  • Bg Animate Button
  • Cosmic Button
  • Family Button
  • Eldora UI
  • Button
  • Attract Button
  • Bell Button
  • Command Button
  • Gradient Button
  • Hold Button
  • Switch Button
  • Tear Button
  • V0 Button
  • Interactive Hover Button
  • Pulsating Button
  • Rainbow Button
  • Shimmer Button
  • Button
  • Toggle Group

Button group

Buttons joined into one control, with shared seams, separators, text addons, nesting and vertical stacks.

npx shadcn@latest add https://hextaui.com/r/button-group.json
Open in v0 Docs Repo
Library
HextaUI
License
MIT
Stack
React · Tailwind CSS · animated · base-ui
Packages
@base-ui/react@tabler/icons-reactclass-variance-authoritycnshadcntw-animate-css
Also adds
separator, button, spinner, use-delayed-loading, use-button-feedback
Size
137 lines
Demo
From HextaUI

Code

components/ui/button-group.tsx

"use client"

import * as React from "react"
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"

import { Separator, type SeparatorProps } from "@/components/ui/separator"

const buttonGroupVariants = cva(
  "group/button-group flex w-fit max-w-full min-w-0 items-stretch has-[>[data-slot=button-group]]:gap-2 [&>*:focus-within]:relative [&>*:focus-within]:z-10 [&>[aria-invalid=true]:not(:focus-within)]:relative [&>[aria-invalid=true]:not(:focus-within)]:z-1 [&>[data-slot=button]:active]:translate-none! [&>[data-slot=button]:active]:scale-none! [&>[data-slot=input]]:min-w-0 [&>[data-slot=input]]:flex-1 [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:min-w-0 [&>input]:flex-1",
  {
    variants: {
      orientation: {
        horizontal:
          "flex-row [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true]):has(~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true]))]:rounded-e-none [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])]:-ms-(--hairline) [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])]:rounded-s-none",
        vertical:
          "flex-col [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true]):has(~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true]))]:rounded-b-none [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])]:-mt-(--hairline) [&:not(:has(>[data-slot=button-group]))>:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])~:not([data-slot=button-status],[data-base-ui-focus-guard],span[aria-owns],input[aria-hidden=true],select[aria-hidden=true])]:rounded-t-none",
      },
    },
    defaultVariants: {
      orientation: "horizontal",
    },
  }
)

type ButtonGroupOrientation = NonNullable<
  VariantProps<typeof buttonGroupVariants>["orientation"]
>

type ButtonGroupProps = useRender.ComponentProps<"div"> & {
  orientation?: ButtonGroupOrientation
}

const ButtonGroupContext =
  React.createContext<ButtonGroupOrientation>("horizontal")

function dataAttributes(slot: string, extra?: Record<string, string>) {
  return { "data-slot": slot, ...extra } as Record<string, string>
}

function ButtonGroup({
  className,
  orientation = "horizontal",
  render,
  ...props
}: ButtonGroupProps) {
  const element = useRender({
    defaultTagName: "div",
    render,
    props: mergeProps<"div">(
      {
        role: "group",
        className: cn(buttonGroupVariants({ orientation }), className),
      },
      props,
      dataAttributes("button-group", { "data-orientation": orientation })
    ),
  })

  return (
    <ButtonGroupContext.Provider value={orientation}>
      {element}
    </ButtonGroupContext.Provider>
  )
}

function ButtonGroupText({
  className,
  render,
  ...props
}: useRender.ComponentProps<"div">) {
  return useRender({
    defaultTagName: "div",
    render,
    props: mergeProps<"div">(
      {
        className: cn(
          "flex max-w-full min-w-0 shrink-0 items-center gap-2 rounded-md bg-muted px-2.5 text-sm font-medium whitespace-nowrap inset-ring-(length:--hairline) inset-ring-border dark:inset-ring-input forced-colors:border [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground",
          className
        ),
      },
      props,
      dataAttributes("button-group-text")
    ),
  })
}

type ButtonGroupSeparatorProps = Omit<SeparatorProps, "align" | "children">

function mergeClassName<State>(
  base: string,
  className: string | ((state: State) => string | undefined) | undefined
) {
  return typeof className === "function"
    ? (state: State) => cn(base, className(state))
    : cn(base, className)
}

function ButtonGroupSeparator({
  className,
  orientation,
  ...props
}: ButtonGroupSeparatorProps) {
  const groupOrientation = React.useContext(ButtonGroupContext)
  const resolvedOrientation =
    orientation ??
    (groupOrientation === "horizontal" ? "vertical" : "horizontal")

  return (
    <Separator
      data-slot="button-group-separator"
      orientation={resolvedOrientation}
      className={mergeClassName(
        resolvedOrientation === "horizontal"
          ? "relative z-1 mx-px w-auto self-stretch bg-input"
          : "relative z-1 my-px bg-input",
        className
      )}
      {...props}
    />
  )
}

export {
  ButtonGroup,
  ButtonGroupSeparator,
  ButtonGroupText,
  buttonGroupVariants,
}
export type {
  ButtonGroupOrientation,
  ButtonGroupProps,
  ButtonGroupSeparatorProps,
}

More like this

Button Group

shadcn/ui·Buttons

Toolbar Expandable

Motion Primitives·Navigation

Carousel

HextaUI·Media & 3D

Menubar

shadcn/ui·Overlays

Toolbar Dynamic

Motion Primitives·Navigation

Toolbar

Kokonut UI·Navigation

Carousel

shadcn/ui·Media & 3D

Menubar

HextaUI·Overlays

Side Panel

Cult UI·Other

Marking Menu

Kokonut UI·Overlays

Border Trail

Motion Primitives·Cursor & effects

Safari

Magic UI·Media & 3D

Textarea

shadcn/ui·Text effects

Bell Button

Kokonut UI·Buttons

Input

shadcn/ui·Inputs & forms