# Tags Input

A field that collects several values as tags, such as emails or keywords.

A field that collects several values as tags: emails, labels, keywords. A field that collects values as tags: the Input's box holding a row of tags (the removable Tag) and a text input; the row wraps onto new lines. Enter or the delimiter adds the text as a tag, Backspace in an empty field selects the last tag and removes it on the second press, a double click edits a tag in place without moving anything. One focus ring, the field's: a selected tag takes the accent fill. Behaviour, focus and ARIA come from the Zag tags-input machine, through Ark UI.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `tags-input` machine, through Ark UI

## Add

```bash
npx fndn add tags-input
```

It writes the folder `tags-input/` into the project's ui folder (`index.ts`, `tags-input.tsx`, `TagsInput.vue`, `TagsInput.svelte`, `tags-input.recipe.ts`), and what it is built from: [Tag](/components/tag.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { TagsInput } from "@/components/ui/tags-input";
```

```vue
import { TagsInput } from "@/components/ui/tags-input";
```

```svelte
import { TagsInput } from "$lib/components/ui/tags-input";
```

## Parts

In the DOM the root carries `data-slot="tags-input"` and each part `data-slot="tags-input-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `tags-input` |  |  |
| `label` | `label` | `tags-input-label` | optional |  |
| `control` | `div` | `tags-input-control` |  | The shared control (`lib/control.ts`), field kind: its sizes and looks; the accent ring for every kind of focus, red when invalid. Wraps its tags and the input onto new rows. One row is the size's height: the 22px tags sit 1, 3, 5 or 7px in (xs, sm, md, lg). data-focus, data-invalid, data-disabled from the engine. |
| `item` | `div` | `tags-input-item` | repeats | One tag: a one-cell grid holding its preview and, while the tag is edited, its edit field on top of it, so editing moves nothing. |
| `item-preview` | `span` | `tags-input-item-preview` |  | The real Tag (size sm, removable, its tone from tones or tone, its variant from tagVariant), marked data-tags-input="item-preview": 22px, chip radius, its label cut at the end with a title, its remove Button (aria-label "Remove <tag>", not a tab stop) wired to the engine's delete trigger. data-highlighted (Backspace, the arrow keys, a click): selected, not focused: the accent fill, its label, dot and × in on-accent, at once; the field's ring stays the only ring and the input's caret hides meanwhile. While the tag is edited it stays in the row (the engine's hidden is undone) and holds its place: the same box with the accent-tint fill, its label and × not drawn (an outline tag keeps its dot), under the edit field. |
| `item-input` | `input` | `tags-input-item-input` |  | Edits a tag in place, lying over it: transparent and ringless (the field's ring already shows the focus), its text on the label's pixels (same size, weight, tracking and inset). As wide as the tag was when the edit started, wider as the text grows (the engine sets its width to its text), never wider than the field. The engine selects the text: the selection takes the accent fill. 16px on coarse pointers. Hidden unless the tag is edited. |
| `input` | `input` | `tags-input-input` |  | Takes the rest of the row, at least 64px (else a row of its own); its text starts where the Input's does (the size's padding: 6, 8, 10 or 12px in). dir=auto. 16px on coarse pointers. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `array` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The tags. |
| `placeholder` | `string` | `"Add a tag"` | Shown only while there is no tag (beside tags it would be cut). |
| `label` | `string` |  | Visible label; otherwise pass aria-label. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The shared control's sizes (`lib/control.ts`): one row is 24, 28, 32 or 36px; tags are 22px at every size. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `tone` | `"neutral" \| "accent" \| "blue" \| "green" \| "orange" \| "red" \| "purple" \| "cyan" \| "yellow" \| "pink" \| "auto"` | `"neutral"` | The tags' tone (Tag's tones): one for every tag, or auto, which gives the same word the same tag hue every time (the records table's hash). |
| `tones` | `object` |  | A tone per tag text ({ bug: "red", billing: "purple" }), for labels with their own colours; wins over tone. Looked up as typed, then trimmed and lower-cased. |
| `tagVariant` | `"tint" \| "outline"` | `"tint"` | The tags' look (Tag's variant). tint: the hue fills the tag. outline: a neutral tag with the hue only in its dot, for a field of labels beside lists that show them the same way (an issue's labels). |
| `max` | `number` |  | Most tags; past it nothing is added. |
| `editable` | `boolean` | `true` | A double click, or Enter on a highlighted tag, edits it. |
| `delimiter` | `string` | `","` | Typing it adds the text as a tag. |
| `addOnPaste` | `boolean` | `true` | Pasted text is split on the delimiter into tags. |
| `disabled` | `boolean` | `false` |  |
| `invalid` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name: a hidden input carries the tags. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string[]) after a tag is added, edited or removed |

