@react-spectrum/s2 3.0.0-nightly-c81a23ccd-250409 → 3.0.0-nightly-9723225d6-250412

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 (201) hide show
  1. package/dist/ActionButton.cjs +1 -0
  2. package/dist/ActionButton.cjs.map +1 -1
  3. package/dist/ActionButton.css.map +1 -1
  4. package/dist/ActionButton.mjs +1 -0
  5. package/dist/ActionButton.mjs.map +1 -1
  6. package/dist/Card.cjs +1 -1
  7. package/dist/Card.cjs.map +1 -1
  8. package/dist/Card.css.map +1 -1
  9. package/dist/Card.mjs +1 -1
  10. package/dist/Card.mjs.map +1 -1
  11. package/dist/ColorSwatchPicker.cjs +2 -2
  12. package/dist/ColorSwatchPicker.cjs.map +1 -1
  13. package/dist/ColorSwatchPicker.css.map +1 -1
  14. package/dist/ColorSwatchPicker.mjs +2 -2
  15. package/dist/ColorSwatchPicker.mjs.map +1 -1
  16. package/dist/Content.cjs +6 -6
  17. package/dist/Content.cjs.map +1 -1
  18. package/dist/Content.mjs +6 -6
  19. package/dist/Content.mjs.map +1 -1
  20. package/dist/Disclosure.cjs +24 -21
  21. package/dist/Disclosure.cjs.map +1 -1
  22. package/dist/Disclosure.css +12 -20
  23. package/dist/Disclosure.css.map +1 -1
  24. package/dist/Disclosure.mjs +24 -21
  25. package/dist/Disclosure.mjs.map +1 -1
  26. package/dist/NotificationBadge.cjs +15 -9
  27. package/dist/NotificationBadge.cjs.map +1 -1
  28. package/dist/NotificationBadge.css +22 -10
  29. package/dist/NotificationBadge.css.map +1 -1
  30. package/dist/NotificationBadge.mjs +15 -9
  31. package/dist/NotificationBadge.mjs.map +1 -1
  32. package/dist/TableView.cjs +28 -16
  33. package/dist/TableView.cjs.map +1 -1
  34. package/dist/TableView.css.map +1 -1
  35. package/dist/TableView.mjs +29 -17
  36. package/dist/TableView.mjs.map +1 -1
  37. package/dist/Toast.cjs +557 -0
  38. package/dist/Toast.cjs.map +1 -0
  39. package/dist/Toast.css +461 -0
  40. package/dist/Toast.css.map +1 -0
  41. package/dist/Toast.mjs +551 -0
  42. package/dist/Toast.mjs.map +1 -0
  43. package/dist/Toast_module.cjs +70 -0
  44. package/dist/Toast_module.cjs.map +1 -0
  45. package/dist/Toast_module.css +119 -0
  46. package/dist/Toast_module.css.map +1 -0
  47. package/dist/Toast_module.mjs +72 -0
  48. package/dist/Toast_module.mjs.map +1 -0
  49. package/dist/ar-AE.cjs +6 -1
  50. package/dist/ar-AE.cjs.map +1 -1
  51. package/dist/ar-AE.mjs +6 -1
  52. package/dist/ar-AE.mjs.map +1 -1
  53. package/dist/bg-BG.cjs +6 -1
  54. package/dist/bg-BG.cjs.map +1 -1
  55. package/dist/bg-BG.mjs +6 -1
  56. package/dist/bg-BG.mjs.map +1 -1
  57. package/dist/cs-CZ.cjs +10 -2
  58. package/dist/cs-CZ.cjs.map +1 -1
  59. package/dist/cs-CZ.mjs +10 -2
  60. package/dist/cs-CZ.mjs.map +1 -1
  61. package/dist/da-DK.cjs +8 -3
  62. package/dist/da-DK.cjs.map +1 -1
  63. package/dist/da-DK.mjs +8 -3
  64. package/dist/da-DK.mjs.map +1 -1
  65. package/dist/de-DE.cjs +7 -3
  66. package/dist/de-DE.cjs.map +1 -1
  67. package/dist/de-DE.mjs +7 -3
  68. package/dist/de-DE.mjs.map +1 -1
  69. package/dist/el-GR.cjs +6 -1
  70. package/dist/el-GR.cjs.map +1 -1
  71. package/dist/el-GR.mjs +6 -1
  72. package/dist/el-GR.mjs.map +1 -1
  73. package/dist/en-US.cjs +5 -1
  74. package/dist/en-US.cjs.map +1 -1
  75. package/dist/en-US.mjs +5 -1
  76. package/dist/en-US.mjs.map +1 -1
  77. package/dist/es-ES.cjs +10 -6
  78. package/dist/es-ES.cjs.map +1 -1
  79. package/dist/es-ES.mjs +10 -6
  80. package/dist/es-ES.mjs.map +1 -1
  81. package/dist/et-EE.cjs +6 -1
  82. package/dist/et-EE.cjs.map +1 -1
  83. package/dist/et-EE.mjs +6 -1
  84. package/dist/et-EE.mjs.map +1 -1
  85. package/dist/fi-FI.cjs +7 -2
  86. package/dist/fi-FI.cjs.map +1 -1
  87. package/dist/fi-FI.mjs +7 -2
  88. package/dist/fi-FI.mjs.map +1 -1
  89. package/dist/fr-FR.cjs +7 -3
  90. package/dist/fr-FR.cjs.map +1 -1
  91. package/dist/fr-FR.mjs +7 -3
  92. package/dist/fr-FR.mjs.map +1 -1
  93. package/dist/he-IL.cjs +9 -5
  94. package/dist/he-IL.cjs.map +1 -1
  95. package/dist/he-IL.mjs +9 -5
  96. package/dist/he-IL.mjs.map +1 -1
  97. package/dist/hr-HR.cjs +11 -3
  98. package/dist/hr-HR.cjs.map +1 -1
  99. package/dist/hr-HR.mjs +11 -3
  100. package/dist/hr-HR.mjs.map +1 -1
  101. package/dist/hu-HU.cjs +6 -1
  102. package/dist/hu-HU.cjs.map +1 -1
  103. package/dist/hu-HU.mjs +6 -1
  104. package/dist/hu-HU.mjs.map +1 -1
  105. package/dist/it-IT.cjs +6 -2
  106. package/dist/it-IT.cjs.map +1 -1
  107. package/dist/it-IT.mjs +6 -2
  108. package/dist/it-IT.mjs.map +1 -1
  109. package/dist/ja-JP.cjs +7 -2
  110. package/dist/ja-JP.cjs.map +1 -1
  111. package/dist/ja-JP.mjs +7 -2
  112. package/dist/ja-JP.mjs.map +1 -1
  113. package/dist/ko-KR.cjs +8 -3
  114. package/dist/ko-KR.cjs.map +1 -1
  115. package/dist/ko-KR.mjs +8 -3
  116. package/dist/ko-KR.mjs.map +1 -1
  117. package/dist/lt-LT.cjs +8 -3
  118. package/dist/lt-LT.cjs.map +1 -1
  119. package/dist/lt-LT.mjs +8 -3
  120. package/dist/lt-LT.mjs.map +1 -1
  121. package/dist/lv-LV.cjs +9 -4
  122. package/dist/lv-LV.cjs.map +1 -1
  123. package/dist/lv-LV.mjs +9 -4
  124. package/dist/lv-LV.mjs.map +1 -1
  125. package/dist/main.cjs +4 -2
  126. package/dist/main.cjs.map +1 -1
  127. package/dist/module.mjs +4 -2
  128. package/dist/module.mjs.map +1 -1
  129. package/dist/nb-NO.cjs +12 -4
  130. package/dist/nb-NO.cjs.map +1 -1
  131. package/dist/nb-NO.mjs +12 -4
  132. package/dist/nb-NO.mjs.map +1 -1
  133. package/dist/nl-NL.cjs +6 -1
  134. package/dist/nl-NL.cjs.map +1 -1
  135. package/dist/nl-NL.mjs +6 -1
  136. package/dist/nl-NL.mjs.map +1 -1
  137. package/dist/pl-PL.cjs +11 -3
  138. package/dist/pl-PL.cjs.map +1 -1
  139. package/dist/pl-PL.mjs +11 -3
  140. package/dist/pl-PL.mjs.map +1 -1
  141. package/dist/pt-BR.cjs +6 -1
  142. package/dist/pt-BR.cjs.map +1 -1
  143. package/dist/pt-BR.mjs +6 -1
  144. package/dist/pt-BR.mjs.map +1 -1
  145. package/dist/pt-PT.cjs +6 -1
  146. package/dist/pt-PT.cjs.map +1 -1
  147. package/dist/pt-PT.mjs +6 -1
  148. package/dist/pt-PT.mjs.map +1 -1
  149. package/dist/ro-RO.cjs +8 -4
  150. package/dist/ro-RO.cjs.map +1 -1
  151. package/dist/ro-RO.mjs +8 -4
  152. package/dist/ro-RO.mjs.map +1 -1
  153. package/dist/ru-RU.cjs +11 -3
  154. package/dist/ru-RU.cjs.map +1 -1
  155. package/dist/ru-RU.mjs +11 -3
  156. package/dist/ru-RU.mjs.map +1 -1
  157. package/dist/sk-SK.cjs +10 -2
  158. package/dist/sk-SK.cjs.map +1 -1
  159. package/dist/sk-SK.mjs +10 -2
  160. package/dist/sk-SK.mjs.map +1 -1
  161. package/dist/sl-SI.cjs +11 -3
  162. package/dist/sl-SI.cjs.map +1 -1
  163. package/dist/sl-SI.mjs +11 -3
  164. package/dist/sl-SI.mjs.map +1 -1
  165. package/dist/sr-SP.cjs +10 -2
  166. package/dist/sr-SP.cjs.map +1 -1
  167. package/dist/sr-SP.mjs +10 -2
  168. package/dist/sr-SP.mjs.map +1 -1
  169. package/dist/sv-SE.cjs +6 -2
  170. package/dist/sv-SE.cjs.map +1 -1
  171. package/dist/sv-SE.mjs +6 -2
  172. package/dist/sv-SE.mjs.map +1 -1
  173. package/dist/tr-TR.cjs +7 -2
  174. package/dist/tr-TR.cjs.map +1 -1
  175. package/dist/tr-TR.mjs +7 -2
  176. package/dist/tr-TR.mjs.map +1 -1
  177. package/dist/types.d.ts +49 -2
  178. package/dist/types.d.ts.map +1 -1
  179. package/dist/uk-UA.cjs +10 -2
  180. package/dist/uk-UA.cjs.map +1 -1
  181. package/dist/uk-UA.mjs +10 -2
  182. package/dist/uk-UA.mjs.map +1 -1
  183. package/dist/zh-CN.cjs +6 -1
  184. package/dist/zh-CN.cjs.map +1 -1
  185. package/dist/zh-CN.mjs +6 -1
  186. package/dist/zh-CN.mjs.map +1 -1
  187. package/dist/zh-TW.cjs +6 -1
  188. package/dist/zh-TW.cjs.map +1 -1
  189. package/dist/zh-TW.mjs +6 -1
  190. package/dist/zh-TW.mjs.map +1 -1
  191. package/package.json +20 -19
  192. package/src/ActionButton.tsx +1 -0
  193. package/src/Card.tsx +1 -1
  194. package/src/ColorSwatchPicker.tsx +2 -1
  195. package/src/Content.tsx +6 -6
  196. package/src/Disclosure.tsx +18 -24
  197. package/src/NotificationBadge.tsx +7 -3
  198. package/src/TableView.tsx +45 -29
  199. package/src/Toast.module.css +153 -0
  200. package/src/Toast.tsx +579 -0
  201. package/src/index.ts +3 -1
