Skip to main content
Decoderen
Browse documentation

Chip

Offer a compact action or a filter choice: Material 3's assist and filter chips.

Assist

Read the docs
Show code
Example.astroAstro
---
import Chip from '@dhekode/ui/primitives/Chip.tsx';
import PhosphorIcon from '@dhekode/icons/PhosphorIcon.tsx';
import { BookIcon } from '@dhekode/icons/phosphor';
---

<Chip>Book a call</Chip>
<Chip href="/decoderen/">
  <PhosphorIcon slot="icon" icon={BookIcon} />
  Read the docs
</Chip>
<Chip disabled>Unavailable</Chip>

Filter

Native checkboxes: they toggle without script and submit with a form.

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

<Chip variant="filter" name="topic" value="design" defaultChecked>Design</Chip>
<Chip variant="filter" name="topic" value="engineering">Engineering</Chip>
<Chip variant="filter" name="topic" value="automation">Automation</Chip>
API reference
variant

Type: 'assist' | 'filter'

Default: 'assist'

An action or link, or a native checkbox that toggles without script.

href

Type: string

Default: unset

Assist only: renders a link instead of a button.

onClick

Type: (event: MouseEvent<HTMLButtonElement>) => void

Default: unset

Assist only: the action for a chip without href. Needs a React island.

name

Type: string

Default: required for filter

Filter only: the submitted name.

value

Type: string

Default: required for filter

Filter only: the submitted value.

defaultChecked

Type: boolean

Default: unset

Filter only: initially selected.

checked

Type: boolean

Default: unset

Filter only: controls the selection from outside; pair it with onCheckedChange. Omit both for a plain form field.

onCheckedChange

Type: (checked: boolean) => void

Default: unset

Filter only: called with the new selection when the chip toggles.

icon

Type: ReactNode

Default: unset

Decorative leading icon; in Astro pass it with slot="icon".

disabled

Type: boolean

Default: false

Disables the chip; a disabled link chip loses its href.

className

Type: string | undefined

Default: unset

Adds classes to the React component root, after its own utilities. React components take className, not class.

Slots: children: the visible label. Named slot icon: a leading icon.

Keep labels to a word or two. Group filter chips in a fieldset with a legend. A selected filter chip shows a check in place of its icon.

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

interface ChipOptions {
  /** A decorative leading icon, such as <PhosphorIcon name="..." /> in the icon slot. */
  icon?: ReactNode;
  disabled?: boolean | undefined;
  className?: string | undefined;
  children: ReactNode;
}

/** An assist chip: a compact action or link. */
interface AssistChipProps extends ChipOptions {
  variant?: 'assist' | undefined;
  href?: string | undefined;
  /** The action, for a chip without `href`. */
  onClick?: ((event: MouseEvent<HTMLButtonElement>) => void) | undefined;
}

/** A filter chip: a native checkbox, so a form submits it and it toggles without script. */
interface FilterChipProps extends ChipOptions {
  variant: 'filter';
  name: string;
  value: string;
  defaultChecked?: boolean | undefined;
  /** Controls the chip from outside, with `onCheckedChange`; omit both for a plain form field. */
  checked?: boolean | undefined;
  onCheckedChange?: ((checked: boolean) => void) | undefined;
}

export type ChipProps = AssistChipProps | FilterChipProps;

/**
 * Material 3's chip: 32px tall with the button radius, an outline, the state layer and a touch
 * target of at least 44px. The start padding depends on a leading icon (`leading`).
 */
const base =
  'dk-chip relative isolate inline-flex h-(--dk-layout-control-height-sm) max-w-full shrink-0 items-center gap-xs rounded-button border-(length:--dk-border-width-hairline) border-solid bg-transparent pe-md text-body-sm leading-snug font-medium no-underline transition-colors duration-(--dk-motion-duration-quick) ease-out before:rounded-button after:absolute after:inset-x-0 after:top-1/2 after:min-h-(--dk-layout-touch-target) after:-translate-y-1/2';

/*
 * A leading icon takes a shorter start padding, as in Material 3, so the icon side does not look
 * heavier than the label side. A filter chip's check counts as one once it is selected.
 */
const leading = (icon: ReactNode, check = false) =>
  icon ? 'ps-sm' : check ? 'ps-md has-checked:ps-sm' : 'ps-md';

/*
 * The colours and cursor of each state. Only one set is applied: both sets set the same properties,
 * and Tailwind's stylesheet order, not the class order, would decide between them.
 */
const states = (disabled: boolean) =>
  disabled
    ? 'cursor-not-allowed border-disabled-border text-disabled-text before:hidden'
    : 'cursor-pointer border-surface-muted text-surface-text hover:before:opacity-(--dk-state-layer-hover) active:before:opacity-(--dk-state-layer-pressed)';

/** Material 3's assist and filter chips. */
export default function Chip(props: ChipProps) {
  const { icon, disabled = false, className, children } = props;
  const label = <span className="min-w-0 truncate">{children}</span>;

  if (props.variant === 'filter') {
    // Selected: tonal container and a leading check; the focus ring moves to the whole chip.
    return (
      <label
        className={classNames(
          base,
          leading(icon, true),
          stateLayer,
          'has-checked:border-transparent has-checked:bg-surface-tint has-checked:text-surface-accent has-focus-visible:outline-(length:--dk-border-width-strong) has-focus-visible:outline-offset-(--dk-layout-focus-offset) has-focus-visible:outline-(color:--dk-surface-focus) has-focus-visible:outline-solid has-focus-visible:before:opacity-(--dk-state-layer-focus)',
          states(disabled),
          className,
        )}
        data-variant="filter"
      >
        <input
          className="peer sr-only"
          type="checkbox"
          name={props.name}
          value={props.value}
          {...(props.checked === undefined
            ? { defaultChecked: props.defaultChecked }
            : {
                checked: props.checked,
                onChange: (event) => props.onCheckedChange?.(event.currentTarget.checked),
              })}
          disabled={disabled}
        />
        <Icon name="check" className="hidden peer-checked:block" />
        {icon && <span className="contents peer-checked:hidden">{icon}</span>}
        {label}
      </label>
    );
  }

  const classes = classNames(
    base,
    leading(icon),
    stateLayer,
    'outline-(color:--dk-surface-focus) focus-visible:before:opacity-(--dk-state-layer-focus)',
    states(disabled),
    className,
  );
  const content = (
    <>
      {icon}
      {label}
    </>
  );

  if (props.href !== undefined) {
    return disabled ? (
      // A link has no disabled state: it loses its href and leaves the tab order.
      <a className={classes} {...inertLink} data-variant="assist">
        {content}
      </a>
    ) : (
      <a className={classes} href={props.href} data-variant="assist">
        {content}
      </a>
    );
  }
  return (
    <button
      className={classes}
      type="button"
      disabled={disabled}
      data-variant="assist"
      onClick={props.onClick}
    >
      {content}
    </button>
  );
}

Best practices

  • Use filter chips for choices that narrow content, and assist chips for small actions.
  • Group filter chips under a visible legend.