Skip to content
This page is a translation of the Japanese version. The Japanese version is authoritative; if they differ, follow the Japanese version.

spst-tag-input

These examples do not call the service. Model rendering is a static placeholder.

Source commit: d204666f04a0ee7b6f18543f8aec414b4128874e · shadcn build 4.23.1

Noto Sans JP · @fontsource/noto-sans-jp 5.3.1 · OFL-1.1

Preview

Open full page

Install (publication pending)

These URLs are planned for publication and are not available yet. Inspect existing file differences before installing; do not use --overwrite by default.

npx shadcn@latest add https://spritestack.dev/r/spst-tag-input.json

Add to registries in components.json

{"registries":{"@spritestack":"https://spritestack.dev/r/{name}.json"}}
npx shadcn@latest add @spritestack/spst-tag-input

Targets React 19, Tailwind v4 and shadcn projects with src/. Layouts without src/ are unverified. Installation copies source files; updates are not automatic.

import "./styles/spritestack/theme.css";

<div className="spst-theme" data-theme="light">
  {/* Render components here. */}
</div>

Pass an element inside .spst-theme to container / portalContainer for portals. Self-host Noto Sans JP weights 400, 600 and 700 in your application; system-ui is the fallback.

Dependencies

Props / API

Generated by parsing the actual source. Rows marked … reference inherited React / Radix public types. — means no default is declared in this function parameter. See the type declarations for union-specific requirements.

src/components/spritestack/tag-input.tsx

TagInput

TagInput({
  messages,
  value,
  onValueChange,
  maxTags,
  label,
  disabled,
  error,
}: TagInputProps)
PropTypeRequiredDefaultEvent
messagesPartial<TagInputMessages>No——
valuereadonly string[]Yes——
onValueChange(tags: string[]) => voidYes—Yes
maxTagsnumberYes——
labelstringNo——
disabledbooleanNo——
errorstringNo——
Inspect type declarations and source
// 出典: SpriteStack デザインシステム設計 §4.2・§4.4(Issue 313)。基本部品は同梱の shadcn/ui 派生を合成。
"use client";
import { useId, useState } from "react";
import { Alert } from "../ui/alert";
import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
export type TagInputMessages = {
  label: string;
  add: string;
  remove: (tag: string) => string;
  limit: (max: number) => string;
  help: (count: number, max: number) => string;
};
const defaultMessages: TagInputMessages = {
  label: "タグ",
  add: "追加",
  remove: (tag: string) => `${tag} を削除`,
  limit: (max: number) => `タグは${max}個までです`,
  help: (count: number, max: number) =>
    `${count} / ${max} 個。空欄で Backspace を押すと最後のタグを削除します。`,
};
export type TagInputProps = {
  messages?: Partial<TagInputMessages>;
  value: readonly string[];
  onValueChange: (tags: string[]) => void;
  maxTags: number;
  label?: string;
  disabled?: boolean;
  error?: string;
};
export function TagInput({
  messages,
  value,
  onValueChange,
  maxTags,
  label,
  disabled,
  error,
}: TagInputProps) {
  const m = { ...defaultMessages, ...messages };
  const id = useId();
  const [draft, setDraft] = useState("");
  const full = value.length >= maxTags;
  function add() {
    const tag = draft.trim();
    if (disabled || full || !tag || value.includes(tag)) return;
    onValueChange([...value, tag]);
    setDraft("");
  }
  return (
    <div data-component="tag-input" className="grid gap-2">
      <Label htmlFor={id}>{label ?? m.label}</Label>
      <div className="flex flex-wrap gap-2">
        {value.map((tag) => (
          <span key={tag} className="inline-flex items-center gap-1">
            <Badge>{tag}</Badge>
            <Button
              variant="ghost"
              disabled={disabled}
              aria-label={m.remove(tag)}
              onClick={() => onValueChange(value.filter((item) => item !== tag))}
            >
              ×
            </Button>
          </span>
        ))}
      </div>
      <div className="flex gap-2">
        <Input
          id={id}
          value={draft}
          disabled={disabled}
          aria-invalid={!!error}
          aria-describedby={`${id}-help`}
          onChange={(event) => setDraft(event.target.value)}
          onKeyDown={(event) => {
            if (event.nativeEvent.isComposing || disabled) return;
            if (event.key === "Enter") {
              event.preventDefault();
              add();
            }
            if (event.key === "Backspace" && !draft && value.length) {
              event.preventDefault();
              onValueChange(value.slice(0, -1));
            }
          }}
        />
        <Button
          variant="outline"
          disabled={disabled || full || !draft.trim() || value.includes(draft.trim())}
          onClick={add}
        >
          {m.add}
        </Button>
      </div>
      {error ? (
        <Alert variant="error" id={`${id}-help`}>
          {error}
        </Alert>
      ) : (
        <p id={`${id}-help`} role="status">
          {full ? m.limit(maxTags) : m.help(value.length, maxTags)}
        </p>
      )}
    </div>
  );
}

Accessibility requirements

  • Give inputs a label or accessible name and associate errors with aria-describedby. Do not communicate state using color alone.
  • Provide model names and image fallbacks. Icon-only buttons need accessible names.
  • Provide dialog and sheet titles and descriptions, and place portals inside the theme. Test Tab, arrow keys, Escape and focus restoration in the consuming app.
  • Slider requires a name and finite min < max. ToggleGroup requires a nonempty single value. The parent controls pending state.

Reference images: 04 · Dark mode and error states extend the reference design.