Button

Runs an action, shows that it's working, and stays reachable when disabled.

Variants

Use primary for the one action a view is about, and quieter variants for the rest.

Preview
variants.tsx
import { Button } from "~/components/ui/button"

export default function Example() {
  return (
    <>
      <Button>Primary</Button>
      <Button variant="secondary">Secondary</Button>
      <Button variant="outline">Outline</Button>
      <Button variant="ghost">Ghost</Button>
      <Button variant="destructive">Delete</Button>
      <Button variant="link">Link</Button>
    </>
  )
}

Sizes

icon is square, for a button holding only an icon. Give it an aria-label.

Preview
sizes.tsx
import { Button } from "~/components/ui/button"

export default function Example() {
  return (
    <>
      <Button size="xs">Extra small</Button>
      <Button size="sm">Small</Button>
      <Button size="md">Medium</Button>
      <Button size="lg">Large</Button>
    </>
  )
}

Pending

Return a Promise from onClick and the button stays pending until it settles: it ignores further presses, keeps focus, and tells screen readers it’s busy after a second. Style the waiting state with the data-pending attribute, or Tailwind’s in-data-pending: variant inside the button.

Preview
Not in Shell yet
Shell doesn't have this example, or every component it uses.

Dependencies

What it's built from and what's built from it in Shell, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages None.

Installation

Set the project up for Shell once first, as Getting started shows. Add the components it uses, listed under Dependencies. Then copy the source:

src/components/ui/button.tsx
import { Text as NativeText, type JSX } from "nsh"
import { Button as NativeButton, type ButtonProps as NativeButtonProps } from "nsh/base"

import { textVariants, type TextSize } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/*
 * Base's button on nsh/base's Button, which owns the presses, the disabled
 * and busy states and the role, and leaves the look to us. nsh draws text
 * only inside a Text, so a label passed as a string gets one on the type
 * scale, in the variant's color.
 */

type ButtonVariant = "primary" | "secondary" | "outline" | "ghost" | "destructive" | "link"
type ButtonSize = "xs" | "sm" | "md" | "lg"

/** Each variant's box, and its label's color. */
const variants: Record<ButtonVariant, { box: string; label: string }> = {
  primary: {
    box: "border-transparent bg-primary hover:bg-primary/80",
    label: "text-primary-foreground",
  },
  secondary: {
    box: "border-transparent bg-secondary hover:bg-secondary/80",
    label: "text-secondary-foreground",
  },
  outline: {
    box: "border-border bg-background hover:bg-muted dark:bg-transparent dark:hover:bg-input/30",
    label: "text-foreground",
  },
  ghost: {
    box: "border-transparent hover:bg-muted dark:hover:bg-muted/50",
    label: "text-foreground",
  },
  destructive: {
    box: "border-transparent bg-destructive/10 hover:bg-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30",
    label: "text-destructive",
  },
  link: { box: "border-transparent", label: "text-primary" },
}

/** Each size's box, and its label's step on the type scale. */
const sizes: Record<ButtonSize, { box: string; label: TextSize }> = {
  xs: { box: "h-6 gap-(--space-2xs) px-(--space-md)", label: "xs" },
  sm: { box: "h-7 gap-(--space-2xs) px-(--space-md)", label: "sm" },
  md: { box: "h-8 gap-(--space-xs) px-(--space-md)", label: "sm" },
  lg: { box: "h-9 gap-(--space-xs) px-(--space-lg)", label: "sm" },
}

type ButtonProps = Omit<NativeButtonProps, "children"> & {
  /** How much the button stands out. `destructive` is a tint, for actions that can't be undone. */
  variant?: ButtonVariant
  /** Height, padding and label size. */
  size?: ButtonSize
  /** A label, set in the variant's color, or your own content. */
  children?: JSX.Element
}

/** Runs an action. */
function Button({
  variant = "primary",
  size = "md",
  class: className,
  children,
  ...props
}: ButtonProps) {
  const label =
    typeof children === "string" || typeof children === "number" ? (
      <NativeText
        class={cn(
          textVariants({ size: sizes[size].label, weight: "medium" }),
          variants[variant].label,
        )}
      >
        {children}
      </NativeText>
    ) : (
      children
    )
  return (
    <NativeButton
      class={cn(
        "shrink-0 flex-row items-center justify-center rounded-lg border",
        "focus-visible:border-ring active:translate-y-px disabled:opacity-50",
        variants[variant].box,
        sizes[size].box,
        className,
      )}
      {...props}
    >
      {label}
    </NativeButton>
  )
}

export { Button }
export type { ButtonProps, ButtonSize, ButtonVariant }

API reference

Generated from Shell's types. Props marked ours are added here; the rest come from the nsh/base part it wraps. Every part also takes nsh's view props, like class, style and testID.

Button

Runs an action.

childrenours

A label, set in the variant's color, or your own content.

Type Element

sizeours

Height, padding and label size.

Type ButtonSize · Default md

variantours

How much the button stands out. destructive is a tint, for actions that can't be undone.

Type ButtonVariant · Default primary

accessibilityHint

What activating the button does, for screen readers, as a view's accessibilityHint.

Type string | null

accessibilityLabel

What screen readers say, when its content is not enough (an icon).

Type string | null

busy

Busy: its action is running (a "Saving…" button), and screen readers say so. Presses still count; the app sets disabled as well to refuse them.

Type boolean

disabled

No presses, no press feedback; disabled: styles apply and screen readers say it is dimmed. A button in a disabled Toolbar is disabled too.

Type boolean

hitSlop

How far outside its bounds presses still count, as a view's hitSlop: one number for every side, or the sides named (each left out 0). A small target (an icon) takes presses a finger lands near it, with no change to its layout or look.

Type number | Partial<Edges>

onPress

The button was pressed.

Type (() => void)

onPressDown

A press went down on it (for the app's own press feedback).

Type (() => void)

onPressUp

The press ended: released, moved away or cancelled.

Type (() => void)

role

What it is for screen readers (default "button": a link, a tab, a menu item, ...).

Type AccessibilityRole | null

selected

Selected (a toggled trigger, the current tab): screen readers say so. The app owns the value and styles it.

Type boolean

selectedStyle

The style while selected (and not disabled), as selected: classes.

Type StyleProp<ViewStyle>