Tag
A filter people can turn on and off, like a service category.
Installation
npm install @base-ui/react @heroicons/react class-variance-authority clsx tailwind-mergeAdd the cn helper if you haven't already:
import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";
const twMerge = extendTailwindMerge({
extend: {
theme: {
text: [
"display",
"heading-xl",
"heading-lg",
"heading-md",
"heading-sm",
"heading-xs",
"body-lg",
"body-md",
"body-sm",
"label",
"helper",
"button",
],
"font-weight": ["regular"],
},
},
});
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}Add the component:
import type { ComponentProps } from "react";
import { Toggle } from "@base-ui/react/toggle";
import { ToggleGroup } from "@base-ui/react/toggle-group";
import { CheckIcon } from "@heroicons/react/16/solid";
import { cn } from "@/lib/utils";
type TagProps = Omit<ComponentProps<typeof Toggle>, "className"> & {
className?: string;
};
function Tag({ className, children, ...props }: TagProps) {
return (
<Toggle
data-slot="tag"
className={cn(
"group inline-flex h-8 shrink-0 items-center gap-1.5 rounded-md border px-4 text-label whitespace-nowrap select-none",
"border-border-strong-01 bg-layer-01 text-text-primary",
"hover:not-data-disabled:border-border-hover hover:not-data-disabled:bg-background-hover",
"data-pressed:border-interactive-default data-pressed:bg-highlight-default data-pressed:pl-3 data-pressed:text-link-primary",
"hover:not-data-disabled:data-pressed:border-interactive-hover hover:not-data-disabled:data-pressed:bg-highlight-default hover:not-data-disabled:data-pressed:text-link-primary-hover",
"data-disabled:cursor-not-allowed data-disabled:border-border-disabled data-disabled:text-text-disabled",
"data-disabled:data-pressed:border-border-disabled data-disabled:data-pressed:bg-layer-accent-01 data-disabled:data-pressed:text-text-disabled",
"focus-visible:focus-ring",
className,
)}
{...props}
>
<CheckIcon className="hidden size-4 shrink-0 group-data-pressed:block" />
{children}
</Toggle>
);
}
type TagGroupProps = Omit<ComponentProps<typeof ToggleGroup>, "className"> & {
className?: string;
};
function TagGroup({ className, ...props }: TagGroupProps) {
return (
<ToggleGroup
data-slot="tag-group"
className={cn("flex flex-wrap gap-2", className)}
{...props}
/>
);
}
export { Tag, TagGroup, type TagProps, type TagGroupProps };Usage
import { Tag, TagGroup } from "@/components/ui/tag";
<TagGroup multiple aria-label="Filter services">
<Tag value="passport">Passport</Tag>
<Tag value="ids">IDs</Tag>
</TagGroup>;Examples
Responding to changes
Use value and onValueChange when other parts of the page depend on the selection, like a results count. Announce the change with aria-live so screen reader users hear it.
Showing 1 of 4 services
Disabled
Props
TagGroup
| Prop | Type | Default | Description |
|---|---|---|---|
multiple | boolean | false | Lets people select more than one tag. Use it for filters. |
defaultValue | string[] | – | The tags selected at first, when you don't control the state. |
value | string[] | – | The selected tags, when you control the state. |
onValueChange | (value: string[]) => void | – | Called when the selection changes. |
disabled | boolean | false | Disables every tag in the group. |
Tag
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | – | Identifies the tag inside a TagGroup. |
defaultPressed | boolean | false | Whether a standalone tag starts selected. |
pressed | boolean | – | Whether a standalone tag is selected, when you control the state. |
onPressedChange | (pressed: boolean) => void | – | Called when a standalone tag is turned on or off. |
disabled | boolean | false | Stops people changing the tag. |
Accessibility
- Name the group: give
TagGroupanaria-label, like "Filter services", so screen reader users know what the tags do. - State: each tag is a toggle button. Screen readers announce whether it's pressed, and a check shows the selected state without relying on color.
- Keyboard: Tab moves into the group, the arrow keys move between tags, and Space or Enter turns a tag on or off.
- Size: tags are 32px tall with 8px gaps, above the 24 x 24px minimum target size in WCAG 2.2.
- Tag or Badge: use a Tag when people can select it. For a status label they can't change, use a Badge.