Ir al contenido
Español

Cómo funciona

Tags are a list before a plain input, so screen readers count them and the field wraps onto new rows as it fills. The first Backspace in an empty field marks the last tag in the accent colour; the second removes it, so one stray key never deletes work. Duplicates, rejected values and a full list give a 360ms shake instead of a silent no-op.

Úsalo para

  • Free-form keywords: topics on a post, labels on a task, skills on a profile.
  • A list of short values typed by hand, such as email recipients, where pasting many at once should work.

Busca otra opción cuando

  • The values come from a fixed list only: use a multi-select or chip group.
  • One value: use a text input or select.

Uso

import { useState } from 'react'
import { TagsInput } from './TagsInput.jsx'

export function RecipeTags() {
  const [tags, setTags] = useState(['breakfast'])
  return (
    <>
      <label htmlFor="tags">Tags</label>
      <TagsInput
        id="tags"
        value={tags}
        onChange={setTags}
        suggestions={['brunch', 'quick', 'sweet', 'weekend']}
        max={6}
      />
    </>
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
valuestring[]—Controlled list of tags.
defaultValuestring[][]Initial tags when uncontrolled.
onChange(tags: string[]) => void—Called with the new list after a tag is added or removed.
suggestionsstring[][]Values offered while typing; up to six that contain the text and are not yet tags are shown, with the match highlighted.
maxnumber—Maximum number of tags; a counter appears and the field locks when full.
separatorsstring[][',', ';']Keys that commit the text besides Enter; pasted text is also split on them and on new lines.
allowDuplicatesbooleanfalseAllow the same tag twice (compared without case).
validate(tag: string) => boolean—Return false to reject a tag; the field gives a short shake.
placeholderstring'Add a tag'Hint shown while there are no tags.
disabledbooleanfalseDisables typing and removing.
invalidbooleanfalseDanger outline and aria-invalid; link your message with aria-describedby.
labelsobject—Replaces built-in text: remove, suggestions, added, removed, limit.
...restHTMLAttributes<HTMLDivElement>—Go to the root. id and the aria-label / labelledby / describedby attributes go to the input. Suggestion layer: --ds-tags-z (default 30).

Accesibilidad

  • The input is a combobox with aria-autocomplete="list"; the suggestions are a listbox and the highlighted one is announced through aria-activedescendant.
  • Keyboard: Enter, comma or semicolon add; Down / Up move through suggestions, Tab or Enter take one, Esc closes them; Backspace marks then removes the last tag.
  • Added and removed tags are announced in a polite live region. Remove buttons are labelled "Remove <tag>" and stay out of the Tab order, since Backspace covers them.
  • Remove buttons are 30 × 32px and suggestion rows grow to 44px on touch. Reduced motion turns off the pop, the shake and the sheet animation.

Movimiento

New tags pop from scale 0.7 with a 320ms spring. The field turns white with an accent halo over 220ms ease-out. Suggestions drop 4px and scale from 0.97 in 200ms. A rejected value shakes the field ±3px over 360ms.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

TagsInput.jsxtags-input.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.