@ui-sdk/react 0.0.0-stage → 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -2
  3. package/dist/accordion.d.ts +331 -0
  4. package/dist/accordion.js +25 -0
  5. package/dist/agent.d.ts +2 -0
  6. package/dist/agent.js +8 -0
  7. package/dist/alert-dialog.d.ts +56 -0
  8. package/dist/alert-dialog.js +45 -0
  9. package/dist/async-options.d.ts +22 -0
  10. package/dist/async-options.js +56 -0
  11. package/dist/autocomplete.d.ts +102 -0
  12. package/dist/autocomplete.js +195 -0
  13. package/dist/avatar.d.ts +301 -0
  14. package/dist/avatar.js +17 -0
  15. package/dist/button.d.ts +21 -0
  16. package/dist/button.js +45 -0
  17. package/dist/calendar-context.d.ts +10 -0
  18. package/dist/calendar-context.js +9 -0
  19. package/dist/calendar.d.ts +80 -0
  20. package/dist/calendar.js +257 -0
  21. package/dist/carousel.d.ts +52 -0
  22. package/dist/carousel.js +151 -0
  23. package/dist/checkbox.d.ts +24 -0
  24. package/dist/checkbox.js +98 -0
  25. package/dist/children.d.ts +38 -0
  26. package/dist/children.js +63 -0
  27. package/dist/collapsible.d.ts +302 -0
  28. package/dist/collapsible.js +19 -0
  29. package/dist/color-picker.d.ts +72 -0
  30. package/dist/color-picker.js +133 -0
  31. package/dist/color.d.ts +2 -0
  32. package/dist/color.js +22 -0
  33. package/dist/combobox.d.ts +152 -0
  34. package/dist/combobox.js +338 -0
  35. package/dist/command.d.ts +88 -0
  36. package/dist/command.js +233 -0
  37. package/dist/controller-target.d.ts +3 -0
  38. package/dist/controller-target.js +59 -0
  39. package/dist/date-input.d.ts +19 -0
  40. package/dist/date-input.js +58 -0
  41. package/dist/date-picker.d.ts +47 -0
  42. package/dist/date-picker.js +310 -0
  43. package/dist/date.d.ts +2 -0
  44. package/dist/date.js +60 -0
  45. package/dist/dialog-parts.d.ts +23 -0
  46. package/dist/dialog-parts.js +142 -0
  47. package/dist/dialog.d.ts +57 -0
  48. package/dist/dialog.js +32 -0
  49. package/dist/drawer.d.ts +70 -0
  50. package/dist/drawer.js +158 -0
  51. package/dist/editor-context.d.ts +4 -0
  52. package/dist/editor-context.js +9 -0
  53. package/dist/field-context.d.ts +47 -0
  54. package/dist/field-context.js +87 -0
  55. package/dist/field.d.ts +49 -0
  56. package/dist/field.js +169 -0
  57. package/dist/file-input.d.ts +46 -0
  58. package/dist/file-input.js +170 -0
  59. package/dist/form-inputs.d.ts +5 -0
  60. package/dist/form-inputs.js +22 -0
  61. package/dist/gesture.d.ts +1 -0
  62. package/dist/gesture.js +16 -0
  63. package/dist/history.d.ts +2 -0
  64. package/dist/history.js +12 -0
  65. package/dist/hover-card.d.ts +33 -0
  66. package/dist/hover-card.js +84 -0
  67. package/dist/image-cropper.d.ts +333 -0
  68. package/dist/image-cropper.js +37 -0
  69. package/dist/image.d.ts +2 -0
  70. package/dist/image.js +6 -0
  71. package/dist/input.d.ts +22 -0
  72. package/dist/input.js +51 -0
  73. package/dist/label.d.ts +294 -0
  74. package/dist/label.js +9 -0
  75. package/dist/list-items.d.ts +15 -0
  76. package/dist/list-items.js +39 -0
  77. package/dist/list-text.d.ts +4 -0
  78. package/dist/list-text.js +15 -0
  79. package/dist/listbox.d.ts +56 -0
  80. package/dist/listbox.js +122 -0
  81. package/dist/menu.d.ts +116 -0
  82. package/dist/menu.js +426 -0
  83. package/dist/menubar-context.d.ts +4 -0
  84. package/dist/menubar-context.js +7 -0
  85. package/dist/menubar.d.ts +11 -0
  86. package/dist/menubar.js +32 -0
  87. package/dist/meter.d.ts +312 -0
  88. package/dist/meter.js +30 -0
  89. package/dist/navigation-menu.d.ts +61 -0
  90. package/dist/navigation-menu.js +169 -0
  91. package/dist/number-input.d.ts +44 -0
  92. package/dist/number-input.js +185 -0
  93. package/dist/number.d.ts +2 -0
  94. package/dist/number.js +40 -0
  95. package/dist/observe.d.ts +2 -0
  96. package/dist/observe.js +8 -0
  97. package/dist/otp-input.d.ts +29 -0
  98. package/dist/otp-input.js +94 -0
  99. package/dist/overlays.d.ts +26 -0
  100. package/dist/overlays.js +82 -0
  101. package/dist/pagination.d.ts +336 -0
  102. package/dist/pagination.js +71 -0
  103. package/dist/password-input.d.ts +23 -0
  104. package/dist/password-input.js +65 -0
  105. package/dist/popover.d.ts +54 -0
  106. package/dist/popover.js +151 -0
  107. package/dist/popup.d.ts +41 -0
  108. package/dist/popup.js +180 -0
  109. package/dist/preferences.d.ts +35 -0
  110. package/dist/preferences.js +93 -0
  111. package/dist/primitive.d.ts +2 -0
  112. package/dist/primitive.js +14 -0
  113. package/dist/progress.d.ts +312 -0
  114. package/dist/progress.js +30 -0
  115. package/dist/radio.d.ts +22 -0
  116. package/dist/radio.js +86 -0
  117. package/dist/rating.d.ts +20 -0
  118. package/dist/rating.js +94 -0
  119. package/dist/render-element.d.ts +35 -0
  120. package/dist/render-element.js +103 -0
  121. package/dist/resizable.d.ts +302 -0
  122. package/dist/resizable.js +23 -0
  123. package/dist/scroll-area.d.ts +324 -0
  124. package/dist/scroll-area.js +49 -0
  125. package/dist/segmented-control.d.ts +24 -0
  126. package/dist/segmented-control.js +78 -0
  127. package/dist/segmented-input.d.ts +15 -0
  128. package/dist/segmented-input.js +48 -0
  129. package/dist/select.d.ts +125 -0
  130. package/dist/select.js +330 -0
  131. package/dist/separator.d.ts +290 -0
  132. package/dist/separator.js +9 -0
  133. package/dist/shortcuts.d.ts +32 -0
  134. package/dist/shortcuts.js +61 -0
  135. package/dist/slider.d.ts +38 -0
  136. package/dist/slider.js +163 -0
  137. package/dist/switch.d.ts +15 -0
  138. package/dist/switch.js +39 -0
  139. package/dist/tabs.d.ts +331 -0
  140. package/dist/tabs.js +28 -0
  141. package/dist/text-control.d.ts +22 -0
  142. package/dist/text-control.js +24 -0
  143. package/dist/text-match.d.ts +2 -0
  144. package/dist/text-match.js +8 -0
  145. package/dist/textarea.d.ts +10 -0
  146. package/dist/textarea.js +48 -0
  147. package/dist/time-input.d.ts +19 -0
  148. package/dist/time-input.js +63 -0
  149. package/dist/timeline.d.ts +327 -0
  150. package/dist/timeline.js +31 -0
  151. package/dist/toast-parts.d.ts +63 -0
  152. package/dist/toast-parts.js +144 -0
  153. package/dist/toast.d.ts +2 -0
  154. package/dist/toast.js +22 -0
  155. package/dist/toggle.d.ts +23 -0
  156. package/dist/toggle.js +76 -0
  157. package/dist/token-input.d.ts +35 -0
  158. package/dist/token-input.js +100 -0
  159. package/dist/toolbar-context.d.ts +6 -0
  160. package/dist/toolbar-context.js +16 -0
  161. package/dist/toolbar.d.ts +38 -0
  162. package/dist/toolbar.js +63 -0
  163. package/dist/tooltip.d.ts +39 -0
  164. package/dist/tooltip.js +108 -0
  165. package/dist/tree.d.ts +51 -0
  166. package/dist/tree.js +150 -0
  167. package/dist/trigger-menu.d.ts +83 -0
  168. package/dist/trigger-menu.js +167 -0
  169. package/dist/use-primitive.d.ts +178 -0
  170. package/dist/use-primitive.js +674 -0
  171. package/dist/waterfall.d.ts +80 -0
  172. package/dist/waterfall.js +108 -0
  173. package/docs/README.md +89 -0
  174. package/docs/agents.md +1523 -0
  175. package/docs/components/accordion.md +1391 -0
  176. package/docs/components/alert-dialog.md +850 -0
  177. package/docs/components/autocomplete.md +1963 -0
  178. package/docs/components/avatar.md +252 -0
  179. package/docs/components/button.md +444 -0
  180. package/docs/components/calendar.md +1687 -0
  181. package/docs/components/carousel.md +1244 -0
  182. package/docs/components/checkbox.md +1069 -0
  183. package/docs/components/collapsible.md +567 -0
  184. package/docs/components/color-picker.md +736 -0
  185. package/docs/components/combobox.md +3647 -0
  186. package/docs/components/command.md +1573 -0
  187. package/docs/components/date-input.md +608 -0
  188. package/docs/components/date-picker.md +1266 -0
  189. package/docs/components/dialog.md +1469 -0
  190. package/docs/components/drawer.md +1197 -0
  191. package/docs/components/field.md +549 -0
  192. package/docs/components/file-input.md +609 -0
  193. package/docs/components/hover-card.md +514 -0
  194. package/docs/components/image-cropper.md +579 -0
  195. package/docs/components/input.md +301 -0
  196. package/docs/components/label.md +217 -0
  197. package/docs/components/listbox.md +1130 -0
  198. package/docs/components/menu.md +1286 -0
  199. package/docs/components/menubar.md +455 -0
  200. package/docs/components/meter.md +304 -0
  201. package/docs/components/navigation-menu.md +1581 -0
  202. package/docs/components/number-input.md +1086 -0
  203. package/docs/components/otp-input.md +565 -0
  204. package/docs/components/pagination.md +648 -0
  205. package/docs/components/password-input.md +737 -0
  206. package/docs/components/popover.md +1552 -0
  207. package/docs/components/preferences.md +220 -0
  208. package/docs/components/progress.md +443 -0
  209. package/docs/components/radio.md +846 -0
  210. package/docs/components/rating.md +458 -0
  211. package/docs/components/resizable.md +982 -0
  212. package/docs/components/scroll-area.md +980 -0
  213. package/docs/components/segmented-control.md +465 -0
  214. package/docs/components/select.md +2133 -0
  215. package/docs/components/separator.md +243 -0
  216. package/docs/components/slider.md +1169 -0
  217. package/docs/components/switch.md +613 -0
  218. package/docs/components/tabs.md +1014 -0
  219. package/docs/components/textarea.md +318 -0
  220. package/docs/components/time-input.md +231 -0
  221. package/docs/components/timeline.md +677 -0
  222. package/docs/components/toast.md +1395 -0
  223. package/docs/components/toggle.md +541 -0
  224. package/docs/components/token-input.md +1007 -0
  225. package/docs/components/toolbar.md +787 -0
  226. package/docs/components/tooltip.md +884 -0
  227. package/docs/components/tree.md +1346 -0
  228. package/docs/components/trigger-menu.md +1257 -0
  229. package/docs/components/waterfall.md +754 -0
  230. package/docs/controllers.md +89 -0
  231. package/docs/core.md +8027 -0
  232. package/docs/custom-parts.md +120 -0
  233. package/docs/first-component.md +83 -0
  234. package/docs/forms.md +148 -0
  235. package/docs/hooks/async-options.md +366 -0
  236. package/docs/hooks/shortcuts.md +221 -0
  237. package/docs/installation.md +45 -0
  238. package/docs/introduction.md +32 -0
  239. package/docs/lists-and-menus.md +170 -0
  240. package/docs/llms.txt +1844 -0
  241. package/docs/parts-and-composition.md +93 -0
  242. package/docs/popups.md +117 -0
  243. package/docs/rendering.md +121 -0
  244. package/docs/server-rendering.md +253 -0
  245. package/docs/state-and-events.md +105 -0
  246. package/docs/styling-and-animation.md +176 -0
  247. package/package.json +333 -3
