Skip to main content
Decoderen
Browse documentation

Button

Use for an action or a prominent navigation link. Choose one primary action per content group.

Variants

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button variant="primary">Continue</Button>
<Button variant="secondary">Continue</Button>
<Button variant="ghost">Continue</Button>
<Button variant="tonal">Continue</Button>
<Button variant="elevated">Continue</Button>

Sizes

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button size="sm">Continue</Button>
<Button size="md">Continue</Button>
<Button size="lg">Continue</Button>

Press shape

Press and hold: the primary pill squares up to the button radius, Material 3 Expressive's press shape. The other variants keep their pill so the motion marks the main action.

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button>Continue</Button>
<Button variant="tonal">Continue</Button>

Arrow

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button>Continue</Button>
<Button arrow>Continue</Button>

Disabled

View FormField
Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button variant="primary" disabled>Continue</Button>
<Button variant="secondary" disabled>Continue</Button>
<Button variant="ghost" disabled>Continue</Button>
<Button variant="tonal" disabled>Continue</Button>
<Button variant="elevated" disabled>Continue</Button>
<Button href="/decoderen/form-field/" disabled>View FormField</Button>

Native types

These specimens sit outside a form; the submit and reset buttons have no form to act on.

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<Button type="button">Button</Button>
<Button type="submit">Submit</Button>
<Button type="reset">Reset</Button>

Surfaces

Off-white

Paper

Steel

Navy

Ink

Show code
Example.astroAstro
---
import Button from '@dhekode/ui/primitives/Button.tsx';
---

<div data-surface="canvas">
  <Button variant="primary">Continue</Button>
  <Button variant="secondary">Continue</Button>
  <Button variant="ghost">Continue</Button>
  <Button variant="tonal">Continue</Button>
  <Button variant="elevated">Continue</Button>
</div>
<div data-surface="subtle">
  <Button variant="primary">Continue</Button>
  <Button variant="secondary">Continue</Button>
  <Button variant="ghost">Continue</Button>
  <Button variant="tonal">Continue</Button>
  <Button variant="elevated">Continue</Button>
</div>
<div data-surface="steel">
  <Button variant="primary">Continue</Button>
  <Button variant="secondary">Continue</Button>
  <Button variant="ghost">Continue</Button>
  <Button variant="tonal">Continue</Button>
  <Button variant="elevated">Continue</Button>
</div>
<div data-surface="brand">
  <Button variant="primary">Continue</Button>
  <Button variant="secondary">Continue</Button>
  <Button variant="ghost">Continue</Button>
  <Button variant="tonal">Continue</Button>
  <Button variant="elevated">Continue</Button>
</div>
<div data-surface="inverse">
  <Button variant="primary">Continue</Button>
  <Button variant="secondary">Continue</Button>
  <Button variant="ghost">Continue</Button>
  <Button variant="tonal">Continue</Button>
  <Button variant="elevated">Continue</Button>
</div>
API reference
variant

Type: 'primary' | 'secondary' | 'ghost' | 'tonal' | 'elevated'

Default: 'primary'

Emphasis, inherited from the enclosing surface: Material 3's filled, outlined, text, tonal and elevated buttons.

size

Type: 'sm' | 'md' | 'lg'

Default: 'md'

sm is the 32px chip height for dense toolbars; md is the 40px button; lg is the 56px button.

href

Type: string

Default: unset

Renders an anchor; omit it for a button.

type

Type: 'button' | 'submit' | 'reset'

Default: 'button'

Used only for the native button.

arrow

Type: boolean

Default: false

Adds a forward chevron; use for forward navigation.

icon

Type: ReactNode

Default: unset

Decorative leading icon, centred on the button beside the label.

disabled

Type: boolean

Default: false

Disables buttons; removes href and tab focus from disabled links.

className

Type: string | undefined

Default: unset

Adds classes to the root, after the component utilities.

other attributes

Type: ComponentProps<'a'> or ComponentProps<'button'>, without style

Default: unset

Forwarded to the element the href selects. style is excluded because the CSP blocks inline styles.

Slots: children: the visible label. Do not nest another interactive element inside it.

A React component (D-170) on Material 3's anatomy, rendered to static HTML with no client JavaScript: a pill container with a 44px touch target, a state layer for hover, focus and press (a wash of the label colour at the --dk-state-layer-* opacities), and Material 3 Expressive's press shape on primary only. Links navigate; buttons perform actions; feature browser code owns actions through the existing lifecycle.

Original component source
packages/ui/src/primitives/Button.tsxAstro
import type { ComponentProps, ReactNode } from 'react';
import Icon from '@dhekode/icons/Icon.tsx';
import { classNames } from '@dhekode/utils/class-names';
import { inertLink, ownStates, stateLayer } from '@dhekode/utils/state-layer';

type Variant = 'primary' | 'secondary' | 'ghost' | 'tonal' | 'elevated';
type Size = 'sm' | 'md' | 'lg';

