@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,1395 @@
1
+ # Toast
2
+
3
+ Show notifications with actions, timed dismissal, and screen reader announcements.
4
+
5
+ A toast tells someone that something happened, like a saved change or a sent message, and goes away on its own. It can offer an action such as Undo, stacks with the toasts before it, and is announced to screen readers without taking focus.
6
+
7
+ ```tsx title="demo.tsx"
8
+ import * as React from "react"
9
+ import {
10
+ Toast,
11
+ ToastAction,
12
+ ToastDescription,
13
+ ToastRegion,
14
+ ToastTitle,
15
+ createToaster,
16
+ } from "@ui-sdk/react/toast"
17
+ import { useLog } from "./log"
18
+
19
+ function Example() {
20
+ const log = useLog()
21
+ const [toast] = React.useState(() => createToaster())
22
+ React.useEffect(
23
+ () => () => {
24
+ toast.dismiss()
25
+ },
26
+ [toast]
27
+ )
28
+
29
+ return (
30
+ <>
31
+ <button
32
+ onClick={() =>
33
+ toast("Event created", {
34
+ description: "Friday, June 12 at 10:00 AM",
35
+ action: { label: "Undo", onAction: () => log("Event removed") },
36
+ })
37
+ }
38
+ >
39
+ Create Event
40
+ </button>
41
+ <ToastRegion toaster={toast}>
42
+ {() => (
43
+ <Toast>
44
+ <div data-slot="stack">
45
+ <ToastTitle />
46
+ <ToastDescription />
47
+ </div>
48
+ <ToastAction data-variant="solid" />
49
+ </Toast>
50
+ )}
51
+ </ToastRegion>
52
+ </>
53
+ )
54
+ }
55
+
56
+ export { Example }
57
+ ```
58
+
59
+ Press Create Event. Choose Undo, swipe the toast away, or wait for it to close. Undo logs Event removed.
60
+
61
+ ## Installation
62
+
63
+ ```bash
64
+ npm install @ui-sdk/react @ui-sdk/core
65
+ ```
66
+
67
+ ## Composition
68
+
69
+ Use the following composition to build a `ToastRegion`:
70
+
71
+ ```text
72
+ ToastRegion
73
+ ├── Toast
74
+ │ ├── ToastTitle
75
+ │ ├── ToastDescription
76
+ │ ├── ToastAction
77
+ │ └── ToastClose
78
+ └── Toast
79
+ ├── ToastTitle
80
+ └── ToastDescription
81
+ ```
82
+
83
+ The region renders one `Toast` for each toast in its queue, newest first, through its children function. A toast is named by its title and described by its description. Empty title, description and action parts show the text from the toast's data, so a region's renderer is usually the same few parts.
84
+
85
+ ## Usage
86
+
87
+ Import the parts and put them together, then call `toast()` when an action finishes:
88
+
89
+ ```tsx
90
+ import {
91
+ Toast,
92
+ ToastDescription,
93
+ ToastRegion,
94
+ ToastTitle,
95
+ toast,
96
+ } from "@ui-sdk/react/toast"
97
+
98
+ function Example() {
99
+ return (
100
+ <>
101
+ <button
102
+ type="button"
103
+ onClick={() =>
104
+ toast("Event created", { description: "Friday at 10:00 AM" })
105
+ }
106
+ >
107
+ Create Event
108
+ </button>
109
+ <ToastRegion>
110
+ {() => (
111
+ <Toast>
112
+ <ToastTitle />
113
+ <ToastDescription />
114
+ </Toast>
115
+ )}
116
+ </ToastRegion>
117
+ </>
118
+ )
119
+ }
120
+
121
+ export { Example }
122
+ ```
123
+
124
+ `toast()` works from anywhere, not only from components, and returns the toast's id. Place the region once, near your app's root. Without children, it renders each toast's title, description, action and cancel button with unstyled parts.
125
+
126
+ The examples below each call `createToaster()` for a queue of their own and pass it to their region as `toaster`, so their toasts don't mix.
127
+
128
+ ## Types
129
+
130
+ `toast.success`, `.info`, `.warning`, `.error` and `.loading` set the toast's type, which the toast exposes as `data-type` for an icon or a color. Errors are announced right away and other types wait for a pause; set `priority` to change that. A loading toast stays until it's updated or dismissed.
131
+
132
+ ```tsx title="types.tsx"
133
+ import * as React from "react"
134
+ import {
135
+ Toast,
136
+ ToastRegion,
137
+ ToastTitle,
138
+ createToaster,
139
+ } from "@ui-sdk/react/toast"
140
+ import {
141
+ CircleCheckIcon,
142
+ InfoIcon,
143
+ LoaderCircleIcon,
144
+ OctagonXIcon,
145
+ TriangleAlertIcon,
146
+ } from "lucide-react"
147
+
148
+ function upload() {
149
+ return new Promise((resolve) => setTimeout(resolve, 2000))
150
+ }
151
+
152
+ function Example() {
153
+ const [toast] = React.useState(() => createToaster())
154
+ React.useEffect(
155
+ () => () => {
156
+ toast.dismiss()
157
+ },
158
+ [toast]
159
+ )
160
+
161
+ return (
162
+ <>
163
+ <div data-slot="row">
164
+ <button onClick={() => toast("Event created")}>Default</button>
165
+ <button onClick={() => toast.success("Changes saved")}>Success</button>
166
+ <button onClick={() => toast.info("A new version is available")}>
167
+ Info
168
+ </button>
169
+ <button onClick={() => toast.warning("Storage is almost full")}>
170
+ Warning
171
+ </button>
172
+ <button onClick={() => toast.error("Couldn't save changes")}>
173
+ Error
174
+ </button>
175
+ <button
176
+ onClick={() =>
177
+ toast.promise(upload, {
178
+ loading: "Uploading photos…",
179
+ success: "Photos uploaded",
180
+ error: "Upload failed",
181
+ })
182
+ }
183
+ >
184
+ Loading
185
+ </button>
186
+ </div>
187
+ <ToastRegion toaster={toast}>
188
+ {() => (
189
+ <Toast>
190
+ <CircleCheckIcon data-show="success" aria-hidden="true" />
191
+ <InfoIcon data-show="info" aria-hidden="true" />
192
+ <TriangleAlertIcon data-show="warning" aria-hidden="true" />
193
+ <OctagonXIcon data-show="error" aria-hidden="true" />
194
+ <LoaderCircleIcon data-show="loading" aria-hidden="true" />
195
+ <ToastTitle />
196
+ </Toast>
197
+ )}
198
+ </ToastRegion>
199
+ </>
200
+ )
201
+ }
202
+
203
+ export { Example }
204
+ ```
205
+
206
+ Choose a type. Each toast shows its type's icon, and Loading turns into Photos uploaded when it finishes.
207
+
208
+ ## Position
209
+
210
+ Set the region's `side` to `top` or `bottom`, and `align` to `start`, `center` or `end`. Start and end follow the writing direction. A toast can be swiped toward the side and, unless it's centered, toward the end it's aligned to.
211
+
212
+ ```tsx title="position.tsx"
213
+ import * as React from "react"
214
+ import {
215
+ Select,
216
+ SelectContent,
217
+ SelectItem,
218
+ SelectItemIndicator,
219
+ SelectLabel,
220
+ SelectTrigger,
221
+ SelectValue,
222
+ } from "@ui-sdk/react/select"
223
+ import {
224
+ Toast,
225
+ ToastDescription,
226
+ ToastRegion,
227
+ ToastTitle,
228
+ createToaster,
229
+ } from "@ui-sdk/react/toast"
230
+ import { CheckIcon, ChevronDownIcon } from "lucide-react"
231
+
232
+ const sides = [
233
+ { value: "top", label: "Top" },
234
+ { value: "bottom", label: "Bottom" },
235
+ ]
236
+ const aligns = [
237
+ { value: "start", label: "Start" },
238
+ { value: "center", label: "Center" },
239
+ { value: "end", label: "End" },
240
+ ]
241
+ type Side = "top" | "bottom"
242
+ type Align = "start" | "center" | "end"
243
+
244
+ function Example() {
245
+ const [toast] = React.useState(() => createToaster())
246
+ const [side, setSide] = React.useState<Side>("bottom")
247
+ const [align, setAlign] = React.useState<Align>("end")
248
+ React.useEffect(
249
+ () => () => {
250
+ toast.dismiss()
251
+ },
252
+ [toast]
253
+ )
254
+
255
+ return (
256
+ <div data-slot="stack">
257
+ <div data-slot="controls">
258
+ <Select
259
+ items={sides}
260
+ value={side}
261
+ onValueChange={(next) => next && setSide(next as Side)}
262
+ >
263
+ <SelectLabel>Side</SelectLabel>
264
+ <SelectTrigger>
265
+ <SelectValue />
266
+ <ChevronDownIcon aria-hidden="true" />
267
+ </SelectTrigger>
268
+ <SelectContent>
269
+ {sides.map((option) => (
270
+ <SelectItem key={option.value} value={option.value}>
271
+ {option.label}
272
+ <SelectItemIndicator>
273
+ <CheckIcon aria-hidden="true" />
274
+ </SelectItemIndicator>
275
+ </SelectItem>
276
+ ))}
277
+ </SelectContent>
278
+ </Select>
279
+ <Select
280
+ items={aligns}
281
+ value={align}
282
+ onValueChange={(next) => next && setAlign(next as Align)}
283
+ >
284
+ <SelectLabel>Align</SelectLabel>
285
+ <SelectTrigger>
286
+ <SelectValue />
287
+ <ChevronDownIcon aria-hidden="true" />
288
+ </SelectTrigger>
289
+ <SelectContent>
290
+ {aligns.map((option) => (
291
+ <SelectItem key={option.value} value={option.value}>
292
+ {option.label}
293
+ <SelectItemIndicator>
294
+ <CheckIcon aria-hidden="true" />
295
+ </SelectItemIndicator>
296
+ </SelectItem>
297
+ ))}
298
+ </SelectContent>
299
+ </Select>
300
+ </div>
301
+ <button
302
+ onClick={() =>
303
+ toast("Event created", { description: "Friday, June 12 at 10:00 AM" })
304
+ }
305
+ >
306
+ Create Event
307
+ </button>
308
+ <ToastRegion toaster={toast} side={side} align={align}>
309
+ {() => (
310
+ <Toast>
311
+ <div data-slot="stack">
312
+ <ToastTitle />
313
+ <ToastDescription />
314
+ </div>
315
+ </Toast>
316
+ )}
317
+ </ToastRegion>
318
+ </div>
319
+ )
320
+ }
321
+
322
+ export { Example }
323
+ ```
324
+
325
+ Choose a side and an alignment, then press Create Event and look for the toast at that edge of the page.
326
+
327
+ ## Action and Cancel
328
+
329
+ Add an `action`, like Undo, and a `cancel` button that dismisses the toast. Their callbacks can update your app. `onDismiss` tells you why the toast is closing, and `details.cancel()` keeps it open; a canceled timeout starts the countdown again.
330
+
331
+ ```tsx title="action.tsx"
332
+ import * as React from "react"
333
+ import {
334
+ Toast,
335
+ ToastAction,
336
+ ToastClose,
337
+ ToastRegion,
338
+ ToastTitle,
339
+ createToaster,
340
+ } from "@ui-sdk/react/toast"
341
+ import { useLog } from "./log"
342
+
343
+ function Example() {
344
+ const log = useLog()
345
+ const [toast] = React.useState(() => createToaster())
346
+ React.useEffect(
347
+ () => () => {
348
+ toast.dismiss()
349
+ },
350
+ [toast]
351
+ )
352
+
353
+ return (
354
+ <>
355
+ <button
356
+ onClick={() =>
357
+ toast("Message archived", {
358
+ action: { label: "Undo", onAction: () => log("Message restored") },
359
+ cancel: { label: "Dismiss", onCancel: () => log("Kept archived") },
360
+ onDismiss: ({ reason }) => log(`Closed (${reason})`),
361
+ })
362
+ }
363
+ >
364
+ Archive Message
365
+ </button>
366
+ <ToastRegion toaster={toast}>
367
+ {(item) => (
368
+ <Toast>
369
+ <ToastTitle />
370
+ {item.cancel && (
371
+ <ToastClose action={item.cancel.onCancel}>
372
+ {item.cancel.label}
373
+ </ToastClose>
374
+ )}
375
+ <ToastAction data-variant="solid" />
376
+ </Toast>
377
+ )}
378
+ </ToastRegion>
379
+ </>
380
+ )
381
+ }
382
+
383
+ export { Example }
384
+ ```
385
+
386
+ Press Archive Message, then choose Undo or Dismiss, swipe the toast away, or let it time out. The console logs why it closed.
387
+
388
+ ## Link
389
+
390
+ Give an action an `href` to render it as a link. It keeps normal link behavior, including opening in a new tab.
391
+
392
+ ```tsx title="link.tsx"
393
+ import * as React from "react"
394
+ import {
395
+ Toast,
396
+ ToastAction,
397
+ ToastDescription,
398
+ ToastRegion,
399
+ ToastTitle,
400
+ createToaster,
401
+ } from "@ui-sdk/react/toast"
402
+
403
+ function Example() {
404
+ const [toast] = React.useState(() => createToaster())
405
+ React.useEffect(
406
+ () => () => {
407
+ toast.dismiss()
408
+ },
409
+ [toast]
410
+ )
411
+
412
+ return (
413
+ <>
414
+ <button
415
+ onClick={() =>
416
+ toast("Invoice sent", {
417
+ description: "Hollis Grant · $240.00",
418
+ action: { label: "View", href: "#" },
419
+ })
420
+ }
421
+ >
422
+ Send Invoice
423
+ </button>
424
+ <ToastRegion toaster={toast}>
425
+ {() => (
426
+ <Toast>
427
+ <div data-slot="stack">
428
+ <ToastTitle />
429
+ <ToastDescription />
430
+ </div>
431
+ <ToastAction data-variant="solid" />
432
+ </Toast>
433
+ )}
434
+ </ToastRegion>
435
+ </>
436
+ )
437
+ }
438
+
439
+ export { Example }
440
+ ```
441
+
442
+ Press Send Invoice, then follow View in the toast.
443
+
444
+ ## Promise
445
+
446
+ `toast.promise()` shows a loading toast, then updates the same toast when the promise resolves or rejects. Each message can be a string, options with a description or an action, or a function of the result. A toast dismissed while loading stays closed.
447
+
448
+ ```tsx title="promise.tsx"
449
+ import * as React from "react"
450
+ import {
451
+ Toast,
452
+ ToastAction,
453
+ ToastDescription,
454
+ ToastRegion,
455
+ ToastTitle,
456
+ createToaster,
457
+ } from "@ui-sdk/react/toast"
458
+
459
+ function Example() {
460
+ const [toast] = React.useState(() => createToaster())
461
+ const attempts = React.useRef(0)
462
+ React.useEffect(
463
+ () => () => {
464
+ toast.dismiss()
465
+ },
466
+ [toast]
467
+ )
468
+
469
+ function save() {
470
+ const succeeds = ++attempts.current % 2 === 1
471
+ return new Promise<void>((resolve, reject) =>
472
+ setTimeout(
473
+ () => (succeeds ? resolve() : reject(new Error("Network error"))),
474
+ 1500
475
+ )
476
+ )
477
+ }
478
+
479
+ function saveChanges() {
480
+ toast.promise(save, {
481
+ loading: "Saving changes…",
482
+ success: "Changes saved",
483
+ error: {
484
+ title: "Couldn't save changes",
485
+ description: "Check your connection and try again.",
486
+ action: { label: "Retry", onAction: saveChanges },
487
+ },
488
+ })
489
+ }
490
+
491
+ return (
492
+ <>
493
+ <button onClick={saveChanges}>Save Changes</button>
494
+ <ToastRegion toaster={toast}>
495
+ {() => (
496
+ <Toast>
497
+ <div data-slot="stack">
498
+ <ToastTitle />
499
+ <ToastDescription />
500
+ </div>
501
+ <ToastAction data-variant="solid" />
502
+ </Toast>
503
+ )}
504
+ </ToastRegion>
505
+ </>
506
+ )
507
+ }
508
+
509
+ export { Example }
510
+ ```
511
+
512
+ Press Save Changes to see Saving changes become Changes saved. Press it again for the error, then choose Retry.
513
+
514
+ ## Stacking
515
+
516
+ `maxVisible` limits how many toasts show at once, three by default; the rest wait in the queue, and their timers start when they show. The stack expands while the pointer or focus is in it. Set `expanded` to keep it open. [Styling](#styling) shows the CSS that lays out the stack.
517
+
518
+ ```tsx title="stacking.tsx"
519
+ import * as React from "react"
520
+ import { Checkbox, CheckboxIndicator } from "@ui-sdk/react/checkbox"
521
+ import {
522
+ Toast,
523
+ ToastDescription,
524
+ ToastRegion,
525
+ ToastTitle,
526
+ createToaster,
527
+ } from "@ui-sdk/react/toast"
528
+ import { CheckIcon } from "lucide-react"
529
+
530
+ const notifications = [
531
+ { title: "Event created", description: "Friday, June 12 at 10:00 AM" },
532
+ { title: "Message sent" },
533
+ {
534
+ title: "Hollis Grant joined",
535
+ description: "They can now edit the Launch plan and see its comments.",
536
+ },
537
+ { title: "Changes saved" },
538
+ { title: "Invoice sent", description: "Inès Moreau · $240.00" },
539
+ { title: "Photos uploaded", description: "12 photos added to Summer trip" },
540
+ ]
541
+
542
+ function Example() {
543
+ const [toast] = React.useState(() => createToaster())
544
+ const [expanded, setExpanded] = React.useState(false)
545
+ const count = React.useRef(0)
546
+ React.useEffect(
547
+ () => () => {
548
+ toast.dismiss()
549
+ },
550
+ [toast]
551
+ )
552
+
553
+ return (
554
+ <div data-slot="stack">
555
+ <div data-slot="controls">
556
+ <label data-slot="label">
557
+ <Checkbox
558
+ checked={expanded}
559
+ onCheckedChange={(next) => setExpanded(next === true)}
560
+ >
561
+ <CheckboxIndicator>
562
+ <CheckIcon aria-hidden="true" />
563
+ </CheckboxIndicator>
564
+ </Checkbox>
565
+ Always expanded
566
+ </label>
567
+ </div>
568
+ <button
569
+ onClick={() =>
570
+ toast(notifications[count.current++ % notifications.length])
571
+ }
572
+ >
573
+ Add Toast
574
+ </button>
575
+ <ToastRegion toaster={toast} maxVisible={4} expanded={expanded}>
576
+ {() => (
577
+ <Toast>
578
+ <div data-slot="stack">
579
+ <ToastTitle />
580
+ <ToastDescription />
581
+ </div>
582
+ </Toast>
583
+ )}
584
+ </ToastRegion>
585
+ </div>
586
+ )
587
+ }
588
+
589
+ export { Example }
590
+ ```
591
+
592
+ Press Add Toast a few times, then hover the stack to expand it. Past four toasts, the rest wait their turn. Check Always expanded to keep it open.
593
+
594
+ ## Duration
595
+
596
+ Set `duration` in milliseconds, on the toaster or on one toast, or `Infinity` for a toast that stays until it's dismissed. The default is five seconds. Timers pause while the pointer or focus is in the region, and while the page is hidden or its window is inactive.
597
+
598
+ ```tsx title="duration.tsx"
599
+ import * as React from "react"
600
+ import {
601
+ Toast,
602
+ ToastClose,
603
+ ToastRegion,
604
+ ToastTitle,
605
+ createToaster,
606
+ } from "@ui-sdk/react/toast"
607
+
608
+ function Example() {
609
+ const [toast] = React.useState(() => createToaster())
610
+ React.useEffect(
611
+ () => () => {
612
+ toast.dismiss()
613
+ },
614
+ [toast]
615
+ )
616
+
617
+ return (
618
+ <>
619
+ <div data-slot="row">
620
+ <button onClick={() => toast("Link copied", { duration: 1000 })}>
621
+ 1 Second
622
+ </button>
623
+ <button onClick={() => toast("Changes saved")}>5 Seconds</button>
624
+ <button
625
+ onClick={() =>
626
+ toast("Your session expires in 5 minutes", {
627
+ duration: Infinity,
628
+ dismissible: false,
629
+ cancel: { label: "Got It" },
630
+ })
631
+ }
632
+ >
633
+ Until Dismissed
634
+ </button>
635
+ </div>
636
+ <ToastRegion toaster={toast}>
637
+ {(item) => (
638
+ <Toast>
639
+ <ToastTitle />
640
+ {item.cancel && (
641
+ <ToastClose data-variant="solid" action={item.cancel.onCancel}>
642
+ {item.cancel.label}
643
+ </ToastClose>
644
+ )}
645
+ </Toast>
646
+ )}
647
+ </ToastRegion>
648
+ </>
649
+ )
650
+ }
651
+
652
+ export { Example }
653
+ ```
654
+
655
+ Press each button. Hover a toast to pause its timer. The session toast stays until you press Got It.
656
+
657
+ ## Update and Dismiss
658
+
659
+ Keep the id `toast()` returns to update or dismiss that toast, or pass your own `id`. `toast.update()` returns whether the toast was still there, and `toast.dismiss()` without an id closes every toast.
660
+
661
+ ```tsx title="update.tsx"
662
+ import * as React from "react"
663
+ import {
664
+ Toast,
665
+ ToastRegion,
666
+ ToastTitle,
667
+ createToaster,
668
+ } from "@ui-sdk/react/toast"
669
+ import { CircleCheckIcon, LoaderCircleIcon } from "lucide-react"
670
+ import { useLog } from "./log"
671
+
672
+ function Example() {
673
+ const log = useLog()
674
+ const [toast] = React.useState(() => createToaster())
675
+ const timer = React.useRef<ReturnType<typeof setInterval> | undefined>(
676
+ undefined
677
+ )
678
+ React.useEffect(
679
+ () => () => {
680
+ clearInterval(timer.current)
681
+ toast.dismiss()
682
+ },
683
+ [toast]
684
+ )
685
+
686
+ function upload() {
687
+ clearInterval(timer.current)
688
+ const id = toast.loading("Uploading report.pdf… 0%", { id: "upload" })
689
+ let progress = 0
690
+ timer.current = setInterval(() => {
691
+ progress += 20
692
+ const done = progress >= 100
693
+ const open = toast.update(
694
+ id,
695
+ done
696
+ ? { title: "Report uploaded", type: "success" }
697
+ : { title: `Uploading report.pdf… ${progress}%` }
698
+ )
699
+ if (!open) {
700
+ log("Upload canceled")
701
+ }
702
+ if (done || !open) {
703
+ clearInterval(timer.current)
704
+ }
705
+ }, 700)
706
+ }
707
+
708
+ return (
709
+ <>
710
+ <div data-slot="row">
711
+ <button onClick={upload}>Upload Report</button>
712
+ <button onClick={() => toast.dismiss("upload")}>Cancel Upload</button>
713
+ <button onClick={() => toast.dismiss()}>Dismiss All</button>
714
+ </div>
715
+ <ToastRegion toaster={toast}>
716
+ {() => (
717
+ <Toast>
718
+ <CircleCheckIcon data-show="success" aria-hidden="true" />
719
+ <LoaderCircleIcon data-show="loading" aria-hidden="true" />
720
+ <ToastTitle />
721
+ </Toast>
722
+ )}
723
+ </ToastRegion>
724
+ </>
725
+ )
726
+ }
727
+
728
+ export { Example }
729
+ ```
730
+
731
+ Press Upload Report and watch one toast count up to Report uploaded. Cancel Upload closes it early, and Dismiss All clears every toast.
732
+
733
+ ## Async Action
734
+
735
+ Return a promise from an action to keep the toast open while it runs. The action's state has `pending` while it runs and `error` if it fails, so the button can say Restoring… or offer a retry, and presses are ignored until it settles. See [Button](./button.md#changes-after-an-await) for changes made after an `await`.
736
+
737
+ ```tsx title="async-action.tsx"
738
+ import * as React from "react"
739
+ import {
740
+ Toast,
741
+ ToastAction,
742
+ ToastDescription,
743
+ ToastRegion,
744
+ ToastTitle,
745
+ createToaster,
746
+ } from "@ui-sdk/react/toast"
747
+ import { useLog } from "./log"
748
+
749
+ function restore() {
750
+ return new Promise((resolve) => setTimeout(resolve, 1500))
751
+ }
752
+
753
+ function Example() {
754
+ const log = useLog()
755
+ const [toast] = React.useState(() => createToaster({ duration: 6000 }))
756
+ React.useEffect(
757
+ () => () => {
758
+ toast.dismiss()
759
+ },
760
+ [toast]
761
+ )
762
+
763
+ return (
764
+ <>
765
+ <button
766
+ onClick={() =>
767
+ toast("File deleted", {
768
+ description: "Q3 report.pdf",
769
+ action: {
770
+ label: "Undo",
771
+ onAction: async () => {
772
+ await restore()
773
+ log("Q3 report.pdf restored")
774
+ },
775
+ },
776
+ })
777
+ }
778
+ >
779
+ Delete File
780
+ </button>
781
+ <ToastRegion toaster={toast}>
782
+ {() => (
783
+ <Toast>
784
+ <div data-slot="stack">
785
+ <ToastTitle />
786
+ <ToastDescription />
787
+ </div>
788
+ <ToastAction data-variant="solid">
789
+ {(state) =>
790
+ state.pending ? "Restoring…" : state.error ? "Retry" : "Undo"
791
+ }
792
+ </ToastAction>
793
+ </Toast>
794
+ )}
795
+ </ToastRegion>
796
+ </>
797
+ )
798
+ }
799
+
800
+ export { Example }
801
+ ```
802
+
803
+ Press Delete File, then Undo. The button shows Restoring… until the restore finishes, then the toast closes.
804
+
805
+ ## Keyboard Shortcut
806
+
807
+ F6 moves focus to the newest toast and back. Set the region's `shortcut` to use another key, or `null` to turn it off. The region advertises it through `aria-keyshortcuts`.
808
+
809
+ ```tsx title="shortcut.tsx"
810
+ import * as React from "react"
811
+ import {
812
+ Toast,
813
+ ToastAction,
814
+ ToastRegion,
815
+ ToastTitle,
816
+ createToaster,
817
+ } from "@ui-sdk/react/toast"
818
+ import { useLog } from "./log"
819
+
820
+ function Example() {
821
+ const log = useLog()
822
+ const [toast] = React.useState(() => createToaster({ duration: 10000 }))
823
+ React.useEffect(
824
+ () => () => {
825
+ toast.dismiss()
826
+ },
827
+ [toast]
828
+ )
829
+
830
+ return (
831
+ <>
832
+ <button
833
+ onClick={() =>
834
+ toast("Message sent", {
835
+ action: { label: "Undo", onAction: () => log("Message unsent") },
836
+ })
837
+ }
838
+ >
839
+ Send Message
840
+ </button>
841
+ <ToastRegion toaster={toast} shortcut="alt+t" aria-label="Alerts">
842
+ {() => (
843
+ <Toast>
844
+ <ToastTitle />
845
+ <ToastAction data-variant="solid" />
846
+ </Toast>
847
+ )}
848
+ </ToastRegion>
849
+ </>
850
+ )
851
+ }
852
+
853
+ export { Example }
854
+ ```
855
+
856
+ Press Send Message, then Alt+T (Option+T on a Mac) to focus the toast. Press Escape to dismiss it, or Alt+T again to go back.
857
+
858
+ ## Overlay Host and Confirm
859
+
860
+ Instead of placing a region, render `OverlayHost` once near your app's root. It renders the toaster's region, with the `toast.children` you give it, and `confirm()` questions as alert dialogs. A region you place yourself takes over from the host for its toaster.
861
+
862
+ To style the question, render an `AlertDialog` with the confirm controller and read the question from its content's `payload`. [Alert Dialog](./alert-dialog.md) covers `createConfirm()` in more detail.
863
+
864
+ ```tsx title="overlay-host.tsx"
865
+ import * as React from "react"
866
+ import {
867
+ AlertDialog,
868
+ AlertDialogBackdrop,
869
+ AlertDialogClose,
870
+ AlertDialogContent,
871
+ AlertDialogDescription,
872
+ AlertDialogTitle,
873
+ createConfirm,
874
+ type ConfirmOptions,
875
+ } from "@ui-sdk/react/alert-dialog"
876
+ import { OverlayHost, createOverlays } from "@ui-sdk/react/overlays"
877
+ import { Toast, ToastTitle, createToaster } from "@ui-sdk/react/toast"
878
+ import { CircleCheckIcon } from "lucide-react"
879
+
880
+ function Example() {
881
+ const [overlays] = React.useState(() => createOverlays())
882
+ const [toast] = React.useState(() => createToaster({ overlays }))
883
+ const [confirm] = React.useState(() => createConfirm({ overlays }))
884
+ const [projects, setProjects] = React.useState(["Acme", "Globex", "Initech"])
885
+ const reset = React.useRef<HTMLButtonElement>(null)
886
+ React.useEffect(
887
+ () => () => {
888
+ toast.dismiss()
889
+ },
890
+ [toast]
891
+ )
892
+
893
+ async function remove(name: string, button: HTMLButtonElement) {
894
+ const confirmed = await confirm({
895
+ title: `Delete ${name}?`,
896
+ description:
897
+ "The project and its files will be deleted for everyone on your team.",
898
+ confirmLabel: "Delete",
899
+ cancelLabel: "Cancel",
900
+ })
901
+ if (!confirmed) {
902
+ return
903
+ }
904
+ const row = button.closest("li")
905
+ const next = (
906
+ row?.nextElementSibling ?? row?.previousElementSibling
907
+ )?.querySelector("button")
908
+ ;(next ?? reset.current)?.focus()
909
+ setProjects((list) => list.filter((project) => project !== name))
910
+ toast.success(`${name} deleted`, { priority: "assertive" })
911
+ }
912
+
913
+ return (
914
+ <div data-slot="stack">
915
+ <div data-slot="controls">
916
+ <button
917
+ ref={reset}
918
+ type="button"
919
+ onClick={() => setProjects(["Acme", "Globex", "Initech"])}
920
+ >
921
+ Reset Projects
922
+ </button>
923
+ </div>
924
+ <ul data-slot="list" aria-label="Projects">
925
+ {projects.map((name) => (
926
+ <li data-slot="row" key={name}>
927
+ {name}
928
+ <button
929
+ type="button"
930
+ data-size="sm"
931
+ aria-label={`Delete ${name}`}
932
+ onClick={(event) => remove(name, event.currentTarget)}
933
+ >
934
+ Delete
935
+ </button>
936
+ </li>
937
+ ))}
938
+ {projects.length === 0 && (
939
+ <li data-slot="row">All projects deleted.</li>
940
+ )}
941
+ </ul>
942
+ <OverlayHost
943
+ overlays={overlays}
944
+ toast={{
945
+ children: () => (
946
+ <Toast>
947
+ <CircleCheckIcon data-show="success" aria-hidden="true" />
948
+ <ToastTitle />
949
+ </Toast>
950
+ ),
951
+ }}
952
+ />
953
+ <AlertDialog controller={confirm.controller}>
954
+ <AlertDialogBackdrop />
955
+ <AlertDialogContent<ConfirmOptions>>
956
+ {(state) => (
957
+ <>
958
+ <div data-slot="stack">
959
+ <AlertDialogTitle>{state.payload?.title}</AlertDialogTitle>
960
+ <AlertDialogDescription>
961
+ {state.payload?.description}
962
+ </AlertDialogDescription>
963
+ </div>
964
+ <div data-slot="row">
965
+ <AlertDialogClose>
966
+ {state.payload?.cancelLabel}
967
+ </AlertDialogClose>
968
+ <AlertDialogClose data-variant="solid" value="confirm">
969
+ {state.payload?.confirmLabel}
970
+ </AlertDialogClose>
971
+ </div>
972
+ </>
973
+ )}
974
+ </AlertDialogContent>
975
+ </AlertDialog>
976
+ </div>
977
+ )
978
+ }
979
+
980
+ export { Example }
981
+ ```
982
+
983
+ Press Delete on a project and confirm. The row goes away and a toast says so. Reset Projects brings them back.
984
+
985
+ ## Custom Rendering
986
+
987
+ Pass anything else your toast needs in `data`, and read it from the toast the region's children function receives. Every part also takes `render`; this example renders each toast's `li` itself, making the ones hidden behind a collapsed stack `inert`.
988
+
989
+ ```tsx title="custom.tsx"
990
+ import * as React from "react"
991
+ import {
992
+ Toast,
993
+ ToastAction,
994
+ ToastClose,
995
+ ToastDescription,
996
+ ToastRegion,
997
+ ToastTitle,
998
+ createToaster,
999
+ } from "@ui-sdk/react/toast"
1000
+ import { XIcon } from "lucide-react"
1001
+ import { useLog } from "./log"
1002
+
1003
+ function Example() {
1004
+ const log = useLog()
1005
+ const [toast] = React.useState(() => createToaster())
1006
+ React.useEffect(
1007
+ () => () => {
1008
+ toast.dismiss()
1009
+ },
1010
+ [toast]
1011
+ )
1012
+
1013
+ return (
1014
+ <>
1015
+ <button
1016
+ onClick={() =>
1017
+ toast({
1018
+ title: "Hollis Grant",
1019
+ description: "Can you review the draft?",
1020
+ data: { initials: "HG" },
1021
+ action: {
1022
+ label: "Reply",
1023
+ onAction: () => log("Replying to Hollis Grant"),
1024
+ },
1025
+ })
1026
+ }
1027
+ >
1028
+ New Message
1029
+ </button>
1030
+ <ToastRegion toaster={toast} aria-label="Messages">
1031
+ {(item) => (
1032
+ <Toast
1033
+ render={(props, state) => (
1034
+ <li
1035
+ {...props}
1036
+ inert={
1037
+ state.leaving ||
1038
+ (!state.expanded && state.index > 0) ||
1039
+ undefined
1040
+ }
1041
+ />
1042
+ )}
1043
+ >
1044
+ <span data-slot="media">
1045
+ {(item.data as { initials: string }).initials}
1046
+ </span>
1047
+ <div data-slot="stack">
1048
+ <ToastTitle />
1049
+ <ToastDescription />
1050
+ </div>
1051
+ <ToastAction data-variant="solid" />
1052
+ <ToastClose aria-label="Close">
1053
+ <XIcon aria-hidden="true" />
1054
+ </ToastClose>
1055
+ </Toast>
1056
+ )}
1057
+ </ToastRegion>
1058
+ </>
1059
+ )
1060
+ }
1061
+
1062
+ export { Example }
1063
+ ```
1064
+
1065
+ Press New Message to show a toast with Hollis Grant's initials. Choose Reply or close it.
1066
+
1067
+ ## Styling
1068
+
1069
+ Toasts are unstyled, and the region and its toasts carry what you need to animate a stack:
1070
+
1071
+ - **The stack.** The region sets `--toast-front-height`, the newest toast's height, and `--toast-stack-height`, all of them added up. Each toast gets `--toast-index`, 0 for the newest, `--toast-offset`, the height of the toasts in front of it, and `--toast-height`, its own height. Position the toasts absolutely in the region and set `height: var(--toast-height)` so changes in height animate.
1072
+ - **Collapsed and expanded.** `data-front` marks the newest toast. While the stack is collapsed, clip the toasts behind it to `--toast-front-height` and peek them out by their index. While it's expanded, the region and its toasts get `data-expanded`; move each toast by its `--toast-offset`.
1073
+ - **Swiping.** While a toast is swiped it gets `data-swiping`, and `--toast-swipe-x` and `--toast-swipe-y` hold how far it moved. Translate it by them and turn off transitions. When a swipe dismisses it, `data-swipe-direction` says which way, so it can leave in that direction. A mouse drag that starts on selectable text selects it, so set `user-select: none` on toasts to swipe them from anywhere.
1074
+ - **Entering and leaving.** A toast has `data-starting-style` for its first frame and `data-ending-style` while it leaves, and stays mounted until its transitions finish.
1075
+
1076
+ A stack at the bottom of the page, in plain CSS:
1077
+
1078
+ ```css
1079
+ [data-slot="toast-region"] {
1080
+ width: 356px;
1081
+ height: var(--toast-front-height);
1082
+ }
1083
+
1084
+ [data-slot="toast-region"][data-expanded] {
1085
+ height: calc(var(--toast-stack-height) + (var(--toast-count) - 1) * 12px);
1086
+ }
1087
+
1088
+ [data-slot="toast"] {
1089
+ --y: calc(var(--toast-index) * -12px);
1090
+ position: absolute;
1091
+ inset-inline: 0;
1092
+ bottom: 0;
1093
+ z-index: calc(100 - var(--toast-index));
1094
+ overflow: hidden;
1095
+ height: var(--toast-height);
1096
+ translate: var(--toast-swipe-x, 0px)
1097
+ calc(var(--y) + var(--toast-swipe-y, 0px));
1098
+ scale: calc(1 - var(--toast-index) * 0.05);
1099
+ transition-property: translate, scale, opacity, height;
1100
+ transition-duration: 400ms;
1101
+ user-select: none;
1102
+ }
1103
+
1104
+ [data-slot="toast"]:not([data-front], [data-expanded]) {
1105
+ height: var(--toast-front-height);
1106
+ }
1107
+
1108
+ [data-slot="toast"][data-expanded] {
1109
+ --y: calc(-1 * (var(--toast-offset) + var(--toast-index) * 12px));
1110
+ scale: 1;
1111
+ }
1112
+
1113
+ [data-slot="toast"][data-starting-style] {
1114
+ transition: none;
1115
+ }
1116
+
1117
+ [data-slot="toast"][data-starting-style][data-front] {
1118
+ --y: 100%;
1119
+ }
1120
+
1121
+ [data-slot="toast"]:is(
1122
+ [data-starting-style]:not([data-front]),
1123
+ [data-ending-style]
1124
+ ) {
1125
+ opacity: 0;
1126
+ }
1127
+
1128
+ [data-slot="toast"][data-swiping] {
1129
+ transition: none;
1130
+ }
1131
+
1132
+ @media (prefers-reduced-motion: reduce) {
1133
+ [data-slot="toast"] {
1134
+ transition-property: opacity;
1135
+ }
1136
+ }
1137
+ ```
1138
+
1139
+ Each part has a `data-slot`, such as `toast`, and its state attributes are listed under [Data Attributes](#data-attributes).
1140
+
1141
+ ### ToastRegion
1142
+ - `--toast-front-height`: The newest toast's height. Clip the toasts behind it to it in a collapsed stack.
1143
+ - `--toast-stack-height`: The toasts' heights added up, for the height of an expanded stack.
1144
+ - `--toast-count`: How many toasts show.
1145
+ - `--keyboard-inset`: How many pixels of the page the on-screen keyboard covers, 0 without one.
1146
+ - `--visual-viewport-width`: The width of the part of the page you can see.
1147
+ - `--visual-viewport-height`: The height of the part of the page you can see, above the keyboard.
1148
+ - `--visual-viewport-top`: 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.
1149
+
1150
+ ### Toast
1151
+ - `--toast-index`: The toast's place in the stack, 0 for the newest. A closing toast keeps its place.
1152
+ - `--toast-offset`: The height of the toasts in front of this one, in pixels. Add gaps with `--toast-index` to place an expanded stack.
1153
+ - `--toast-height`: The toast's own height with its content laid out freely, whatever height it's given. Set it as the height to animate the stack.
1154
+ - `--toast-swipe-x`: How far a swipe moved the toast sideways, in pixels. Translate the toast by it.
1155
+ - `--toast-swipe-y`: How far a swipe moved the toast up or down, in pixels.
1156
+
1157
+ ## Data Attributes
1158
+
1159
+ `ToastTitle` and `ToastDescription` have no state attributes. `ToastClose` gets `data-pending`, like `ToastAction`, while a Promise from its `action` runs.
1160
+
1161
+ ### ToastRegion
1162
+ `[data-slot="toast-region"]`
1163
+
1164
+ - `data-side` (`top`, `bottom`): The edge of the viewport the region sits on.
1165
+ - `data-align` (`start`, `center`, `end`): Where along that edge.
1166
+ - `data-expanded`: Present while the stack is expanded: hovered, focused, or always with `expanded`.
1167
+ - `data-keyboard`: Present on the region while the on-screen keyboard covers part of the page.
1168
+
1169
+ ### Toast
1170
+ `[data-slot="toast"]`
1171
+
1172
+ - `data-type` (`default`, `success`, `error`, `info`, `warning`, `loading`): What the toast is about.
1173
+ - `data-front`: Present on the newest toast, in front of a collapsed stack. A closing toast keeps it while it animates out.
1174
+ - `data-expanded`: Present while the stack is expanded: hovered, focused, or always with `expanded`.
1175
+ - `data-starting-style`: Present for a toast's first frame. Style the state you animate from.
1176
+ - `data-ending-style`: Present while a toast animates out. Style the state you animate to.
1177
+
1178
+ ### ToastAction
1179
+ `[data-slot="toast-action"]`
1180
+
1181
+ - `data-pending`: Present on an action or close button while its handler's Promise runs.
1182
+
1183
+ ## API Reference
1184
+
1185
+ ### Toast
1186
+
1187
+ - `id?` (`string`): Reuses a toast: a toast with this id that's still showing is updated instead of added again.
1188
+ - `title?` (`Content`): The main message.
1189
+ - `description?` (`Content`): More detail, under the title.
1190
+ - `type?` (`ToastType`): What it's about, for styling through `data-type`, and for how it's announced.
1191
+ - `duration?` (`number`): How long it shows, in ms, while nothing pauses it. `Infinity` keeps it until dismissed.
1192
+ - `priority?` (`"assertive" | "polite"`): How screen readers announce it: `"polite"` waits for a pause, `"assertive"` interrupts.
1193
+ - `action?` (`ToastAction<Content>`): A button that does something, like Undo.
1194
+ - `cancel?` (`ToastCancel<Content>`): A button that dismisses it, like Cancel.
1195
+ - `dismissible?` (`boolean`): Whether the user can dismiss it: swiping, Escape and Close parts. Timers and `dismiss()` still close it.
1196
+ - `data?` (`unknown`): Anything else, for your own rendering, like an avatar.
1197
+ - `onDismiss?` (`((details: ToastDismissDetails) => void)`): Runs when it closes, with why. Calling `details.cancel()` keeps it.
1198
+
1199
+ `toast(message, options)` and `toast(options)` return the toast's id. `toast.success`, `.info`, `.warning`, `.error` and `.loading` set the type. `toast.promise(promise, { loading, success, error })` returns the id too, and `toast.update(id, options)` returns whether the toast was still there. `toast.dismiss(id)` closes one, and `toast.dismiss()` every one.
1200
+
1201
+ ### Actions
1202
+
1203
+ - `label` (`Content`): The button's text.
1204
+ - `onAction?` (`((event: Event | undefined, details: ToastActionDetails) => unknown)`): Runs when it's pressed. A returned Promise marks the action pending, then dismisses the toast once it resolves. A rejection keeps the toast. `details.source` says how it was pressed, for changes made after an `await`.
1205
+ - `href?` (`string`): Makes the action a link.
1206
+ - `dismiss?` (`boolean`): Whether pressing it dismisses the toast.
1207
+
1208
+ - `label` (`Content`): The button's text.
1209
+ - `onCancel?` (`((event: Event | undefined, details: ToastActionDetails) => void)`): Runs when it's pressed, before the toast closes. `details.source` says how it was pressed.
1210
+
1211
+ ### ToastRegion
1212
+
1213
+ - `toaster?` (`Toaster<any>`): The queue it shows. A second toaster, from `createToaster()`, gives a second region its own toasts.
1214
+ - `side?` (`"bottom" | "top"`): The edge of the viewport it sits on. Swiping toward it dismisses a toast.
1215
+ - `align?` (`"center" | "end" | "start"`): Where along that edge. `"start"` and `"end"` follow the writing direction, and swiping toward them dismisses too.
1216
+ - `maxVisible?` (`number`): How many toasts show at once. Older ones wait, with their timers stopped, until there's room.
1217
+ - `expanded?` (`boolean`): Whether the stack is always expanded. Otherwise it expands while the pointer or focus is in it.
1218
+ - `shortcut?` (`string | null`): The key that moves focus into the region and back, like `"F6"` or `"alt+t"`. `null` turns it off.
1219
+ - `pendingAnnouncement?` (`string`): What screen readers hear once a toast's action or close button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
1220
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
1221
+
1222
+ ### ToastAction
1223
+
1224
+ - `onAction?` (`((event: Event | undefined, details: ToastActionDetails) => unknown)`): Runs when it's pressed, instead of the toast's own `action.onAction`. A returned Promise marks it pending. `details.source` says how it was pressed, for changes made after an `await`.
1225
+ - `dismiss?` (`boolean`): Whether pressing it dismisses the toast.
1226
+
1227
+ ### ToastClose
1228
+
1229
+ - `action?` (`((event: Event | undefined, details: ToastActionDetails) => unknown)`): Runs before the toast closes. A returned Promise keeps it open and the button pending until it settles. `details.source` says how it was pressed, for changes made after an `await`.
1230
+
1231
+ ### Confirm
1232
+
1233
+ - `title` (`string`): The question, like "Delete this project?".
1234
+ - `description?` (`string`): What agreeing does.
1235
+ - `confirmLabel?` (`string`): The agreeing button's text.
1236
+ - `cancelLabel?` (`string`): The other button's text.
1237
+
1238
+ ### State
1239
+
1240
+ `ToastRegion`:
1241
+
1242
+ - `side` (`"top" | "bottom"`): The edge it sits on.
1243
+ - `align` (`"start" | "center" | "end"`): Where along that edge.
1244
+ - `expanded` (`boolean`): Whether the stack is expanded.
1245
+ - `toasts` (`readonly ToastItem<unknown>[]`): The toasts to render, newest first, including ones animating out.
1246
+
1247
+ `Toast`:
1248
+
1249
+ - `type` (`ToastType`): What it's about.
1250
+ - `index` (`number`): Its place in the stack, 0 for the newest.
1251
+ - `front` (`boolean`): Whether it's the newest, on top of a collapsed stack. A closing toast keeps it.
1252
+ - `expanded` (`boolean`): Whether the stack is expanded.
1253
+ - `leaving` (`boolean`): Whether it's animating out.
1254
+
1255
+ `ToastAction` and `ToastClose`:
1256
+
1257
+ - `pending` (`boolean`): Whether a Promise its handler returned is still running.
1258
+ - `error` (`unknown`): Why its handler failed, until it's pressed again.
1259
+
1260
+ ### Reasons
1261
+
1262
+ - `timeout` (`none`): A duration ran out, like a toast's, or a rotating carousel's interval.
1263
+ - `close-press` (`MouseEvent | KeyboardEvent | SubmitEvent`): A Close part was pressed, or a form with `method="dialog"` was submitted.
1264
+ - `swipe` (`TouchEvent | PointerEvent`): A swipe gesture: a finger, mouse or pen dragged it.
1265
+ - `escape-key` (`KeyboardEvent`): Escape, or a submenu's close key (ArrowLeft in left-to-right layouts).
1266
+ - `action` (`MouseEvent | KeyboardEvent`): A Close part's `action` finished.
1267
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
1268
+
1269
+ ## Accessibility
1270
+
1271
+ Toasts are announced through shared [live regions](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Live_regions), once each, without making the visible region live: politely by default and assertively for errors. While it has toasts, the region is a landmark named Notifications; set `aria-label` to name it in the page's language. Keep messages short, and use `duration: Infinity` for one that has to stay available.
1272
+
1273
+ ### Keyboard
1274
+
1275
+ F6, or the region's `shortcut`, moves focus to the newest toast, and pressing it again returns focus to where it came from. Tab moves through the toasts and their buttons, and Tab past the last one goes back too. Escape dismisses the focused toast; focus then moves to the next toast, or back where it came from after the last one. The region stays reachable while a modal dialog is open, and its timers pause while focus is inside.
1276
+
1277
+ ### Tested Behavior
1278
+
1279
+ Every line below is an automated test.
1280
+
1281
+ **Showing**
1282
+
1283
+ - A toast shows its title, description and action in the Notifications region.
1284
+ - Newer toasts come first.
1285
+ - The newest toast is marked as the front one.
1286
+ - Each toast says what it's about through data-type.
1287
+ - The region sits on its side and alignment.
1288
+ - It shows above everything, in the top layer.
1289
+ - A promise toast loads, then shows the result.
1290
+ - A toast with an id that's showing is updated in place.
1291
+
1292
+ **Closing**
1293
+
1294
+ - A toast closes after its duration.
1295
+ - Infinity keeps it until it's dismissed.
1296
+ - The Dismiss button closes it.
1297
+ - The action runs and closes it.
1298
+ - Escape on a focused toast closes it.
1299
+ - A closing toast stays while it animates out.
1300
+ - A toast that can't be dismissed ignores Escape.
1301
+
1302
+ **Timers**
1303
+
1304
+ - Hovering the region pauses its timers, and leaving resumes them.
1305
+ - Focus in the region pauses its timers.
1306
+
1307
+ **Keyboard**
1308
+
1309
+ - F6 moves focus to the newest toast, and again back where it was.
1310
+ - The shortcut is configurable.
1311
+ - The region names its shortcut.
1312
+ - When the focused toast closes, focus goes to the next one, then back where it was.
1313
+
1314
+ **Stacking**
1315
+
1316
+ - Only maxVisible toasts show; the rest wait for room.
1317
+ - The stack expands while hovered or focused.
1318
+ - Expanded keeps the stack expanded.
1319
+ - Toasts measure their heights and offsets for the stack.
1320
+ - A toast measures its content's height, whatever height it's given.
1321
+ - A toast given its measured height animates to a new one when its content changes.
1322
+ - A toast measures again when a child, its class or its padding changes.
1323
+ - A toast closing from the front stays in front while it animates out.
1324
+
1325
+ **Swiping**
1326
+
1327
+ - Swiping toward the region's side dismisses.
1328
+ - Swiping toward the aligned edge dismisses too.
1329
+ - A short swipe snaps back.
1330
+ - Swiping away from the side never dismisses.
1331
+ - A toast that can't be dismissed doesn't swipe.
1332
+
1333
+ **Over a modal dialog**
1334
+
1335
+ - A toast shown from a modal dialog is visible, usable and above it.
1336
+
1337
+ **Updates after closing**
1338
+
1339
+ - A promise toast dismissed while loading stays closed.
1340
+ - An update after a toast closed never brings it back.
1341
+
1342
+ **More pausing and keys**
1343
+
1344
+ - The window losing focus pauses timers, and getting it back resumes them.
1345
+ - F6 does nothing while there are no toasts, so the browser keeps it.
1346
+ - A toast on the top edge swipes up, and a swipe down snaps back.
1347
+
1348
+ Runs in React, Vue and web components.
1349
+
1350
+ Also tested:
1351
+
1352
+ **Enhancements**
1353
+
1354
+ - Update() says whether the toast was still there.
1355
+ - A toast beyond maxVisible never expires unseen.
1356
+ - There's no empty Notifications landmark.
1357
+ - Focus never falls to the page when a toast focused by the mouse closes.
1358
+ - A pending action shows it and holds the toast open until it settles.
1359
+
1360
+ **Pausing**
1361
+
1362
+ - A hidden page pauses timers.
1363
+ - Pause reasons hold separately: getting the window back doesn't resume while focus is in the region.
1364
+
1365
+ **Announcements**
1366
+
1367
+ - The live regions exist before the first toast.
1368
+ - A toast is announced politely, with its title and description.
1369
+ - An error is announced assertively.
1370
+ - Toasts that arrive together are announced together.
1371
+ - The visible region isn't a live region, so nothing is read twice.
1372
+
1373
+ **The shortcut**
1374
+
1375
+ - F6 works from a text field and types nothing.
1376
+ - A shortcut of null turns it off.
1377
+ - Tab past the last toast goes back where F6 came from.
1378
+
1379
+ **Over modal dialogs**
1380
+
1381
+ - F6 reaches the toasts from inside a modal dialog, and goes back into it.
1382
+ - Escape on a toast closes the toast, not the dialog beneath.
1383
+ - A dialog opened after a toast doesn't cover it, and pressing it doesn't close the dialog.
1384
+
1385
+ **Swiping**
1386
+
1387
+ - Dragging across a toast's text with a mouse selects it instead of dismissing.
1388
+
1389
+ Runs in React, Vue and web components.
1390
+
1391
+ Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
1392
+
1393
+ - Announcements while reaching a toast with F6 and leaving it.
1394
+
1395
+ Runs in React, Vue and web components.