Button
Compact actions, links, and toggles. Seven variants, sizes for common layouts, and Base UI focus and disabled behavior.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/button.jsonCopies Button and the components it uses (Spinner) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/button. 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: Spinner.
Copy and paste the following code into your project.
'use client'; import type { Button as BaseButton } from '@base-ui/react/button'; import { useId, type ReactElement } from 'react'; import { cn, stateClassName } from '@/lib/sprout/classes'; import { Pressable } from '@/lib/sprout/pressable'; import { Spinner } from '@/components/ui/spinner'; export const buttonVariantNames = [ 'default', 'outline', 'secondary', 'ghost', 'destructive', 'ghost-destructive', 'link', 'scope', ] as const; export const buttonSizeNames = ['md', 'sm', 'lg', 'icon', 'icon-xs', 'icon-sm', 'icon-lg'] as const; export const buttonShapeNames = ['default', 'pill'] as const; export type ButtonVariant = (typeof buttonVariantNames)[number]; export type ButtonSize = (typeof buttonSizeNames)[number]; export type ButtonShape = (typeof buttonShapeNames)[number]; export interface ButtonVariantOptions { variant?: ButtonVariant | null; size?: ButtonSize | null; shape?: ButtonShape | null; className?: string; } export function buttonVariants({ variant, size, shape, className }: ButtonVariantOptions = {}) { return cn( 'sprout-button', `sprout-button-variant-${variant ?? 'default'}`, `sprout-button-size-${size ?? 'md'}`, shape === 'pill' && 'sprout-button-shape-pill', className, ); } export type ButtonBlockerFix = | { kind: 'jump'; label: string; target: string } | { kind: 'link'; label: string; link: ReactElement } | { kind: 'action'; label: string; onClick: () => void }; export type ButtonState = | { kind: 'ready' } | { kind: 'busy' } | { kind: 'blocked'; reason: string; fix?: ButtonBlockerFix | undefined }; export interface ButtonProps extends BaseButton.Props { variant?: ButtonVariant | null; size?: ButtonSize | null; shape?: ButtonShape | null; state?: ButtonState | undefined; pendingLabel?: string | undefined; } export function Button({ state, pendingLabel, ...props }: ButtonProps) { return state ? <StatefulButton state={state} pendingLabel={pendingLabel} {...props} /> : <ButtonElement {...props} />; } function ButtonElement({ variant, size, shape, className, ...props }: Omit<ButtonProps, 'state' | 'pendingLabel'>) { return ( <Pressable data-slot="button" data-variant={variant ?? 'default'} data-size={size ?? 'md'} data-shape={shape === 'pill' ? 'pill' : undefined} {...props} className={stateClassName<BaseButton.State>(buttonVariants({ variant, size, shape }), className)} /> ); } function StatefulButton({ state, pendingLabel, className, children, ...props }: ButtonProps & { state: ButtonState }) { const action = useActionControl(state); const pending = state.kind === 'busy' && pendingLabel; return ( <div data-slot="button-action" data-state={state.kind} className={cn('sprout-button-action', className)}> <ButtonElement {...props} {...action.button} disabled={action.button.disabled || props.disabled}> {pending ? ( <> <Spinner aria-hidden="true" /> {pendingLabel} </> ) : ( children )} </ButtonElement> {action.reason} </div> ); } export interface ActionGroupItem { label: string; onClick: () => void; variant?: ButtonVariant | undefined; } export interface ActionGroupProps { state: ButtonState; actions: readonly ActionGroupItem[]; className?: string | undefined; } export function ActionGroup({ state, actions, className }: ActionGroupProps) { const action = useActionControl(state); return ( <div data-slot="action-group" data-state={state.kind} className={cn('sprout-action-group', className)}> <div className="sprout-action-group-row"> {actions.map(item => ( <Button key={item.label} variant={item.variant} onClick={item.onClick} {...action.button}> {item.label} </Button> ))} </div> {action.reason} </div> ); } const focusable = ':is(a[href], button, input, select, textarea, [tabindex]):not([disabled], [tabindex="-1"], [type="hidden"])'; export function jumpTo(target: string | Element) { const element = typeof target === 'string' ? document.getElementById(target) : target; if (!element) return; const control = element.matches(focusable) || element.hasAttribute('tabindex') ? element : element.querySelector(focusable); (control ?? element).scrollIntoView({ block: 'center' }); if (control instanceof HTMLElement) control.focus({ preventScroll: true }); } export function useActionControl(state: ButtonState) { const reasonId = useId(); return { button: { disabled: state.kind !== 'ready', focusableWhenDisabled: true, ...(state.kind === 'busy' && { 'aria-busy': true as const }), ...(state.kind === 'blocked' && { 'aria-describedby': reasonId }), }, reason: state.kind === 'blocked' ? ( <p data-slot="action-reason" className="sprout-action-reason"> <span id={reasonId}>{state.reason}</span> {state.fix && <FixButton fix={state.fix} />} </p> ) : null, }; } function FixButton({ fix }: { fix: ButtonBlockerFix }) { if (fix.kind === 'link') return ( <Button variant="link" size="sm" nativeButton={false} render={fix.link}> {fix.label} </Button> ); return ( <Button variant="link" size="sm" onClick={fix.kind === 'jump' ? () => jumpTo(fix.target) : fix.onClick}> {fix.label} </Button> ); }And the files it imports:
lib/sprout/pressable.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-button { --sprout-button-icon: 1rem; box-sizing: border-box; height: var(--sprout-control-height); color: var(--sprout-text); font-family: inherit; font-size: var(--sprout-text-sm); white-space: nowrap; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; background-clip: padding-box; border: 1px solid #0000; border-radius: 10px; outline: none; flex-shrink: 0; justify-content: center; align-items: center; gap: 6px; padding: 0 10px; font-weight: 500; line-height: 20px; text-decoration: none; display: inline-flex; } .sprout-button svg { pointer-events: none; flex-shrink: 0; } .sprout-button svg:not([class*="size-"]) { width: var(--sprout-button-icon); height: var(--sprout-button-icon); } .sprout-button:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-button:disabled, .sprout-button[data-disabled], .sprout-button[aria-disabled="true"] { pointer-events: none; opacity: .5; } .sprout-button[aria-busy="true"] { cursor: progress; } .sprout-button[aria-invalid="true"] { border-color: var(--sprout-danger); } .sprout-button-variant-default { background: var(--sprout-primary); color: var(--sprout-on-primary); } @media (hover: hover) { .sprout-button-variant-default:hover { background: var(--sprout-primary-hover); } .sprout-button-variant-outline:hover { background: var(--sprout-hover); } .sprout-button-variant-secondary:hover { background: var(--sprout-active); } .sprout-button-variant-ghost:hover { background: var(--sprout-hover); color: var(--sprout-text); } .sprout-button-variant-destructive:hover { background: color-mix(in srgb, var(--sprout-danger) 20%, transparent); } .sprout-button-variant-ghost-destructive:hover { background: color-mix(in srgb, var(--sprout-danger) 10%, transparent); } .sprout-button-variant-link:hover { text-decoration: underline; } .sprout-button-variant-scope:hover { background: color-mix(in srgb, var(--sprout-info) 16%, transparent); } } .sprout-button-variant-default:active { background: var(--sprout-primary-active); } .sprout-button-variant-outline { border-color: var(--sprout-input); background: var(--sprout-surface); } .sprout-button-variant-outline:active, .sprout-button-variant-outline[aria-expanded="true"] { background: var(--sprout-active); } .sprout-button-variant-secondary { background: var(--sprout-hover); } .sprout-button-variant-secondary:active, .sprout-button-variant-secondary[aria-expanded="true"] { background: var(--sprout-active); } .sprout-button-variant-ghost { color: var(--sprout-muted); background: none; } .sprout-button-variant-ghost[aria-expanded="true"], .sprout-button-variant-ghost[aria-pressed="true"] { background: var(--sprout-hover); color: var(--sprout-text); } .sprout-button-variant-ghost:active { background: var(--sprout-active); color: var(--sprout-text); } .sprout-button-variant-destructive { background: color-mix(in srgb, var(--sprout-danger) 10%, transparent); color: var(--sprout-danger); } .sprout-button-variant-destructive:active { background: color-mix(in srgb, var(--sprout-danger) 20%, transparent); } .sprout-button-variant-destructive:focus-visible { outline-color: color-mix(in srgb, var(--sprout-danger) 40%, transparent); } .sprout-button-variant-ghost-destructive { color: var(--sprout-danger); background: none; } .sprout-button-variant-ghost-destructive:active { background: color-mix(in srgb, var(--sprout-danger) 10%, transparent); } .sprout-button-variant-ghost-destructive:focus-visible { outline-color: var(--sprout-danger); } .sprout-button-variant-link { color: var(--sprout-primary); text-underline-offset: 4px; background: none; } .sprout-button-variant-scope { background: color-mix(in srgb, var(--sprout-info) 10%, transparent); color: var(--sprout-info); } .sprout-button-variant-scope:active, .sprout-button-variant-scope[aria-expanded="true"] { background: color-mix(in srgb, var(--sprout-info) 16%, transparent); } .sprout-button-size-md:has([data-icon="inline-start"]), .sprout-button-size-lg:has([data-icon="inline-start"]) { padding-inline-start: 8px; } .sprout-button-size-md:has([data-icon="inline-end"]), .sprout-button-size-lg:has([data-icon="inline-end"]) { padding-inline-end: 8px; } .sprout-button-size-sm { --sprout-button-icon: .875rem; height: calc(var(--sprout-control-height) - 4px); border-radius: min(var(--sprout-radius-control), 12px); font-size: var(--sprout-text-xs); gap: 4px; padding: 0 10px; line-height: 16px; } .sprout-button-size-sm:has([data-icon="inline-start"]) { padding-inline-start: 6px; } .sprout-button-size-sm:has([data-icon="inline-end"]) { padding-inline-end: 6px; } .sprout-button-size-lg { height: calc(var(--sprout-control-height) + 4px); } .sprout-button-size-icon, .sprout-button-size-icon-xs, .sprout-button-size-icon-sm, .sprout-button-size-icon-lg { padding: 0; } .sprout-button-size-icon { width: var(--sprout-control-height); height: var(--sprout-control-height); } .sprout-button-size-icon-xs { --sprout-button-icon: .75rem; border-radius: min(var(--sprout-radius-control), 10px); width: 24px; height: 24px; } .sprout-button-size-icon-sm { width: calc(var(--sprout-control-height) - 4px); height: calc(var(--sprout-control-height) - 4px); border-radius: min(var(--sprout-radius-control), 12px); } .sprout-button-size-icon-lg { width: calc(var(--sprout-control-height) + 4px); height: calc(var(--sprout-control-height) + 4px); } .sprout-button.sprout-button-shape-pill { border-radius: 9999px; } .sprout-button-shape-pill:is(.sprout-button-size-md, .sprout-button-size-lg) { padding-inline: 14px; } .sprout-button-shape-pill.sprout-button-size-sm { padding-inline: 12px; } .sprout-button-shape-pill:is(.sprout-button-size-md, .sprout-button-size-lg):has([data-icon="inline-start"]) { padding-inline-start: 10px; } .sprout-button-shape-pill:is(.sprout-button-size-md, .sprout-button-size-lg):has([data-icon="inline-end"]) { padding-inline-end: 10px; } .sprout-button-shape-pill.sprout-button-size-sm:has([data-icon="inline-start"]) { padding-inline-start: 8px; } .sprout-button-shape-pill.sprout-button-size-sm:has([data-icon="inline-end"]) { padding-inline-end: 8px; } [data-slot="button-group"] .sprout-button:not(.sprout-button-shape-pill) { border-radius: var(--sprout-radius-control); } .sprout-button-action, .sprout-action-group { min-width: 0; max-width: 100%; font-family: var(--sprout-font); justify-items: start; gap: .375rem; display: grid; } .sprout-button-action > :is(.sprout-button-size-md, .sprout-button-size-sm, .sprout-button-size-lg), .sprout-action-group-row > .sprout-button { max-width: 100%; height: auto; min-height: var(--sprout-control-height); white-space: normal; overflow-wrap: anywhere; text-align: center; padding-block: 3px; } .sprout-button-action > .sprout-button-size-sm { min-height: calc(var(--sprout-control-height) - 4px); } .sprout-button-action > .sprout-button-size-lg { min-height: calc(var(--sprout-control-height) + 4px); } .sprout-action-group-row { flex-wrap: wrap; align-items: center; gap: .5rem; min-width: 0; max-width: 100%; display: flex; } .sprout-button-action .sprout-action-reason, .sprout-action-group .sprout-action-reason { max-width: 100%; } @media (pointer: coarse) { .sprout-button, .sprout-button-action > .sprout-button, .sprout-action-group-row > .sprout-button { min-width: 2.75rem; min-height: 2.75rem; } } @media (prefers-reduced-motion: no-preference) { .sprout-button { transition: background-color var(--sprout-motion-fast) ease-out, border-color var(--sprout-motion-fast) ease-out, color var(--sprout-motion-fast) ease-out, box-shadow var(--sprout-motion-fast) ease-out, opacity var(--sprout-motion-fast) ease-out; } .sprout-form-footer-dirty, .sprout-action-reason, .sprout-form-footer > .sprout-alert { transition: opacity var(--sprout-motion-normal) var(--sprout-motion-ease); } @starting-style { .sprout-form-footer-dirty, .sprout-action-reason, .sprout-form-footer > .sprout-alert { opacity: 0; } } } .sprout-form { min-width: 0; } .sprout-form-section { border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-panel); background: var(--sprout-surface); min-width: 0; color: var(--sprout-text); font-family: var(--sprout-font); gap: 1rem; padding: 1rem; display: grid; } @media (width >= 40rem) { .sprout-form-section { padding: 1.25rem; } } .sprout-form-section-header { flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: .5rem 1rem; display: flex; } .sprout-form-section-text { overflow-wrap: anywhere; flex: 16rem; gap: .25rem; min-width: 0; display: grid; } .sprout-form-section-title { color: var(--sprout-text); font-size: var(--sprout-text-base); letter-spacing: normal; margin: 0; font-weight: 500; line-height: 1.5rem; } .sprout-form-section-description { color: var(--sprout-muted); font-size: var(--sprout-text-sm); margin: 0; line-height: 1.25rem; } .sprout-form-section-content { gap: 1.25rem; min-width: 0; display: grid; } .sprout-form-footer { min-width: 0; color: var(--sprout-text); font-family: var(--sprout-font); gap: .75rem; display: grid; } .sprout-form-footer[data-layout="bar"] { border-top: 1px solid var(--sprout-line); padding-top: 1rem; } .sprout-form-footer[data-layout="compact"] { padding-top: .75rem; } .sprout-form-footer-row { flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .5rem 1rem; min-width: 0; display: flex; } .sprout-form-footer[data-layout="compact"] .sprout-form-footer-row { column-gap: .25rem; } .sprout-form-footer-status { gap: .25rem; min-width: 0; display: grid; } .sprout-form-footer-actions { flex-wrap: wrap; align-items: center; gap: .5rem; min-width: 0; margin-left: auto; display: flex; } .sprout-form-footer-stack { gap: .5rem; display: grid; } .sprout-form-footer-wide { width: 100%; } .sprout-form-footer-dirty, .sprout-form-footer-note, .sprout-action-reason { font-size: var(--sprout-text-sm); overflow-wrap: anywhere; margin: 0; line-height: 1.25rem; } .sprout-form-footer-dirty { color: var(--sprout-text); align-items: center; gap: .5rem; display: flex; } .sprout-form-footer-dirty-dot { background: var(--sprout-warning); border-radius: 999px; flex-shrink: 0; width: .5rem; height: .5rem; } .sprout-form-footer-note { color: var(--sprout-muted); } .sprout-form-footer-sr-only { clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; width: 1px; height: 1px; margin: -1px; padding: 0; position: absolute; overflow: hidden; } .sprout-action-reason { color: var(--sprout-muted); flex-wrap: wrap; align-items: center; column-gap: .25rem; display: flex; } }Import it from
@/components/ui/button. 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.
Usage
import { Button } from "@tulipfarm/sprout/button";
<Button onClick={save}>Save</Button>
<Button variant="outline" size="sm">
<PlusIcon data-icon="inline-start" />
Add item
</Button>
<Button variant="ghost" size="icon-sm" aria-label="Delete file">
<TrashIcon />
</Button>Variants are default, outline, secondary, ghost, destructive, ghost-destructive, link, and scope. Sizes are sm (28px), md (32px, the default), lg (36px), and the square icon, icon-xs, icon-sm, and icon-lg. Give icon-only buttons an aria-label. Mark leading or trailing icons with data-icon="inline-start" or "inline-end" to tighten padding.
Pill shape
<Button variant="secondary" shape="pill" size="sm">Connect</Button>
<Button variant="ghost" shape="pill" size="icon-sm" aria-label="Add app">
<PlusIcon />
</Button>shape="pill" fully rounds the ends. It works with every variant and size. Text sizes get 4px more side padding, and icon sizes become circles. The focus ring follows the round edge. Leave out shape, or pass "default", for the usual corners. Inside a Button Group the joined edges stay square.
Links
<Button
variant="outline"
nativeButton={false}
render={<Link to="/files" />}
>
Open files
</Button>With nativeButton=, Button renders a real link. It keeps link semantics, so screen readers announce a link and Enter follows it. A disabled link gets aria-disabled, leaves the tab order, and ignores clicks.
States
<Button aria-invalid aria-describedby="owner-error">Assign</Button>
<Button
variant={selected ? "secondary" : "ghost"}
aria-pressed={selected}
onClick={toggle}
>
Board
</Button>Button styles disabled, aria-disabled, aria-busy, aria-invalid, aria-expanded, and aria-pressed. Your application owns the error text and the pressed state. For busy and blocked actions, pass state as shown below.
Action states
import { Button, type ButtonState } from "@tulipfarm/sprout/button";
const state: ButtonState = sending
? { kind: "busy" }
: !enabled
? {
kind: "blocked",
reason: "Turn on Enable execution first.",
fix: { kind: "jump", label: "Go to Enable execution", target: "enable-execution" },
}
: { kind: "ready" };
<Button state={state} pendingLabel="Activating…" onClick={activate}>
Activate saved instructions
</Button>Pass state when an action can be busy or blocked. Busy setsaria-busy and, with pendingLabel, swaps the label for a spinner and that text. Busy and blocked buttons are disabled but stay in the tab order, so keyboard users reach them and hear the reason through aria-describedby.
With state, Button renders a data-slot="button-action" wrapper with data-state that also holds the reason, andclassName styles that wrapper. Keep passing a state, even{ kind: "ready" }, for the button's lifetime. Changing its kind then keeps the same button element and its focus. Long labels wrap in default, sm, and lgsizes.
type ButtonBlockerFix =
| { kind: "jump"; label: string; target: string } // element id on this page
| { kind: "link"; label: string; link: ReactElement } // your router <Link>
| { kind: "action"; label: string; onClick: () => void };A jump fix calls jumpTo, which scrolls to the element and focuses it, or its first focusable child. A linkfix renders your link through render. An actionfix runs a callback, such as Try again. Leave fix out to show only the reason. useActionControl(state) returns the same button props and reason for controls you build yourself.
Action groups
import { ActionGroup } from "@tulipfarm/sprout/button";
<ActionGroup
state={reviewState}
actions={[
{ label: "Keep my draft", variant: "outline", onClick: keepDraft },
{ label: "Use current settings", variant: "outline", onClick: adoptCurrent },
]}
/>ActionGroup puts several actions under one state. Every button turns busy or blocked together, and one reason sits under the row.
Triggers and class-only styling
import { Menu } from "@base-ui/react/menu";
import { Button, buttonVariants } from "@tulipfarm/sprout/button";
<Menu.Trigger render={<Button variant="outline" size="sm" />}>
Sort
</Menu.Trigger>
<a className={buttonVariants({ variant: "ghost", size: "sm", shape: "pill" })} href={url}>
Open in calendar
</a>Pass Button to any Base UI trigger or close part through render. The trigger supplies its ARIA state and Button styles it. Use buttonVariants() when another component owns the element. Tailwind utilities passed throughclassName override Button's own styles.
Pattern sizes
Pattern sizes fit common layouts: back (icon-only below 640px when its label is hidden), onboarding, bud and bud-labeled floating launchers, chat-row, task-link, card-title, result-card, calendar-title, calendar, calendar-icon, nav (tree rows), column-header, column-header-end, table-link, and badge. buttonVariantNames and buttonSizeNames list every option.
Behavior and accessibility
Buttons default to type="button"; pass type="submit" or "reset", and form for an external submit. Tab reaches every enabled button. Enter and Space activate buttons; Enter follows links. Focus shows a 3px ring. Coarse pointers get 44px minimum targets. Hover and press transitions take 120ms and stop when the user prefers reduced motion.
Button renders data-slot="button", data-variant, and data-size, and data-shape="pill" for pills. Override data-slot when a wrapping component needs its own hook. Refs reach the rendered element.