## States

`hover`, `focus`, `highlighted`, `editing`, `disabled`, `invalid`

## Keyboard and accessibility

Keyboard:

- Enter, or the delimiter, adds the text as a tag; a tag past max is not added. A text that is already a tag is not added twice: the tag it names is selected (the accent fill), so it is seen to be there, and the field is emptied; typing on clears the selection. Left in the field when the focus leaves, a duplicate is dropped quietly
- Backspace in an empty field highlights the last tag; Backspace again removes it
- ArrowLeft / ArrowRight (mirrored right-to-left) walk the tags from an empty field; Delete or Backspace removes the highlighted tag
- Enter on a highlighted tag edits it; Enter commits the edit, Escape cancels it; either way focus returns to the input and the tag stays selected. A click away commits it and leaves the field; so does Tab (or Shift+Tab), and the focus goes where it was put. Emptied, the tag is removed
- An edit committed with the text of another tag is not taken: the edited tag keeps its former text; committed with Enter, the tag that already has that text is selected instead (the sign a text typed twice gives). An edit that leaves the tag's own text is not a duplicate
- Escape clears the highlight; Tab, or a click away, adds the text left in the field and moves on

Notes:

- Give it a visible label or an aria-label.
- Focus stays in the text input while tags are walked; the field's ring is the only ring, the highlighted tag is shown selected (the accent fill) and the input's caret hides meanwhile. A click on a tag selects it the same way. Each tag's remove button has its own name ("Remove <tag>") and is not a tab stop.
- The engine announces additions and removals through its live region.
- Tags keep their own direction and are cut at the end with a title when the field is narrower than one.

## Motion

