本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

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

使い方

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}
      />
    </>
  )
}

プロパティ

プロパティ型既定値説明
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).

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

TagsInput.jsxtags-input.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。