# Tags input

SvTagsInput: a token editor (Enter / comma to add, Backspace / x to remove) with unique + max enforced - skills and email recipients with validation.

Uses `@svgrid/grid` (MIT). Category: Selection. Live demo: https://svgrid.com/demos/316-tags-input/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvTagsInput in Svelte 5 as a token editor: type and press Enter or comma to add a chip, Backspace or the chip's x to remove one, with unique values and a max count enforced, shown as a skills field and an email recipients field with validation.

SvTagsInput - a production token editor: type + Enter/comma to add, Backspace or the chip x to remove. Unique + max enforced. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How do I limit the number of tags?

Set max. Once it is reached the input stops accepting new tokens until one is removed, and the count can be shown in the hint.

### Can I validate each tag?

Yes. Check the array in onChange and reject entries that fail, for example invalid email addresses, and pass invalid with an error message to show it.

### Are duplicates allowed?

No. Adding a value that already exists is ignored, so the array is always unique.

## Related documentation

- [SvTagsInput](https://svgrid.com/docs/help/ui-components/sv-tags-input.md)

## Related articles

- [A Tri-State Select-All Checkbox in SvGrid](https://svgrid.com/blog/tri-state-select-all/) - How SvGrid's rowSelectionFeature drives the header checkbox through unchecked, indeterminate, and checked states - and the pagination edge case that catches everyone.
- [Stable Row Identity in SvGrid (and why it matters)](https://svgrid.com/blog/stable-row-identity-getrowid/) - How SvGrid tracks rows across mutations, why object reference stability is the key to correct selection and editing, and the exact patterns to use with live data feeds.
- [A Fill Handle (Drag to Fill) in SvGrid](https://svgrid.com/blog/fill-handle-drag-to-fill/) - Build a working spreadsheet-style fill handle on top of SvGrid's cell selection and editing - pointer tracking, range highlighting, series fill, and undo/redo integration all covered.

## Source code (316-tags-input.svelte)

```svelte
<script lang="ts">
  /**
   * SvTagsInput - a production token editor: type + Enter/comma to add, Backspace
   * or the chip x to remove. Unique + max enforced. Copy-paste ready.
   */
  import { SvTagsInput } from '@svgrid/grid'

  let skills = $state<string[]>(['Svelte', 'TypeScript', 'CSS'])
  let recipients = $state<string[]>([])
  let submitted = $state(false)
  const recipientsError = $derived(submitted && recipients.length === 0 ? 'Add at least one recipient' : undefined)
</script>

<div class="wrap">
  <header>
    <h2>Tags input</h2>
    <p>An editable set of tokens - skills, labels, email recipients, filters. Rejects duplicates and caps the count.</p>
  </header>

  <section class="block">
    <h3>Skills (max 6, unique)</h3>
    <SvTagsInput value={skills} max={6} placeholder="Add a skill..." onChange={(v) => (skills = v)} />
    <p class="out">{skills.length}/6 tags</p>
  </section>

  <section class="block">
    <SvTagsInput
      value={recipients}
      label="Recipients"
      hint="Press Enter or comma to add"
      required
      invalid={!!recipientsError}
      error={recipientsError}
      placeholder="name@company.com"
      onChange={(v) => (recipients = v)}
    />
    <button class="btn" onclick={() => (submitted = true)}>Send invites</button>
  </section>
</div>

<style>
  .wrap { padding: 20px; max-width: 460px; display: flex; flex-direction: column; gap: 22px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .block h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .out { margin: 8px 0 0; font-size: 12px; color: var(--sg-muted, #94a3b8); }
  .btn { margin-top: 12px; font: inherit; font-size: 13px; font-weight: 600; padding: 7px 16px; border: 0; border-radius: 8px; background: var(--sg-accent, #2563eb); color: var(--sg-on-accent, #fff); cursor: pointer; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/316-tags-input.svelte)
