Components / ActionsStable

Chip

A small pill that starts one quiet action, such as View all at the end of a panel header or Edit beside a section title.

View as Markdown

Playground

Code

Chip.tsx
import type { MouseEventHandler, ReactNode } from 'react'

// Nacre Chip: a small pill that starts one quiet action, such as View all in a panel header. React plus Tailwind
// classes only; needs nacre-theme.css. A link when it has an href, otherwise a button. Motion: colour quick 160 out on
// hover; the press scale comes from the Nacre base (pointer only, motion-safe).

export type ChipProps = {
  children: ReactNode
  /** Shows a chevron after the label, for a chip that goes somewhere. */
  chevron?: boolean
  /** Makes the chip a link. */
  href?: string
  onClick?: MouseEventHandler<HTMLElement>
  disabled?: boolean
  className?: string
}

const base =
  'inline-flex h-9 shrink-0 cursor-pointer items-center gap-1 rounded-full bg-sunken px-3.5 text-body font-[600] whitespace-nowrap text-fg ' +
  'transition-colors duration-(--nx-dur-quick) ease-out hover:bg-[color-mix(in_oklab,var(--nx-fg)_7%,var(--nx-sunken))] hover:text-fg-strong ' +
  'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus disabled:cursor-not-allowed disabled:text-fg-disabled'

export function Chip({ children, chevron = false, href, onClick, disabled = false, className = '' }: ChipProps) {
  const content = (
    <>
      {children}
      {chevron && (
        <svg data-part="chevron" aria-hidden="true" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" className="-me-1 size-4 rtl:scale-x-[-1]">
          <path d="M6.5 4l4 4-4 4" />
        </svg>
      )}
    </>
  )
  if (href && !disabled) {
    return <a href={href} data-part="root" data-press="" onClick={onClick} className={`${base} ${className}`}>{content}</a>
  }
  return <button type="button" data-part="root" disabled={disabled} onClick={onClick} className={`${base} ${className}`}>{content}</button>
}

Usage

When to use

  • The action at the end of a Panel header: View all, Edit, Manage.
  • A quiet shortcut that sits next to the content it opens, with the chevron when it goes to another screen.

Choose something else when

  • It is the main action of the screen: use Button.
  • It shows a status or a count and does nothing: use Badge.
  • It switches between views of the same content: use Segmented control.

In the Nacre workspace

View all

A Panel lists three active audits. A Chip with a chevron at the end of its header opens the full list.

Edit a section

The Brand section of a token page ends its header with an Edit chip that opens the editing drawer.

Do and don't

  • DoName the action in one or two words: View all, Edit.

    Don'tWrite a sentence, or repeat the panel title inside the chip.

  • DoUse one chip per header.

    Don'tLine up several chips; use a Button group or a menu.

Anatomy

  1. Chip. Full pill, 36 px high, sunken background, label 14/20 at weight 600 in fg.
  2. Chevron. 16 px stroke chevron after the label, drawn with currentColor.

API

childrenType ReactNodeDefault -The label. Keep it to one or two words.
chevronType booleanDefault falseDraws a chevron after the label, mirrored in right-to-left text.
hrefType stringDefault -Makes the chip a link (an a element) instead of a button.
onClickType MouseEventHandler<HTMLElement>Default -Called on click, for a button or a link.
disabledType booleanDefault falseDims the label and stops the click; a disabled chip is never a link.
classNameType stringDefault -Extra classes on the chip.

Motion

HoverProperty background-color and colorDuration quick 160Easing outKeyboard Same, colour only.
PressProperty scale .97 (pointer only, motion-safe)Duration quick 160Easing outKeyboard Enter and Space activate it with no scale.

Accessibility

Keyboard

Tab
Moves focus to the chip.
Enter, Space
Activates a button chip. Enter follows a link chip.

ARIA and semantics

  • A native button or a native link: no role is added.
  • The chevron is aria-hidden, so the name is the label alone. Say where it goes in the label when the panel title does not.
  • disabled uses the native disabled attribute.

Contrast

Label on the chipfg on sunkenLight 13.79:1Dark 13.55:1Needs 4.5:1
Label on hoverfg-strong on sunkenLight 17.60:1Dark 17.91:1Needs 4.5:1
Focus ringfocus on pageLight 3.74:1Dark 13.14:1Needs 3:1