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.
Usage
Usage
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
| Key | Does |
|---|---|
| ← / → | 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 + Enter | A 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 + Y | Redo |
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-onlyspan ("routes.ts file"). Its remove button is hidden from screen readers — the keyboard removes a token with Backspace. scTokenInputStatusis a polite live region: write in it, fromlastChange(), 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
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
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
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.
| Part | Selector | Description |
|---|---|---|
ScTokenInput | div[scTokenInput] | The root: the value. exportAs scTokenInput: insert(), updateToken(), removeToken(), undo(), redo(), canUndo(), canRedo(), clear(), focus(), lastChange(), selectedToken(). |
ScTokenInputEditor | div[scTokenInputEditor] | Where you type: a contenteditable textbox (multi-line). Writes its own content. |
ScTokenInputToken | ng-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. |
ScTokenInputStatus | div[scTokenInputStatus] | A polite live region (sr-only): your words, from lastChange(). |
| Input | Type | Default | Description |
|---|---|---|---|
value | ScTokenInputValue | [] | 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' | null | null | The key that emits (submitted); null: Enter is a new line. |
codec | ScTokenCodec | [@label](type:value) | Tokens as text: copy and paste. |
disabled, readonly, required, invalid, touched | boolean | false | Bound by [formField]. |
| Input | Type | Default | Description |
|---|---|---|---|
placeholder | string | '' | Shown while it is empty (aria-placeholder). |