Button Group
Joins related buttons into one control. Use it for pagers, steppers, split actions, and toolbars.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/button-group.jsonCopies Button Group and the components it uses (Separator) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/button-group. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Separator.
Copy and paste the following code into your project.
'use client'; import type { ComponentProps } from 'react'; import { mergeProps } from '@base-ui/react/merge-props'; import { useRender } from '@base-ui/react/use-render'; import { cn } from '@/lib/sprout/classes'; import { Separator, type SeparatorLineProps } from '@/components/ui/separator'; export const buttonGroupOrientationNames = ['horizontal', 'vertical'] as const; export type ButtonGroupOrientation = (typeof buttonGroupOrientationNames)[number]; export interface ButtonGroupVariantOptions { orientation?: ButtonGroupOrientation | null; className?: string; } export function buttonGroupVariants({ orientation, className }: ButtonGroupVariantOptions = {}) { return cn('sprout-button-group', `sprout-button-group-${orientation ?? 'horizontal'}`, className); } export interface ButtonGroupProps extends ComponentProps<'div'> { orientation?: ButtonGroupOrientation | null; } export function ButtonGroup({ className, orientation, ...props }: ButtonGroupProps) { return ( <div role="group" data-slot="button-group" data-orientation={orientation ?? 'horizontal'} className={buttonGroupVariants({ orientation, className })} {...props} /> ); } export type ButtonGroupTextProps = useRender.ComponentProps<'div'>; export function ButtonGroupText({ className, render, ref, ...props }: ButtonGroupTextProps) { return useRender({ defaultTagName: 'div', ref, render, props: mergeProps<'div'>( { 'data-slot': 'button-group-text', className: cn('sprout-button-group-text', className), } as ComponentProps<'div'>, props, ), }); } export type ButtonGroupSeparatorProps = SeparatorLineProps; export function ButtonGroupSeparator({ className, orientation = 'vertical', tone = 'input', ...props }: ButtonGroupSeparatorProps) { return ( <Separator data-slot="button-group-separator" orientation={orientation} tone={tone} className={state => cn('sprout-button-group-separator', typeof className === 'function' ? className(state) : className) } {...props} /> ); }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-button-group { align-items: stretch; width: fit-content; max-width: 100%; display: flex; } .sprout-button-group > :focus-visible { z-index: 1; position: relative; } .sprout-button-group:has( > [data-slot="button-group"]) { gap: 8px; } .sprout-button-group > input { flex: 1 1 0; min-width: 0; } .sprout-button-group.sprout-button-group-vertical { flex-direction: column; } .sprout-button-group.sprout-button-group-horizontal > [data-slot] { border-start-end-radius: 0; border-end-end-radius: 0; } .sprout-button-group.sprout-button-group-horizontal > [data-slot]:not(:has( ~ [data-slot])) { border-start-end-radius: var(--sprout-radius-control); border-end-end-radius: var(--sprout-radius-control); } .sprout-button-group.sprout-button-group-horizontal > [data-slot] ~ [data-slot] { border-inline-start-width: 0; border-start-start-radius: 0; border-end-start-radius: 0; } .sprout-button-group.sprout-button-group-vertical > [data-slot] { border-end-end-radius: 0; border-end-start-radius: 0; } .sprout-button-group.sprout-button-group-vertical > [data-slot]:not(:has( ~ [data-slot])) { border-end-end-radius: var(--sprout-radius-control); border-end-start-radius: var(--sprout-radius-control); } .sprout-button-group.sprout-button-group-vertical > [data-slot] ~ [data-slot] { border-block-start-width: 0; border-start-start-radius: 0; border-start-end-radius: 0; } .sprout-button-group-text { box-sizing: border-box; min-height: var(--sprout-control-height); border: 1px solid var(--sprout-input); border-radius: var(--sprout-radius-control); background: var(--sprout-secondary); color: var(--sprout-text); font-family: inherit; font-size: var(--sprout-text-sm); white-space: nowrap; align-items: center; gap: 8px; padding-inline: 10px; font-weight: 500; line-height: 20px; display: flex; } .sprout-button-group-text svg { pointer-events: none; flex-shrink: 0; } .sprout-button-group-text svg:not([class*="size-"]) { width: 1rem; height: 1rem; } .sprout-button-group-separator { align-self: stretch; position: relative; } .sprout-separator.sprout-button-group-separator[data-orientation="horizontal"] { width: auto; margin-inline: 1px; } .sprout-separator.sprout-button-group-separator[data-orientation="vertical"] { height: auto; min-height: 0; margin-block: 1px; } @media (forced-colors: active) { .sprout-button-group-text { border-color: canvastext; } } @media (prefers-reduced-motion: no-preference) { .sprout-button-group-text { transition: background-color var(--sprout-motion-fast) var(--sprout-motion-ease), border-color var(--sprout-motion-fast) var(--sprout-motion-ease), color var(--sprout-motion-fast) var(--sprout-motion-ease); } } }Import it from
@/components/ui/button-group. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/button-group.
Usage
import { Button } from "@tulipfarm/sprout/button";
import {
ButtonGroup,
ButtonGroupSeparator,
ButtonGroupText,
} from "@tulipfarm/sprout/button-group";
<ButtonGroup aria-label="Pages">
<Button variant="outline" disabled={page === 1}>Previous</Button>
<ButtonGroupSeparator />
<Button variant="outline" disabled={page === 3}>Next</Button>
</ButtonGroup>
<ButtonGroup orientation="vertical" aria-label="Quantity">
<ButtonGroupText>Quantity: {quantity}</ButtonGroupText>
<Button variant="outline" aria-label="Add one"><Plus /></Button>
</ButtonGroup>ButtonGroup renders a div with role="group". orientation is "horizontal", the default, or"vertical". Direct children with a data-slot, such as Sprõut buttons, lose their inner corners and the border where they meet. The outer corners use --sprout-radius-control for every button size.
ButtonGroupText is a muted cell for a label or value. It acceptsrender, so it can be an output or alabel. Icons inside it are 16px unless they have a size class.
ButtonGroupSeparator is a Separator with the"input" tone. It runs across the group, so it is "vertical" by default and must be "horizontal" in a vertical group. Use it between filled buttons, which have no border of their own.
A group that holds other groups spaces them 8px apart. A directinput child grows to fill the free space. buttonGroupVariants({ orientation }) returns the same class names for other elements.
Accessibility
Give each group an aria-label that names what it controls. Every button stays in the tab order, and a focused button draws its ring above its neighbors. Icon-only buttons need their own aria-label. Render a changing value with render={<output />} so screen readers hear it.
Color changes follow the theme in 120ms and stop when reduced motion is requested. In forced colors mode text cells keep a visible border.
Theme tokens
Text cells use --sprout-secondary, --sprout-text, and --sprout-input. Separators use --sprout-input. Radius follows --sprout-radius-control.