package/src/Toast.tsx ADDED
@@ -0,0 +1,579 @@
1
+ /*
2
+ * Copyright 2025 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {ActionButton} from './ActionButton';
14
+ import AlertIcon from '../s2wf-icons/S2_Icon_AlertTriangle_20_N.svg';
15
+ import {Button} from './Button';
16
+ import {CenterBaseline} from './CenterBaseline';
17
+ import CheckmarkIcon from '../s2wf-icons/S2_Icon_CheckmarkCircle_20_N.svg';
18
+ import Chevron from '../s2wf-icons/S2_Icon_ChevronDown_20_N.svg';
19
+ import {CloseButton} from './CloseButton';
20
+ import {createContext, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
21
+ import {DOMProps} from '@react-types/shared';
22
+ import {filterDOMProps, useEvent} from '@react-aria/utils';
23
+ import {flushSync} from 'react-dom';
24
+ import {focusRing, style} from '../style' with {type: 'macro'};
25
+ import {FocusScope, useModalOverlay} from 'react-aria';
26
+ import InfoIcon from '../s2wf-icons/S2_Icon_InfoCircle_20_N.svg';
27
+ // @ts-ignore
28
+ import intlMessages from '../intl/*.json';
29
+ import {mergeStyles} from '../style/runtime';
30
+ import {ToastOptions as RACToastOptions, UNSTABLE_Toast as Toast, UNSTABLE_ToastContent as ToastContent, UNSTABLE_ToastList as ToastList, ToastProps, UNSTABLE_ToastQueue as ToastQueue, UNSTABLE_ToastRegion as ToastRegion, ToastRegionProps, UNSTABLE_ToastStateContext as ToastStateContext} from 'react-aria-components';
31
+ import {Text} from './Content';
32
+ import toastCss from './Toast.module.css';
33
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
34
+ import {useMediaQuery} from '@react-spectrum/utils';
35
+ import {useOverlayTriggerState} from 'react-stately';
36
+
37
+ export type ToastPlacement = 'top' | 'top end' | 'bottom' | 'bottom end';
38
+ export interface ToastContainerProps extends Omit<ToastRegionProps<SpectrumToastValue>, 'queue' | 'children'> {
39
+ /**
40
+ * Placement of the toast container on the page.
41
+ * @default "bottom"
42
+ */
43
+ placement?: ToastPlacement
44
+ }
45
+
46
+ export interface ToastOptions extends Omit<RACToastOptions, 'priority'>, DOMProps {
47
+ /** A label for the action button within the toast. */
48
+ actionLabel?: string,
49
+ /** Handler that is called when the action button is pressed. */
50
+ onAction?: () => void,
51
+ /** Whether the toast should automatically close when an action is performed. */
52
+ shouldCloseOnAction?: boolean
53
+ }
54
+
55
+ export interface SpectrumToastValue extends DOMProps {
56
+ /** The content of the toast. */
57
+ children: string,
58
+ /** The variant (i.e. color) of the toast. */
59
+ variant: 'positive' | 'negative' | 'info' | 'neutral',
60
+ /** A label for the action button within the toast. */
61
+ actionLabel?: string,
62
+ /** Handler that is called when the action button is pressed. */
63
+ onAction?: () => void,
64
+ /** Whether the toast should automatically close when an action is performed. */
65
+ shouldCloseOnAction?: boolean
66
+ }
67
+
68
+ function startViewTransition(fn: () => void, type: string) {
69
+ if ('startViewTransition' in document) {
70
+ // Safari doesn't support :active-view-transition-type() yet, so we fall back to a class on the html element.
71
+ document.documentElement.classList.add(toastCss[type]);
72
+ let viewTransition = document.startViewTransition({
73
+ update: () => flushSync(fn),
74
+ types: [toastCss[type]]
75
+ });
76
+
77
+ viewTransition.ready.catch(() => {});
78
+ viewTransition.finished.then(() => {
79
+ document.documentElement.classList.remove(toastCss[type]);
80
+ });
81
+ } else {
82
+ fn();
83
+ }
84
+ }
85
+
86
+ // There is a single global toast queue instance for the whole app, initialized lazily.
87
+ let globalToastQueue: ToastQueue<SpectrumToastValue> | null = null;
88
+ function getGlobalToastQueue() {
89
+ if (!globalToastQueue) {
90
+ globalToastQueue = new ToastQueue({
91
+ maxVisibleToasts: Infinity,
92
+ wrapUpdate(fn, action) {
93
+ startViewTransition(fn, `toast-${action}`);
94
+ }
95
+ });
96
+ }
97
+
98
+ return globalToastQueue;
99
+ }
100
+
101
+ function addToast(children: string, variant: SpectrumToastValue['variant'], options: ToastOptions = {}) {
102
+ let value = {
103
+ children,
104
+ variant,
105
+ actionLabel: options.actionLabel,
106
+ onAction: options.onAction,
107
+ shouldCloseOnAction: options.shouldCloseOnAction,
108
+ ...filterDOMProps(options)
109
+ };
110
+
111
+ // Minimum time of 5s from https://spectrum.adobe.com/page/toast/#Auto-dismissible
112
+ // Actionable toasts cannot be auto dismissed. That would fail WCAG SC 2.2.1.
113
+ // It is debatable whether non-actionable toasts would also fail.
114
+ let timeout = options.timeout && !options.actionLabel ? Math.max(options.timeout, 5000) : undefined;
115
+ let queue = getGlobalToastQueue();
116
+ let key = queue.add(value, {timeout, onClose: options.onClose});
117
+ return () => queue.close(key);
118
+ }
119
+
120
+ type CloseFunction = () => void;
121
+
122
+ const SpectrumToastQueue = {
123
+ /** Queues a neutral toast. */
124
+ neutral(children: string, options: ToastOptions = {}): CloseFunction {
125
+ return addToast(children, 'neutral', options);
126
+ },
127
+ /** Queues a positive toast. */
128
+ positive(children: string, options: ToastOptions = {}): CloseFunction {
129
+ return addToast(children, 'positive', options);
130
+ },
131
+ /** Queues a negative toast. */
132
+ negative(children: string, options: ToastOptions = {}): CloseFunction {
133
+ return addToast(children, 'negative', options);
134
+ },
135
+ /** Queues an informational toast. */
136
+ info(children: string, options: ToastOptions = {}): CloseFunction {
137
+ return addToast(children, 'info', options);
138
+ }
139
+ };
140
+
141
+ export {SpectrumToastQueue as ToastQueue};
142
+
143
+ const toastRegion = style({
144
+ ...focusRing(),
145
+ display: 'flex',
146
+ flexDirection: {
147
+ placement: {
148
+ top: 'column',
149
+ bottom: 'column-reverse'
150
+ }
151
+ },
152
+ position: 'fixed',
153
+ insetX: 0,
154
+ width: 'fit',
155
+ top: {
156
+ placement: {
157
+ top: {
158
+ default: 16,
159
+ isExpanded: 0
160
+ }
161
+ }
162
+ },
163
+ bottom: {
164
+ placement: {
165
+ bottom: {
166
+ default: 16,
167
+ isExpanded: 0
168
+ }
169
+ }
170
+ },
171
+ marginStart: {
172
+ align: {
173
+ start: 16,
174
+ center: 'auto',
175
+ end: 'auto'
176
+ }
177
+ },
178
+ marginEnd: {
179
+ align: {
180
+ start: 'auto',
181
+ center: 'auto',
182
+ end: 16
183
+ }
184
+ },
185
+ boxSizing: 'border-box',
186
+ maxHeight: 'screen',
187
+ borderRadius: 'lg'
188
+ });
189
+
190
+ const toastList = style({
191
+ position: 'relative',
192
+ flexGrow: 1,
193
+ display: 'flex',
194
+ gap: 8,
195
+ flexDirection: {
196
+ placement: {
197
+ top: 'column',
198
+ bottom: 'column-reverse'
199
+ }
200
+ },
201
+ boxSizing: 'border-box',
202
+ marginY: 0,
203
+ padding: {
204
+ default: 0,
205
+ // Add padding when expanded to account for focus ring.
206
+ isExpanded: 8
207
+ },
208
+ paddingBottom: {
209
+ isExpanded: {
210
+ placement: {
211
+ top: 8,
212
+ bottom: 16
213
+ }
214
+ }
215
+ },
216
+ paddingTop: {
217
+ isExpanded: {
218
+ placement: {
219
+ top: 16,
220
+ bottom: 8
221
+ }
222
+ }
223
+ },
224
+ margin: 0,
225
+ marginX: {
226
+ default: 0,
227
+ // Undo padding for focus ring.
228
+ isExpanded: -8
229
+ },
230
+ overflow: {
231
+ isExpanded: 'auto'
232
+ }
233
+ });
234
+
235
+ // Separate style macro for focus ring and toast so that
236
+ // isFocusVisible doesn't cause toast background to change.
237
+ const toastFocusRing = style({
238
+ ...focusRing(),
239
+ outlineColor: {
240
+ default: 'focus-ring',
241
+ isExpanded: 'white'
242
+ }
243
+ });
244
+
245
+ const toastStyle = style({
246
+ display: 'flex',
247
+ gap: 16,
248
+ paddingStart: 16,
249
+ paddingEnd: 8,
250
+ paddingY: 12,
251
+ borderRadius: 'lg',
252
+ minHeight: 56,
253
+ '--maxWidth': {
254
+ type: 'maxWidth',
255
+ value: 336
256
+ },
257
+ maxWidth: '[min(var(--maxWidth), 90vw)]',
258
+ boxSizing: 'border-box',
259
+ flexShrink: 0,
260
+ font: 'ui',
261
+ color: 'white',
262
+ backgroundColor: {
263
+ variant: {
264
+ neutral: 'neutral-subdued',
265
+ info: 'informative',
266
+ positive: 'positive',
267
+ negative: 'negative'
268
+ }
269
+ },
270
+ '--iconPrimary': {
271
+ type: 'fill',
272
+ value: 'currentColor'
273
+ },
274
+ boxShadow: {
275
+ default: 'elevated',
276
+ isExpanded: 'none'
277
+ }
278
+ });
279
+
280
+ const toastBody = style({
281
+ // The top toast in a non-expanded stack has the expand button, so it is rendered as a grid.
282
+ // Otherwise it uses flex so the content can wrap when needed.
283
+ display: {
284
+ default: 'grid',
285
+ isSingle: 'flex'
286
+ },
287
+ gridTemplateColumns: ['auto', '1fr', 'auto'],
288
+ gridTemplateAreas: [
289
+ 'content content content',
290
+ 'expand . action'
291
+ ],
292
+ flexGrow: 1,
293
+ flexWrap: 'wrap',
294
+ alignItems: 'center',
295
+ columnGap: 24,
296
+ rowGap: 8
297
+ });
298
+
299
+ const toastContent = style({
300
+ display: 'flex',
301
+ gap: 8,
302
+ alignItems: 'baseline',
303
+ gridArea: 'content',
304
+ cursor: 'default',
305
+ width: 'fit'
306
+ });
307
+
308
+ const controls = style({
309
+ colorScheme: 'light',
310
+ display: {
311
+ default: 'none',
312
+ isExpanded: 'flex'
313
+ },
314
+ justifyContent: 'end',
315
+ gap: 8,
316
+ opacity: {
317
+ default: 0,
318
+ isExpanded: 1
319
+ }
320
+ });
321
+
322
+ const ICONS = {
323
+ info: InfoIcon,
324
+ negative: AlertIcon,
325
+ positive: CheckmarkIcon
326
+ };
327
+
328
+ interface ToastContainerContextValue {
329
+ isExpanded: boolean,
330
+ toggleExpanded: () => void
331
+ }
332
+
333
+ const ToastContainerContext = createContext<ToastContainerContextValue | null>(null);
334
+
335
+ /**
336
+ * A ToastContainer renders the queued toasts in an application. It should be placed
337
+ * at the root of the app.
338
+ */
339
+ export function ToastContainer(props: ToastContainerProps): ReactNode {
340
+ let {
341
+ placement = 'bottom'
342
+ } = props;
343
+ let queue = getGlobalToastQueue();
344
+ let align = 'center';
345
+ [placement, align = 'center'] = placement.split(' ') as any;
346
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
347
+ let regionRef = useRef<HTMLDivElement | null>(null);
348
+
349
+ let state = useOverlayTriggerState({});
350
+ let {isOpen: isExpanded, close, toggle} = state;
351
+ let ctx = useMemo(() => ({
352
+ isExpanded,
353
+ toggleExpanded() {
354
+ if (!isExpanded && queue.visibleToasts.length <= 1) {
355
+ return;
356
+ }
357
+
358
+ startViewTransition(
359
+ () => toggle(),
360
+ isExpanded ? 'toast-collapse' : 'toast-expand'
361
+ );
362
+ }
363
+ }), [isExpanded, toggle, queue]);
364
+
365
+ // Set the state to collapsed whenever the queue is emptied.
366
+ useEffect(() => {
367
+ return queue.subscribe(() => {
368
+ if (queue.visibleToasts.length === 0 && isExpanded) {
369
+ close();
370
+ }
371
+ });
372
+ }, [queue, isExpanded, close]);
373
+
374
+ let collapse = () => {
375
+ regionRef.current?.focus();
376
+ ctx.toggleExpanded();
377
+ };
378
+
379
+ // Prevent scroll, aria hide outside, and contain focus when expanded, since we take over the whole screen.
380
+ // Attach event handler to the ref since ToastRegion doesn't pass through onKeyDown.
381
+ useModalOverlay({}, state, regionRef);
382
+ useEvent(regionRef, 'keydown', isExpanded ? (e) => {
383
+ if (e.key === 'Escape') {
384
+ collapse();
385
+ }
386
+ } : undefined);
387
+
388
+ return (
389
+ <ToastRegion
390
+ {...props}
391
+ ref={regionRef}
392
+ queue={queue}
393
+ className={renderProps => toastRegion({
394
+ ...renderProps,
395
+ placement,
396
+ align,
397
+ isExpanded
398
+ })}>
399
+ <FocusScope contain={isExpanded}>
400
+ <ToastContainerContext.Provider value={ctx}>
401
+ {isExpanded && (
402
+ // eslint-disable-next-line
403
+ <div
404
+ className={toastCss['toast-background'] + style({position: 'fixed', inset: 0, backgroundColor: 'transparent-black-500'})}
405
+ onClick={collapse} />
406
+ )}
407
+ <SpectrumToastList placement={placement} align={align} />
408
+ <div className={toastCss['toast-controls'] + controls({isExpanded})}>
409
+ <ActionButton
410
+ size="S"
411
+ onPress={() => queue.clear()}
412
+ // Default focus ring does not have enough contrast against gray background.
413
+ UNSAFE_style={{outlineColor: 'white'}}>
414
+ {stringFormatter.format('toast.clearAll')}
415
+ </ActionButton>
416
+ <ActionButton
417
+ size="S"
418
+ onPress={collapse}
419
+ UNSAFE_style={{outlineColor: 'white'}}>
420
+ {stringFormatter.format('toast.collapse')}
421
+ </ActionButton>
422
+ </div>
423
+ </ToastContainerContext.Provider>
424
+ </FocusScope>
425
+ </ToastRegion>
426
+ );
427
+ }
428
+
429
+ function SpectrumToastList({placement, align}) {
430
+ let {isExpanded, toggleExpanded} = useContext(ToastContainerContext)!;
431
+
432
+ // Attach click handler to ref since ToastList doesn't pass through onClick/onPress.
433
+ let toastListRef = useRef(null);
434
+ useEvent(toastListRef, 'click', (e) => {
435
+ // Have to check if this is a button because stopPropagation in react events doesn't affect native events.
436
+ if (!isExpanded && !(e.target as Element)?.closest('button')) {
437
+ toggleExpanded();
438
+ }
439
+ });
440
+
441
+ let reduceMotion = useMediaQuery('(prefers-reduced-motion)');
442
+
443
+ return (
444
+ <ToastList<SpectrumToastValue>
445
+ ref={toastListRef}
446
+ style={({isHovered}) => {
447
+ let origin = isHovered ? 95 : 55;
448
+ return {
449
+ perspective: 80,
450
+ perspectiveOrigin: 'center ' + (placement === 'top' ? `calc(100% + ${origin}px)` : `${-origin}px`),
451
+ transition: 'perspective-origin 400ms'
452
+ };
453
+ }}
454
+ className={toastCss[isExpanded ? 'toast-list-expanded' : 'toast-list-collapsed'] + toastList({placement, align, isExpanded})}>
455
+ {({toast}) => (
456
+ <SpectrumToast
457
+ toast={toast}
458
+ placement={placement}
459
+ align={align}
460
+ reduceMotion={reduceMotion} />
461
+ )}
462
+ </ToastList>
463
+ );
464
+ }
465
+
466
+ interface SpectrumToastProps extends ToastProps<SpectrumToastValue> {
467
+ placement?: 'top' | 'bottom',
468
+ align?: 'start' | 'center' | 'end',
469
+ reduceMotion?: boolean
470
+ }
471
+
472
+ // Exported locally for stories.
473
+ export function SpectrumToast(props: SpectrumToastProps): ReactNode {
474
+ let {toast, placement = 'bottom', align = 'center'} = props;
475
+ let variant = toast.content.variant || 'info';
476
+ let Icon = ICONS[variant];
477
+ let state = useContext(ToastStateContext)!;
478
+ let visibleToasts = state.visibleToasts;
479
+ let index = visibleToasts.indexOf(toast);
480
+ let isMain = index <= 0;
481
+ let ctx = useContext(ToastContainerContext);
482
+ let isExpanded = ctx?.isExpanded || false;
483
+ let toastRef = useRef<HTMLDivElement | null>(null);
484
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
485
+
486
+ // When not expanded, use a presentational div for the toasts behind the top one.
487
+ // The content is invisible, all we show is the background color.
488
+ if (!isMain && ctx && !ctx.isExpanded) {
489
+ return (
490
+ <div
491
+ role="presentation"
492
+ style={{
493
+ position: 'absolute',
494
+ [placement === 'top' ? 'bottom' : 'top']: 0,
495
+ left: 0,
496
+ width: '100%',
497
+ translate: `0 0 ${(-12 * index)}px`,
498
+ // Only 3 toasts are visible in the stack at once, but all toasts are in the DOM.
499
+ // This allows view transitions to smoothly animate them from where they would be
500
+ // in the collapsed stack to their final position in the expanded list.
501
+ opacity: index >= 3 ? 0 : 1,
502
+ zIndex: visibleToasts.length - index - 1,
503
+ // When reduced motion is enabled, use append index to view-transition-name
504
+ // so that adding/removing a toast cross fades instead of transitioning the position.
505
+ // This works because the toasts are seen as separate elements instead of the same one when their index changes.
506
+ viewTransitionName: toast.key + (props.reduceMotion ? '-' + index : ''),
507
+ viewTransitionClass: [toastCss.toast, toastCss['background-toast']].map(c => CSS.escape(c)).join(' ')
508
+ }}
509
+ className={toastCss.toast + toastStyle({variant: toast.content.variant || 'info', index, isExpanded})} />
510
+ );
511
+ }
512
+
513
+ return (
514
+ <Toast
515
+ ref={toastRef}
516
+ toast={toast}
517
+ style={{
518
+ zIndex: visibleToasts.length - index - 1,
519
+ viewTransitionName: toast.key,
520
+ viewTransitionClass: [toastCss.toast, !isMain ? toastCss['background-toast'] : '', toastCss[placement], toastCss[align]].filter(Boolean).map(c => CSS.escape(c)).join(' ')
521
+ }}
522
+ className={renderProps => toastCss.toast + mergeStyles(
523
+ toastFocusRing({...renderProps, isExpanded}),
524
+ toastStyle({
525
+ variant: toast.content.variant || 'info',
526
+ index,
527
+ isExpanded
528
+ })
529
+ )}>
530
+ <div role="presentation" className={toastBody({isSingle: !isMain || visibleToasts.length <= 1 || isExpanded})}>
531
+ <ToastContent className={toastContent + (ctx && isMain ? ` ${toastCss['toast-content']}` : null)}>
532
+ {Icon &&
533
+ <CenterBaseline>
534
+ <Icon />
535
+ </CenterBaseline>
536
+ }
537
+ <Text slot="title">{toast.content.children}</Text>
538
+ </ToastContent>
539
+ {!isExpanded && visibleToasts.length > 1 &&
540
+ <ActionButton
541
+ isQuiet
542
+ staticColor="white"
543
+ styles={style({gridArea: 'expand'})}
544
+ // Make the chevron line up with the toast text, even though there is padding within the button.
545
+ UNSAFE_style={{marginInlineStart: variant === 'neutral' ? -10 : 14}}
546
+ UNSAFE_className={ctx && isMain ? toastCss['toast-expand'] : undefined}
547
+ onPress={() => {
548
+ // This button disappears when clicked, so move focus to the toast.
549
+ toastRef.current?.focus();
550
+ ctx?.toggleExpanded();
551
+ }}>
552
+ <Text>{stringFormatter.format('toast.showAll')}</Text>
553
+ {/* @ts-ignore */}
554
+ <Chevron UNSAFE_style={{rotate: placement === 'bottom' ? '180deg' : undefined}} />
555
+ </ActionButton>
556
+ }
557
+ {toast.content.actionLabel &&
558
+ <Button
559
+ variant="secondary"
560
+ fillStyle="outline"
561
+ staticColor="white"
562
+ onPress={() => {
563
+ toast.content.onAction?.();
564
+ if (toast.content.shouldCloseOnAction) {
565
+ state.close(toast.key);
566
+ }
567
+ }}
568
+ UNSAFE_className={ctx && isMain ? toastCss['toast-action'] : undefined}
569
+ styles={style({marginStart: 'auto', gridArea: 'action'})}>
570
+ {toast.content.actionLabel}
571
+ </Button>
572
+ }
573
+ </div>
574
+ <CloseButton
575
+ staticColor="white"
576
+ UNSAFE_className={ctx && isMain ? toastCss['toast-close'] : undefined} />
577
+ </Toast>
578
+ );
579
+ }
package/src/index.ts CHANGED
@@ -74,6 +74,7 @@ export {TableView, TableHeader, TableBody, Row, Cell, Column, TableContext} from
74
74
  export {Tabs, TabList, Tab, TabPanel, TabsContext} from './Tabs';
