Forms

Chip & Kbd

32px selectable and removable tokens for criteria builders and tag editors, plus keyboard keys.

react-aria-componentslucide-react

Preview

VP SalesHead of RevenueCRO
Press ⌘K to open quick actions

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a chip.tsx file and paste the following code into it.

    src/components/ui/chip.tsx
    "use client";
    
    import { Check, X } from "lucide-react";
    import type { ReactNode } from "react";
    import { Button as AriaButton, ToggleButton, type ToggleButtonProps } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    const chipBase =
      "inline-flex h-8 cursor-default items-center gap-1.5 rounded-sm px-3 text-base font-medium outline-none transition-colors " +
      "data-focus-visible:shadow-focus [&_svg]:size-3.5";
    
    /** Selectable token used in criteria builders and persona pickers. */
    export function Chip({ children, className, ...props }: Omit<ToggleButtonProps, "children" | "className"> & { children: ReactNode; className?: string }) {
      return (
        <ToggleButton
          {...props}
          className={cx(
            chipBase,
            "bg-surface text-fg shadow-button data-hovered:bg-subtle",
            "data-selected:bg-accent-soft data-selected:text-accent-text data-selected:shadow-[inset_0_0_0_1px_var(--color-accent-soft-border)]",
            className,
          )}
        >
          {({ isSelected }) => (
            <>
              {isSelected && <Check aria-hidden />}
              {children}
            </>
          )}
        </ToggleButton>
      );
    }
    
    /** Removable token used in tag editors. */
    export function RemovableChip({ children, onRemove, className }: { children: ReactNode; onRemove?: () => void; className?: string }) {
      return (
        <span className={cx(chipBase, "bg-surface pr-1.5 text-fg shadow-button", className)}>
          {children}
          <AriaButton
            aria-label={`Remove ${typeof children === "string" ? children : "item"}`}
            onPress={onRemove}
            className="inline-flex size-5 items-center justify-center rounded-xs text-fg-muted outline-none data-hovered:bg-muted data-hovered:text-fg data-focus-visible:shadow-focus"
          >
            <X />
          </AriaButton>
        </span>
      );
    }
  3. Create a kbd.tsx file and paste the following code into it.

    src/components/ui/kbd.tsx
    import { cx } from "@/utils/cx";
    
    /** Marks keyboard shortcuts. */
    export function Kbd({ className, children }: { className?: string; children: React.ReactNode }) {
      return (
        <kbd
          className={cx(
            "inline-flex h-5 min-w-5 items-center justify-center rounded-[6px] border border-line bg-surface px-1.5 font-sans text-xs font-medium text-fg-muted",
            className,
          )}
        >
          {children}
        </kbd>
      );
    }
  4. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.