@@ -0,0 +1,1007 @@
1
+ # Token Input
2
+
3
+ Mix editable text with tokens for files, people, tags, and other references.
4
+
5
+ A token input is a text field that holds tokens alongside the text, like the people mentioned in a message or the files attached to a prompt. Each token moves, deletes and copies as one piece.
6
+
7
+ ```tsx title="demo.tsx"
8
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
9
+ import {
10
+ TokenInput,
11
+ TokenInputEditor,
12
+ TokenInputToken,
13
+ TokenInputTokenRemove,
14
+ } from "@ui-sdk/react/token-input"
15
+ import {
16
+ type TriggerMenuDataItem,
17
+ TriggerMenu,
18
+ TriggerMenuContent,
19
+ TriggerMenuItem,
20
+ TriggerMenuList,
21
+ } from "@ui-sdk/react/trigger-menu"
22
+ import { XIcon } from "lucide-react"
23
+
24
+ const people = [
25
+ { value: "hollis", label: "Hollis Grant" },
26
+ { value: "ines", label: "Inès Moreau" },
27
+ { value: "kofi", label: "Kofi Mensah" },
28
+ { value: "wren", label: "Wren Takahashi" },
29
+ { value: "tomasz", label: "Tomasz Wójcik" },
30
+ ]
31
+
32
+ function Example() {
33
+ return (
34
+ <Field>
35
+ <FieldLabel>Message</FieldLabel>
36
+ <TokenInput
37
+ renderToken={(token) => (
38
+ <TokenInputToken>
39
+ {token.label}
40
+ <TokenInputTokenRemove>
41
+ <XIcon aria-hidden="true" />
42
+ </TokenInputTokenRemove>
43
+ </TokenInputToken>
44
+ )}
45
+ >
46
+ <TokenInputEditor placeholder="Type @ to mention someone…" />
47
+ <TriggerMenu triggers={[{ char: "@", type: "mention", items: people }]}>
48
+ <TriggerMenuContent>
49
+ <TriggerMenuList>
50
+ {(state) =>
51
+ (state.items as TriggerMenuDataItem[]).map((item) => (
52
+ <TriggerMenuItem
53
+ key={item.value}
54
+ value={item.value}
55
+ label={item.label}
56
+ >
57
+ {item.label}
58
+ </TriggerMenuItem>
59
+ ))
60
+ }
61
+ </TriggerMenuList>
62
+ </TriggerMenuContent>
63
+ </TriggerMenu>
64
+ </TokenInput>
65
+ </Field>
66
+ )
67
+ }
68
+
69
+ export { Example }
70
+ ```
71
+
72
+ Type @ and choose a person with the arrow keys and Enter. Remove a token with Backspace, or press its remove button.
73
+
74
+ ## Installation
75
+
76
+ ```bash
77
+ npm install @ui-sdk/react @ui-sdk/core
78
+ ```
79
+
80
+ ## Composition
81
+
82
+ Use the following composition to build a `TokenInput`:
83
+
84
+ ```text
85
+ TokenInput
86
+ ├── TokenInputEditor
87
+ └── TriggerMenu
88
+ ```
89
+
90
+ And for each token, inside `renderToken`:
91
+
92
+ ```text
93
+ TokenInputToken
94
+ └── TokenInputTokenRemove
95
+ ```
96
+
97
+ `TokenInput` holds the value, and `TokenInputEditor` is the text people edit. A [Trigger Menu](./trigger-menu.md) inside it suggests tokens when someone types `@`, `/`, or another trigger. Pass `renderToken` to give each token your own markup.
98
+
99
+ ## Usage
100
+
101
+ Import the parts and put them together:
102
+
103
+ ```tsx
104
+ import {
105
+ TokenInput,
106
+ TokenInputEditor,
107
+ type TokenInputValue,
108
+ } from "@ui-sdk/react/token-input"
109
+
110
+ const draft: TokenInputValue = [
111
+ "Explain ",
112
+ { type: "file", value: "src/routes.ts", label: "routes.ts" },
113
+ ]
114
+
115
+ function Example() {
116
+ return (
117
+ <TokenInput defaultValue={draft}>
118
+ <TokenInputEditor aria-label="Prompt" />
119
+ </TokenInput>
120
+ )
121
+ }
122
+
123
+ export { Example }
124
+ ```
125
+
126
+ A value is an array of text and tokens. Each token has a type, a value for your app, and a label for the reader. The editor manages its own content, including tokens, selection, and undo, so let it render that content and use `renderToken` for custom token markup.
127
+
128
+ ## Token Input or Trigger Menu
129
+
130
+ Token Input holds the text and tokens. [Trigger Menu](./trigger-menu.md) suggests what to insert when someone types a trigger character.
131
+
132
+ Use Token Input on its own when your code supplies the tokens. Add a Trigger Menu for suggestions, as in the demo above. Trigger Menu also works with ordinary inputs and textareas.
133
+
134
+ ## Prompt Input
135
+
136
+ Combine file suggestions, skills, and a send action. This example marks files as pending while they load, then uses `updateToken()` to show a skill running. Enter sends; Shift+Enter adds a line. An open suggestion menu or an active input method takes precedence over sending.
137
+
138
+ ```tsx title="prompt-input.tsx"
139
+ import {
140
+ createTokenInputController,
141
+ toText,
142
+ TokenInput,
143
+ TokenInputEditor,
144
+ TokenInputToken,
145
+ TokenInputTokenRemove,
146
+ type TokenInputValue,
147
+ } from "@ui-sdk/react/token-input"
148
+ import {
149
+ type TriggerMenuDataItem,
150
+ TriggerMenu,
151
+ TriggerMenuContent,
152
+ TriggerMenuEmpty,
153
+ TriggerMenuHint,
154
+ TriggerMenuItem,
155
+ TriggerMenuList,
156
+ } from "@ui-sdk/react/trigger-menu"
157
+ import {
158
+ ArrowUpIcon,
159
+ CheckIcon,
160
+ ClockIcon,
161
+ CodeIcon,
162
+ FileIcon,
163
+ XIcon,
164
+ } from "lucide-react"
165
+ import * as React from "react"
166
+ import { useLog } from "./log"
167
+
168
+ const files = [
169
+ { value: "src/app.tsx", label: "app.tsx", keywords: ["src"] },
170
+ { value: "src/routes.ts", label: "routes.ts", keywords: ["src"] },
171
+ { value: "src/styles.css", label: "styles.css", keywords: ["src"] },
172
+ { value: "README.md", label: "README.md" },
173
+ { value: "package.json", label: "package.json" },
174
+ ]
175
+
176
+ const skills = [
177
+ {
178
+ value: "search",
179
+ label: "search",
180
+ arguments: [{ name: "query", hint: "<query>", required: true }],
181
+ },
182
+ { value: "review", label: "review" },
183
+ { value: "summarize", label: "summarize" },
184
+ ]
185
+
186
+ const loadFile = () => new Promise<void>((resolve) => setTimeout(resolve, 800))
187
+
188
+ function Example() {
189
+ const log = useLog()
190
+ const [prompt] = React.useState(createTokenInputController)
191
+ const [running, setRunning] = React.useState(false)
192
+ const sendTimer = React.useRef<ReturnType<typeof setTimeout> | undefined>(
193
+ undefined
194
+ )
195
+
196
+ React.useEffect(() => () => clearTimeout(sendTimer.current), [])
197
+
198
+ function send(value: TokenInputValue) {
199
+ if (running || !prompt.getLabel().trim()) {
200
+ return
201
+ }
202
+ setRunning(true)
203
+ const skillTokens = value.filter(
204
+ (segment) => typeof segment !== "string" && segment.type === "skill"
205
+ )
206
+ for (const skill of skillTokens) {
207
+ if (typeof skill !== "string") {
208
+ prompt.updateToken(skill.id!, {
209
+ data: { ...skill.data, run: "running" },
210
+ })
211
+ }
212
+ }
213
+ sendTimer.current = setTimeout(() => {
214
+ for (const skill of skillTokens) {
215
+ if (typeof skill !== "string") {
216
+ prompt.updateToken(skill.id!, {
217
+ data: { ...skill.data, run: "done" },
218
+ })
219
+ }
220
+ }
221
+ log(`Sent ${toText(prompt.getValue())}`)
222
+ prompt.clear()
223
+ setRunning(false)
224
+ }, 1500)
225
+ }
226
+
227
+ return (
228
+ <TokenInput
229
+ controller={prompt}
230
+ onSubmit={send}
231
+ renderToken={(token) => (
232
+ <TokenInputToken>
233
+ {token.type === "skill" ? (
234
+ <CodeIcon aria-hidden="true" />
235
+ ) : (
236
+ <FileIcon aria-hidden="true" />
237
+ )}
238
+ {token.type === "skill" ? `/${token.label}` : token.label}
239
+ {token.data?.run === "running" && <ClockIcon aria-hidden="true" />}
240
+ {token.data?.run === "done" && <CheckIcon aria-hidden="true" />}
241
+ <TokenInputTokenRemove>
242
+ <XIcon aria-hidden="true" />
243
+ </TokenInputTokenRemove>
244
+ </TokenInputToken>
245
+ )}
246
+ >
247
+ <div data-slot="card">
248
+ <TokenInputEditor aria-label="Prompt" placeholder="Ask anything. Type @ or /" />
249
+ <div data-slot="row">
250
+ <button
251
+ type="button"
252
+ data-size="sm"
253
+ onMouseDown={(event) => event.preventDefault()}
254
+ onClick={() =>
255
+ prompt.insert(
256
+ { type: "file", value: "README.md", label: "README.md" },
257
+ { resolve: loadFile() }
258
+ )
259
+ }
260
+ >
261
+ Attach README
262
+ </button>
263
+ <button
264
+ type="button"
265
+ data-variant="solid"
266
+ data-size="sm"
267
+ data-shape="round"
268
+ onClick={() => send(prompt.getValue())}
269
+ aria-label={running ? "Running" : "Send"}
270
+ disabled={running}
271
+ >
272
+ {running ? (
273
+ <ClockIcon aria-hidden="true" />
274
+ ) : (
275
+ <ArrowUpIcon aria-hidden="true" />
276
+ )}
277
+ </button>
278
+ </div>
279
+ </div>
280
+ <TriggerMenu
281
+ triggers={[
282
+ { char: "@", type: "file", items: files },
283
+ { char: "/", boundary: "start", type: "skill", items: skills },
284
+ ]}
285
+ onSelect={(value, details) =>
286
+ details.trigger === "@" ? loadFile() : undefined
287
+ }
288
+ >
289
+ <TriggerMenuHint />
290
+ <TriggerMenuContent>
291
+ <TriggerMenuEmpty>No matches</TriggerMenuEmpty>
292
+ <TriggerMenuList>
293
+ {(state) =>
294
+ (state.items as TriggerMenuDataItem[]).map((item) => (
295
+ <TriggerMenuItem
296
+ key={item.value}
297
+ value={item.value}
298
+ label={item.label}
299
+ disabled={item.disabled}
300
+ >
301
+ {item.label}
302
+ </TriggerMenuItem>
303
+ ))
304
+ }
305
+ </TriggerMenuList>
306
+ </TriggerMenuContent>
307
+ </TriggerMenu>
308
+ </TokenInput>
309
+ )
310
+ }
311
+
312
+ export { Example }
313
+ ```
314
+
315
+ Type @ for files or / at the start for skills. For /search, enter a query and press Enter to insert the command. Press Enter again to send, and the console logs the message.
316
+
317
+ ## Deleting Tokens
318
+
319
+ Choose what Backspace does at a token with `tokenDeletion`:
320
+
321
+ - `whole` removes the token in one press.
322
+ - `select` selects it first, then removes it on the next press.
323
+ - `revert` turns it back into editable text, so the trigger menu opens on it again.
324
+
325
+ ```tsx title="deletion.tsx"
326
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
327
+ import {
328
+ Select,
329
+ SelectContent,
330
+ SelectItem,
331
+ SelectItemIndicator,
332
+ SelectLabel,
333
+ SelectTrigger,
334
+ SelectValue,
335
+ } from "@ui-sdk/react/select"
336
+ import {
337
+ TokenInput,
338
+ TokenInputEditor,
339
+ TokenInputToken,
340
+ type TokenDeletion,
341
+ type TokenInputValue,
342
+ } from "@ui-sdk/react/token-input"
343
+ import {
344
+ type TriggerMenuDataItem,
345
+ TriggerMenu,
346
+ TriggerMenuContent,
347
+ TriggerMenuItem,
348
+ TriggerMenuList,
349
+ } from "@ui-sdk/react/trigger-menu"
350
+ import { CheckIcon, ChevronDownIcon, FileIcon } from "lucide-react"
351
+ import * as React from "react"
352
+ import { useLog } from "./log"
353
+
354
+ const modes = [
355
+ { value: "whole", label: "Whole" },
356
+ { value: "select", label: "Select" },
357
+ { value: "revert", label: "Revert" },
358
+ ]
359
+
360
+ const files = [
361
+ { value: "src/app.tsx", label: "app.tsx", keywords: ["src"] },
362
+ { value: "src/routes.ts", label: "routes.ts", keywords: ["src"] },
363
+ { value: "src/styles.css", label: "styles.css", keywords: ["src"] },
364
+ { value: "README.md", label: "README.md" },
365
+ { value: "package.json", label: "package.json" },
366
+ ]
367
+
368
+ const draft: TokenInputValue = [
369
+ "Explain ",
370
+ {
371
+ type: "file",
372
+ value: "src/routes.ts",
373
+ label: "routes.ts",
374
+ data: { trigger: "@" },
375
+ },
376
+ " in two sentences.",
377
+ ]
378
+
379
+ function Example() {
380
+ const log = useLog()
381
+ const [deletion, setDeletion] = React.useState<TokenDeletion>("whole")
382
+
383
+ return (
384
+ <div data-slot="stack">
385
+ <div data-slot="controls">
386
+ <Select
387
+ items={modes}
388
+ value={deletion}
389
+ onValueChange={(next) => next && setDeletion(next as TokenDeletion)}
390
+ >
391
+ <SelectLabel>Backspace</SelectLabel>
392
+ <SelectTrigger>
393
+ <SelectValue />
394
+ <ChevronDownIcon aria-hidden="true" />
395
+ </SelectTrigger>
396
+ <SelectContent>
397
+ {modes.map((mode) => (
398
+ <SelectItem key={mode.value} value={mode.value}>
399
+ {mode.label}
400
+ <SelectItemIndicator>
401
+ <CheckIcon aria-hidden="true" />
402
+ </SelectItemIndicator>
403
+ </SelectItem>
404
+ ))}
405
+ </SelectContent>
406
+ </Select>
407
+ </div>
408
+ <Field>
409
+ <FieldLabel>Prompt</FieldLabel>
410
+ <TokenInput
411
+ tokenDeletion={deletion}
412
+ defaultValue={draft}
413
+ renderToken={(token) => (
414
+ <TokenInputToken>
415
+ <FileIcon aria-hidden="true" />
416
+ {token.label}
417
+ </TokenInputToken>
418
+ )}
419
+ onValueChange={(value, details) => {
420
+ for (const token of details.removed) {
421
+ log(`Removed ${token.label} (${deletion})`)
422
+ }
423
+ }}
424
+ >
425
+ <TokenInputEditor />
426
+ <TriggerMenu triggers={[{ char: "@", type: "file", items: files }]}>
427
+ <TriggerMenuContent>
428
+ <TriggerMenuList>
429
+ {(state) =>
430
+ (state.items as TriggerMenuDataItem[]).map((item) => (
431
+ <TriggerMenuItem key={item.value} value={item.value} label={item.label}>
432
+ {item.label}
433
+ </TriggerMenuItem>
434
+ ))
435
+ }
436
+ </TriggerMenuList>
437
+ </TriggerMenuContent>
438
+ </TriggerMenu>
439
+ </TokenInput>
440
+ </Field>
441
+ </div>
442
+ )
443
+ }
444
+
445
+ export { Example }
446
+ ```
447
+
448
+ Choose a Backspace behavior, click just after routes.ts, and press Backspace. Undo to try another behavior.
449
+
450
+ ## Codec
451
+
452
+ A codec converts tokens to text for the clipboard, forms, and saved drafts. The default writes `[@label](type:value)`. Copy that text into another app and paste it back to restore the token.
453
+
454
+ Use `toText`, `fromText`, and `toLabel` to convert values yourself. Supply a custom codec for another format, as this example does for `<@hollis>`. With `copy="label"`, copying writes what people read, and pasting back into a token input still restores the tokens. The default parser accepts letters, digits, `_`, and `-` in token types; other types need a custom codec.
455
+
456
+ ```tsx title="codec.tsx"
457
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
458
+ import {
459
+ fromText,
460
+ toText,
461
+ TokenInput,
462
+ TokenInputEditor,
463
+ type TokenCodec,
464
+ } from "@ui-sdk/react/token-input"
465
+ import { useLog } from "./log"
466
+
467
+ const chat: TokenCodec = {
468
+ format: (token) => `<@${token.value}>`,
469
+ parse: (text) =>
470
+ text.split(/(<@[\w.-]+>)/).map((part) => {
471
+ const id = /^<@([\w.-]+)>$/.exec(part)?.[1]
472
+ return id ? { type: "mention", value: id, label: `@${id}` } : part
473
+ }),
474
+ }
475
+
476
+ const draft = fromText("Ping <@hollis> and <@ines> about the launch", chat)
477
+
478
+ function Example() {
479
+ const log = useLog()
480
+
481
+ return (
482
+ <Field>
483
+ <FieldLabel>Message</FieldLabel>
484
+ <TokenInput
485
+ codec={chat}
486
+ copy="label"
487
+ defaultValue={draft}
488
+ onValueChange={(value) => log(toText(value, chat))}
489
+ >
490
+ <TokenInputEditor />
491
+ </TokenInput>
492
+ </Field>
493
+ )
494
+ }
495
+
496
+ export { Example }
497
+ ```
498
+
499
+ Edit the message. The console logs it in the custom format, with each mention as <@id>.
500
+
501
+ ## Form
502
+
503
+ Give the input a `name` to submit its serialized text. `required`, `disabled`, and `readOnly` control form behavior. The `form` option associates it with a form elsewhere on the page, as shown here.
504
+
505
+ ```tsx title="form.tsx"
506
+ import { Checkbox, CheckboxIndicator } from "@ui-sdk/react/checkbox"
507
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
508
+ import {
509
+ TokenInput,
510
+ TokenInputEditor,
511
+ type TokenInputValue,
512
+ } from "@ui-sdk/react/token-input"
513
+ import { CheckIcon } from "lucide-react"
514
+ import * as React from "react"
515
+ import { useLog } from "./log"
516
+
517
+ const draft: TokenInputValue = [
518
+ "Explain ",
519
+ { type: "file", value: "src/routes.ts", label: "routes.ts" },
520
+ " in two sentences.",
521
+ ]
522
+
523
+ function Example() {
524
+ const log = useLog()
525
+ const [disabled, setDisabled] = React.useState(false)
526
+ const [readOnly, setReadOnly] = React.useState(false)
527
+ const formId = React.useId()
528
+
529
+ return (
530
+ <div data-slot="stack">
531
+ <div data-slot="controls">
532
+ <label data-slot="label">
533
+ <Checkbox
534
+ checked={disabled}
535
+ onCheckedChange={(next) => setDisabled(next === true)}
536
+ >
537
+ <CheckboxIndicator>
538
+ <CheckIcon aria-hidden="true" />
539
+ </CheckboxIndicator>
540
+ </Checkbox>
541
+ Disabled
542
+ </label>
543
+ <label data-slot="label">
544
+ <Checkbox
545
+ checked={readOnly}
546
+ onCheckedChange={(next) => setReadOnly(next === true)}
547
+ >
548
+ <CheckboxIndicator>
549
+ <CheckIcon aria-hidden="true" />
550
+ </CheckboxIndicator>
551
+ </Checkbox>
552
+ Read-only
553
+ </label>
554
+ </div>
555
+ <div data-slot="stack">
556
+ <Field disabled={disabled}>
557
+ <FieldLabel>Prompt</FieldLabel>
558
+ <TokenInput form={formId} name="prompt" required defaultValue={draft} readOnly={readOnly}>
559
+ <TokenInputEditor />
560
+ </TokenInput>
561
+ </Field>
562
+ <form
563
+ id={formId}
564
+ data-slot="row"
565
+ onSubmit={(event) => {
566
+ event.preventDefault()
567
+ log(
568
+ `Submitted ${new FormData(event.currentTarget).get("prompt") ?? ""}`
569
+ )
570
+ }}
571
+ >
572
+ <button type="submit" data-variant="solid">
573
+ Submit
574
+ </button>
575
+ <button type="reset">Reset</button>
576
+ </form>
577
+ </div>
578
+ </div>
579
+ )
580
+ }
581
+
582
+ export { Example }
583
+ ```
584
+
585
+ Press Submit to log the prompt's serialized value. Try Disabled and Read-only, then Reset.
586
+
587
+ ## Controlled
588
+
589
+ Pass `value` and update it in `onValueChange`. The change details include the reason, input type, and removed tokens. The input still manages its editing history.
590
+
591
+ ```tsx title="controlled.tsx"
592
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
593
+ import {
594
+ toText,
595
+ TokenInput,
596
+ TokenInputEditor,
597
+ type TokenInputValue,
598
+ } from "@ui-sdk/react/token-input"
599
+ import {
600
+ type TriggerMenuDataItem,
601
+ TriggerMenu,
602
+ TriggerMenuContent,
603
+ TriggerMenuItem,
604
+ TriggerMenuList,
605
+ } from "@ui-sdk/react/trigger-menu"
606
+ import * as React from "react"
607
+ import { useLog } from "./log"
608
+
609
+ const files = [
610
+ { value: "src/app.tsx", label: "app.tsx", keywords: ["src"] },
611
+ { value: "src/routes.ts", label: "routes.ts", keywords: ["src"] },
612
+ { value: "src/styles.css", label: "styles.css", keywords: ["src"] },
613
+ { value: "README.md", label: "README.md" },
614
+ { value: "package.json", label: "package.json" },
615
+ ]
616
+
617
+ const draft: TokenInputValue = [
618
+ "Explain ",
619
+ { type: "file", value: "src/routes.ts", label: "routes.ts" },
620
+ " in two sentences.",
621
+ ]
622
+
623
+ function Example() {
624
+ const log = useLog()
625
+ const [value, setValue] = React.useState<TokenInputValue>(draft)
626
+
627
+ return (
628
+ <Field>
629
+ <FieldLabel>Prompt</FieldLabel>
630
+ <TokenInput
631
+ value={value}
632
+ onValueChange={(next, details) => {
633
+ setValue(next)
634
+ const removed = details.removed.map((token) => token.label).join(", ")
635
+ log(
636
+ `${toText(next)} (${details.reason}${removed ? `, removed ${removed}` : ""})`
637
+ )
638
+ }}
639
+ history={{ limit: 50, groupDelay: 1000 }}
640
+ getTokenDescription={(token) =>
641
+ token.type === "file" ? "attached file" : token.type
642
+ }
643
+ >
644
+ <TokenInputEditor />
645
+ <TriggerMenu triggers={[{ char: "@", type: "file", items: files }]}>
646
+ <TriggerMenuContent>
647
+ <TriggerMenuList>
648
+ {(state) =>
649
+ (state.items as TriggerMenuDataItem[]).map((item) => (
650
+ <TriggerMenuItem key={item.value} value={item.value} label={item.label}>
651
+ {item.label}
652
+ </TriggerMenuItem>
653
+ ))
654
+ }
655
+ </TriggerMenuList>
656
+ </TriggerMenuContent>
657
+ </TriggerMenu>
658
+ </TokenInput>
659
+ </Field>
660
+ )
661
+ }
662
+
663
+ export { Example }
664
+ ```
665
+
666
+ Type, add a file with @, or remove a token. The console logs the value and the reason for each change.
667
+
668
+ ## Controller
669
+
670
+ Use `createTokenInputController()` to insert, update, or remove tokens from code. Pass a promise as `resolve` to mark an inserted token as pending until its data is ready. Updating a token doesn't add an undo step.
671
+
672
+ ```tsx title="controller.tsx"
673
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
674
+ import {
675
+ createTokenInputController,
676
+ TokenInput,
677
+ TokenInputEditor,
678
+ type Token,
679
+ type TokenInputValue,
680
+ } from "@ui-sdk/react/token-input"
681
+ import * as React from "react"
682
+
683
+ const draft: TokenInputValue = [
684
+ "Compare ",
685
+ { type: "file", value: "src/routes.ts", label: "routes.ts" },
686
+ " with ",
687
+ ]
688
+
689
+ const loadFile = () => new Promise<void>((resolve) => setTimeout(resolve, 800))
690
+
691
+ function Example() {
692
+ const [prompt] = React.useState(createTokenInputController)
693
+ const firstToken = () =>
694
+ prompt
695
+ .getValue()
696
+ .find((segment): segment is Token => typeof segment !== "string")
697
+
698
+ return (
699
+ <div data-slot="stack">
700
+ <div data-slot="controls" onMouseDown={(event) => event.preventDefault()}>
701
+ <button
702
+ type="button"
703
+ onClick={() =>
704
+ prompt.insert(
705
+ { type: "file", value: "package.json", label: "package.json" },
706
+ { at: "end", resolve: loadFile() }
707
+ )
708
+ }
709
+ >
710
+ Attach File
711
+ </button>
712
+ <button
713
+ type="button"
714
+ onClick={() => {
715
+ const token = firstToken()
716
+ if (token) {
717
+ prompt.updateToken(token.id!, { label: token.value })
718
+ }
719
+ }}
720
+ >
721
+ Show Path
722
+ </button>
723
+ <button
724
+ type="button"
725
+ onClick={() => {
726
+ const token = firstToken()
727
+ if (token) {
728
+ prompt.removeToken(token.id!)
729
+ }
730
+ }}
731
+ >
732
+ Remove First
733
+ </button>
734
+ <button type="button" onClick={() => prompt.undo()}>
735
+ Undo
736
+ </button>
737
+ <button type="button" onClick={() => prompt.clear()}>
738
+ Clear
739
+ </button>
740
+ </div>
741
+ <Field>
742
+ <FieldLabel>Prompt</FieldLabel>
743
+ <TokenInput controller={prompt} defaultValue={draft}>
744
+ <TokenInputEditor />
745
+ </TokenInput>
746
+ </Field>
747
+ </div>
748
+ )
749
+ }
750
+
751
+ export { Example }
752
+ ```
753
+
754
+ Attach a file and watch it load. Show the first token's full path or remove it, then use Undo or Clear.
755
+
756
+ ## Submit Shortcut
757
+
758
+ Set `submitShortcut` to `mod+enter` for a multiline message. Enter then inserts a line, and Command or Control+Enter submits.
759
+
760
+ ```tsx title="submit-shortcut.tsx"
761
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
762
+ import {
763
+ toLabel,
764
+ TokenInput,
765
+ TokenInputEditor,
766
+ } from "@ui-sdk/react/token-input"
767
+ import { useLog } from "./log"
768
+
769
+ function Example() {
770
+ const log = useLog()
771
+
772
+ return (
773
+ <Field>
774
+ <FieldLabel>Message</FieldLabel>
775
+ <TokenInput
776
+ submitShortcut="mod+enter"
777
+ onSubmit={(value) => log(`Sent ${toLabel(value)}`)}
778
+ >
779
+ <TokenInputEditor placeholder="Write a message…" />
780
+ </TokenInput>
781
+ </Field>
782
+ )
783
+ }
784
+
785
+ export { Example }
786
+ ```
787
+
788
+ Write a message. Enter adds a line; Command+Enter on a Mac or Control+Enter elsewhere sends it to the console.
789
+
790
+ ## Custom Rendering
791
+
792
+ Pass `renderToken` to render each token's content, usually a `TokenInputToken` with a `TokenInputTokenRemove` inside. It receives the token, so you can choose an icon by its type or show its `data`. Without it, each token shows its label.
793
+
794
+ Every part also takes `render`. [Parts and Composition](../parts-and-composition.md#choose-the-element) explains how props merge, and each part's state is listed under [State](#state).
795
+
796
+ ## Styling
797
+
798
+ Style the editor, the tokens and their remove buttons. The editor shows `data-empty` while it holds nothing; show the placeholder with `content: attr(aria-placeholder)` on its `::before`. Tokens show `data-token-type`, `data-pending`, `data-unresolved` and `data-selected`. Without a custom token renderer, the element that holds each token, `token-input-host`, carries the same attributes, so you can style tokens without `renderToken`.
799
+
800
+ Each part has a `data-slot`, such as `token-input-token`, and shows its state through the attributes listed under [Data Attributes](#data-attributes).
801
+
802
+ ## Data Attributes
803
+
804
+ ### TokenInputEditor
805
+ `[data-slot="token-input-editor"]`
806
+
807
+ - `data-empty`: Present while the input holds nothing. Show the placeholder with it.
808
+ - `data-disabled`: Present when the input is disabled, by its own prop, a field or a `<fieldset>`.
809
+ - `data-readonly`: Present when the input is read-only.
810
+
811
+ ### TokenInputToken
812
+ `[data-slot="token-input-token"]`
813
+
814
+ - `data-token-type` (`file`): The token's type, like `file` or `mention`. Also on its host.
815
+ - `data-pending`: Present while the token loads. Also on its host.
816
+ - `data-disabled`: Present when the input is disabled, by its own prop, a field or a `<fieldset>`.
817
+ - `data-readonly`: Present when the input is read-only.
818
+ - `data-slot`
819
+ - `data-selected`: Present while the token is in the selection, or selected for removal. Also on its host.
820
+ - `data-unresolved`: Present while the token needs the user, like an ambiguous mention. Also on its host.
821
+
822
+ ### TokenInputTokenRemove
823
+ `[data-slot="token-input-token-remove"]`
824
+
825
+ - `data-disabled`: Present when the input is disabled, by its own prop, a field or a `<fieldset>`.
826
+ - `data-readonly`: Present when the input is read-only.
827
+
828
+ ## API Reference
829
+
830
+ Props and callbacks are the same as below, as React props.
831
+
832
+ ### TokenInput
833
+
834
+ - `value?` (`TokenInputValue`): The value. Pass it to control the input.
835
+ - `defaultValue?` (`TokenInputValue`): The value at first, when uncontrolled. A form reset returns to it.
836
+ - `onValueChange?` (`((value: TokenInputValue, details: TokenInputValueChangeDetails) => void)`): Runs when typing, pasting, a menu, undo or a controller wants to change the value. `details.reason` says why, `details.removed` lists removed tokens, and `details.cancel()` stops it.
837
+ - `onSubmit?` (`((value: TokenInputValue, details: TokenInputSubmitDetails) => void)`): Runs on the submit shortcut, unless an input method is composing or a trigger menu is open. Without it, the shortcut submits the form the input is in. With neither, the key is left alone, so Enter starts a new line.
838
+ - `submitShortcut?` (`"enter" | "mod+enter"`): Which keys submit: `"enter"`, with Shift+Enter for a new line, or `"mod+enter"`, with Enter for a new line, which many Japanese and Chinese writers prefer.
839
+ - `tokenDeletion?` (`TokenDeletion`): How Backspace and Delete treat a token next to the caret. `"whole"` removes it in one press, `"select"` selects it first and removes it on the second, and `"revert"` turns it back into its text, so a trigger menu opens on it again.
840
+ - `copy?` (`"codec" | "label"`): What copying writes as plain text: `"codec"` keeps tokens through any app that pastes text, `"label"` writes what people read.
841
+ - `codec?` (`TokenCodec`): How tokens become text and back, for the clipboard and forms.
842
+ - `getTokenDescription?` (`((token: Token) => string)`): What a token says to screen readers besides its label, like its kind and status.
843
+ - `history?` (`UndoStackOptions`): Undo history: how many steps it keeps, and how long a typing pause starts a new step.
844
+ - `disabled?` (`boolean`): Disables the input: it can't be focused or changed, and isn't submitted.
845
+ - `readOnly?` (`boolean`): Keeps the input focusable and submitted, but stops changes.
846
+ - `required?` (`boolean`): Blocks form submission while the input is empty.
847
+ - `name?` (`string`): The name the codec text submits under. Without one, the input submits nothing.
848
+ - `form?` (`string`): The id of a form to belong to, when the input isn't inside it.
849
+ - `controller?` (`object`): Reads, edits and focuses the input from anywhere. Create one with `createTokenInputController()`.
850
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
851
+
852
+ `renderToken` receives each token and returns its content, usually a `TokenInputToken`.
853
+
854
+ ### TokenInputEditor
855
+
856
+ - `placeholder?` (`string`): Text shown while the input is empty, through `aria-placeholder`. Show it with `data-empty:before:content-[attr(aria-placeholder)]`.
857
+
858
+ ### TokenInputToken
859
+
860
+ A token's content, inside `renderToken`, with its state for styling. Renders a `span`.
861
+
862
+ ### TokenInputTokenRemove
863
+
864
+ Removes its token when pressed and keeps focus in the editor. Renders a `span`, hidden from assistive technology, since keyboard users remove tokens with Backspace.
865
+
866
+ ### Token
867
+
868
+ - `type` (`string`): Kind, like `"mention"`, `"file"` or `"skill"`.
869
+ - `value` (`string`): The id it stands for.
870
+ - `label` (`string`): The text it shows, reads as and falls back to.
871
+ - `id?` (`string`): Stable identity, generated when missing.
872
+ - `status?` (`"pending" | "unresolved"`): `"pending"` while it loads, `"unresolved"` when it needs the user, like an ambiguous mention.
873
+ - `data?` (`Record<string, unknown>`): What the chip renders from, like an icon or a run state. Never serialized.
874
+
875
+ ### Codec
876
+
877
+ - `format` (`(token: Token) => string`): The text a token is written as.
878
+ - `parse` (`(text: string) => TokenInputValue`): Text back into a value, with tokens wherever their text appears.
879
+ - `escape?` (`((text: string, next: Token | undefined) => string)`): How plain text is written so `parse` reads it back as text. `next` is the token that follows it, if any. Defaults to the text as is.
880
+
881
+ ### State
882
+
883
+ `TokenInputEditor`:
884
+
885
+ - `empty` (`boolean`): Whether the input holds nothing.
886
+ - `disabled` (`boolean`): Whether the input is disabled, by itself or by a `<fieldset>`.
887
+ - `readOnly` (`boolean`): Whether the input is read-only: it stays focusable, but typing, pastes and drops don't change it.
888
+
889
+ `TokenInputToken`:
890
+
891
+ - `token` (`Token`): The token this part shows: the one the token input renders its content for.
892
+ - `selected` (`boolean`): Whether the token is in the selection, or selected for removal.
893
+ - `pending` (`boolean`): Whether the token is loading.
894
+ - `unresolved` (`boolean`): Whether the token needs the user, like an ambiguous mention.
895
+ - `disabled` (`boolean`): Whether the input is disabled, by itself or by a `<fieldset>`.
896
+ - `readOnly` (`boolean`): Whether the input is read-only, so the token's remove button does nothing.
897
+
898
+ ### Reasons
899
+
900
+ - `input-change` (`Event`): Typing, pasting or dropping changed a text field's value; in a combobox it can also open or close it, or Enter keeps the typed text as the value.
901
+ - `paste` (`ClipboardEvent`): Files were pasted, like a screenshot, or text and tokens into a token input.
902
+ - `drop` (`DragEvent`): Files were dropped on a drop zone, or text and tokens on a token input.
903
+ - `cut` (`ClipboardEvent`): A selection was cut from a token input.
904
+ - `undo` (`KeyboardEvent | InputEvent`): Undo, from the keyboard, the Edit menu or a controller.
905
+ - `redo` (`KeyboardEvent | InputEvent`): Redo, from the keyboard, the Edit menu or a controller.
906
+ - `remove-press` (`MouseEvent | KeyboardEvent`): A chip's remove part was pressed, or Backspace or Delete removed a chip or a span.
907
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
908
+ - `reset` (`none`): A form reset.
909
+ - `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
910
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
911
+
912
+ ### Controller
913
+
914
+ Create one with `createTokenInputController()` and pass it as `controller`.
915
+
916
+ - `getValue()` (`() => TokenInputValue`): The value: text and tokens.
917
+ - `setValue(value, options?)` (`(value: TokenInputValue, options?: TokenInputChangeOptions | undefined) => boolean`): Replaces the value as one undoable step. Returns false when the change was canceled or nothing changed.
918
+ - `getText(codec?)` (`(codec?: TokenCodec | undefined) => string`): The value as codec text, like `Ask [@Ada](mention:ada)`.
919
+ - `getLabel()` (`() => string`): The value as people read it, with each token's label.
920
+ - `insert(content, options?)` (`(content: string | Token | TokenInputValue, options?: (TokenInputChangeOptions & { at?: TextRange | "caret" | "end" | undefined; resolve?: PromiseLike<...> | undefined; }) | undefined) => boolean`): Inserts text, a token or a value at the caret, the end or a range, as one undoable step. With `resolve`, a token is pending until it settles: updated with what it resolves to, removed if it rejects. Returns false when the change was canceled.
921
+ - `updateToken(id, patch)` (`(id: string, patch: Partial<Omit<Token, "id">>) => boolean`): Changes a token's status, data or label without an undo step or a caret move. Returns false when there's no such token or nothing changed.
922
+ - `removeToken(id, options?)` (`(id: string, options?: TokenInputChangeOptions | undefined) => boolean`): Removes a token as one undoable step. Returns false when the change was canceled or nothing changed.
923
+ - `undo()` (`() => boolean`): Undoes the last step. Returns false when there was none or the change was canceled.
924
+ - `redo()` (`() => boolean`): Redoes the last undone step. Returns false when there was none or the change was canceled.
925
+ - `canUndo()` (`() => boolean`): Whether there's a step to undo.
926
+ - `canRedo()` (`() => boolean`): Whether there's a step to redo.
927
+ - `clear(options?)` (`(options?: TokenInputChangeOptions | undefined) => boolean`): Empties the input as one undoable step. Returns false when the change was canceled or nothing changed.
928
+ - `focus(options?)` (`(options?: { at?: "end" | "start" | undefined; } | undefined) => void`): Focuses the editor, with the caret at its start or end.
929
+ - `editor` (`TextEditorAdapter`): The editor a TriggerMenu outside the token input reads and writes. A menu inside it finds it on its own.
930
+
931
+ ### Events
932
+
933
+ Callbacks get the new state first, then the details.
934
+
935
+ | Prop | On | Receives |
936
+ | --------------- | ------------ | -------------------------------------------------------------------------------- |
937
+ | `onValueChange` | `TokenInput` | `(value, details)`: the value, the [reason](#reasons), `inputType` and `removed` |
938
+ | `onSubmit` | `TokenInput` | `(value, details)`: the value, and the key press or menu choice that submitted |
939
+
940
+ ## Accessibility
941
+
942
+ The editor is a multiline text box: it has the `textbox` role with `aria-multiline`, and a Trigger Menu inside it adds `aria-autocomplete` and `aria-haspopup` for its suggestions. Give it a visible label through a [Field](./field.md), or an `aria-label`. Each token reads as its label and its kind, like "routes.ts file", and inserting or removing one is announced. Typing keeps the browser's spelling, autocorrect, and input method support.
943
+
944
+ ### Keyboard
945
+
946
+ | Key | Does |
947
+ | ---------------------------- | ---------------------------------------------------------------------------- |
948
+ | Backspace, Delete | Removes the token before or after the caret, as `tokenDeletion` says |
949
+ | ArrowLeft, ArrowRight | Moves the caret past a token in one press; the caret never stops inside one |
950
+ | Enter | Submits. While a trigger menu is open, chooses the highlighted suggestion |
951
+ | Shift+Enter | Starts a new line |
952
+ | Command+Enter, Control+Enter | Submits when `submitShortcut` is `"mod+enter"`; Enter then starts a new line |
953
+ | Command+Z, Control+Z | Undoes the last step. Typing is grouped; pastes and token changes are steps |
954
+ | Shift+Command+Z, Control+Y | Redoes the last undone step |
955
+
956
+ ### Tested Behavior
957
+
958
+ Every line below is an automated test.
959
+
960
+ **Value**
961
+
962
+ - Renders text and tokens, each token as one chip.
963
+ - Tokens it starts with show their rendered content once, and read as their label and kind.
964
+ - Tokens and their remove parts mark a read-only or disabled input.
965
+ - Is empty with a placeholder until something is typed.
966
+ - Typing after a token goes after it.
967
+ - Shift+Enter starts a new line.
968
+
969
+ **Tokens**
970
+
971
+ - @ opens the menu inside the input, and choosing inserts a token and a space.
972
+ - A controlled input takes tokens from its menu and keeps typing in order.
973
+ - Enter in an open menu chooses and doesn't submit.
974
+ - One Backspace removes a whole token, and says so.
975
+ - Backspace removes tokens one by one, with no keystroke spent on anchors.
976
+ - Delete removes the token after the caret.
977
+ - The caret crosses a token with one arrow press each way.
978
+ - The caret can sit between two adjacent tokens.
979
+ - Clicking past a final token and typing lands after it.
980
+ - Word deletion stops at a token.
981
+ - The remove button removes its token and keeps focus.
982
+ - With tokenDeletion select, the first Backspace selects the token and the second removes it.
983
+ - With tokenDeletion revert, Backspace turns the token back into text and the menu opens on it.
984
+
985
+ **History**
986
+
987
+ - Undo and Redo step through edits, a token insertion on its own.
988
+ - Undo brings back a removed token.
989
+ - A pending token shows loading, then settles without an undo step.
990
+ - A pending token whose load fails is removed.
991
+
992
+ **Clipboard**
993
+
994
+ - Copying tokens into plain text and back keeps them.
995
+ - Copy writes codec text and HTML with the tokens.
996
+ - Pasting codec text makes tokens.
997
+ - With copy label, plain text gets what people read.
998
+
999
+ **Submit**
1000
+
1001
+ - Enter submits the codec text.
1002
+ - With mod+enter, Enter is a new line and Cmd or Ctrl+Enter submits.
1003
+ - A form submits the codec text under the input's name.
1004
+ - Enter submits the form the input's form attribute names, not the one it's in.
1005
+ - Enter while an input method composes doesn't submit.
1006
+
1007
+ Runs in React, Vue and web components.