@react-spectrum/s2 3.0.0-nightly-58d96cace-250410 → 3.0.0-nightly-9723225d6-250413
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.
- package/dist/Card.cjs +1 -1
- package/dist/Card.cjs.map +1 -1
- package/dist/Card.css.map +1 -1
- package/dist/Card.mjs +1 -1
- package/dist/Card.mjs.map +1 -1
- package/dist/ColorSwatchPicker.cjs +2 -2
- package/dist/ColorSwatchPicker.cjs.map +1 -1
- package/dist/ColorSwatchPicker.css.map +1 -1
- package/dist/ColorSwatchPicker.mjs +2 -2
- package/dist/ColorSwatchPicker.mjs.map +1 -1
- package/dist/Content.cjs +6 -6
- package/dist/Content.cjs.map +1 -1
- package/dist/Content.mjs +6 -6
- package/dist/Content.mjs.map +1 -1
- package/dist/TableView.cjs +28 -16
- package/dist/TableView.cjs.map +1 -1
- package/dist/TableView.css.map +1 -1
- package/dist/TableView.mjs +29 -17
- package/dist/TableView.mjs.map +1 -1
- package/dist/Toast.cjs +557 -0
- package/dist/Toast.cjs.map +1 -0
- package/dist/Toast.css +461 -0
- package/dist/Toast.css.map +1 -0
- package/dist/Toast.mjs +551 -0
- package/dist/Toast.mjs.map +1 -0
- package/dist/Toast_module.cjs +70 -0
- package/dist/Toast_module.cjs.map +1 -0
- package/dist/Toast_module.css +119 -0
- package/dist/Toast_module.css.map +1 -0
- package/dist/Toast_module.mjs +72 -0
- package/dist/Toast_module.mjs.map +1 -0
- package/dist/ar-AE.cjs +4 -1
- package/dist/ar-AE.cjs.map +1 -1
- package/dist/ar-AE.mjs +4 -1
- package/dist/ar-AE.mjs.map +1 -1
- package/dist/bg-BG.cjs +4 -1
- package/dist/bg-BG.cjs.map +1 -1
- package/dist/bg-BG.mjs +4 -1
- package/dist/bg-BG.mjs.map +1 -1
- package/dist/cs-CZ.cjs +4 -1
- package/dist/cs-CZ.cjs.map +1 -1
- package/dist/cs-CZ.mjs +4 -1
- package/dist/cs-CZ.mjs.map +1 -1
- package/dist/da-DK.cjs +4 -1
- package/dist/da-DK.cjs.map +1 -1
- package/dist/da-DK.mjs +4 -1
- package/dist/da-DK.mjs.map +1 -1
- package/dist/de-DE.cjs +4 -1
- package/dist/de-DE.cjs.map +1 -1
- package/dist/de-DE.mjs +4 -1
- package/dist/de-DE.mjs.map +1 -1
- package/dist/el-GR.cjs +4 -1
- package/dist/el-GR.cjs.map +1 -1
- package/dist/el-GR.mjs +4 -1
- package/dist/el-GR.mjs.map +1 -1
- package/dist/en-US.cjs +5 -1
- package/dist/en-US.cjs.map +1 -1
- package/dist/en-US.mjs +5 -1
- package/dist/en-US.mjs.map +1 -1
- package/dist/es-ES.cjs +4 -1
- package/dist/es-ES.cjs.map +1 -1
- package/dist/es-ES.mjs +4 -1
- package/dist/es-ES.mjs.map +1 -1
- package/dist/et-EE.cjs +4 -1
- package/dist/et-EE.cjs.map +1 -1
- package/dist/et-EE.mjs +4 -1
- package/dist/et-EE.mjs.map +1 -1
- package/dist/fi-FI.cjs +4 -1
- package/dist/fi-FI.cjs.map +1 -1
- package/dist/fi-FI.mjs +4 -1
- package/dist/fi-FI.mjs.map +1 -1
- package/dist/fr-FR.cjs +4 -1
- package/dist/fr-FR.cjs.map +1 -1
- package/dist/fr-FR.mjs +4 -1
- package/dist/fr-FR.mjs.map +1 -1
- package/dist/he-IL.cjs +4 -1
- package/dist/he-IL.cjs.map +1 -1
- package/dist/he-IL.mjs +4 -1
- package/dist/he-IL.mjs.map +1 -1
- package/dist/hr-HR.cjs +4 -1
- package/dist/hr-HR.cjs.map +1 -1
- package/dist/hr-HR.mjs +4 -1
- package/dist/hr-HR.mjs.map +1 -1
- package/dist/hu-HU.cjs +4 -1
- package/dist/hu-HU.cjs.map +1 -1
- package/dist/hu-HU.mjs +4 -1
- package/dist/hu-HU.mjs.map +1 -1
- package/dist/it-IT.cjs +4 -1
- package/dist/it-IT.cjs.map +1 -1
- package/dist/it-IT.mjs +4 -1
- package/dist/it-IT.mjs.map +1 -1
- package/dist/ja-JP.cjs +4 -1
- package/dist/ja-JP.cjs.map +1 -1
- package/dist/ja-JP.mjs +4 -1
- package/dist/ja-JP.mjs.map +1 -1
- package/dist/ko-KR.cjs +4 -1
- package/dist/ko-KR.cjs.map +1 -1
- package/dist/ko-KR.mjs +4 -1
- package/dist/ko-KR.mjs.map +1 -1
- package/dist/lt-LT.cjs +4 -1
- package/dist/lt-LT.cjs.map +1 -1
- package/dist/lt-LT.mjs +4 -1
- package/dist/lt-LT.mjs.map +1 -1
- package/dist/lv-LV.cjs +4 -1
- package/dist/lv-LV.cjs.map +1 -1
- package/dist/lv-LV.mjs +4 -1
- package/dist/lv-LV.mjs.map +1 -1
- package/dist/main.cjs +4 -0
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +3 -1
- package/dist/module.mjs.map +1 -1
- package/dist/nb-NO.cjs +4 -1
- package/dist/nb-NO.cjs.map +1 -1
- package/dist/nb-NO.mjs +4 -1
- package/dist/nb-NO.mjs.map +1 -1
- package/dist/nl-NL.cjs +4 -1
- package/dist/nl-NL.cjs.map +1 -1
- package/dist/nl-NL.mjs +4 -1
- package/dist/nl-NL.mjs.map +1 -1
- package/dist/pl-PL.cjs +4 -1
- package/dist/pl-PL.cjs.map +1 -1
- package/dist/pl-PL.mjs +4 -1
- package/dist/pl-PL.mjs.map +1 -1
- package/dist/pt-BR.cjs +4 -1
- package/dist/pt-BR.cjs.map +1 -1
- package/dist/pt-BR.mjs +4 -1
- package/dist/pt-BR.mjs.map +1 -1
- package/dist/pt-PT.cjs +4 -1
- package/dist/pt-PT.cjs.map +1 -1
- package/dist/pt-PT.mjs +4 -1
- package/dist/pt-PT.mjs.map +1 -1
- package/dist/ro-RO.cjs +4 -1
- package/dist/ro-RO.cjs.map +1 -1
- package/dist/ro-RO.mjs +4 -1
- package/dist/ro-RO.mjs.map +1 -1
- package/dist/ru-RU.cjs +4 -1
- package/dist/ru-RU.cjs.map +1 -1
- package/dist/ru-RU.mjs +4 -1
- package/dist/ru-RU.mjs.map +1 -1
- package/dist/sk-SK.cjs +4 -1
- package/dist/sk-SK.cjs.map +1 -1
- package/dist/sk-SK.mjs +4 -1
- package/dist/sk-SK.mjs.map +1 -1
- package/dist/sl-SI.cjs +4 -1
- package/dist/sl-SI.cjs.map +1 -1
- package/dist/sl-SI.mjs +4 -1
- package/dist/sl-SI.mjs.map +1 -1
- package/dist/sr-SP.cjs +4 -1
- package/dist/sr-SP.cjs.map +1 -1
- package/dist/sr-SP.mjs +4 -1
- package/dist/sr-SP.mjs.map +1 -1
- package/dist/sv-SE.cjs +4 -1
- package/dist/sv-SE.cjs.map +1 -1
- package/dist/sv-SE.mjs +4 -1
- package/dist/sv-SE.mjs.map +1 -1
- package/dist/tr-TR.cjs +4 -1
- package/dist/tr-TR.cjs.map +1 -1
- package/dist/tr-TR.mjs +4 -1
- package/dist/tr-TR.mjs.map +1 -1
- package/dist/types.d.ts +47 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/uk-UA.cjs +4 -1
- package/dist/uk-UA.cjs.map +1 -1
- package/dist/uk-UA.mjs +4 -1
- package/dist/uk-UA.mjs.map +1 -1
- package/dist/zh-CN.cjs +4 -1
- package/dist/zh-CN.cjs.map +1 -1
- package/dist/zh-CN.mjs +4 -1
- package/dist/zh-CN.mjs.map +1 -1
- package/dist/zh-TW.cjs +4 -1
- package/dist/zh-TW.cjs.map +1 -1
- package/dist/zh-TW.mjs +4 -1
- package/dist/zh-TW.mjs.map +1 -1
- package/package.json +20 -19
- package/src/Card.tsx +1 -1
- package/src/ColorSwatchPicker.tsx +2 -1
- package/src/Content.tsx +6 -6
- package/src/TableView.tsx +45 -29
- package/src/Toast.module.css +153 -0
- package/src/Toast.tsx +579 -0
- package/src/index.ts +2 -0
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';
|
|
@@ -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';
|