@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,1963 @@
1
+ # Autocomplete
2
+
3
+ Suggest text while someone types, without limiting what they can enter.
4
+
5
+ Use Autocomplete for a text field with helpful suggestions. Choosing one fills in the field, but any text is a valid value. Use [Combobox](./combobox.md) when the value should be a selected option instead.
6
+
7
+ ```tsx title="demo.tsx"
8
+ "use client"
9
+
10
+ import {
11
+ Autocomplete,
12
+ AutocompleteContent,
13
+ AutocompleteEmpty,
14
+ AutocompleteInput,
15
+ AutocompleteItem,
16
+ AutocompleteLabel,
17
+ AutocompleteList,
18
+ } from "@ui-sdk/react/autocomplete"
19
+
20
+ const fruits = [
21
+ "Apple",
22
+ "Apricot",
23
+ "Avocado",
24
+ "Banana",
25
+ "Blackberry",
26
+ "Blueberry",
27
+ "Cherry",
28
+ "Grape",
29
+ "Lemon",
30
+ "Mango",
31
+ "Orange",
32
+ "Peach",
33
+ "Pear",
34
+ "Pineapple",
35
+ "Strawberry",
36
+ ]
37
+
38
+ function Example() {
39
+ return (
40
+ <Autocomplete>
41
+ <div data-slot="stack">
42
+ <AutocompleteLabel>Favorite fruit</AutocompleteLabel>
43
+ <AutocompleteInput placeholder="e.g. Mango" />
44
+ </div>
45
+ <AutocompleteContent>
46
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
47
+ <AutocompleteList>
48
+ {fruits.map((fruit) => (
49
+ <AutocompleteItem key={fruit} value={fruit}>
50
+ {fruit}
51
+ </AutocompleteItem>
52
+ ))}
53
+ </AutocompleteList>
54
+ </AutocompleteContent>
55
+ </Autocomplete>
56
+ )
57
+ }
58
+
59
+ export { Example }
60
+ ```
61
+
62
+ Type “man”, press Down Arrow, then Enter to fill in Mango. You can also leave text that is not in the list.
63
+
64
+ ## Installation
65
+
66
+ ```bash
67
+ npm install @ui-sdk/react @ui-sdk/core
68
+ ```
69
+
70
+ ## Composition
71
+
72
+ Use the following composition to build an `Autocomplete`:
73
+
74
+ ```text
75
+ Autocomplete
76
+ ├── AutocompleteLabel
77
+ ├── AutocompleteInput
78
+ └── AutocompleteContent
79
+ ├── AutocompleteEmpty
80
+ └── AutocompleteList
81
+ ├── AutocompleteItem
82
+ └── AutocompleteItem
83
+ ```
84
+
85
+ Name the input, then put suggestions in a list inside the popup. Each suggestion needs a unique value.
86
+
87
+ ## Usage
88
+
89
+ Import the parts and put them together:
90
+
91
+ ```tsx
92
+ import {
93
+ Autocomplete,
94
+ AutocompleteContent,
95
+ AutocompleteInput,
96
+ AutocompleteItem,
97
+ AutocompleteLabel,
98
+ AutocompleteList,
99
+ } from "@ui-sdk/react/autocomplete"
100
+
101
+ function Example() {
102
+ return (
103
+ <Autocomplete>
104
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
105
+ <AutocompleteInput placeholder="Type a fruit…" />
106
+ <AutocompleteContent>
107
+ <AutocompleteList>
108
+ <AutocompleteItem value="Apple">Apple</AutocompleteItem>
109
+ <AutocompleteItem value="Banana">Banana</AutocompleteItem>
110
+ </AutocompleteList>
111
+ </AutocompleteContent>
112
+ </Autocomplete>
113
+ )
114
+ }
115
+
116
+ export { Example }
117
+ ```
118
+
119
+ ## Autocomplete vs Combobox
120
+
121
+ Both filter a list as you type. They differ in what the value is.
122
+
123
+ - **Autocomplete**: the value is the text. Suggestions help fill it in, and anything typed is valid. Use it for a search box, an address or a free-form field.
124
+ - **[Combobox](./combobox.md)**: the value is an option from the list, or several with `multiple`. The text is only a query; closing without choosing restores the selected label, and a form submits the value, not the text. Use it for a country, an assignee or a set of labels.
125
+
126
+ ## Build Your Own
127
+
128
+ `@ui-sdk/react/autocomplete` comes with a ready-made Autocomplete. If you'd like to build your own, with your own names, start from the `autocomplete` primitive in `@ui-sdk/core`:
129
+
130
+ ```ts title="fruit-search.ts"
131
+ "use client"
132
+
133
+ import { autocomplete } from "@ui-sdk/core/autocomplete"
134
+ import { createParts } from "@ui-sdk/react/primitive"
135
+
136
+ const {
137
+ Root: FruitSearch,
138
+ Label: FruitLabel,
139
+ Input: FruitInput,
140
+ Content: FruitPopup,
141
+ List: FruitList,
142
+ Item: FruitOption,
143
+ Empty: FruitEmpty,
144
+ } = createParts(autocomplete, {
145
+ label: "label",
146
+ input: "input",
147
+ content: "div",
148
+ list: "div",
149
+ item: "div",
150
+ empty: "div",
151
+ })
152
+
153
+ export {
154
+ FruitEmpty,
155
+ FruitInput,
156
+ FruitLabel,
157
+ FruitList,
158
+ FruitOption,
159
+ FruitPopup,
160
+ FruitSearch,
161
+ }
162
+ ```
163
+
164
+ ```tsx
165
+ <FruitSearch>
166
+ <FruitLabel>Fruit</FruitLabel>
167
+ <FruitInput />
168
+ <FruitPopup>
169
+ <FruitEmpty>No fruits found.</FruitEmpty>
170
+ <FruitList>
171
+ <FruitOption value="Apple">Apple</FruitOption>
172
+ <FruitOption value="Mango">Mango</FruitOption>
173
+ </FruitList>
174
+ </FruitPopup>
175
+ </FruitSearch>
176
+ ```
177
+
178
+ Each part keeps the behavior, state and data attributes of its ready-made counterpart: typing filters the options, arrow keys move the highlight and the popup opens in the top layer. See [Rendering](../rendering.md) and [Custom Parts](../custom-parts.md) for more.
179
+
180
+ ## Disabled
181
+
182
+ A disabled field cannot be focused or changed and submits nothing. A read-only field stays focusable and submits its value, but does not open or accept changes. Disabled suggestions are skipped by keyboard navigation and cannot fill in the field.
183
+
184
+ ```tsx title="disabled.tsx"
185
+ "use client"
186
+
187
+ import {
188
+ Autocomplete,
189
+ AutocompleteContent,
190
+ AutocompleteInput,
191
+ AutocompleteItem,
192
+ AutocompleteLabel,
193
+ AutocompleteList,
194
+ } from "@ui-sdk/react/autocomplete"
195
+
196
+ const fruits = [
197
+ "Apple",
198
+ "Apricot",
199
+ "Avocado",
200
+ "Banana",
201
+ "Blackberry",
202
+ "Blueberry",
203
+ "Cherry",
204
+ "Grape",
205
+ "Lemon",
206
+ "Mango",
207
+ "Orange",
208
+ "Peach",
209
+ "Pear",
210
+ "Pineapple",
211
+ "Strawberry",
212
+ ]
213
+
214
+ function Example() {
215
+ return (
216
+ <div data-slot="stack">
217
+ <Autocomplete disabled defaultValue="Banana">
218
+ <div data-slot="stack">
219
+ <AutocompleteLabel>Disabled</AutocompleteLabel>
220
+ <AutocompleteInput />
221
+ </div>
222
+ </Autocomplete>
223
+ <Autocomplete readOnly defaultValue="Cherry">
224
+ <div data-slot="stack">
225
+ <AutocompleteLabel>Read-only</AutocompleteLabel>
226
+ <AutocompleteInput />
227
+ </div>
228
+ </Autocomplete>
229
+ <Autocomplete>
230
+ <div data-slot="stack">
231
+ <AutocompleteLabel>Some suggestions disabled</AutocompleteLabel>
232
+ <AutocompleteInput placeholder="Type “b”" />
233
+ </div>
234
+ <AutocompleteContent>
235
+ <AutocompleteList>
236
+ {fruits.map((fruit, index) => (
237
+ <AutocompleteItem key={fruit} value={fruit} disabled={index % 3 === 0}>
238
+ {fruit}
239
+ </AutocompleteItem>
240
+ ))}
241
+ </AutocompleteList>
242
+ </AutocompleteContent>
243
+ </Autocomplete>
244
+ </div>
245
+ )
246
+ }
247
+
248
+ export { Example }
249
+ ```
250
+
251
+ Try the Disabled and Read-only fields. In Disabled suggestion, type “b”: arrow keys skip Banana and a press cannot choose it.
252
+
253
+ ## Clear and Trigger
254
+
255
+ `AutocompleteClear` empties the field and returns focus to it. `AutocompleteTrigger` lets someone open the suggestions without typing. Give the trigger a label that describes what it opens.
256
+
257
+ ```tsx title="clear.tsx"
258
+ "use client"
259
+
260
+ import {
261
+ Autocomplete,
262
+ AutocompleteClear,
263
+ AutocompleteContent,
264
+ AutocompleteEmpty,
265
+ AutocompleteInput,
266
+ AutocompleteItem,
267
+ AutocompleteLabel,
268
+ AutocompleteList,
269
+ AutocompleteTrigger,
270
+ } from "@ui-sdk/react/autocomplete"
271
+ import { ChevronDownIcon, XIcon } from "lucide-react"
272
+
273
+ const fruits = [
274
+ "Apple",
275
+ "Apricot",
276
+ "Avocado",
277
+ "Banana",
278
+ "Blackberry",
279
+ "Blueberry",
280
+ "Cherry",
281
+ "Grape",
282
+ "Lemon",
283
+ "Mango",
284
+ "Orange",
285
+ "Peach",
286
+ "Pear",
287
+ "Pineapple",
288
+ "Strawberry",
289
+ ]
290
+
291
+ function Example() {
292
+ return (
293
+ <Autocomplete defaultValue="Mango">
294
+ <div data-slot="stack">
295
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
296
+ <div data-slot="row">
297
+ <AutocompleteInput />
298
+ <AutocompleteClear aria-label="Clear">
299
+ <XIcon aria-hidden="true" />
300
+ </AutocompleteClear>
301
+ <AutocompleteTrigger aria-label="Show fruits">
302
+ <ChevronDownIcon aria-hidden="true" />
303
+ </AutocompleteTrigger>
304
+ </div>
305
+ </div>
306
+ <AutocompleteContent>
307
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
308
+ <AutocompleteList>
309
+ {fruits.map((fruit) => (
310
+ <AutocompleteItem key={fruit} value={fruit}>
311
+ {fruit}
312
+ </AutocompleteItem>
313
+ ))}
314
+ </AutocompleteList>
315
+ </AutocompleteContent>
316
+ </Autocomplete>
317
+ )
318
+ }
319
+
320
+ export { Example }
321
+ ```
322
+
323
+ Clear Mango, then press Show fruits to open the suggestions. The clear button disappears while the field is empty.
324
+
325
+ ## Open on Focus
326
+
327
+ Add `openOnFocus` to offer suggestions as soon as the input receives focus. This works well when there are useful choices before someone types.
328
+
329
+ ```tsx title="open-on-focus.tsx"
330
+ "use client"
331
+
332
+ import {
333
+ Autocomplete,
334
+ AutocompleteContent,
335
+ AutocompleteEmpty,
336
+ AutocompleteGroup,
337
+ AutocompleteGroupLabel,
338
+ AutocompleteInput,
339
+ AutocompleteItem,
340
+ AutocompleteList,
341
+ } from "@ui-sdk/react/autocomplete"
342
+
343
+ const recent = ["react hooks", "tailwind grid", "web components", "vue v-model"]
344
+
345
+ function Example() {
346
+ return (
347
+ <Autocomplete openOnFocus>
348
+ <AutocompleteInput aria-label="Search" placeholder="Search…" />
349
+ <AutocompleteContent>
350
+ <AutocompleteEmpty>No results.</AutocompleteEmpty>
351
+ <AutocompleteList>
352
+ <AutocompleteGroup>
353
+ <AutocompleteGroupLabel>Recent searches</AutocompleteGroupLabel>
354
+ {recent.map((search) => (
355
+ <AutocompleteItem key={search} value={search}>
356
+ {search}
357
+ </AutocompleteItem>
358
+ ))}
359
+ </AutocompleteGroup>
360
+ </AutocompleteList>
361
+ </AutocompleteContent>
362
+ </Autocomplete>
363
+ )
364
+ }
365
+
366
+ export { Example }
367
+ ```
368
+
369
+ Focus Search docs to see recent searches before typing. Escape closes them; pressing the field opens them again.
370
+
371
+ ## Auto Highlight
372
+
373
+ `autoHighlight` highlights the first match while typing, so Enter can choose it immediately. Set `loop` to `false` to stop arrow keys at either end of the list.
374
+
375
+ ```tsx title="auto-highlight.tsx"
376
+ "use client"
377
+
378
+ import {
379
+ Autocomplete,
380
+ AutocompleteContent,
381
+ AutocompleteEmpty,
382
+ AutocompleteInput,
383
+ AutocompleteItem,
384
+ AutocompleteLabel,
385
+ AutocompleteList,
386
+ } from "@ui-sdk/react/autocomplete"
387
+
388
+ const fruits = [
389
+ "Apple",
390
+ "Apricot",
391
+ "Avocado",
392
+ "Banana",
393
+ "Blackberry",
394
+ "Blueberry",
395
+ "Cherry",
396
+ "Grape",
397
+ "Lemon",
398
+ "Mango",
399
+ "Orange",
400
+ "Peach",
401
+ "Pear",
402
+ "Pineapple",
403
+ "Strawberry",
404
+ ]
405
+
406
+ function Example() {
407
+ return (
408
+ <Autocomplete autoHighlight loop={false}>
409
+ <div data-slot="stack">
410
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
411
+ <AutocompleteInput placeholder="Type, then Enter" />
412
+ </div>
413
+ <AutocompleteContent>
414
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
415
+ <AutocompleteList>
416
+ {fruits.map((fruit) => (
417
+ <AutocompleteItem key={fruit} value={fruit}>
418
+ {fruit}
419
+ </AutocompleteItem>
420
+ ))}
421
+ </AutocompleteList>
422
+ </AutocompleteContent>
423
+ </Autocomplete>
424
+ )
425
+ }
426
+
427
+ export { Example }
428
+ ```
429
+
430
+ Type “pe” and press Enter to choose the first match. Arrow keys stop at the ends because this example disables looping.
431
+
432
+ ## Inline Completion
433
+
434
+ `inlineComplete` fills in the rest of the first suggestion that starts with the typed text, with the added text selected. Arrow keys fill in the suggestion they highlight. End or Right Arrow accepts the completion; Backspace or Escape takes it back out.
435
+
436
+ ```tsx title="inline.tsx"
437
+ "use client"
438
+
439
+ import {
440
+ Autocomplete,
441
+ AutocompleteContent,
442
+ AutocompleteEmpty,
443
+ AutocompleteInput,
444
+ AutocompleteItem,
445
+ AutocompleteLabel,
446
+ AutocompleteList,
447
+ } from "@ui-sdk/react/autocomplete"
448
+
449
+ const fruits = [
450
+ "Apple",
451
+ "Apricot",
452
+ "Avocado",
453
+ "Banana",
454
+ "Blackberry",
455
+ "Blueberry",
456
+ "Cherry",
457
+ "Grape",
458
+ "Lemon",
459
+ "Mango",
460
+ "Orange",
461
+ "Peach",
462
+ "Pear",
463
+ "Pineapple",
464
+ "Strawberry",
465
+ ]
466
+
467
+ function Example() {
468
+ return (
469
+ <Autocomplete inlineComplete>
470
+ <div data-slot="stack">
471
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
472
+ <AutocompleteInput placeholder="Type “bl”" />
473
+ </div>
474
+ <AutocompleteContent>
475
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
476
+ <AutocompleteList>
477
+ {fruits.map((fruit) => (
478
+ <AutocompleteItem key={fruit} value={fruit}>
479
+ {fruit}
480
+ </AutocompleteItem>
481
+ ))}
482
+ </AutocompleteList>
483
+ </AutocompleteContent>
484
+ </Autocomplete>
485
+ )
486
+ }
487
+
488
+ export { Example }
489
+ ```
490
+
491
+ Type “bl” to complete blackberry inline. Backspace removes the completion; Down Arrow fills in Blueberry. Press End to accept the text.
492
+
493
+ ## Highlight Matches
494
+
495
+ `AutocompleteItemText` marks the matching text, ignoring case and accents using the current locale. Its state exposes the matching segments when you want to render them yourself, as this example does in bold.
496
+
497
+ ```tsx title="highlight.tsx"
498
+ "use client"
499
+
500
+ import {
501
+ Autocomplete,
502
+ AutocompleteContent,
503
+ AutocompleteEmpty,
504
+ AutocompleteInput,
505
+ AutocompleteItem,
506
+ AutocompleteItemText,
507
+ AutocompleteLabel,
508
+ AutocompleteList,
509
+ } from "@ui-sdk/react/autocomplete"
510
+
511
+ const fruits = [
512
+ "Apple",
513
+ "Apricot",
514
+ "Avocado",
515
+ "Banana",
516
+ "Blackberry",
517
+ "Blueberry",
518
+ "Cherry",
519
+ "Grape",
520
+ "Lemon",
521
+ "Mango",
522
+ "Orange",
523
+ "Peach",
524
+ "Pear",
525
+ "Pineapple",
526
+ "Strawberry",
527
+ ]
528
+
529
+ function Example() {
530
+ return (
531
+ <Autocomplete>
532
+ <div data-slot="stack">
533
+ <AutocompleteLabel>Favorite fruit</AutocompleteLabel>
534
+ <AutocompleteInput placeholder="e.g. berry" />
535
+ </div>
536
+ <AutocompleteContent>
537
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
538
+ <AutocompleteList>
539
+ {fruits.map((fruit) => (
540
+ <AutocompleteItem key={fruit} value={fruit}>
541
+ <AutocompleteItemText>
542
+ {(state) =>
543
+ state.segments.map((segment, index) =>
544
+ segment.match ? (
545
+ <strong key={index}>{segment.text}</strong>
546
+ ) : (
547
+ segment.text
548
+ )
549
+ )
550
+ }
551
+ </AutocompleteItemText>
552
+ </AutocompleteItem>
553
+ ))}
554
+ </AutocompleteList>
555
+ </AutocompleteContent>
556
+ </Autocomplete>
557
+ )
558
+ }
559
+
560
+ export { Example }
561
+ ```
562
+
563
+ Type “berry”. The matching letters are bold while each suggestion keeps its original spelling.
564
+
565
+ ## Filter and Keywords
566
+
567
+ The default filter matches text anywhere in a suggestion. Use `"startsWith"` or `"fuzzy"` for a different match, a function for your own filter, or `false` for results already filtered by a server. `keywords` adds searchable text without changing what goes in the field.
568
+
569
+ ```tsx title="filter.tsx"
570
+ "use client"
571
+
572
+ import {
573
+ Autocomplete,
574
+ AutocompleteContent,
575
+ AutocompleteEmpty,
576
+ AutocompleteInput,
577
+ AutocompleteItem,
578
+ AutocompleteLabel,
579
+ AutocompleteList,
580
+ } from "@ui-sdk/react/autocomplete"
581
+
582
+ const tags = [
583
+ { value: "feature", label: "feature", keywords: ["enhancement", "request"] },
584
+ { value: "fix", label: "fix", keywords: ["bug", "patch"] },
585
+ { value: "docs", label: "docs", keywords: ["documentation", "readme"] },
586
+ { value: "performance", label: "performance", keywords: ["speed", "slow"] },
587
+ { value: "security", label: "security", keywords: ["vulnerability"] },
588
+ { value: "refactor", label: "refactor", keywords: ["cleanup"] },
589
+ ]
590
+
591
+ function Example() {
592
+ return (
593
+ <Autocomplete filter="startsWith">
594
+ <div data-slot="stack">
595
+ <AutocompleteLabel>Tag</AutocompleteLabel>
596
+ <AutocompleteInput placeholder="Try “bug”" />
597
+ </div>
598
+ <AutocompleteContent>
599
+ <AutocompleteEmpty>No tags found.</AutocompleteEmpty>
600
+ <AutocompleteList>
601
+ {tags.map((tag) => (
602
+ <AutocompleteItem key={tag.value} value={tag.value} label={tag.label} keywords={tag.keywords}>
603
+ {tag.label}
604
+ <span data-slot="hint">{tag.keywords[0]}</span>
605
+ </AutocompleteItem>
606
+ ))}
607
+ </AutocompleteList>
608
+ </AutocompleteContent>
609
+ </Autocomplete>
610
+ )
611
+ }
612
+
613
+ export { Example }
614
+ ```
615
+
616
+ Type “bug” to find fix through its keywords. Type “re” to find labels or keywords that start with those letters.
617
+
618
+ ## Groups
619
+
620
+ Use `AutocompleteGroup` and `AutocompleteGroupLabel` to name related suggestions. Filtering hides empty groups and separators.
621
+
622
+ ```tsx title="groups.tsx"
623
+ "use client"
624
+
625
+ import {
626
+ Autocomplete,
627
+ AutocompleteContent,
628
+ AutocompleteEmpty,
629
+ AutocompleteGroup,
630
+ AutocompleteGroupLabel,
631
+ AutocompleteInput,
632
+ AutocompleteItem,
633
+ AutocompleteLabel,
634
+ AutocompleteList,
635
+ AutocompleteSeparator,
636
+ } from "@ui-sdk/react/autocomplete"
637
+ import * as React from "react"
638
+
639
+ const fruitGroups = [
640
+ {
641
+ label: "Tree fruits",
642
+ items: ["Apple", "Apricot", "Cherry", "Lemon", "Peach", "Pear"],
643
+ },
644
+ {
645
+ label: "Berries",
646
+ items: ["Blackberry", "Blueberry", "Grape", "Strawberry"],
647
+ },
648
+ { label: "Tropical", items: ["Avocado", "Banana", "Mango", "Pineapple"] },
649
+ ]
650
+
651
+ function Example() {
652
+ return (
653
+ <Autocomplete>
654
+ <div data-slot="stack">
655
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
656
+ <AutocompleteInput placeholder="e.g. Pear" />
657
+ </div>
658
+ <AutocompleteContent>
659
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
660
+ <AutocompleteList>
661
+ {fruitGroups.map((group, index) => (
662
+ <React.Fragment key={group.label}>
663
+ {index > 0 && <AutocompleteSeparator />}
664
+ <AutocompleteGroup>
665
+ <AutocompleteGroupLabel>{group.label}</AutocompleteGroupLabel>
666
+ {group.items.map((fruit) => (
667
+ <AutocompleteItem key={fruit} value={fruit}>
668
+ {fruit}
669
+ </AutocompleteItem>
670
+ ))}
671
+ </AutocompleteGroup>
672
+ </React.Fragment>
673
+ ))}
674
+ </AutocompleteList>
675
+ </AutocompleteContent>
676
+ </Autocomplete>
677
+ )
678
+ }
679
+
680
+ export { Example }
681
+ ```
682
+
683
+ Type “an”. Banana and Mango remain in Tropical; groups with no matches disappear.
684
+
685
+ ## Icons
686
+
687
+ `AutocompleteItemIcon` keeps decorative icons out of accessible names, filtering, and the text put in the field. Add `mirror` for directional icons. An icon-only suggestion still needs an `aria-label`.
688
+
689
+ ```tsx title="icons.tsx"
690
+ "use client"
691
+
692
+ import {
693
+ Autocomplete,
694
+ AutocompleteContent,
695
+ AutocompleteEmpty,
696
+ AutocompleteInput,
697
+ AutocompleteItem,
698
+ AutocompleteItemIcon,
699
+ AutocompleteList,
700
+ } from "@ui-sdk/react/autocomplete"
701
+ import {
702
+ ArchiveIcon,
703
+ ClockIcon,
704
+ FileIcon,
705
+ PencilIcon,
706
+ ReplyIcon,
707
+ StarIcon,
708
+ } from "lucide-react"
709
+
710
+ const mailSearches = [
711
+ { value: "Starred", icon: StarIcon },
712
+ { value: "Drafts", icon: PencilIcon },
713
+ { value: "Replies", icon: ReplyIcon, mirror: true },
714
+ { value: "Attachments", icon: FileIcon },
715
+ { value: "Archived", icon: ArchiveIcon },
716
+ { value: "Scheduled", icon: ClockIcon },
717
+ ]
718
+
719
+ function Example() {
720
+ return (
721
+ <Autocomplete>
722
+ <AutocompleteInput type="search" aria-label="Search mail" placeholder="Search mail…" />
723
+ <AutocompleteContent>
724
+ <AutocompleteEmpty>No searches found.</AutocompleteEmpty>
725
+ <AutocompleteList>
726
+ {mailSearches.map((search) => (
727
+ <AutocompleteItem key={search.value} value={search.value}>
728
+ <AutocompleteItemIcon mirror={search.mirror}>
729
+ <search.icon />
730
+ </AutocompleteItemIcon>
731
+ {search.value}
732
+ </AutocompleteItem>
733
+ ))}
734
+ </AutocompleteList>
735
+ </AutocompleteContent>
736
+ </Autocomplete>
737
+ )
738
+ }
739
+
740
+ export { Example }
741
+ ```
742
+
743
+ Type “rep” and choose Replies. The icon stays out of the text put in the field.
744
+
745
+ ## Items as Data
746
+
747
+ Map an array into parts when you want to write the markup yourself. This example maps fruit groups and adds a separator between them.
748
+
749
+ ```tsx title="data.tsx"
750
+ "use client"
751
+
752
+ import {
753
+ Autocomplete,
754
+ AutocompleteContent,
755
+ AutocompleteEmpty,
756
+ AutocompleteGroup,
757
+ AutocompleteGroupLabel,
758
+ AutocompleteInput,
759
+ AutocompleteItem,
760
+ AutocompleteLabel,
761
+ AutocompleteList,
762
+ AutocompleteSeparator,
763
+ } from "@ui-sdk/react/autocomplete"
764
+ import * as React from "react"
765
+
766
+ const fruitGroups = [
767
+ {
768
+ label: "Tree fruits",
769
+ fruits: ["Apple", "Apricot", "Cherry", "Lemon", "Peach", "Pear"],
770
+ },
771
+ {
772
+ label: "Berries",
773
+ fruits: ["Blackberry", "Blueberry", "Grape", "Strawberry"],
774
+ },
775
+ { label: "Tropical", fruits: ["Avocado", "Banana", "Mango", "Pineapple"] },
776
+ ]
777
+
778
+ function Example() {
779
+ return (
780
+ <Autocomplete>
781
+ <div data-slot="stack">
782
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
783
+ <AutocompleteInput placeholder="e.g. Peach" />
784
+ </div>
785
+ <AutocompleteContent>
786
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
787
+ <AutocompleteList>
788
+ {fruitGroups.map((group, index) => (
789
+ <React.Fragment key={group.label}>
790
+ {index > 0 && <AutocompleteSeparator />}
791
+ <AutocompleteGroup>
792
+ <AutocompleteGroupLabel>{group.label}</AutocompleteGroupLabel>
793
+ {group.fruits.map((fruit) => (
794
+ <AutocompleteItem key={fruit} value={fruit}>
795
+ {fruit}
796
+ </AutocompleteItem>
797
+ ))}
798
+ </AutocompleteGroup>
799
+ </React.Fragment>
800
+ ))}
801
+ </AutocompleteList>
802
+ </AutocompleteContent>
803
+ </Autocomplete>
804
+ )
805
+ }
806
+
807
+ export { Example }
808
+ ```
809
+
810
+ Open the list to see three fruit groups. Type “an” to narrow them to Banana and Mango.
811
+
812
+ Pass `items` when the autocomplete needs the whole collection, such as server results or recent searches. It can render bare parts for you. To style changing results, render from the list’s `state.items`. [Lists and Menus](../lists-and-menus.md) covers the `ListData` format.
813
+
814
+ Suggestions can change while the list is open. The highlight stays with its suggestion, and the typed text stays unchanged. Removing the highlighted suggestion clears the highlight.
815
+
816
+ ## Controlled
817
+
818
+ The value is always a string. Control it with `value` and `onValueChange`, or start uncontrolled with `defaultValue`. Suggestions have a separate `open` state and `onOpenChange`. Callbacks receive the proposed value first, then change details. Call `details.cancel()` to reject a text or open change. [State and Events](../state-and-events.md) covers controlled state and cancellation.
819
+
820
+ ```tsx title="controlled.tsx"
821
+ import {
822
+ Autocomplete,
823
+ AutocompleteContent,
824
+ AutocompleteEmpty,
825
+ AutocompleteInput,
826
+ AutocompleteItem,
827
+ AutocompleteLabel,
828
+ AutocompleteList,
829
+ } from "@ui-sdk/react/autocomplete"
830
+ import * as React from "react"
831
+ import { useLog } from "./log"
832
+
833
+ const fruits = [
834
+ "Apple",
835
+ "Apricot",
836
+ "Avocado",
837
+ "Banana",
838
+ "Blackberry",
839
+ "Blueberry",
840
+ "Cherry",
841
+ "Grape",
842
+ "Lemon",
843
+ "Mango",
844
+ "Orange",
845
+ "Peach",
846
+ "Pear",
847
+ "Pineapple",
848
+ "Strawberry",
849
+ ]
850
+
851
+ export function Example() {
852
+ const [value, setValue] = React.useState("")
853
+ const [open, setOpen] = React.useState(false)
854
+ const log = useLog()
855
+ return (
856
+ <div data-slot="stack">
857
+ <Autocomplete
858
+ value={value}
859
+ onValueChange={(next, details) => {
860
+ if (next.length > 12) {
861
+ return details.cancel()
862
+ }
863
+ setValue(next)
864
+ log(`text: ${details.reason}`)
865
+ }}
866
+ open={open}
867
+ onOpenChange={(next, details) => {
868
+ setOpen(next)
869
+ log(`${next ? "opened" : "closed"}: ${details.reason}`)
870
+ }}
871
+ onOpenChangeComplete={(next) =>
872
+ log(`${next ? "opened" : "closed"} and settled`)
873
+ }
874
+ onHighlight={(highlighted) =>
875
+ highlighted && log(`highlighted ${highlighted}`)
876
+ }
877
+ debug
878
+ >
879
+ <AutocompleteLabel>Fruit (12 characters at most)</AutocompleteLabel>
880
+ <AutocompleteInput />
881
+ <AutocompleteContent>
882
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
883
+ <AutocompleteList>
884
+ {fruits.map((fruit) => (
885
+ <AutocompleteItem key={fruit} value={fruit}>
886
+ {fruit}
887
+ </AutocompleteItem>
888
+ ))}
889
+ </AutocompleteList>
890
+ </AutocompleteContent>
891
+ </Autocomplete>
892
+ </div>
893
+ )
894
+ }
895
+ ```
896
+
897
+ Type more than 12 characters and the extra text is refused. The console shows each change and its reason.
898
+
899
+ ## Controller
900
+
901
+ Create a `createAutocompleteController()` for one instance and pass it as `controller`. It can `setValue()`, `clear()`, `focus()`, `setOpen()`, or read `getValue()` and `isOpen()`. `open()` returns a promise that resolves when the popup closes, including abort and unmount; `close()` closes it. See [Controllers](../controllers.md) for lifecycle and change options.
902
+
903
+ ```tsx title="controller.tsx"
904
+ import {
905
+ Autocomplete,
906
+ AutocompleteContent,
907
+ AutocompleteEmpty,
908
+ AutocompleteInput,
909
+ AutocompleteItem,
910
+ AutocompleteLabel,
911
+ AutocompleteList,
912
+ createAutocompleteController,
913
+ } from "@ui-sdk/react/autocomplete"
914
+
915
+ const fruits = [
916
+ "Apple",
917
+ "Apricot",
918
+ "Avocado",
919
+ "Banana",
920
+ "Blackberry",
921
+ "Blueberry",
922
+ "Cherry",
923
+ "Grape",
924
+ "Lemon",
925
+ "Mango",
926
+ "Orange",
927
+ "Peach",
928
+ "Pear",
929
+ "Pineapple",
930
+ "Strawberry",
931
+ ]
932
+
933
+ const fruit = createAutocompleteController()
934
+
935
+ export function Example() {
936
+ return (
937
+ <div data-slot="stack">
938
+ <div data-slot="controls">
939
+ <button type="button" data-size="sm" onClick={() => fruit.open()}>
940
+ Open
941
+ </button>
942
+ <button
943
+ type="button"
944
+ data-size="sm"
945
+ onClick={() => fruit.setValue("Pea")}
946
+ >
947
+ Type “Pea”
948
+ </button>
949
+ <button type="button" data-size="sm" onClick={() => fruit.clear()}>
950
+ Clear
951
+ </button>
952
+ <button type="button" data-size="sm" onClick={() => fruit.focus()}>
953
+ Focus
954
+ </button>
955
+ </div>
956
+ <Autocomplete controller={fruit}>
957
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
958
+ <AutocompleteInput />
959
+ <AutocompleteContent>
960
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
961
+ <AutocompleteList>
962
+ {fruits.map((item) => (
963
+ <AutocompleteItem key={item} value={item}>
964
+ {item}
965
+ </AutocompleteItem>
966
+ ))}
967
+ </AutocompleteList>
968
+ </AutocompleteContent>
969
+ </Autocomplete>
970
+ </div>
971
+ )
972
+ }
973
+ ```
974
+
975
+ Use the buttons to open the suggestions, type Pea, clear the field or focus it from outside.
976
+
977
+ ## Positioning
978
+
979
+ Use `side`, `align`, offsets, or a custom `anchor` to position the suggestions. `container` chooses where they render, and `keepMounted` keeps closed content in the DOM. [Popups](../popups.md) covers positioning, portals, animation, and modal behavior.
980
+
981
+ ```tsx title="positioning.tsx"
982
+ import {
983
+ Autocomplete,
984
+ AutocompleteContent,
985
+ AutocompleteEmpty,
986
+ AutocompleteInput,
987
+ AutocompleteItem,
988
+ AutocompleteLabel,
989
+ AutocompleteList,
990
+ } from "@ui-sdk/react/autocomplete"
991
+ import * as React from "react"
992
+
993
+ const fruits = [
994
+ "Apple",
995
+ "Apricot",
996
+ "Avocado",
997
+ "Banana",
998
+ "Blackberry",
999
+ "Blueberry",
1000
+ ]
1001
+
1002
+ export function Example() {
1003
+ const [box, setBox] = React.useState<HTMLElement | null>(null)
1004
+ return (
1005
+ <div ref={setBox} data-slot="card">
1006
+ <Autocomplete modal="pointer" container={box}>
1007
+ <div data-slot="stack">
1008
+ <AutocompleteLabel>Fruit</AutocompleteLabel>
1009
+ <AutocompleteInput />
1010
+ </div>
1011
+ <AutocompleteContent
1012
+ side="top"
1013
+ align="end"
1014
+ sideOffset={10}
1015
+ alignOffset={-4}
1016
+ collisionPadding={8}
1017
+ anchor={box}
1018
+ keepMounted
1019
+ >
1020
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
1021
+ <AutocompleteList>
1022
+ {fruits.map((fruit) => (
1023
+ <AutocompleteItem key={fruit} value={fruit}>
1024
+ {fruit}
1025
+ </AutocompleteItem>
1026
+ ))}
1027
+ </AutocompleteList>
1028
+ </AutocompleteContent>
1029
+ </Autocomplete>
1030
+ </div>
1031
+ )
1032
+ }
1033
+ ```
1034
+
1035
+ Open the suggestions. They appear above the card, aligned to its end.
1036
+
1037
+ ## Animation
1038
+
1039
+ The content has `data-starting-style` while it opens and `data-ending-style` while it closes. Transition from those states, and the popup stays mounted until the exit finishes:
1040
+
1041
+ ```css
1042
+ [data-slot="autocomplete-content"] {
1043
+ transition:
1044
+ opacity 0.15s,
1045
+ translate 0.2s cubic-bezier(0.22, 1, 0.36, 1);
1046
+
1047
+ &:is([data-starting-style], [data-ending-style]) {
1048
+ opacity: 0;
1049
+ translate: 0 -6px;
1050
+ }
1051
+ }
1052
+ ```
1053
+
1054
+ ```tsx title="animation.tsx"
1055
+ "use client"
1056
+
1057
+ import {
1058
+ Autocomplete,
1059
+ AutocompleteContent,
1060
+ AutocompleteEmpty,
1061
+ AutocompleteInput,
1062
+ AutocompleteItem,
1063
+ AutocompleteLabel,
1064
+ AutocompleteList,
1065
+ } from "@ui-sdk/react/autocomplete"
1066
+
1067
+ const fruits = [
1068
+ "Apple",
1069
+ "Apricot",
1070
+ "Avocado",
1071
+ "Banana",
1072
+ "Blackberry",
1073
+ "Blueberry",
1074
+ "Cherry",
1075
+ "Grape",
1076
+ "Lemon",
1077
+ "Mango",
1078
+ "Orange",
1079
+ "Peach",
1080
+ "Pear",
1081
+ "Pineapple",
1082
+ "Strawberry",
1083
+ ]
1084
+
1085
+ function Example() {
1086
+ return (
1087
+ <Autocomplete>
1088
+ <div data-slot="stack">
1089
+ <AutocompleteLabel>Favorite fruit</AutocompleteLabel>
1090
+ <AutocompleteInput placeholder="e.g. Mango" />
1091
+ </div>
1092
+ <AutocompleteContent>
1093
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
1094
+ <AutocompleteList>
1095
+ {fruits.map((fruit) => (
1096
+ <AutocompleteItem key={fruit} value={fruit}>
1097
+ {fruit}
1098
+ </AutocompleteItem>
1099
+ ))}
1100
+ </AutocompleteList>
1101
+ </AutocompleteContent>
1102
+ </Autocomplete>
1103
+ )
1104
+ }
1105
+
1106
+ export { Example }
1107
+ ```
1108
+
1109
+ Type to open the suggestions. They slide and fade in, and fade out before they unmount.
1110
+
1111
+ ## Presentation
1112
+
1113
+ `presentation="sheet"` opens suggestions in a sheet. `"auto"` chooses a sheet on small touch screens and a popover elsewhere, each time the list opens. The server starts with the popover.
1114
+
1115
+ The same input moves into the sheet’s header. Opening by touch leaves the keyboard closed; tapping the input opens it and expands the sheet. Choosing a suggestion or swiping down closes it. Keyboard and mouse opening preserve input focus.
1116
+
1117
+ A sheet is modal unless `modal` is `false`. Give it a height, keep suggestions inside `AutocompleteList`, and leave the content’s `translate` alone so dragging can move it. Use `data-presentation` to style both presentations and `--sheet-backdrop` to set the dimmed background.
1118
+
1119
+ ```tsx title="presentation.tsx"
1120
+ "use client"
1121
+
1122
+ import {
1123
+ Autocomplete,
1124
+ AutocompleteContent,
1125
+ AutocompleteEmpty,
1126
+ AutocompleteInput,
1127
+ AutocompleteItem,
1128
+ AutocompleteLabel,
1129
+ AutocompleteList,
1130
+ } from "@ui-sdk/react/autocomplete"
1131
+
1132
+ const fruits = [
1133
+ "Apple",
1134
+ "Apricot",
1135
+ "Avocado",
1136
+ "Banana",
1137
+ "Blackberry",
1138
+ "Blueberry",
1139
+ "Cherry",
1140
+ "Grape",
1141
+ "Lemon",
1142
+ "Mango",
1143
+ "Orange",
1144
+ "Peach",
1145
+ "Pear",
1146
+ "Pineapple",
1147
+ "Strawberry",
1148
+ ]
1149
+
1150
+ function Example() {
1151
+ return (
1152
+ <Autocomplete presentation="sheet">
1153
+ <div data-slot="stack">
1154
+ <AutocompleteLabel>Favorite fruit</AutocompleteLabel>
1155
+ <AutocompleteInput placeholder="e.g. Mango" />
1156
+ </div>
1157
+ <AutocompleteContent>
1158
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
1159
+ <AutocompleteList>
1160
+ {fruits.map((fruit) => (
1161
+ <AutocompleteItem key={fruit} value={fruit}>
1162
+ {fruit}
1163
+ </AutocompleteItem>
1164
+ ))}
1165
+ </AutocompleteList>
1166
+ </AutocompleteContent>
1167
+ </Autocomplete>
1168
+ )
1169
+ }
1170
+
1171
+ export { Example }
1172
+ ```
1173
+
1174
+ Open the field to see a sheet. On touch, tap the field in its header to show the keyboard, or swipe down to close it.
1175
+
1176
+ ## Forms
1177
+
1178
+ The native input is the form control: `name` gives its submitted key, `required` rejects empty text, and reset restores `defaultValue`. Text does not have to match a suggestion. Use `form` to associate the field with a form elsewhere in the page.
1179
+
1180
+ ```tsx title="form.tsx"
1181
+ "use client"
1182
+
1183
+ import {
1184
+ Autocomplete,
1185
+ AutocompleteContent,
1186
+ AutocompleteEmpty,
1187
+ AutocompleteInput,
1188
+ AutocompleteItem,
1189
+ AutocompleteList,
1190
+ } from "@ui-sdk/react/autocomplete"
1191
+ import {
1192
+ Field,
1193
+ FieldDescription,
1194
+ FieldError,
1195
+ FieldLabel,
1196
+ } from "@ui-sdk/react/field"
1197
+ import { useLog } from "./log"
1198
+
1199
+ const fruits = [
1200
+ "Apple",
1201
+ "Apricot",
1202
+ "Avocado",
1203
+ "Banana",
1204
+ "Blackberry",
1205
+ "Blueberry",
1206
+ "Cherry",
1207
+ "Grape",
1208
+ "Lemon",
1209
+ "Mango",
1210
+ "Orange",
1211
+ "Peach",
1212
+ "Pear",
1213
+ "Pineapple",
1214
+ "Strawberry",
1215
+ ]
1216
+
1217
+ function Example() {
1218
+ const log = useLog()
1219
+ return (
1220
+ <form
1221
+ id="order"
1222
+ data-slot="stack"
1223
+ onSubmit={(event) => {
1224
+ event.preventDefault()
1225
+ const data = [...new FormData(event.currentTarget)].map(
1226
+ ([name, value]) => `${name}=${value}`
1227
+ )
1228
+ log(`Submitted ${data.join("&")}`)
1229
+ }}
1230
+ >
1231
+ <Field>
1232
+ <FieldLabel>Fruit</FieldLabel>
1233
+ <Autocomplete name="fruit" form="order" required defaultValue="Apple">
1234
+ <AutocompleteInput />
1235
+ <AutocompleteContent>
1236
+ <AutocompleteEmpty>No fruits found.</AutocompleteEmpty>
1237
+ <AutocompleteList>
1238
+ {fruits.map((fruit) => (
1239
+ <AutocompleteItem key={fruit} value={fruit}>
1240
+ {fruit}
1241
+ </AutocompleteItem>
1242
+ ))}
1243
+ </AutocompleteList>
1244
+ </AutocompleteContent>
1245
+ </Autocomplete>
1246
+ <FieldDescription>Any fruit, listed or not.</FieldDescription>
1247
+ <div>
1248
+ <FieldError />
1249
+ </div>
1250
+ </Field>
1251
+ <div data-slot="row">
1252
+ <button type="submit" data-variant="solid">
1253
+ Submit
1254
+ </button>
1255
+ <button type="reset">Reset</button>
1256
+ </div>
1257
+ </form>
1258
+ )
1259
+ }
1260
+
1261
+ export { Example }
1262
+ ```
1263
+
1264
+ Submit Apple, then choose Mango. Choosing it fills the field without submitting. Reset restores Apple.
1265
+
1266
+ ## Search
1267
+
1268
+ Enter with no suggestion highlighted runs the `onSubmit` callback with the text, then allows the surrounding form to submit. Call `preventDefault()` on `details.event` if your handler already performs the search.
1269
+
1270
+ Choosing a suggestion normally only fills in the field. Add `submitOnSelect` to search as soon as someone chooses one.
1271
+
1272
+ ```tsx title="search.tsx"
1273
+ "use client"
1274
+
1275
+ import {
1276
+ Autocomplete,
1277
+ AutocompleteContent,
1278
+ AutocompleteEmpty,
1279
+ AutocompleteInput,
1280
+ AutocompleteItem,
1281
+ AutocompleteList,
1282
+ } from "@ui-sdk/react/autocomplete"
1283
+ import { useLog } from "./log"
1284
+
1285
+ const recent = ["react hooks", "tailwind grid", "web components", "vue v-model"]
1286
+
1287
+ function Example() {
1288
+ const log = useLog()
1289
+ return (
1290
+ <form
1291
+ role="search"
1292
+ data-slot="stack"
1293
+ onSubmit={(event) => event.preventDefault()}
1294
+ >
1295
+ <Autocomplete
1296
+ submitOnSelect
1297
+ name="q"
1298
+ onSubmit={(value) => log(`Searched “${value}”`)}
1299
+ >
1300
+ <AutocompleteInput type="search" aria-label="Search" placeholder="Search…" />
1301
+ <AutocompleteContent>
1302
+ <AutocompleteEmpty>No results.</AutocompleteEmpty>
1303
+ <AutocompleteList>
1304
+ {recent.map((search) => (
1305
+ <AutocompleteItem key={search} value={search}>
1306
+ {search}
1307
+ </AutocompleteItem>
1308
+ ))}
1309
+ </AutocompleteList>
1310
+ </AutocompleteContent>
1311
+ </Autocomplete>
1312
+ </form>
1313
+ )
1314
+ }
1315
+
1316
+ export { Example }
1317
+ ```
1318
+
1319
+ Type “kotlin” and press Enter to search. Choose a suggestion to search for that text instead.
1320
+
1321
+ ## Recent Searches
1322
+
1323
+ Pair `emptyItems` with `items` to show one collection while the field is empty and search the full collection once someone types. Add `openOnFocus` to show the recent searches on focus.
1324
+
1325
+ ```tsx title="recent.tsx"
1326
+ "use client"
1327
+
1328
+ import {
1329
+ Autocomplete,
1330
+ AutocompleteContent,
1331
+ AutocompleteEmpty,
1332
+ AutocompleteGroup,
1333
+ AutocompleteGroupLabel,
1334
+ AutocompleteInput,
1335
+ AutocompleteItem,
1336
+ AutocompleteItemText,
1337
+ AutocompleteList,
1338
+ } from "@ui-sdk/react/autocomplete"
1339
+
1340
+ const recent = ["react hooks", "tailwind grid", "web components", "vue v-model"]
1341
+ const topics = [
1342
+ ...recent,
1343
+ "react server components",
1344
+ "tailwind colors",
1345
+ "vue router",
1346
+ ]
1347
+ const recentSearches = [
1348
+ { type: "group" as const, label: "Recent searches", items: recent },
1349
+ ]
1350
+
1351
+ function Example() {
1352
+ return (
1353
+ <Autocomplete
1354
+ items={topics}
1355
+ emptyItems={recentSearches}
1356
+ openOnFocus
1357
+ announcement={(change) =>
1358
+ change.type === "results"
1359
+ ? `${change.count} ${change.count === 1 ? "topic" : "topics"}`
1360
+ : null
1361
+ }
1362
+ >
1363
+ <AutocompleteInput type="search" aria-label="Search docs" placeholder="Search docs…" />
1364
+ <AutocompleteContent>
1365
+ <AutocompleteEmpty>No topics found.</AutocompleteEmpty>
1366
+ <AutocompleteList>
1367
+ {(state) =>
1368
+ (state.items as (string | (typeof recentSearches)[number])[]).map(
1369
+ (entry) =>
1370
+ typeof entry === "string" ? (
1371
+ <AutocompleteItem key={entry} value={entry}>
1372
+ <AutocompleteItemText />
1373
+ </AutocompleteItem>
1374
+ ) : (
1375
+ <AutocompleteGroup key={entry.label}>
1376
+ <AutocompleteGroupLabel>
1377
+ {entry.label}
1378
+ </AutocompleteGroupLabel>
1379
+ {entry.items.map((topic) => (
1380
+ <AutocompleteItem key={topic} value={topic}>
1381
+ <AutocompleteItemText />
1382
+ </AutocompleteItem>
1383
+ ))}
1384
+ </AutocompleteGroup>
1385
+ )
1386
+ )
1387
+ }
1388
+ </AutocompleteList>
1389
+ </AutocompleteContent>
1390
+ </Autocomplete>
1391
+ )
1392
+ }
1393
+
1394
+ export { Example }
1395
+ ```
1396
+
1397
+ Focus the empty field to see Recent searches. Type “react” to search all topics, then clear the text to return to recent searches.
1398
+
1399
+ ## Async Suggestions
1400
+
1401
+ Use [async options](../hooks/async-options.md) to wait for typing to pause and discard stale results. Pass its `items` and `status`, set `filter` to `false`, and send text changes to its `setQuery()` method.
1402
+
1403
+ `AutocompleteStatus` and `AutocompleteEmpty` provide loading, error, and empty messages. Without a status part, `announcement` controls what screen readers hear when typing pauses. `onLoadMore` can request another page when scrolling or highlighting reaches the last suggestion.
1404
+
1405
+ ```tsx title="async.tsx"
1406
+ "use client"
1407
+
1408
+ import { useAsyncOptions } from "@ui-sdk/react/async-options"
1409
+ import {
1410
+ Autocomplete,
1411
+ AutocompleteContent,
1412
+ AutocompleteEmpty,
1413
+ AutocompleteInput,
1414
+ AutocompleteItem,
1415
+ AutocompleteLabel,
1416
+ AutocompleteList,
1417
+ AutocompleteStatus,
1418
+ } from "@ui-sdk/react/autocomplete"
1419
+
1420
+ function searchCities(query: string, { signal }: { signal: AbortSignal }) {
1421
+ const cities = [
1422
+ "Amsterdam",
1423
+ "Athens",
1424
+ "Barcelona",
1425
+ "Berlin",
1426
+ "Boston",
1427
+ "Brussels",
1428
+ "Buenos Aires",
1429
+ "Cairo",
1430
+ "Chicago",
1431
+ "Copenhagen",
1432
+ "Dublin",
1433
+ "Lisbon",
1434
+ "London",
1435
+ "Los Angeles",
1436
+ "Madrid",
1437
+ "Paris",
1438
+ "Prague",
1439
+ "Rome",
1440
+ "Seoul",
1441
+ "Tokyo",
1442
+ ]
1443
+ return new Promise<string[]>((resolve, reject) => {
1444
+ const timer = setTimeout(() => {
1445
+ const needle = query.trim().toLowerCase()
1446
+ resolve(cities.filter((city) => city.toLowerCase().startsWith(needle)))
1447
+ }, 300)
1448
+ signal.addEventListener("abort", () => {
1449
+ clearTimeout(timer)
1450
+ reject(new DOMException("Aborted", "AbortError"))
1451
+ })
1452
+ })
1453
+ }
1454
+
1455
+ function Example() {
1456
+ const results = useAsyncOptions(searchCities, { debounce: 200 })
1457
+
1458
+ return (
1459
+ <Autocomplete
1460
+ items={results.items}
1461
+ filter={false}
1462
+ status={results.status}
1463
+ onValueChange={(value) => results.setQuery(value)}
1464
+ onLoadMore={results.loadMore}
1465
+ >
1466
+ <div data-slot="stack">
1467
+ <AutocompleteLabel>City</AutocompleteLabel>
1468
+ <AutocompleteInput placeholder="e.g. Paris" />
1469
+ </div>
1470
+ <AutocompleteContent>
1471
+ <AutocompleteStatus>
1472
+ {(state) =>
1473
+ state.loading
1474
+ ? "Searching…"
1475
+ : state.count
1476
+ ? `${state.count} ${state.count === 1 ? "city" : "cities"}`
1477
+ : ""
1478
+ }
1479
+ </AutocompleteStatus>
1480
+ <AutocompleteEmpty>
1481
+ {(state) =>
1482
+ state.empty && !state.loading
1483
+ ? `No cities start with “${state.query}”.`
1484
+ : null
1485
+ }
1486
+ </AutocompleteEmpty>
1487
+ <AutocompleteList>
1488
+ {(state) =>
1489
+ (state.items as string[]).map((city) => (
1490
+ <AutocompleteItem key={city} value={city}>
1491
+ {city}
1492
+ </AutocompleteItem>
1493
+ ))
1494
+ }
1495
+ </AutocompleteList>
1496
+ </AutocompleteContent>
1497
+ </Autocomplete>
1498
+ )
1499
+ }
1500
+
1501
+ export { Example }
1502
+ ```
1503
+
1504
+ Type “b” and wait for the city suggestions. Keep typing to replace the search; the status describes the latest results.
1505
+
1506
+ ## Custom Rendering
1507
+
1508
+ Use `render` to give a suggestion a different element. Here each suggestion is a link, so the autocomplete works as a jump-to menu: Enter or a click follows the highlighted link, and the text after the title is a hint that doesn't affect filtering. [Parts and Composition](../parts-and-composition.md) covers the render API.
1509
+
1510
+ ```tsx title="custom-rendering.tsx"
1511
+ "use client"
1512
+
1513
+ import {
1514
+ Autocomplete,
1515
+ AutocompleteContent,
1516
+ AutocompleteEmpty,
1517
+ AutocompleteInput,
1518
+ AutocompleteItem,
1519
+ AutocompleteLabel,
1520
+ AutocompleteList,
1521
+ } from "@ui-sdk/react/autocomplete"
1522
+
1523
+ const pages = [
1524
+ { title: "Installation", section: "Get Started", href: "#installation" },
1525
+ { title: "Composition", section: "Concepts", href: "#composition" },
1526
+ { title: "Build Your Own", section: "Concepts", href: "#build-your-own" },
1527
+ { title: "Styling", section: "Guides", href: "#styling" },
1528
+ { title: "API Reference", section: "Reference", href: "#api-reference" },
1529
+ { title: "Accessibility", section: "Reference", href: "#accessibility" },
1530
+ ]
1531
+
1532
+ function Example() {
1533
+ return (
1534
+ <Autocomplete>
1535
+ <div data-slot="stack">
1536
+ <AutocompleteLabel>Jump to</AutocompleteLabel>
1537
+ <AutocompleteInput placeholder="e.g. Styling" />
1538
+ </div>
1539
+ <AutocompleteContent>
1540
+ <AutocompleteEmpty>No sections found.</AutocompleteEmpty>
1541
+ <AutocompleteList>
1542
+ {pages.map((page) => (
1543
+ <AutocompleteItem
1544
+ key={page.href}
1545
+ value={page.title}
1546
+ label={page.title}
1547
+ render={<a href={page.href} />}
1548
+ >
1549
+ {page.title}
1550
+ <span data-slot="hint">{page.section}</span>
1551
+ </AutocompleteItem>
1552
+ ))}
1553
+ </AutocompleteList>
1554
+ </AutocompleteContent>
1555
+ </Autocomplete>
1556
+ )
1557
+ }
1558
+
1559
+ export { Example }
1560
+ ```
1561
+
1562
+ Type “sty” and press Enter to jump to the Styling section of this page.
1563
+
1564
+ ## Styling
1565
+
1566
+ Each part has a `data-slot`, such as `autocomplete-content`, and shows its state through the attributes listed under [Data Attributes](#data-attributes).
1567
+
1568
+ The popup exposes sizing and animation variables. `--anchor-width` is the input’s width; use `--available-height` on the content to keep a long list in the viewport.
1569
+
1570
+ While focus stays in the input, use the content’s `data-modality="keyboard"` with an item’s `data-highlighted` to show a keyboard focus indicator on the highlighted suggestion.
1571
+
1572
+ ### AutocompleteContent
1573
+ - `--anchor-width`: Width of the anchor, usually the trigger. Use it to match widths.
1574
+ - `--anchor-height`: Height of the anchor.
1575
+ - `--available-width`: Space beside the popup, up to the viewport edge. Not inherited either.
1576
+ - `--available-height`: Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like `[--list-height:var(--available-height)]`, and reads `max-h-(--list-height)`. The first popup registers both names for the whole page with `CSS.registerProperty`, so they don't inherit anywhere. A page that needs them to inherit calls it first, with `inherits: true`. An `@property` rule can't, since a registration in script wins.
1577
+ - `--transform-origin`: The point on the anchor the popup opens from. Use it to scale from the right place.
1578
+ - `--arrow-offset`: Where the anchor's center falls along the popup's edge, kept `arrowPadding` from the corners. Position a ::before arrow with it.
1579
+ - `--drawer-progress`: While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.
1580
+ - `--drawer-dim`: While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.
1581
+ - `--drawer-expand`: While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.
1582
+ - `--drawer-stack`: While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.
1583
+ - `--sheet-backdrop`: Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.
1584
+
1585
+ ### Viewport
1586
+ - `--keyboard-inset`: While it presents as a sheet: how many pixels of the page the on-screen keyboard covers, 0 without one.
1587
+ - `--visual-viewport-width`: While it presents as a sheet: the width of the part of the page you can see.
1588
+ - `--visual-viewport-height`: While it presents as a sheet: the height of the part of the page you can see, above the keyboard.
1589
+ - `--visual-viewport-top`: While it presents as a sheet: how far the part of the page you can see is scrolled from the top of the layout, as when the keyboard pushes it up.
1590
+
1591
+ ## Data Attributes
1592
+
1593
+ ### AutocompleteLabel
1594
+ `[data-slot="autocomplete-label"]`
1595
+
1596
+ - `data-disabled`: Present when the field is disabled, by its own prop, a field or a `<fieldset>`.
1597
+ - `data-readonly`: Present when the field is read-only.
1598
+ - `data-required`: Present when the field is required.
1599
+
1600
+ ### AutocompleteInput
1601
+ `[data-slot="autocomplete-input"]`
1602
+
1603
+ - `data-closed`: Present while the suggestions are closed, including while they animate out.
1604
+ - `data-open`: Present while the suggestions are open.
1605
+ - `data-disabled`: Present when the field is disabled, by its own prop, a field or a `<fieldset>`.
1606
+ - `data-readonly`: Present when the field is read-only.
1607
+ - `data-required`: Present when the field is required.
1608
+
1609
+ ### AutocompleteTrigger
1610
+ `[data-slot="autocomplete-trigger"]`
1611
+
1612
+ - `data-closed`: Present while the suggestions are closed, including while they animate out.
1613
+ - `data-open`: Present while the suggestions are open.
1614
+ - `data-disabled`: Present when the field is disabled, by its own prop, a field or a `<fieldset>`.
1615
+ - `data-readonly`: Present when the field is read-only.
1616
+ - `data-required`: Present when the field is required.
1617
+
1618
+ ### AutocompleteClear
1619
+ `[data-slot="autocomplete-clear"]`
1620
+
1621
+ - `data-disabled`: Present when the field is disabled or read-only.
1622
+
1623
+ ### AutocompleteContent
1624
+ `[data-slot="autocomplete-content"]`
1625
+
1626
+ - `data-closed`: Present while the suggestions are closed, including while they animate out.
1627
+ - `data-presentation` (`popover`, `sheet`): How the suggestions present this time they're open.
1628
+ - `data-open`: Present while the suggestions are open.
1629
+ - `data-starting-style`: Present for the first frame. Style the state you animate from.
1630
+ - `data-ending-style`: Present while the list animates out. Style the state you animate to.
1631
+ - `data-modality` (`keyboard`, `pointer`): Whether the keyboard or a pointer moves the highlight. Style a focus ring for the keyboard only.
1632
+ - `data-has-icons`: Present while any suggestion inside holds an icon part, so items without one can line up.
1633
+
1634
+ ### AutocompleteItem
1635
+ `[data-slot="autocomplete-item"]`
1636
+
1637
+ - `data-disabled`: Present when the suggestion can't be chosen. Arrow keys skip it.
1638
+ - `data-highlighted`: Present on the highlighted suggestion, which Enter fills in. Focus stays in the field.
1639
+ - `data-has-icon`: Present on a suggestion while it holds an icon part.
1640
+
1641
+ ### AutocompleteItemText
1642
+ `[data-slot="autocomplete-item-text"]`
1643
+
1644
+ - `data-matched`: Present on a suggestion's text while the field's text matched part of it, which is marked.
1645
+
1646
+ ### AutocompleteEmpty
1647
+ `[data-slot="autocomplete-empty"]`
1648
+
1649
+ - `data-loading`: Present while results load, with `status="loading"`.
1650
+ - `data-empty`: Present while no suggestion matches.
1651
+
1652
+ ### AutocompleteStatus
1653
+ `[data-slot="autocomplete-status"]`
1654
+
1655
+ - `data-loading`: Present while results load, with `status="loading"`.
1656
+
1657
+ ### AutocompleteArrow
1658
+ `[data-slot="autocomplete-arrow"]`
1659
+
1660
+ - `data-closed`: Present while the suggestions are closed, including while they animate out.
1661
+ - `data-open`: Present while the suggestions are open.
1662
+
1663
+ ### AutocompleteItemIcon
1664
+ `[data-slot="autocomplete-item-icon"]`
1665
+
1666
+ - `data-mirror`: Present on an icon that flips in right-to-left text, with `mirror`.
1667
+
1668
+ ## API Reference
1669
+
1670
+ Props and callbacks are the same as below, as React props.
1671
+
1672
+ ### Autocomplete
1673
+
1674
+ - `container?` (`DocumentFragment | Element | null`): Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
1675
+ - `value?` (`string`): The text. Pass it to control it.
1676
+ - `defaultValue?` (`string`): The text at first, when uncontrolled. A form reset returns to it.
1677
+ - `onValueChange?` (`((value: string, details: ChangeDetails<AutocompleteValueChangeReason>) => void)`): Runs when the text wants to change: typing, choosing a suggestion, a Clear part, a form reset or a controller. `details.cancel()` keeps it as it was.
1678
+ - `open?` (`boolean`): Whether the suggestions are open. Pass it to control them.
1679
+ - `defaultOpen?` (`boolean`): Whether the suggestions start open, when uncontrolled.
1680
+ - `onOpenChange?` (`((open: boolean, details: ChangeDetails<AutocompleteOpenChangeReason>) => void)`): Runs when the suggestions want to open or close. `details.reason` says why, and `details.cancel()` stops it.
1681
+ - `onOpenChangeComplete?` (`((open: boolean) => void)`): Runs once the suggestions have finished opening or closing, after any animation.
1682
+ - `onSubmit?` (`((value: string, details: AutocompleteSubmitDetails) => void)`): Runs with the text when Enter is pressed with no suggestion highlighted, or after choosing one with `submitOnSelect`. The surrounding form submits next, unless the event's `preventDefault()` is called.
1683
+ - `items?` (`ListData<string>`): The suggestions as data. Renders them for you, and filters them without rendering every one.
1684
+ - `emptyItems?` (`ListData<string>`): Suggestions shown instead of `items` while the field is empty, like recent searches, with `openOnFocus`. Groups work, like `{ type: "group", label: "Recent", items }`. Needs `items`.
1685
+ - `filter?` (`SearchFilter<string>`): How typing narrows the suggestions: `"contains"`, `"startsWith"`, `"fuzzy"`, a function of the item and the text, or `false` to keep every one, for results a server already filtered.
1686
+ - `inlineComplete?` (`boolean`): Whether typing completes the text with the first suggestion that starts with it, with the rest selected after the caret: ArrowRight or End accepts it, Backspace and Escape take it back out, and arrow keys fill in the suggestion they highlight.
1687
+ - `autoHighlight?` (`boolean`): Whether the first suggestion is highlighted while typing, so Enter chooses it.
1688
+ - `openOnFocus?` (`boolean`): Whether the suggestions open when the field gets focus, like recent searches, even before anything is typed.
1689
+ - `submitOnSelect?` (`boolean`): Whether choosing a suggestion submits, like a search box: `onSubmit` runs, then the form submits.
1690
+ - `status?` (`AsyncOptionsStatus`): Whether results are loading or failed, for the empty and status parts. Sets `aria-busy` on the list while loading.
1691
+ - `onHighlight?` (`((value: string | null, details: ListHighlightDetails) => void)`): Runs when the highlight moves, with the suggestion's index among the visible ones, so a virtualized list can scroll to it.
1692
+ - `onLoadMore?` (`(() => void)`): Runs when the highlight or scrolling reaches the last suggestion, so the next page can load, like `createAsyncOptions`'s `loadMore`. Not again for the same suggestions, unless the last load failed.
1693
+ - `announcement?` (`((change: ListAnnouncement) => string | null | undefined)`): What screen readers hear after typing pauses, when no `AutocompleteStatus` is rendered. Return null for nothing.
1694
+ - `loop?` (`boolean`): Whether arrow keys wrap from the last suggestion to the first.
1695
+ - `modal?` (`"pointer" | boolean`): `false`: the page stays usable while the suggestions are open. `"pointer"`: an outside press only closes them. `true`: also locks scroll and makes the rest of the page inert.
1696
+ - `presentation?` (`PresentationSetting<"popover">`): How the suggestions present. `"sheet"` slides them up from the bottom of the screen with the field in its header, where they can be swiped away, and `"auto"` does that on small touch screens. A tap that opens a sheet leaves the keyboard closed until the field in the header is tapped. Decided each time they open; the server renders `"popover"` for `"auto"`.
1697
+ - `disabled?` (`boolean`): Disables the field like a native input: it can't be focused or changed, and its text isn't submitted.
1698
+ - `readOnly?` (`boolean`): Keeps the field focusable and its text submitted, but stops changes.
1699
+ - `required?` (`boolean`): Blocks form submission while the field is empty, with the browser's own message.
1700
+ - `name?` (`string`): The name the text submits under.
1701
+ - `form?` (`string`): The id of a form to belong to, when the field isn't inside that form.
1702
+ - `controller?` (`object`): Opens, closes, reads and sets the autocomplete from anywhere. Create one with `createAutocompleteController()`.
1703
+ - `dismissible?` (`boolean`): Whether Escape, an outside press, focus or the window leaving, Android's back gesture and a sheet's swipe can close it. Triggers, Close parts and the controller always can. Pass `false` while it holds unsaved work, and ask in `onDismissAttempt`.
1704
+ - `onDismissAttempt?` (`function`): Runs when `dismissible` refused a dismissal made on purpose: Escape, an outside press, back or a swipe. Focus or the window leaving is refused without it. Ask "Discard changes?" here.
1705
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
1706
+
1707
+ ### AutocompleteLabel
1708
+
1709
+ Names the input. Renders a `label` tied to the input, so you don't need a `Field`.
1710
+
1711
+ ### AutocompleteInput
1712
+
1713
+ The text field. Renders an `input` with the combobox role, and accepts its props.
1714
+
1715
+ ### AutocompleteTrigger
1716
+
1717
+ An optional button that opens and closes the suggestions. Renders a `button`.
1718
+
1719
+ ### AutocompleteClear
1720
+
1721
+ - `keepMounted?` (`boolean`): Whether the clear button stays in the page while the field is empty.
1722
+
1723
+ ### AutocompleteContent
1724
+
1725
+ - `side?` (`Side`): Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the field the suggestions open on.
1726
+ - `align?` (`Align`): How the popup lines up with the anchor along that side. `start` and `end` follow the reading direction. How the suggestions line up with the field along that side.
1727
+ - `sideOffset?` (`number`): Gap between the anchor and the popup, in pixels. Gap between the field and the suggestions, in pixels.
1728
+ - `alignOffset?` (`number`): Shift along the anchor, in pixels. Shift along the field, in pixels.
1729
+ - `collisionPadding?` (`number`): Space to keep from the viewport edges, in pixels.
1730
+ - `arrowPadding?` (`number`): Space to keep between the arrow and the popup's corners, in pixels.
1731
+ - `anchor?` (`string | Anchor | null`): Positions the popup against this instead of its trigger: an element, a CSS selector, a point like `{ x, y }`, or anything with `getBoundingClientRect()`. It's read on every check, so it can move. A controller's `open({ anchor })` wins for that open.
1732
+ - `container?` (`DocumentFragment | Element | null`): Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
1733
+ - `keepMounted?` (`boolean`): Whether the suggestions stay in the page, hidden, while closed.
1734
+
1735
+ ### AutocompleteArrow
1736
+
1737
+ An optional arrow pointing at the input. Renders a `div` positioned on the content's edge.
1738
+
1739
+ ### AutocompleteStatus
1740
+
1741
+ Announces loading and result counts to screen readers. Its children can be a function of the state.
1742
+
1743
+ ### AutocompleteEmpty
1744
+
1745
+ Shows when no suggestion matches. Its children can be a function of the state.
1746
+
1747
+ ### AutocompleteList
1748
+
1749
+ Holds the suggestions. Renders the listbox, and renders `items` when you pass no children.
1750
+
1751
+ ### AutocompleteGroup
1752
+
1753
+ Groups related suggestions. Renders a `div` with the group role.
1754
+
1755
+ ### AutocompleteGroupLabel
1756
+
1757
+ Names its group. Renders a `div` the group is labelled by.
1758
+
1759
+ ### AutocompleteItem
1760
+
1761
+ - `value` (`string`): Identifies the suggestion. Choosing it fills the field with its `label`, or with this when there's none.
1762
+ - `label?` (`string`): The text the field gets and filtering matches, when the content isn't plain text. Not the accessible name: use `aria-label` for that.
1763
+ - `keywords?` (`string[]`): More words filtering matches.
1764
+ - `disabled?` (`boolean`): Keeps the suggestion from being chosen. Sets `data-disabled` and `aria-disabled`.
1765
+ - `index?` (`number`): The suggestion's position among all visible ones, for virtualized lists that render only some. Sets `aria-posinset` and `aria-setsize`.
1766
+
1767
+ ### AutocompleteItemIcon
1768
+
1769
+ - `mirror?` (`boolean`): Flips the icon horizontally in right-to-left text, for icons that point a way, like arrows and chevrons. Icons that picture a thing, like a clock or a check, stay as they are.
1770
+
1771
+ ### AutocompleteItemText
1772
+
1773
+ The suggestion's text, with the matched part marked. Its children can be a function that receives the matched `segments`.
1774
+
1775
+ ### AutocompleteSeparator
1776
+
1777
+ A divider between suggestions or groups. Renders a `div` with the separator role.
1778
+
1779
+ ### Items as Data
1780
+
1781
+ Each suggestion in `items`, or a plain string. Groups are `{ type: "group", label, items }` and separators `{ type: "separator" }`.
1782
+
1783
+ - `value` (`T`): Identifies the item, and is what choosing it reports. A string, or any value with `itemToValue`.
1784
+ - `label` (`string`): The text shown and matched by typeahead and filtering. A select's trigger shows it once chosen.
1785
+ - `keywords?` (`string[]`): More words filtering matches, like `["team"]` for a plan called Pro.
1786
+ - `disabled?` (`boolean`): Keeps the item from being chosen. The keyboard skips it, like in a native select.
1787
+
1788
+ ### State
1789
+
1790
+ What render functions, state-based class names and slots receive, per part.
1791
+
1792
+ `AutocompleteInput` and `AutocompleteTrigger`:
1793
+
1794
+ - `open` (`boolean`): Whether the suggestions are open.
1795
+ - `disabled` (`boolean`): Whether the field is disabled, by itself or by a `<fieldset>`.
1796
+ - `readOnly` (`boolean`): Whether the field is read-only.
1797
+ - `required` (`boolean`): Whether the field is required.
1798
+
1799
+ `AutocompleteContent` and `AutocompleteList`:
1800
+
1801
+ - `open` (`boolean`): Whether the suggestions are open. False while they animate out.
1802
+ - `presentation` (`"popover" | "sheet"`): How they present this time they're open.
1803
+ - `status` (`AsyncOptionsStatus`): Whether results are loading or failed.
1804
+ - `items` (`ListData<string>`): The visible suggestions, for rendering a virtualized list yourself.
1805
+
1806
+ `AutocompleteItem`:
1807
+
1808
+ - `highlighted` (`boolean`): Whether the suggestion is highlighted, by the keyboard or the pointer.
1809
+ - `disabled` (`boolean`): Whether the suggestion can't be chosen.
1810
+
1811
+ `AutocompleteEmpty`:
1812
+
1813
+ - `loading` (`boolean`): Whether results are loading.
1814
+ - `empty` (`boolean`): Whether no item matches.
1815
+ - `query` (`string`): The text the list is filtered by.
1816
+
1817
+ `AutocompleteStatus`:
1818
+
1819
+ - `status` (`"idle" | "loading" | "error"`): Whether results are loading or failed.
1820
+ - `loading` (`boolean`): Whether results are loading.
1821
+ - `count` (`number`): How many items are visible.
1822
+
1823
+ `AutocompleteClear`:
1824
+
1825
+ - `disabled` (`boolean`): Whether the field is disabled or read-only.
1826
+
1827
+ `AutocompleteArrow`:
1828
+
1829
+ - `open` (`boolean`): Whether the suggestions are open.
1830
+
1831
+ `AutocompleteItemText`, which takes no props of its own:
1832
+
1833
+ - `segments` (`MatchSegment[]`): The text in pieces, each marked when it matched. Joined, they're the whole text.
1834
+ - `matched` (`boolean`): Whether any piece matched.
1835
+
1836
+ `AutocompleteItemIcon`:
1837
+
1838
+ - `mirror` (`boolean`): Whether it flips in right-to-left text.
1839
+
1840
+ ### Reasons
1841
+
1842
+ Why the text changed:
1843
+
1844
+ - `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.
1845
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
1846
+ - `list-navigation` (`KeyboardEvent`): Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or ⌘ with A selected every item of a listbox.
1847
+ - `escape-key` (`KeyboardEvent`): Escape, or a submenu's close key (ArrowLeft in left-to-right layouts).
1848
+ - `clear-press` (`MouseEvent | KeyboardEvent`): A Clear part was pressed, like on an input, a select or a combobox.
1849
+ - `reset` (`none`): A form reset.
1850
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1851
+
1852
+ And why the suggestions opened or closed:
1853
+
1854
+ - `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.
1855
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
1856
+ - `escape-key` (`KeyboardEvent`): Escape, or a submenu's close key (ArrowLeft in left-to-right layouts).
1857
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1858
+ - `trigger-press` (`MouseEvent | KeyboardEvent | TouchEvent`): Click, tap, Enter or Space on the part that opens, closes or toggles. Arrow keys that open a menu or submenu count too. A tap on a combobox's input that opens a sheet reports its `touchend`.
1859
+ - `trigger-focus` (`FocusEvent`): Keyboard focus reached a trigger that opens on focus, like a tooltip's.
1860
+ - `input-commit` (`KeyboardEvent | FocusEvent`): Enter, or leaving a number input, committed what was typed: a number input parses, clamps and snaps it to the step, and an autocomplete closes its suggestions on it.
1861
+ - `outside-press` (`MouseEvent`): A press outside the layer.
1862
+ - `close-press` (`MouseEvent | KeyboardEvent | SubmitEvent`): A Close part was pressed, or a form with `method="dialog"` was submitted.
1863
+ - `focus-out` (`KeyboardEvent | FocusEvent`): Focus left the layer, or Tab moved it out.
1864
+ - `window-blur` (`FocusEvent`): The window lost focus.
1865
+ - `back` (`Event`): Going back: the Android back button or gesture, or Backspace in an empty command input, which returns to the previous page.
1866
+ - `swipe` (`TouchEvent | PointerEvent`): A swipe gesture: a finger, mouse or pen dragged it.
1867
+
1868
+ With `dismissible` false, the refused dismissals `onDismissAttempt` reports:
1869
+
1870
+ - `escape-key` (`KeyboardEvent`): Escape, or a submenu's close key (ArrowLeft in left-to-right layouts).
1871
+ - `outside-press` (`MouseEvent`): A press outside the layer.
1872
+ - `back` (`Event`): Going back: the Android back button or gesture, or Backspace in an empty command input, which returns to the previous page.
1873
+ - `swipe` (`TouchEvent | PointerEvent`): A swipe gesture: a finger, mouse or pen dragged it.
1874
+
1875
+ ### Controller
1876
+
1877
+ Create one with `createAutocompleteController()` and pass it as `controller`.
1878
+
1879
+ - `isOpen()` (`() => boolean`): Whether the suggestions are open.
1880
+ - `setOpen(open, options?)` (`(open: boolean, options?: AutocompleteChangeOptions<AutocompleteOpenChangeReason> | undefined) => boolean`): Opens or closes the suggestions. Returns false when the change was canceled or it was already open or closed.
1881
+ - `open(options?)` (`(options?: (AutocompleteChangeOptions<AutocompleteOpenChangeReason> & { signal?: AbortSignal | undefined; }) | undefined) => Promise<...>`): Opens the suggestions, and resolves once they close, with why and how. Resolves exactly once and never rejects: on the next close, when `signal` aborts, or with reason `"unmount"`.
1882
+ - `close(options?)` (`(options?: AutocompleteChangeOptions<AutocompleteOpenChangeReason> | undefined) => boolean`): Closes the suggestions. Returns false when the close was canceled or it was already closed.
1883
+ - `toggle(options?)` (`(options?: AutocompleteChangeOptions<AutocompleteOpenChangeReason> | undefined) => boolean`): Opens the suggestions when they're closed, and closes them when they're open. Returns false when the change was canceled.
1884
+ - `getValue()` (`() => string`): The text.
1885
+ - `setValue(value, options?)` (`(value: string, options?: AutocompleteChangeOptions<AutocompleteValueChangeReason> | undefined) => boolean`): Changes the text, which filters the suggestions. Returns false when the change was canceled or nothing changed.
1886
+ - `clear(options?)` (`(options?: AutocompleteChangeOptions<AutocompleteValueChangeReason> | undefined) => boolean`): Empties the field. Returns false when the change was canceled or nothing changed.
1887
+ - `focus()` (`() => void`): Moves focus to the field.
1888
+
1889
+ ### Events
1890
+
1891
+ Callbacks get the new state first, then the details.
1892
+
1893
+ | Prop | On | Receives |
1894
+ | ---------------------- | -------------- | ------------------------------------------------------------ |
1895
+ | `onValueChange` | `Autocomplete` | `(value, details)`: the new text, and the [reason](#reasons) |
1896
+ | `onOpenChange` | `Autocomplete` | `(open, details)`: the new state, and the [reason](#reasons) |
1897
+ | `onOpenChangeComplete` | `Autocomplete` | `(open)`, after the animation |
1898
+ | `onSubmit` | `Autocomplete` | `(value, { event })`: the text, when Enter submits |
1899
+ | `onHighlight` | `Autocomplete` | `(value, { index, event })`: the highlighted suggestion |
1900
+ | `onLoadMore` | `Autocomplete` | `()`, when the last suggestion is reached |
1901
+ | `onDismissAttempt` | `Autocomplete` | `({ reason, event })`, when a dismissal was refused |
1902
+
1903
+ ## Accessibility
1904
+
1905
+ The autocomplete follows the [WAI-ARIA combobox pattern with list autocomplete](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-autocomplete-list/), and [list and inline](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-autocomplete-both/) with `inlineComplete`.
1906
+
1907
+ Typing filters the suggestions. Arrow keys move the highlight; Enter fills in the highlighted text and Escape closes the list. Focus usually stays in the input while `aria-activedescendant` identifies the highlighted suggestion. A touch screen-reader trigger can move focus into the list, and a sheet has its own [focus behavior](#presentation).
1908
+
1909
+ Every line below is an automated test.
1910
+
1911
+ **Field**
1912
+
1913
+ - Is a combobox named by its label, with list autocomplete.
1914
+ - Typing opens the suggestions that match, and the text stays what was typed.
1915
+ - Text that matches nothing stays, with the empty message.
1916
+ - Typing in the middle of the text keeps the caret where it was.
1917
+
1918
+ **Choosing**
1919
+
1920
+ - Arrow keys move the highlight while focus stays in the field.
1921
+ - Enter fills in the highlighted suggestion and closes, without submitting.
1922
+ - Clicking a suggestion fills it in and keeps focus in the field.
1923
+ - A disabled suggestion can't be chosen.
1924
+ - Escape takes an inline completion back out.
1925
+ - Escape closes the suggestions and keeps the text.
1926
+
1927
+ **Forms**
1928
+
1929
+ - Enter with nothing highlighted submits the text as typed.
1930
+ - The field submits a chosen suggestion's text under its name.
1931
+ - Required blocks submitting an empty field.
1932
+ - A disabled field can't be focused or changed.
1933
+
1934
+ **Clear**
1935
+
1936
+ - The clear button empties the field and keeps focus in it.
1937
+
1938
+ Runs in React, Vue and web components.
1939
+
1940
+ Also tested:
1941
+
1942
+ **Enhancements**
1943
+
1944
+ - Inline completion fills in the first match with the rest selected after the caret.
1945
+ - Backspace takes an inline completion out and doesn't complete again.
1946
+ - ArrowRight accepts an inline completion.
1947
+ - With inline completion, arrow keys fill in the suggestion they highlight.
1948
+ - The suggestions open when the field gets focus.
1949
+ - Enter with nothing highlighted runs onSubmit with the text.
1950
+ - Filtering matches a suggestion's keywords.
1951
+ - A form reset puts the default text back.
1952
+ - The list is named by the field's label.
1953
+ - An immediate search runs once when the autocomplete mounts.
1954
+ - A newer search aborts the older one, and only its results land.
1955
+ - Arrow keys keep the highlighted item in view when the … scrolls.
1956
+
1957
+ Runs in React, Vue and web components.
1958
+
1959
+ Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
1960
+
1961
+ - Announcements while typing and choosing with a keyboard.
1962
+
1963
+ Runs in React, Vue and web components.