interface ButtonOptions {
  /**
   * Material 3's five buttons under DheKode names: primary (filled), secondary (outlined),
   * ghost (text), tonal and elevated.
   */
  variant?: Variant | undefined;
  size?: Size | undefined;
  /** Appends a chevron that opens into an arrow on hover. Use for forward navigation only. */
  arrow?: boolean | undefined;
  /**
   * A decorative leading icon, such as <PhosphorIcon icon={PlusIcon} size={16} />. It sits
   * beside the label in the button's own row, so both centre on the button rather than on the
   * text's baseline.
   */
  icon?: ReactNode;
  disabled?: boolean | undefined;
  className?: string | undefined;
  children: ReactNode;
}

/** `style` is left out: the hash-based CSP blocks inline styles (AGENTS.md section 5). */
type Native<Element extends 'a' | 'button'> = Omit<
  ComponentProps<Element>,
  keyof ButtonOptions | 'style'
>;

export type ButtonProps = ButtonOptions &
  ((Native<'button'> & { href?: undefined }) | (Omit<Native<'a'>, 'href'> & { href: string }));

/**
 * Material 3's anatomy: a pill container whose touch target (::after) reaches the 44px
 * minimum even though the container is 32 to 56px tall. transition-all carries the press morph;
 * the outline colour is pinned to the focus colour so the global focus ring appears at once
 * instead of fading in from the label.
 */
const base =
  'dk-button relative isolate inline-flex max-w-full cursor-pointer items-center justify-center gap-xs border-(length:--dk-border-width-hairline) border-solid text-center font-medium leading-snug no-underline outline-(color:--dk-surface-focus) transition-all duration-(--dk-motion-duration-quick) ease-smooth-out after:absolute after:inset-x-0 after:top-1/2 after:min-h-(--dk-layout-touch-target) after:-translate-y-1/2 disabled:cursor-not-allowed disabled:text-disabled-text aria-disabled:cursor-not-allowed aria-disabled:text-disabled-text';

/** Component tokens, Material 3's component tier: each variant's container, label and outline. */
const variants: Record<Variant, string> = {
  // Material 3 Expressive's press shape: the pill squares up to the button radius while held.
  // It is reserved for the primary action so the motion keeps its meaning.
  primary:
    'border-surface-action bg-surface-action text-surface-action-text not-aria-disabled:active:rounded-button not-aria-disabled:active:before:rounded-button disabled:border-disabled-border disabled:bg-disabled-background aria-disabled:border-disabled-border aria-disabled:bg-disabled-background',
  secondary:
    'border-surface-rule bg-transparent text-surface-accent disabled:border-disabled-border aria-disabled:border-disabled-border',
  // The transparent outline still draws in forced-colors mode.
  ghost: 'border-transparent bg-transparent text-surface-accent',
  tonal:
    'border-transparent bg-surface-tint text-surface-accent disabled:bg-disabled-background aria-disabled:bg-disabled-background',
  elevated:
    'border-transparent bg-surface-raised text-surface-accent shadow-(--dk-elevation-1) disabled:bg-disabled-background disabled:shadow-none aria-disabled:bg-disabled-background aria-disabled:shadow-none',
};

const sizes: Record<Size, string> = {
  // The chip height, for dense toolbars. Its round corners are half its height, like md's, so
  // the primary press can tween to the button radius instead of snapping.
  sm: 'min-h-(--dk-layout-control-height-sm) rounded-button-round-sm px-md py-2xs text-body-sm before:rounded-button-round-sm',
  md: 'min-h-(--dk-layout-button-height) rounded-button-round px-lg py-xs text-body-sm before:rounded-button-round',
  lg: 'min-h-(--dk-layout-control-height-lg) rounded-button-round-lg px-lg py-xs text-body before:rounded-button-round-lg',
};

/**
 * Primary interactive primitive on Material 3's button system and anatomy, in DheKode's colours.
 * Renders <a> when `href` is set, otherwise <button>. Other attributes pass through to the element.
 */
export default function Button({
  variant = 'primary',
  size = 'md',
  arrow = false,
  icon,
  disabled = false,
  className,
  children,
  ...native
}: ButtonProps) {
  const learn = arrow && !disabled;
  const shared = {
    className: classNames(
      base,
      stateLayer,
      ownStates,
      variants[variant],
      sizes[size],
      learn && 'dk-learn',
      className,
    ),
    'data-variant': variant,
    'data-size': size,
  };
  const content = (
    <>
      {icon && (
        <span className="dk-button__icon flex flex-none" aria-hidden="true">
          {icon}
        </span>
      )}
      <span className="dk-button__label min-w-0 wrap-anywhere">{children}</span>
      {arrow && <Icon name="chevron-learn" className="dk-learn__chevron" />}
    </>
  );

  if (native.href !== undefined) {
    const { href, ...rest } = native;
    if (disabled) {
      // A link has no disabled state, so it loses its href and leaves the tab order while
      // still announcing itself as a dimmed link.
      return (
        <a {...rest} {...shared} {...inertLink}>
          {content}
        </a>
      );
    }
    return (
      <a {...rest} {...shared} href={href}>
        {content}
      </a>
    );
  }

  const { type = 'button', ...rest } = native;
  return (
    <button {...rest} {...shared} type={type} disabled={disabled}>
      {content}
    </button>
  );
}

Best practices

  • Use a link for navigation and a button for an action.
  • Give each group one primary action and keep labels specific.
  • Keep interactive elements outside the button rather than nesting them.