Token Input

A text field that holds tokens in its text — people mentioned, files attached to a prompt — each moving, deleting and copying whole. With a Trigger Menu, typing @ or / suggests them.

FormsExperimental

Usage

Usage

Prompt

Backspace removes a file; the arrows step over it.

Explain [@routes.ts](file:src/routes.ts) and how it loads the pages.

import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import {  ScField,  ScFieldDescription,  ScLabel,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputToken,  ScTokenInputTokenRemove,  type ScTokenInputValue,

Anatomy

The root holds the value; the editor is where you type; the token's template is written once and stamped in each token's chip. A Trigger Menu and a status go inside the root too.

import { ScTokenInput, ScTokenInputEditor, ScTokenInputStatus, ScTokenInputToken, ScTokenInputTokenRemove } from '@semantic-components/ui';
<div scTokenInput #input="scTokenInput" [formField]="form.prompt">  <div scTokenInputEditor placeholder="…"></div>  <ng-template scTokenInputToken let-token>    {{ token.label }}    <span scTokenInputTokenRemove><svg siXIcon></svg></span>  </ng-template>  <div scTokenInputStatus>…</div></div>

The value

Text and tokens in order: (string | ScToken)[], a token { type, value, label, id?, status?: 'pending' | 'unresolved' }. As text — to copy, paste or send — each token is written by the codec, by default [@label](type:value); tokenInputToText, tokenInputFromText and tokenInputToLabel turn a value into text and back. In a form, the value is the array: an empty one is not empty to Signal Forms' required() — check its text with validate(), as the Form example does.

Keyboard

KeyDoes
← / → Over a token in one press: the caret never stops inside one
Backspace / Delete The token before / after the caret, as tokenDeletion says; a word or a line stops at a token
Shift + EnterA new line
Enter / Ctrl + Enter Sends, as submitShortcut says (else a new line)
Ctrl + Z Undo: typing a second at a time, each paste or token a step
Ctrl + Shift + Z / Ctrl + YRedo

Accessibility

  • The editor is a multi-line textbox, named by its field's label or aria-label; spellcheck and input methods work as in any text field.
  • A token reads as what its template writes: its label, and its kind in an sr-only span ("routes.ts file"). Its remove button is hidden from screen readers — the keyboard removes a token with Backspace.
  • scTokenInputStatus is a polite live region: write in it, from lastChange(), what to announce when a token is added or removed.
  • With a Trigger Menu, the editor points at the list (aria-controls, aria-activedescendant); focus never leaves it.

Examples

Mentions

Message

import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import {  ScField,  ScLabel,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputStatus,  ScTokenInputToken,  ScTokenInputTokenRemove,  type ScTokenInputValue,

Prompt

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputToken,  ScTokenInputTokenRemove,  type ScTokenInputValue,  type ScTrigger,  ScTriggerMenu,

Deleting tokens

Message
import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScField,  ScLabel,  ScNativeSelect,  ScNativeSelectContainer,  ScNativeSelectIcon,  ScNativeSelectOption,  type ScTokenDeletion,  ScTokenInput,

Codec

Ship it, <@u1> and <@u2>!

import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import {  type ScTokenCodec,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputToken,  type ScTokenInputValue,  tokenInputFromText,  tokenInputToText,} from '@semantic-components/ui';

Form

Review request

Mention a reviewer with @.

import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, FormRoot, form, validate } from '@angular/forms/signals';import {  ScButton,  ScField,  ScFieldDescription,  ScFieldErrors,  ScLabel,  ScTokenInput,  ScTokenInputEditor,

Methods

Notes on

import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import {  ScButton,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputToken,  ScTokenInputTokenRemove,  type ScTokenInputValue,  tokenInputToText,} from '@semantic-components/ui';

Submit shortcut

Ctrl + Enter sends.

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScKbd,  ScTokenInput,  ScTokenInputEditor,  type ScTokenInputValue,  tokenInputToLabel,} from '@semantic-components/ui';
/** submitShortcut="mod+enter": Ctrl + Enter (⌘ + Enter) sends — (submitted) gets the value; Enter makes a new line. */

RTL

الرسالة
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import {  ScField,  ScLabel,  ScTokenInput,  ScTokenInputEditor,  ScTokenInputToken,  ScTokenInputTokenRemove,  type ScTokenInputValue,

API reference

Each part takes class, merged over its own (cn: a later class wins). The editor has the style's textarea look, a token the style's combobox chip.

PartSelectorDescription
ScTokenInputdiv[scTokenInput] The root: the value. exportAs scTokenInput: insert(), updateToken(), removeToken(), undo(), redo(), canUndo(), canRedo(), clear(), focus(), lastChange(), selectedToken().
ScTokenInputEditordiv[scTokenInputEditor] Where you type: a contenteditable textbox (multi-line). Writes its own content.
ScTokenInputTokenng-template[scTokenInputToken] A token's content, written once (let-token). Without it, a token shows its label.
ScTokenInputTokenRemove[scTokenInputTokenRemove] Inside the token template: removes its token; hidden from screen readers, focus stays in the editor.
ScTokenInputStatusdiv[scTokenInputStatus] A polite live region (sr-only): your words, from lastChange().
InputTypeDefaultDescription
valueScTokenInputValue[] Two-way, or [formField]: text and tokens in order.
tokenDeletion'whole' | 'select' | 'revert''whole' Backspace or Delete at a token: removes it; selects then removes it; turns it back into its text (the menu opens on it).
submitShortcut'enter' | 'mod+enter' | nullnull The key that emits (submitted); null: Enter is a new line.
codecScTokenCodec[@label](type:value) Tokens as text: copy and paste.
disabled, readonly, required, invalid, touchedbooleanfalse Bound by [formField].
InputTypeDefaultDescription
placeholderstring'' Shown while it is empty (aria-placeholder).