75
75
  export {TagGroup, Tag, TagGroupContext} from './TagGroup';
76
76
  export {TextArea, TextField, TextAreaContext, TextFieldContext} from './TextField';
77
+ export {ToastContainer as UNSTABLE_ToastContainer, ToastQueue as UNSTABLE_ToastQueue} from './Toast';
77
78
  export {ToggleButton, ToggleButtonContext} from './ToggleButton';
78
79
  export {ToggleButtonGroup, ToggleButtonGroupContext} from './ToggleButtonGroup';
79
80
  export {Tooltip, TooltipTrigger} from './Tooltip';
@@ -81,7 +82,7 @@ export {TreeView, TreeViewItem, TreeViewItemContent} from './TreeView';
81
82
 
82
83
  export {pressScale} from './pressScale';
83
84
 
84
- export {Autocomplete, AutocompleteContext, AutocompleteStateContext} from 'react-aria-components';
85
+ export {Autocomplete} from 'react-aria-components';
85
86
  export {Collection} from 'react-aria-components';
86
87
  export {FileTrigger} from 'react-aria-components';
87
88
 
@@ -145,6 +146,7 @@ export type {TableViewProps, TableHeaderProps, TableBodyProps, RowProps, CellPro
145
146
  export type {TabsProps, TabProps, TabListProps, TabPanelProps} from './Tabs';
146
147
  export type {TagGroupProps, TagProps} from './TagGroup';
147
148
  export type {TextFieldProps, TextAreaProps} from './TextField';
149
+ export type {ToastOptions, ToastContainerProps} from './Toast';
148
150
  export type {ToggleButtonProps} from './ToggleButton';
149
151
  export type {ToggleButtonGroupProps} from './ToggleButtonGroup';
150
152
  export type {TooltipProps} from './Tooltip';