- control: fill over duration.fast, ring over duration.base (the Input's)
- tags: appear at once (typing is keyboard work); Backspace / Delete remove at once; a tag removed with its × (the pointer) fades over duration.fast while its cell gives its width back over duration.reveal (easing.out-strong), so the tags after it slide into the room and are in place when it leaves (under reduced motion the cell closes at once); a press on the × does not select the tag; the selected fill is instant (keyboard and pointer alike)
- editing: nothing moves when it starts, ends or is cancelled; the edited tag only grows, at once, when its text gets longer
- remove button: the Button's press (motion.press-scale over duration.press)

## Tokens

`color.field`, `color.surface`, `color.hover`, `color.ink`, `color.ink-3`, `color.accent`, `color.accent-tint`, `color.on-accent`, `color.red`, `color.red-tint`, `shadow.hairline`, `radius.control`, `radius.chip`, `text.xs`, `text.sm`, `duration.fast`, `duration.base`, `duration.reveal`, `easing.out-strong`

## Dependencies

- [Tag](/components/tag.md)

## Examples

### Project labels

React · `labels.tsx`

```tsx
import { TagsInput } from "@/components/ui/tags-input";

/* each label keeps its colour; a new label gets one from its word (tone "auto") */
const tones = { bug: "red", billing: "purple", webhooks: "cyan" } as const;

export default function Labels() {
  return <TagsInput label="Labels" defaultValue={["bug", "billing", "webhooks"]} tone="auto" tones={tones} placeholder="Add a label" style={{ width: 320 }} />;
}
```

Vue · `labels.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { TagsInput } from "@/components/ui/tags-input";

/* each label keeps its colour; a new label gets one from its word (tone "auto") */
const tones = { bug: "red", billing: "purple", webhooks: "cyan" } as const;
const labels = ref(["bug", "billing", "webhooks"]);
</script>

<template>
  <TagsInput v-model:value="labels" label="Labels" tone="auto" :tones="tones" placeholder="Add a label" style="width: 320px" />
</template>
```

Svelte · `labels.svelte`

```svelte
<script lang="ts">
  import { TagsInput } from "$lib/components/ui/tags-input";

  /* each label keeps its colour; a new label gets one from its word (tone "auto") */
  const tones = { bug: "red", billing: "purple", webhooks: "cyan" } as const;
  let labels = $state(["bug", "billing", "webhooks"]);
</script>

<TagsInput bind:value={labels} label="Labels" tone="auto" {tones} placeholder="Add a label" style="width: 320px" />
```

### Issue labels, outline

React · `issue-labels.tsx`

```tsx
import { TagsInput } from "@/components/ui/tags-input";

/* an issue's labels in the outline look: neutral tags, the colour only in the dot, as the issue list shows them */
const tones = { bug: "red", regression: "orange", billing: "purple", webhooks: "cyan" } as const;

export default function IssueLabels() {
  return <TagsInput label="Labels" defaultValue={["bug", "regression", "billing"]} tagVariant="outline" tone="auto" tones={tones} placeholder="Add a label" style={{ width: 320 }} />;
}
```

Vue · `issue-labels.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { TagsInput } from "@/components/ui/tags-input";

/* an issue's labels in the outline look: neutral tags, the colour only in the dot, as the issue list shows them */
const tones = { bug: "red", regression: "orange", billing: "purple", webhooks: "cyan" } as const;
const labels = ref(["bug", "regression", "billing"]);
</script>

<template>
  <TagsInput v-model:value="labels" label="Labels" tag-variant="outline" tone="auto" :tones="tones" placeholder="Add a label" style="width: 320px" />
</template>
```

Svelte · `issue-labels.svelte`

```svelte
<script lang="ts">
  import { TagsInput } from "$lib/components/ui/tags-input";

  /* an issue's labels in the outline look: neutral tags, the colour only in the dot, as the issue list shows them */
  const tones = { bug: "red", regression: "orange", billing: "purple", webhooks: "cyan" } as const;
  let labels = $state(["bug", "regression", "billing"]);
</script>

<TagsInput bind:value={labels} label="Labels" tagVariant="outline" tone="auto" {tones} placeholder="Add a label" style="width: 320px" />
```

### Invite by email

React · `invite-emails.tsx`

```tsx
import { useState } from "react";
import { TagsInput } from "@/components/ui/tags-input";

/* controlled: paste a list of addresses, check each one */
export default function InviteEmails() {
  const [emails, setEmails] = useState<string[]>(["priya@northwind.example", "lucas@northwind"]);
  const invalid = emails.some((email) => !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email));
  return (
    <div className="flex flex-col gap-2" style={{ width: 320 }}>
      <TagsInput label="Invite by email" value={emails} onValueChange={setEmails} invalid={invalid} placeholder="name@northwind.example" />
      <span className="text-sm text-ink-2">
        {invalid ? "Every address needs a domain, like .example." : `${emails.length} ${emails.length === 1 ? "invite" : "invites"} will be sent.`}
      </span>
    </div>
  );
}
```

Vue · `invite-emails.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { TagsInput } from "@/components/ui/tags-input";

const emails = ref<string[]>(["priya@northwind.example", "lucas@northwind"]);
const invalid = computed(() => emails.value.some((email) => !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)));
</script>

<!-- controlled: paste a list of addresses, check each one -->
<template>
  <div class="flex flex-col gap-2" style="width: 320px">
    <TagsInput v-model:value="emails" label="Invite by email" :invalid="invalid" placeholder="name@northwind.example" />
    <span class="text-sm text-ink-2">
      {{ invalid ? "Every address needs a domain, like .example." : `${emails.length} ${emails.length === 1 ? "invite" : "invites"} will be sent.` }}
    </span>
  </div>
</template>
```

Svelte · `invite-emails.svelte`

```svelte
<script lang="ts">
  import { TagsInput } from "$lib/components/ui/tags-input";

  let emails = $state<string[]>(["priya@northwind.example", "lucas@northwind"]);
  const invalid = $derived(emails.some((email) => !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)));
</script>

<!-- controlled: paste a list of addresses, check each one -->
<div class="flex flex-col gap-2" style="width: 320px">
  <TagsInput bind:value={emails} label="Invite by email" {invalid} placeholder="name@northwind.example" />
  <span class="text-sm text-ink-2">
    {invalid ? "Every address needs a domain, like .example." : `${emails.length} ${emails.length === 1 ? "invite" : "invites"} will be sent.`}
  </span>
</div>
```

### Up to three reviewers

React · `required-reviewers.tsx`

```tsx
import { TagsInput } from "@/components/ui/tags-input";

/* max: the input stops taking tags at three */
export default function RequiredReviewers() {
  return <TagsInput label="Required reviewers, up to 3" max={3} defaultValue={["Priya Raman", "Daniel Okafor"]} placeholder="Add a reviewer" style={{ width: 320 }} />;
}
```

Vue · `required-reviewers.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { TagsInput } from "@/components/ui/tags-input";

const reviewers = ref(["Priya Raman", "Daniel Okafor"]);
</script>

<!-- max: the input stops taking tags at three -->
<template>
  <TagsInput v-model:value="reviewers" label="Required reviewers, up to 3" :max="3" placeholder="Add a reviewer" style="width: 320px" />
</template>
```

Svelte · `required-reviewers.svelte`

```svelte
<script lang="ts">
  import { TagsInput } from "$lib/components/ui/tags-input";

  let reviewers = $state(["Priya Raman", "Daniel Okafor"]);
</script>

<!-- max: the input stops taking tags at three -->
<TagsInput bind:value={reviewers} label="Required reviewers, up to 3" max={3} placeholder="Add a reviewer" style="width: 320px" />
```

### Label filter in a toolbar

React · `label-filter.tsx`

```tsx
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { TagsInput } from "@/components/ui/tags-input";

/* sm: 28px, beside an sm Button in a list's toolbar */
export default function LabelFilter() {
  const [labels, setLabels] = useState<string[]>(["bug", "p1"]);
  return (
    <div className="flex items-center gap-2" style={{ width: 320 }}>
      <TagsInput size="sm" aria-label="Filter by label" value={labels} onValueChange={setLabels} placeholder="Label" className="min-w-0 flex-1" />
      <Button size="sm" variant="secondary" disabled={labels.length === 0} onClick={() => setLabels([])}>Clear</Button>
    </div>
  );
}
```

Vue · `label-filter.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Button } from "@/components/ui/button";
import { TagsInput } from "@/components/ui/tags-input";

const labels = ref<string[]>(["bug", "p1"]);
</script>

<!-- sm: 28px, beside an sm Button in a list's toolbar -->
<template>
  <div class="flex items-center gap-2" style="width: 320px">
    <TagsInput v-model:value="labels" size="sm" aria-label="Filter by label" placeholder="Label" class="min-w-0 flex-1" />
    <Button size="sm" variant="secondary" :disabled="labels.length === 0" @click="labels = []">Clear</Button>
  </div>
</template>
```

Svelte · `label-filter.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { TagsInput } from "$lib/components/ui/tags-input";

  let labels = $state<string[]>(["bug", "p1"]);
</script>

<!-- sm: 28px, beside an sm Button in a list's toolbar -->
<div class="flex items-center gap-2" style="width: 320px">
  <TagsInput bind:value={labels} size="sm" aria-label="Filter by label" placeholder="Label" class="min-w-0 flex-1" />
  <Button size="sm" variant="secondary" disabled={labels.length === 0} onclick={() => (labels = [])}>Clear</Button>
</div>
```

## Recipe

`tags-input.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { control, controlSize, controlVariant } from "../lib/control";

const ctl = control({ kind: "field" });

/* tags-input — a field that collects values as tags: the shared control recipe (lib/control.ts, field
 * kind: sizes xs 24 · sm 28 · md 32 · lg 36, looks outline · subtle · ghost, the accent ring for
 * every kind of focus) holding a row of tags and a text input
 * that wraps onto new lines. Behaviour comes from the Zag tags-input machine (Enter or the
 * delimiter adds, Backspace highlights then removes the last tag, the arrow keys walk the tags,
 * a click highlights one, a double click edits one).
 *
 * One field, one focus: the control's ring is the only ring. Focus never leaves the text input
 * while the tags are walked, so a highlighted tag is shown as selected (the accent fill, its text,
 * dot and × in on-accent, like a selected token in a mail client) and the input's caret is hidden
 * meanwhile: one of the two shows, never both.
 *
 * A tag is the real Tag (size sm, removable: 22px, chip radius, its remove
 * Button), marked data-tags-input="item-preview"; only its states and its place are set here.
 * The 22px tags sit as far from the start as from the top, so one row is the size's height
 * (xs 1px, sm 3px, md 5px, lg 7px in), and the text starts where the Input's would.
 *
 * Motion: one thing moves. A tag removed with its × (the pointer) fades while its cell gives its
 * width back, so the tags after it slide into the room instead of jumping there (the leaving
 * variant). Everything done from the keyboard is instant: Enter, the delimiter and a paste add at
 * once, Backspace and Delete remove at once, the arrow keys select at once.
 *
 * Editing a tag moves nothing: the item is a one-cell grid holding the tag and its edit field.
 * The engine marks the tag `hidden` while it is edited, which the adapters undo, so the tag keeps
 * its box (a quiet accent-tint fill, its label and × invisible) and holds the cell at its own width;
 * the edit field lies over it, transparent, its text on the label's pixels, at least as wide as the
 * cell and wider when the text grows (the engine sets its width to its text). The selectors below
 * start at the item ([data-part=item]) so they out-rank the Tag's own colours, dark ones included. */

export const tagsInput = tv({
  slots: {
    base: "flex flex-col gap-1.5 min-w-0 max-w-full",
    label: "text-sm font-medium text-ink",
    /* the shared control recipe (lib/control.ts), field kind; it wraps, so its height is a minimum.
     * The text ends where the control's padding says (pe), the tags sit as far in as they are from
     * the top (the size's ps and py below) */
    control: [ctl.base(), "flex flex-wrap items-center gap-1 h-auto cursor-text"],
    /* one cell: the tag and its edit field on top of each other. The cell is a flexible track (as
     * wide as what it holds), so it can close when the tag leaves; the margin moves with it */
    item: [
      "inline-grid grid-cols-[minmax(0,1fr)] min-w-0 max-w-full [&>*]:[grid-area:1/1]",
      "motion-size-reveal motion-also-[margin_var(--fndn-duration-reveal)_var(--fndn-easing-out-strong)]",
    ],
    itemPreview: [
      "max-w-full motion-opacity-fast",
      /* selected (data-highlighted: Backspace, the arrow keys, a click): the accent fill, at once.
       * The ×'s colour follows at once too (its own hover fill still fades) */
      "[[data-part=item]>&[data-highlighted]]:bg-accent [[data-part=item]>&[data-highlighted]]:border-accent [[data-part=item]>&[data-highlighted]]:text-on-accent",
      "[[data-part=item]>&[data-highlighted]_[data-slot=tag-dot]]:bg-on-accent",
      "[&_[data-tag=remove]]:[--fndn-m-colors:background-color_var(--motion-fast)]",
      /* being edited (its edit field is shown): the same box with a quiet fill, its label and ×
       * kept for their width but not drawn; the dot of an outline tag stays, in its own hue (the
       * engine keeps the tag highlighted while it is edited, so these out-rank the selected look) */
      "[[data-part=item]:has(>[data-part=item-input]:not([hidden]))>&]:bg-accent-tint [[data-part=item]:has(>[data-part=item-input]:not([hidden]))>&]:border-transparent",
      "[[data-part=item]:has(>[data-part=item-input]:not([hidden]))>&_[data-slot=tag-dot]]:bg-(--_hue)",
      "[[data-part=item]:has(>[data-part=item-input]:not([hidden]))>&>:not([data-slot=tag-dot])]:invisible",
    ],
    /* the edit field: over the tag, transparent, the label's type; its text starts where the label's
     * does (tagVariant below), and 1px of bottom padding lifts it onto the label's baseline (an
     * input centres its line a hair lower than the Tag's flex row). Before the engine sets its
     * width, it is the cell's (w-0, min-w-full).
     * Selected text (the engine selects it all) is the accent fill, as the selected tag is. */
    itemInput: [
      "relative z-1 h-[22px] w-0 min-w-full max-w-full m-0 border border-solid border-transparent p-0 pe-1.5 pb-px rounded-chip bg-transparent outline-none",
      "text-[11.5px] font-normal leading-none text-ink [font-family:inherit] [unicode-bidi:plaintext]",
      "selection:bg-accent selection:text-on-accent",
      "pointer-coarse:text-[16px]",
    ],
    /* the control's bare input, one tag high: takes what is left of the row, or a row of its own
     * when a tag leaves it less than 64px. Its 5px and the tags' inset put the text where the
     * control's padding would (6 · 8 · 10 · 12px in). Its caret hides while a tag is selected. */
    input: [
      ctl.input(), "w-auto min-w-16 h-[22px] [:dir(ltr)>&]:pl-[5px] [:dir(rtl)>&]:pr-[5px]",
      "[:has(>[data-part=item]>[data-highlighted])>&]:caret-transparent",
    ],
  },
  variants: {
    size: {
      xs: { control: [controlSize.xs, "py-px ps-px"] },
      sm: { control: [controlSize.sm, "py-[3px] ps-[3px]"] },
      md: { control: [controlSize.md, "py-[5px] ps-[5px]"] },
      lg: { control: [controlSize.lg, "py-[7px] ps-[7px]"] },
    },
    variant: {
      outline: { control: controlVariant.outline },
      subtle: { control: controlVariant.subtle },
      ghost: { control: controlVariant.ghost },
    },
    /* where the Tag's label starts, so the edit field's text lands on it: after the same 1px border
     * (drawn transparent, so it snaps to the device pixels as the Tag's does) the padding, 6px; an
     * outline tag also has its dot and gap first (6 + 6 + 5px) */
    tagVariant: {
      tint: { itemInput: "ps-1.5" },
      outline: { itemInput: "ps-[17px]" },
    },
    /* removed with the pointer: the tag fades (duration.fast) while its cell closes to nothing and
     * takes the row's 4px gap with it (duration.reveal, out-strong), so the tags after it slide
     * into place as it goes; the adapter removes it when both are over, and nothing is left to
     * jump. The tag keeps its width and is clipped by the closing cell: its label is not re-cut on
     * the way. Under reduced motion duration.reveal is 0: the cell closes at once and the tag is gone
     * with it, nothing travels. */
    leaving: {
      true: {
        item: "grid-cols-[minmax(0,0fr)] -me-1 overflow-hidden",
        itemPreview: "opacity-0 pointer-events-none min-w-max",
      },
    },
  },
  defaultVariants: { size: "md", variant: "outline", tagVariant: "tint" },
});

export type TagsInputRecipeProps = VariantProps<typeof tagsInput>;
```
