@fluentui/react-toast 9.0.5 → 9.1.0
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/CHANGELOG.json +87 -3
- package/CHANGELOG.md +31 -4
- package/lib/components/ToastContainer/useToastContainer.js +28 -11
- package/lib/components/ToastContainer/useToastContainer.js.map +1 -1
- package/lib/components/ToastContainer/useToastContainerStyles.styles.js +2 -2
- package/lib/components/ToastContainer/useToastContainerStyles.styles.js.map +1 -1
- package/lib/components/ToastTrigger/useToastTrigger.js +1 -1
- package/lib/components/ToastTrigger/useToastTrigger.js.map +1 -1
- package/lib/components/Toaster/useToastAnnounce.js +47 -0
- package/lib/components/Toaster/useToastAnnounce.js.map +1 -0
- package/lib/components/Toaster/useToaster.js +32 -30
- package/lib/components/Toaster/useToaster.js.map +1 -1
- package/lib/components/Toaster/useToasterFocusManagement.js +85 -0
- package/lib/components/Toaster/useToasterFocusManagement.js.map +1 -0
- package/lib/components/Toaster/useToasterStyles.styles.js +4 -1
- package/lib/components/Toaster/useToasterStyles.styles.js.map +1 -1
- package/lib/state/useToaster.js +24 -6
- package/lib/state/useToaster.js.map +1 -1
- package/lib-commonjs/components/ToastContainer/useToastContainer.js +28 -11
- package/lib-commonjs/components/ToastContainer/useToastContainer.js.map +1 -1
- package/lib-commonjs/components/ToastContainer/useToastContainerStyles.styles.js +3 -3
- package/lib-commonjs/components/ToastContainer/useToastContainerStyles.styles.js.map +1 -1
- package/lib-commonjs/components/ToastTrigger/useToastTrigger.js +1 -1
- package/lib-commonjs/components/ToastTrigger/useToastTrigger.js.map +1 -1
- package/lib-commonjs/components/Toaster/useToastAnnounce.js +50 -0
- package/lib-commonjs/components/Toaster/useToastAnnounce.js.map +1 -0
- package/lib-commonjs/components/Toaster/useToaster.js +31 -29
- package/lib-commonjs/components/Toaster/useToaster.js.map +1 -1
- package/lib-commonjs/components/Toaster/useToasterFocusManagement.js +92 -0
- package/lib-commonjs/components/Toaster/useToasterFocusManagement.js.map +1 -0
- package/lib-commonjs/components/Toaster/useToasterStyles.styles.js +4 -1
- package/lib-commonjs/components/Toaster/useToasterStyles.styles.js.map +1 -1
- package/lib-commonjs/state/useToaster.js +24 -6
- package/lib-commonjs/state/useToaster.js.map +1 -1
- package/package.json +9 -8
package/CHANGELOG.json
CHANGED
|
@@ -2,7 +2,91 @@
|
|
|
2
2
|
"name": "@fluentui/react-toast",
|
|
3
3
|
"entries": [
|
|
4
4
|
{
|
|
5
|
-
"date": "
|
|
5
|
+
"date": "Fri, 04 Aug 2023 08:48:23 GMT",
|
|
6
|
+
"tag": "@fluentui/react-toast_v9.1.0",
|
|
7
|
+
"version": "9.1.0",
|
|
8
|
+
"comments": {
|
|
9
|
+
"minor": [
|
|
10
|
+
{
|
|
11
|
+
"author": "lingfangao@hotmail.com",
|
|
12
|
+
"package": "@fluentui/react-toast",
|
|
13
|
+
"commit": "495a08579f2d16f082f2b18c299f1a647906b236",
|
|
14
|
+
"comment": "feat: Add extra keyboard behaviour for multi toast scenario"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"author": "beachball",
|
|
18
|
+
"package": "@fluentui/react-toast",
|
|
19
|
+
"comment": "Bump @fluentui/react-aria to v9.3.28",
|
|
20
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"author": "beachball",
|
|
24
|
+
"package": "@fluentui/react-toast",
|
|
25
|
+
"comment": "Bump @fluentui/react-jsx-runtime to v9.0.0-alpha.13",
|
|
26
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"author": "beachball",
|
|
30
|
+
"package": "@fluentui/react-toast",
|
|
31
|
+
"comment": "Bump @fluentui/react-portal to v9.3.5",
|
|
32
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"author": "beachball",
|
|
36
|
+
"package": "@fluentui/react-toast",
|
|
37
|
+
"comment": "Bump @fluentui/react-shared-contexts to v9.7.1",
|
|
38
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"author": "beachball",
|
|
42
|
+
"package": "@fluentui/react-toast",
|
|
43
|
+
"comment": "Bump @fluentui/react-tabster to v9.12.0",
|
|
44
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"author": "beachball",
|
|
48
|
+
"package": "@fluentui/react-toast",
|
|
49
|
+
"comment": "Bump @fluentui/react-theme to v9.1.10",
|
|
50
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"author": "beachball",
|
|
54
|
+
"package": "@fluentui/react-toast",
|
|
55
|
+
"comment": "Bump @fluentui/react-utilities to v9.11.0",
|
|
56
|
+
"commit": "0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca"
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"date": "Tue, 01 Aug 2023 10:17:21 GMT",
|
|
63
|
+
"tag": "@fluentui/react-toast_v9.0.6",
|
|
64
|
+
"version": "9.0.6",
|
|
65
|
+
"comments": {
|
|
66
|
+
"patch": [
|
|
67
|
+
{
|
|
68
|
+
"author": "beachball",
|
|
69
|
+
"package": "@fluentui/react-toast",
|
|
70
|
+
"comment": "Bump @fluentui/react-portal to v9.3.4",
|
|
71
|
+
"commit": "412d815f88863bde8bb16abc8fd9c4140a37f1f5"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"author": "beachball",
|
|
75
|
+
"package": "@fluentui/react-toast",
|
|
76
|
+
"comment": "Bump @fluentui/react-shared-contexts to v9.7.0",
|
|
77
|
+
"commit": "412d815f88863bde8bb16abc8fd9c4140a37f1f5"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"author": "beachball",
|
|
81
|
+
"package": "@fluentui/react-toast",
|
|
82
|
+
"comment": "Bump @fluentui/react-tabster to v9.11.1",
|
|
83
|
+
"commit": "412d815f88863bde8bb16abc8fd9c4140a37f1f5"
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"date": "Tue, 25 Jul 2023 13:29:22 GMT",
|
|
6
90
|
"tag": "@fluentui/react-toast_v9.0.5",
|
|
7
91
|
"version": "9.0.5",
|
|
8
92
|
"comments": {
|
|
@@ -17,13 +101,13 @@
|
|
|
17
101
|
"author": "beachball",
|
|
18
102
|
"package": "@fluentui/react-toast",
|
|
19
103
|
"comment": "Bump @fluentui/react-portal to v9.3.3",
|
|
20
|
-
"commit": "
|
|
104
|
+
"commit": "e59b4b305eb656c5af005fefbfa2b1f69afcdc43"
|
|
21
105
|
},
|
|
22
106
|
{
|
|
23
107
|
"author": "beachball",
|
|
24
108
|
"package": "@fluentui/react-toast",
|
|
25
109
|
"comment": "Bump @fluentui/react-tabster to v9.11.0",
|
|
26
|
-
"commit": "
|
|
110
|
+
"commit": "e59b4b305eb656c5af005fefbfa2b1f69afcdc43"
|
|
27
111
|
}
|
|
28
112
|
]
|
|
29
113
|
}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,19 +1,46 @@
|
|
|
1
1
|
# Change Log - @fluentui/react-toast
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Fri, 04 Aug 2023 08:48:23 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
+
## [9.1.0](https://github.com/microsoft/fluentui/tree/@fluentui/react-toast_v9.1.0)
|
|
8
|
+
|
|
9
|
+
Fri, 04 Aug 2023 08:48:23 GMT
|
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-toast_v9.0.6..@fluentui/react-toast_v9.1.0)
|
|
11
|
+
|
|
12
|
+
### Minor changes
|
|
13
|
+
|
|
14
|
+
- feat: Add extra keyboard behaviour for multi toast scenario ([PR #28660](https://github.com/microsoft/fluentui/pull/28660) by lingfangao@hotmail.com)
|
|
15
|
+
- Bump @fluentui/react-aria to v9.3.28 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
16
|
+
- Bump @fluentui/react-jsx-runtime to v9.0.0-alpha.13 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
17
|
+
- Bump @fluentui/react-portal to v9.3.5 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
18
|
+
- Bump @fluentui/react-shared-contexts to v9.7.1 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
19
|
+
- Bump @fluentui/react-tabster to v9.12.0 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
20
|
+
- Bump @fluentui/react-theme to v9.1.10 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
21
|
+
- Bump @fluentui/react-utilities to v9.11.0 ([commit](https://github.com/microsoft/fluentui/commit/0bf7d9438c1d0ff90cd2b28bc4cceb4f807afbca) by beachball)
|
|
22
|
+
|
|
23
|
+
## [9.0.6](https://github.com/microsoft/fluentui/tree/@fluentui/react-toast_v9.0.6)
|
|
24
|
+
|
|
25
|
+
Tue, 01 Aug 2023 10:17:21 GMT
|
|
26
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-toast_v9.0.5..@fluentui/react-toast_v9.0.6)
|
|
27
|
+
|
|
28
|
+
### Patches
|
|
29
|
+
|
|
30
|
+
- Bump @fluentui/react-portal to v9.3.4 ([PR #28689](https://github.com/microsoft/fluentui/pull/28689) by beachball)
|
|
31
|
+
- Bump @fluentui/react-shared-contexts to v9.7.0 ([PR #28689](https://github.com/microsoft/fluentui/pull/28689) by beachball)
|
|
32
|
+
- Bump @fluentui/react-tabster to v9.11.1 ([PR #28689](https://github.com/microsoft/fluentui/pull/28689) by beachball)
|
|
33
|
+
|
|
7
34
|
## [9.0.5](https://github.com/microsoft/fluentui/tree/@fluentui/react-toast_v9.0.5)
|
|
8
35
|
|
|
9
|
-
Tue, 25 Jul 2023 13:
|
|
36
|
+
Tue, 25 Jul 2023 13:29:22 GMT
|
|
10
37
|
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-toast_v9.0.4..@fluentui/react-toast_v9.0.5)
|
|
11
38
|
|
|
12
39
|
### Patches
|
|
13
40
|
|
|
14
41
|
- chore: Update react-icons version to pick up fowardref change. ([PR #28590](https://github.com/microsoft/fluentui/pull/28590) by ololubek@microsoft.com)
|
|
15
|
-
- Bump @fluentui/react-portal to v9.3.3 ([PR #
|
|
16
|
-
- Bump @fluentui/react-tabster to v9.11.0 ([PR #
|
|
42
|
+
- Bump @fluentui/react-portal to v9.3.3 ([PR #28622](https://github.com/microsoft/fluentui/pull/28622) by beachball)
|
|
43
|
+
- Bump @fluentui/react-tabster to v9.11.0 ([PR #28622](https://github.com/microsoft/fluentui/pull/28622) by beachball)
|
|
17
44
|
|
|
18
45
|
## [9.0.4](https://github.com/microsoft/fluentui/tree/@fluentui/react-toast_v9.0.4)
|
|
19
46
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { getNativeElementProps, useMergedRefs, useEventCallback, resolveShorthand, useId } from '@fluentui/react-utilities';
|
|
3
3
|
import { useFluent_unstable } from '@fluentui/react-shared-contexts';
|
|
4
|
+
import { Delete, Tab } from '@fluentui/keyboard-keys';
|
|
5
|
+
import { useFocusableGroup, useFocusFinders } from '@fluentui/react-tabster';
|
|
4
6
|
import { Timer } from '../Timer/Timer';
|
|
5
|
-
import { useFocusFinders } from '@fluentui/react-tabster';
|
|
6
7
|
const intentPolitenessMap = {
|
|
7
8
|
success: 'assertive',
|
|
8
9
|
warning: 'assertive',
|
|
@@ -26,6 +27,16 @@ const intentPolitenessMap = {
|
|
|
26
27
|
const [running, setRunning] = React.useState(false);
|
|
27
28
|
const imperativePauseRef = React.useRef(false);
|
|
28
29
|
const focusedToastBeforeClose = React.useRef(false);
|
|
30
|
+
const focusableGroupAttribute = useFocusableGroup({
|
|
31
|
+
tabBehavior: 'limited-trap-focus',
|
|
32
|
+
// Users should only use Tab to focus into the toast
|
|
33
|
+
// Escape is already reserved to dismiss all toasts
|
|
34
|
+
ignoreDefaultKeydown: {
|
|
35
|
+
Tab: true,
|
|
36
|
+
Escape: true,
|
|
37
|
+
Enter: true
|
|
38
|
+
}
|
|
39
|
+
});
|
|
29
40
|
const close = useEventCallback(()=>{
|
|
30
41
|
var _toastRef_current;
|
|
31
42
|
const activeElement = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.activeElement;
|
|
@@ -57,18 +68,12 @@ const intentPolitenessMap = {
|
|
|
57
68
|
setRunning(true);
|
|
58
69
|
}
|
|
59
70
|
});
|
|
60
|
-
const { findFirstFocusable } = useFocusFinders();
|
|
61
71
|
React.useImperativeHandle(imperativeRef, ()=>({
|
|
62
72
|
focus: ()=>{
|
|
63
73
|
if (!toastRef.current) {
|
|
64
74
|
return;
|
|
65
75
|
}
|
|
66
|
-
|
|
67
|
-
if (firstFocusable) {
|
|
68
|
-
firstFocusable.focus();
|
|
69
|
-
} else {
|
|
70
|
-
toastRef.current.focus();
|
|
71
|
-
}
|
|
76
|
+
toastRef.current.focus();
|
|
72
77
|
},
|
|
73
78
|
play: ()=>{
|
|
74
79
|
imperativePauseRef.current = false;
|
|
@@ -141,12 +146,23 @@ const intentPolitenessMap = {
|
|
|
141
146
|
play();
|
|
142
147
|
userRootSlot === null || userRootSlot === void 0 ? void 0 : (_userRootSlot_onMouseEnter = userRootSlot.onMouseEnter) === null || _userRootSlot_onMouseEnter === void 0 ? void 0 : _userRootSlot_onMouseEnter.call(userRootSlot, e);
|
|
143
148
|
});
|
|
149
|
+
const { findFirstFocusable , findLastFocusable } = useFocusFinders();
|
|
144
150
|
const onKeyDown = useEventCallback((e)=>{
|
|
145
151
|
var _userRootSlot_onKeyDown;
|
|
146
|
-
if (e.key ===
|
|
152
|
+
if (e.key === Delete) {
|
|
147
153
|
e.preventDefault();
|
|
148
154
|
close();
|
|
149
155
|
}
|
|
156
|
+
if (e.key === Tab && e.currentTarget === e.target) {
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
if (e.shiftKey) {
|
|
159
|
+
var _findLastFocusable;
|
|
160
|
+
(_findLastFocusable = findLastFocusable(e.currentTarget)) === null || _findLastFocusable === void 0 ? void 0 : _findLastFocusable.focus();
|
|
161
|
+
} else {
|
|
162
|
+
var _findFirstFocusable;
|
|
163
|
+
(_findFirstFocusable = findFirstFocusable(e.currentTarget)) === null || _findFirstFocusable === void 0 ? void 0 : _findFirstFocusable.focus();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
150
166
|
userRootSlot === null || userRootSlot === void 0 ? void 0 : (_userRootSlot_onKeyDown = userRootSlot.onKeyDown) === null || _userRootSlot_onKeyDown === void 0 ? void 0 : _userRootSlot_onKeyDown.call(userRootSlot, e);
|
|
151
167
|
});
|
|
152
168
|
React.useEffect(()=>{
|
|
@@ -193,12 +209,13 @@ const intentPolitenessMap = {
|
|
|
193
209
|
root: getNativeElementProps('div', {
|
|
194
210
|
ref: useMergedRefs(ref, toastRef, toastAnimationRef),
|
|
195
211
|
children,
|
|
196
|
-
tabIndex:
|
|
197
|
-
role: '
|
|
212
|
+
tabIndex: 0,
|
|
213
|
+
role: 'listitem',
|
|
198
214
|
'aria-labelledby': titleId,
|
|
199
215
|
'aria-describedby': bodyId,
|
|
200
216
|
...rest,
|
|
201
217
|
...userRootSlot,
|
|
218
|
+
...focusableGroupAttribute,
|
|
202
219
|
onMouseEnter,
|
|
203
220
|
onMouseLeave,
|
|
204
221
|
onKeyDown
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useToastContainer.ts"],"sourcesContent":["import * as React from 'react';\nimport {\n getNativeElementProps,\n useMergedRefs,\n ExtractSlotProps,\n Slot,\n useEventCallback,\n resolveShorthand,\n useId,\n} from '@fluentui/react-utilities';\nimport { useFluent_unstable } from '@fluentui/react-shared-contexts';\nimport { ToastStatus } from '../../state';\nimport type { ToastContainerProps, ToastContainerState } from './ToastContainer.types';\nimport { Timer, TimerProps } from '../Timer/Timer';\nimport { useFocusFinders } from '@fluentui/react-tabster';\n\nconst intentPolitenessMap = {\n success: 'assertive',\n warning: 'assertive',\n error: 'assertive',\n info: 'polite',\n} as const;\n\n/**\n * Create the state required to render ToastContainer.\n *\n * The returned state can be modified with hooks such as useToastContainerStyles_unstable,\n * before being passed to renderToastContainer_unstable.\n *\n * @param props - props from this instance of ToastContainer\n * @param ref - reference to root HTMLElement of ToastContainer\n */\nexport const useToastContainer_unstable = (\n props: ToastContainerProps,\n ref: React.Ref<HTMLElement>,\n): ToastContainerState => {\n const {\n visible,\n children,\n close: closeProp,\n remove,\n updateId,\n announce,\n data,\n timeout: timerTimeout,\n politeness: desiredPoliteness,\n intent = 'info',\n pauseOnHover,\n pauseOnWindowBlur,\n imperativeRef,\n tryRestoreFocus,\n ...rest\n } = props;\n const titleId = useId('toast-title');\n const bodyId = useId('toast-body');\n const toastRef = React.useRef<HTMLDivElement | null>(null);\n const { targetDocument } = useFluent_unstable();\n const [running, setRunning] = React.useState(false);\n const imperativePauseRef = React.useRef(false);\n const focusedToastBeforeClose = React.useRef(false);\n\n const close = useEventCallback(() => {\n const activeElement = targetDocument?.activeElement;\n if (activeElement && toastRef.current?.contains(activeElement)) {\n focusedToastBeforeClose.current = true;\n }\n\n closeProp();\n });\n const onStatusChange = useEventCallback((status: ToastStatus) => props.onStatusChange?.(null, { status, ...props }));\n const pause = useEventCallback(() => setRunning(false));\n const play = useEventCallback(() => {\n if (imperativePauseRef.current) {\n return;\n }\n const containsActive = !!toastRef.current?.contains(targetDocument?.activeElement ?? null);\n if (timerTimeout < 0) {\n setRunning(true);\n return;\n }\n\n if (!containsActive) {\n setRunning(true);\n }\n });\n\n const { findFirstFocusable } = useFocusFinders();\n\n React.useImperativeHandle(imperativeRef, () => ({\n focus: () => {\n if (!toastRef.current) {\n return;\n }\n\n const firstFocusable = findFirstFocusable(toastRef.current);\n if (firstFocusable) {\n firstFocusable.focus();\n } else {\n toastRef.current.focus();\n }\n },\n\n play: () => {\n imperativePauseRef.current = false;\n play();\n },\n pause: () => {\n imperativePauseRef.current = true;\n pause();\n },\n }));\n\n React.useEffect(() => {\n return () => onStatusChange('unmounted');\n }, [onStatusChange]);\n\n React.useEffect(() => {\n if (!targetDocument) {\n return;\n }\n\n if (pauseOnWindowBlur) {\n targetDocument.defaultView?.addEventListener('focus', play);\n targetDocument.defaultView?.addEventListener('blur', pause);\n return () => {\n targetDocument.defaultView?.removeEventListener('focus', play);\n targetDocument.defaultView?.removeEventListener('blur', pause);\n };\n }\n }, [targetDocument, pause, play, pauseOnWindowBlur]);\n\n // It's impossible to animate to height: auto in CSS, the actual pixel value must be known\n // Get the height of the toast before animation styles have been applied and set a CSS\n // variable with its height. The CSS variable will be used by the styles\n const onTransitionEntering = () => {\n if (!toastRef.current) {\n return;\n }\n\n const element = toastRef.current;\n element.style.setProperty('--fui-toast-height', `${element.scrollHeight}px`);\n };\n\n // Users never actually use ToastContainer as a JSX but imperatively through useToastContainerController\n const userRootSlot = (data as { root?: ExtractSlotProps<Slot<'div'>> }).root;\n\n // Using a ref callback here because addEventListener supports `once`\n const toastAnimationRef = React.useCallback(\n (el: HTMLDivElement | null) => {\n if (el && toastRef.current) {\n toastRef.current.addEventListener(\n 'animationend',\n () => {\n // start toast once it's fully animated in\n play();\n onStatusChange('visible');\n },\n { once: true },\n );\n }\n },\n [play, onStatusChange],\n );\n\n const onMouseEnter = useEventCallback((e: React.MouseEvent<HTMLDivElement>) => {\n pause();\n userRootSlot?.onMouseEnter?.(e);\n });\n\n const onMouseLeave = useEventCallback((e: React.MouseEvent<HTMLDivElement>) => {\n play();\n userRootSlot?.onMouseEnter?.(e);\n });\n\n const onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLDivElement>) => {\n if (e.key === 'Escape') {\n e.preventDefault();\n close();\n }\n\n userRootSlot?.onKeyDown?.(e);\n });\n\n React.useEffect(() => {\n if (!visible) {\n return;\n }\n\n const politeness = desiredPoliteness ?? intentPolitenessMap[intent];\n announce(toastRef.current?.textContent ?? '', { politeness });\n }, [announce, desiredPoliteness, toastRef, visible, updateId, intent]);\n\n React.useEffect(() => {\n return () => {\n if (focusedToastBeforeClose.current) {\n focusedToastBeforeClose.current = false;\n tryRestoreFocus();\n }\n };\n }, [tryRestoreFocus]);\n\n return {\n components: {\n timer: Timer,\n root: 'div',\n },\n timer: resolveShorthand<TimerProps>(\n { key: updateId, onTimeout: close, running, timeout: timerTimeout ?? -1 },\n { required: true },\n ),\n root: getNativeElementProps('div', {\n ref: useMergedRefs(ref, toastRef, toastAnimationRef),\n children,\n tabIndex: -1,\n role: 'group',\n 'aria-labelledby': titleId,\n 'aria-describedby': bodyId,\n ...rest,\n ...userRootSlot,\n onMouseEnter,\n onMouseLeave,\n onKeyDown,\n }),\n timerTimeout,\n transitionTimeout: 500,\n running,\n visible,\n remove,\n close,\n onTransitionEntering,\n updateId,\n nodeRef: toastRef,\n intent,\n titleId,\n bodyId,\n };\n};\n"],"names":["React","getNativeElementProps","useMergedRefs","useEventCallback","resolveShorthand","useId","useFluent_unstable","Timer","useFocusFinders","intentPolitenessMap","success","warning","error","info","useToastContainer_unstable","props","ref","visible","children","close","closeProp","remove","updateId","announce","data","timeout","timerTimeout","politeness","desiredPoliteness","intent","pauseOnHover","pauseOnWindowBlur","imperativeRef","tryRestoreFocus","rest","titleId","bodyId","toastRef","useRef","targetDocument","running","setRunning","useState","imperativePauseRef","focusedToastBeforeClose","activeElement","current","contains","onStatusChange","status","pause","play","containsActive","findFirstFocusable","useImperativeHandle","focus","firstFocusable","useEffect","defaultView","addEventListener","removeEventListener","onTransitionEntering","element","style","setProperty","scrollHeight","userRootSlot","root","toastAnimationRef","useCallback","el","once","onMouseEnter","e","onMouseLeave","onKeyDown","key","preventDefault","textContent","components","timer","onTimeout","required","tabIndex","role","transitionTimeout","nodeRef"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SACEC,qBAAqB,EACrBC,aAAa,EAGbC,gBAAgB,EAChBC,gBAAgB,EAChBC,KAAK,QACA,4BAA4B;AACnC,SAASC,kBAAkB,QAAQ,kCAAkC;AAGrE,SAASC,KAAK,QAAoB,iBAAiB;AACnD,SAASC,eAAe,QAAQ,0BAA0B;AAE1D,MAAMC,sBAAsB;IAC1BC,SAAS;IACTC,SAAS;IACTC,OAAO;IACPC,MAAM;AACR;AAEA;;;;;;;;CAQC,GACD,OAAO,MAAMC,6BAA6B,CACxCC,OACAC,MACwB;IACxB,MAAM,EACJC,QAAO,EACPC,SAAQ,EACRC,OAAOC,UAAS,EAChBC,OAAM,EACNC,SAAQ,EACRC,SAAQ,EACRC,KAAI,EACJC,SAASC,aAAY,EACrBC,YAAYC,kBAAiB,EAC7BC,QAAS,OAAM,EACfC,aAAY,EACZC,kBAAiB,EACjBC,cAAa,EACbC,gBAAe,EACf,GAAGC,MACJ,GAAGnB;IACJ,MAAMoB,UAAU9B,MAAM;IACtB,MAAM+B,SAAS/B,MAAM;IACrB,MAAMgC,WAAWrC,MAAMsC,MAAM,CAAwB,IAAI;IACzD,MAAM,EAAEC,eAAc,EAAE,GAAGjC;IAC3B,MAAM,CAACkC,SAASC,WAAW,GAAGzC,MAAM0C,QAAQ,CAAC,KAA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A,KAAAA,IAAAA,kBAAkByC,WAAW,cAA7BzC,2CAAAA,gCAAiC,EAAE,EAAE;YAAEV;QAAW;IAC7D,GAAG;QAACJ;QAAUK;QAAmBS;QAAUpB;QAASK;QAAUO;KAAO;IAErE7B,MAAMyD,SAAS,CAAC,IAAM;QACpB,OAAO,IAAM;YACX,IAAIb,wBAAwBE,OAAO,EAAE;gBACnCF,wBAAwBE,OAAO,GAAG,KAAK;gBACvCb;YACF,CAAC;QACH;IACF,GAAG;QAACA;KAAgB;IAEpB,OAAO;QACL8C,YAAY;YACVC,OAAOzE;YACP4D,MAAM;QACR;QACAa,OAAO5E,iBACL;YAAEwE,KAAKtD;YAAU2D,WAAW9D;YAAOqB;YAASf,SAASC,yBAAAA,0BAAAA,eAAgB,CAAC,CAAC;QAAC,GACxE;YAAEwD,UAAU,IAAI;QAAC;QAEnBf,MAAMlE,sBAAsB,OAAO;YACjCe,KAAKd,cAAcc,KAAKqB,UAAU+B;YAClClD;YACAiE,UAAU,CAAC;YACXC,MAAM;YACN,mBAAmBjD;YACnB,oBAAoBC;YACpB,GAAGF,IAAI;YACP,GAAGgC,YAAY;YACfM;YACAE;YACAC;QACF;QACAjD;QACA2D,mBAAmB;QACnB7C;QACAvB;QACAI;QACAF;QACA0C;QACAvC;QACAgE,SAASjD;QACTR;QACAM;QACAC;IACF;AACF,EAAE"}
|
|
1
|
+
{"version":3,"sources":["useToastContainer.ts"],"sourcesContent":["import * as React from 'react';\nimport {\n getNativeElementProps,\n useMergedRefs,\n ExtractSlotProps,\n Slot,\n useEventCallback,\n resolveShorthand,\n useId,\n} from '@fluentui/react-utilities';\nimport { useFluent_unstable } from '@fluentui/react-shared-contexts';\nimport { Delete, Tab } from '@fluentui/keyboard-keys';\nimport { useFocusableGroup, useFocusFinders } from '@fluentui/react-tabster';\nimport { ToastStatus } from '../../state';\nimport type { ToastContainerProps, ToastContainerState } from './ToastContainer.types';\nimport { Timer, TimerProps } from '../Timer/Timer';\n\nconst intentPolitenessMap = {\n success: 'assertive',\n warning: 'assertive',\n error: 'assertive',\n info: 'polite',\n} as const;\n\n/**\n * Create the state required to render ToastContainer.\n *\n * The returned state can be modified with hooks such as useToastContainerStyles_unstable,\n * before being passed to renderToastContainer_unstable.\n *\n * @param props - props from this instance of ToastContainer\n * @param ref - reference to root HTMLElement of ToastContainer\n */\nexport const useToastContainer_unstable = (\n props: ToastContainerProps,\n ref: React.Ref<HTMLElement>,\n): ToastContainerState => {\n const {\n visible,\n children,\n close: closeProp,\n remove,\n updateId,\n announce,\n data,\n timeout: timerTimeout,\n politeness: desiredPoliteness,\n intent = 'info',\n pauseOnHover,\n pauseOnWindowBlur,\n imperativeRef,\n tryRestoreFocus,\n ...rest\n } = props;\n const titleId = useId('toast-title');\n const bodyId = useId('toast-body');\n const toastRef = React.useRef<HTMLDivElement | null>(null);\n const { targetDocument } = useFluent_unstable();\n const [running, setRunning] = React.useState(false);\n const imperativePauseRef = React.useRef(false);\n const focusedToastBeforeClose = React.useRef(false);\n const focusableGroupAttribute = useFocusableGroup({\n tabBehavior: 'limited-trap-focus',\n // Users should only use Tab to focus into the toast\n // Escape is already reserved to dismiss all toasts\n ignoreDefaultKeydown: { Tab: true, Escape: true, Enter: true },\n });\n\n const close = useEventCallback(() => {\n const activeElement = targetDocument?.activeElement;\n if (activeElement && toastRef.current?.contains(activeElement)) {\n focusedToastBeforeClose.current = true;\n }\n\n closeProp();\n });\n const onStatusChange = useEventCallback((status: ToastStatus) => props.onStatusChange?.(null, { status, ...props }));\n const pause = useEventCallback(() => setRunning(false));\n const play = useEventCallback(() => {\n if (imperativePauseRef.current) {\n return;\n }\n const containsActive = !!toastRef.current?.contains(targetDocument?.activeElement ?? null);\n if (timerTimeout < 0) {\n setRunning(true);\n return;\n }\n\n if (!containsActive) {\n setRunning(true);\n }\n });\n\n React.useImperativeHandle(imperativeRef, () => ({\n focus: () => {\n if (!toastRef.current) {\n return;\n }\n\n toastRef.current.focus();\n },\n\n play: () => {\n imperativePauseRef.current = false;\n play();\n },\n pause: () => {\n imperativePauseRef.current = true;\n pause();\n },\n }));\n\n React.useEffect(() => {\n return () => onStatusChange('unmounted');\n }, [onStatusChange]);\n\n React.useEffect(() => {\n if (!targetDocument) {\n return;\n }\n\n if (pauseOnWindowBlur) {\n targetDocument.defaultView?.addEventListener('focus', play);\n targetDocument.defaultView?.addEventListener('blur', pause);\n return () => {\n targetDocument.defaultView?.removeEventListener('focus', play);\n targetDocument.defaultView?.removeEventListener('blur', pause);\n };\n }\n }, [targetDocument, pause, play, pauseOnWindowBlur]);\n\n // It's impossible to animate to height: auto in CSS, the actual pixel value must be known\n // Get the height of the toast before animation styles have been applied and set a CSS\n // variable with its height. The CSS variable will be used by the styles\n const onTransitionEntering = () => {\n if (!toastRef.current) {\n return;\n }\n\n const element = toastRef.current;\n element.style.setProperty('--fui-toast-height', `${element.scrollHeight}px`);\n };\n\n // Users never actually use ToastContainer as a JSX but imperatively through useToastContainerController\n const userRootSlot = (data as { root?: ExtractSlotProps<Slot<'div'>> }).root;\n\n // Using a ref callback here because addEventListener supports `once`\n const toastAnimationRef = React.useCallback(\n (el: HTMLDivElement | null) => {\n if (el && toastRef.current) {\n toastRef.current.addEventListener(\n 'animationend',\n () => {\n // start toast once it's fully animated in\n play();\n onStatusChange('visible');\n },\n { once: true },\n );\n }\n },\n [play, onStatusChange],\n );\n\n const onMouseEnter = useEventCallback((e: React.MouseEvent<HTMLDivElement>) => {\n pause();\n userRootSlot?.onMouseEnter?.(e);\n });\n\n const onMouseLeave = useEventCallback((e: React.MouseEvent<HTMLDivElement>) => {\n play();\n userRootSlot?.onMouseEnter?.(e);\n });\n\n const { findFirstFocusable, findLastFocusable } = useFocusFinders();\n const onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLDivElement>) => {\n if (e.key === Delete) {\n e.preventDefault();\n close();\n }\n\n if (e.key === Tab && e.currentTarget === e.target) {\n e.preventDefault();\n if (e.shiftKey) {\n findLastFocusable(e.currentTarget)?.focus();\n } else {\n findFirstFocusable(e.currentTarget)?.focus();\n }\n }\n\n userRootSlot?.onKeyDown?.(e);\n });\n\n React.useEffect(() => {\n if (!visible) {\n return;\n }\n\n const politeness = desiredPoliteness ?? intentPolitenessMap[intent];\n announce(toastRef.current?.textContent ?? '', { politeness });\n }, [announce, desiredPoliteness, toastRef, visible, updateId, intent]);\n\n React.useEffect(() => {\n return () => {\n if (focusedToastBeforeClose.current) {\n focusedToastBeforeClose.current = false;\n tryRestoreFocus();\n }\n };\n }, [tryRestoreFocus]);\n\n return {\n components: {\n timer: Timer,\n root: 'div',\n },\n timer: resolveShorthand<TimerProps>(\n { key: updateId, onTimeout: close, running, timeout: timerTimeout ?? -1 },\n { required: true },\n ),\n root: getNativeElementProps('div', {\n ref: useMergedRefs(ref, toastRef, toastAnimationRef),\n children,\n tabIndex: 0,\n role: 'listitem',\n 'aria-labelledby': titleId,\n 'aria-describedby': bodyId,\n ...rest,\n ...userRootSlot,\n ...focusableGroupAttribute,\n onMouseEnter,\n onMouseLeave,\n onKeyDown,\n }),\n timerTimeout,\n transitionTimeout: 500,\n running,\n visible,\n remove,\n close,\n onTransitionEntering,\n updateId,\n nodeRef: toastRef,\n intent,\n titleId,\n bodyId,\n };\n};\n"],"names":["React","getNativeElementProps","useMergedRefs","useEventCallback","resolveShorthand","useId","useFluent_unstable","Delete","Tab","useFocusableGroup","useFocusFinders","Timer","intentPolitenessMap","success","warning","error","info","useToastContainer_unstable","props","ref","visible","children","close","closeProp","remove","updateId","announce","data","timeout","timerTimeout","politeness","desiredPoliteness","intent","pauseOnHover","pauseOnWindowBlur","imperativeRef","tryRestoreFocus","rest","titleId","bodyId","toastRef","useRef","targetDocument","running","setRunning","useState","imperativePauseRef","focusedToastBeforeClose","focusableGroupAttribute","tabBehavior","ignoreDefaultKeydown","Escape","Enter","activeElement","current","contains","onStatusChange","status","pause","play","containsActive","useImperativeHandle","focus","useEffect","defaultView","addEventListener","removeEventListener","onTransitionEntering","element","style","setProperty","scrollHeight","userRootSlot","root","toastAnimationRef","useCallback","el","once","onMouseEnter","e","onMouseLeave","findFirstFocusable","findLastFocusable","onKeyDown","key","preventDefault","currentTarget","target","shiftKey","textContent","components","timer","onTimeout","required","tabIndex","role","transitionTimeout","nodeRef"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SACEC,qBAAqB,EACrBC,aAAa,EAGbC,gBAAgB,EAChBC,gBAAgB,EAChBC,KAAK,QACA,4BAA4B;AACnC,SAASC,kBAAkB,QAAQ,kCAAkC;AACrE,SAASC,MAAM,EAAEC,GAAG,QAAQ,0BAA0B;AACtD,SAASC,iBAAiB,EAAEC,eAAe,QAAQ,0BAA0B;AAG7E,SAASC,KAAK,QAAoB,iBAAiB;AAEnD,MAAMC,sBAAsB;IAC1BC,SAAS;IACTC,SAAS;IACTC,OAAO;IACPC,MAAM;AACR;AAEA;;;;;;;;CAQC,GACD,OAAO,MAAMC,6BAA6B,CACxCC,OACAC,MACwB;IACxB,MAAM,EACJC,QAAO,EACPC,SAAQ,EACRC,OAAOC,UAAS,EAChBC,OAAM,EACNC,SAAQ,EACRC,SAAQ,EACRC,KAAI,EACJC,SAASC,aAAY,EACrBC,YAAYC,kBAAiB,EAC7BC,QAAS,OAAM,EACfC,aAAY,EACZC,kBAAiB,EACjBC,cAAa,EACbC,gBAAe,EACf,GAAGC,MACJ,GAAGnB;IACJ,MAAMoB,UAAUjC,MAAM;IACtB,MAAMkC,SAASlC,MAAM;IACrB,MAAMmC,WAAWxC,MAAMyC,MAAM,CAAwB,IAAI;IACzD,MAAM,EAAEC,eAAc,EAAE,GAAGpC;IAC3B,MAAM,CAACqC,SAASC,WAAW,GAAG5C,MAAM6C,QAAQ,CAAC,KAAK;IAClD,MAAMC,qBAAqB9C,MAAMyC,MAAM,CAAC,KAAK;IAC7C,MAAMM,0BAA0B/C,MAAMyC,MAAM,CAAC,KAAK;IAClD,MAAMO,0BAA0BvC,kBAAkB;QAChDwC,aAAa;QACb,oDAAoD;QACpD,mDAAmD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AAAA,IAAAA,wBAAAA,KAAAA,cAA0BO;IAC5B;IAEA/E,MAAM+D,SAAS,CAAC,IAAM;YAMXvB;QALT,IAAI,CAACpB,SAAS;YACZ;QACF,CAAC;QAED,MAAMU,aAAaC,8BAAAA,+BAAAA,oBAAqBnB,mBAAmB,CAACoB,OAAO;YAC1DQ;QAATd,SAASc,CAAAA,gCAAAA,CAAAA,oBAAAA,SAASc,OAAO,cAAhBd,+BAAAA,KAAAA,IAAAA,kBAAkBiD,WAAW,cAA7BjD,2CAAAA,gCAAiC,EAAE,EAAE;YAAEV;QAAW;IAC7D,GAAG;QAACJ;QAAUK;QAAmBS;QAAUpB;QAASK;QAAUO;KAAO;IAErEhC,MAAM+D,SAAS,CAAC,IAAM;QACpB,OAAO,IAAM;YACX,IAAIhB,wBAAwBO,OAAO,EAAE;gBACnCP,wBAAwBO,OAAO,GAAG,KAAK;gBACvClB;YACF,CAAC;QACH;IACF,GAAG;QAACA;KAAgB;IAEpB,OAAO;QACLsD,YAAY;YACVC,OAAOhF;YACP8D,MAAM;QACR;QACAkB,OAAOvF,iBACL;YAAEgF,KAAK3D;YAAUmE,WAAWtE;YAAOqB;YAASf,SAASC,yBAAAA,0BAAAA,eAAgB,CAAC,CAAC;QAAC,GACxE;YAAEgE,UAAU,IAAI;QAAC;QAEnBpB,MAAMxE,sBAAsB,OAAO;YACjCkB,KAAKjB,cAAciB,KAAKqB,UAAUkC;YAClCrD;YACAyE,UAAU;YACVC,MAAM;YACN,mBAAmBzD;YACnB,oBAAoBC;YACpB,GAAGF,IAAI;YACP,GAAGmC,YAAY;YACf,GAAGxB,uBAAuB;YAC1B8B;YACAE;YACAG;QACF;QACAtD;QACAmE,mBAAmB;QACnBrD;QACAvB;QACAI;QACAF;QACA6C;QACA1C;QACAwE,SAASzD;QACTR;QACAM;QACAC;IACF;AACF,EAAE"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __resetStyles, __styles, mergeClasses, shorthands } from '@griffel/react';
|
|
2
2
|
import { tokens } from '@fluentui/react-theme';
|
|
3
3
|
import { createCustomFocusIndicatorStyle } from '@fluentui/react-tabster';
|
|
4
|
-
export const
|
|
4
|
+
export const toastContainerClassNames = {
|
|
5
5
|
root: 'fui-ToastContainer',
|
|
6
6
|
timer: 'fui-ToastContainer__timer'
|
|
7
7
|
};
|
|
@@ -30,7 +30,7 @@ const useStyles = /*#__PURE__*/__styles({
|
|
|
30
30
|
export const useToastContainerStyles_unstable = state => {
|
|
31
31
|
const rootBaseClassName = useRootBaseClassName();
|
|
32
32
|
const styles = useStyles();
|
|
33
|
-
state.root.className = mergeClasses(
|
|
33
|
+
state.root.className = mergeClasses(toastContainerClassNames.root, rootBaseClassName, state.visible ? styles.enter : styles.exit, state.root.className);
|
|
34
34
|
return state;
|
|
35
35
|
};
|
|
36
36
|
//# sourceMappingURL=useToastContainerStyles.styles.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["__resetStyles","__styles","mergeClasses","shorthands","tokens","createCustomFocusIndicatorStyle","
|
|
1
|
+
{"version":3,"names":["__resetStyles","__styles","mergeClasses","shorthands","tokens","createCustomFocusIndicatorStyle","toastContainerClassNames","root","timer","useRootBaseClassName","useStyles","enter","vin17d","m1gqa9","Bv12yb3","exit","d","k","useToastContainerStyles_unstable","state","rootBaseClassName","styles","className","visible"],"sources":["useToastContainerStyles.styles.js"],"sourcesContent":["import { makeResetStyles, makeStyles, mergeClasses, shorthands } from '@griffel/react';\nimport { tokens } from '@fluentui/react-theme';\nimport { createCustomFocusIndicatorStyle } from '@fluentui/react-tabster';\nexport const toastContainerClassNames = {\n root: 'fui-ToastContainer',\n timer: 'fui-ToastContainer__timer'\n};\nconst useRootBaseClassName = makeResetStyles({\n boxSizing: 'border-box',\n marginTop: '16px',\n minHeight: '44px',\n pointerEvents: 'all',\n ...shorthands.borderRadius(tokens.borderRadiusMedium),\n '--fui-toast-height': '44px',\n ...createCustomFocusIndicatorStyle({\n ...shorthands.outline(tokens.strokeWidthThick, 'solid', tokens.colorStrokeFocus2)\n })\n});\n/**\n * Styles for the root slot\n */ const useStyles = makeStyles({\n enter: {\n animationDuration: '200ms, 400ms',\n animationDelay: '0ms, 200ms',\n animationName: [\n {\n from: {\n maxHeight: 0,\n opacity: 0,\n marginTop: 0\n },\n to: {\n marginTop: '16px',\n opacity: 0,\n maxHeight: 'var(--fui-toast-height)'\n }\n },\n {\n from: {\n opacity: 0\n },\n to: {\n opacity: 1\n }\n }\n ]\n },\n exit: {\n animationDuration: '400ms, 200ms',\n animationDelay: '0ms, 400ms',\n animationName: [\n {\n from: {\n opacity: 1\n },\n to: {\n opacity: 0\n }\n },\n {\n from: {\n opacity: 0\n },\n to: {\n opacity: 0,\n marginTop: 0,\n maxHeight: 0\n }\n }\n ]\n }\n});\n/**\n * Apply styling to the ToastContainer slots based on the state\n */ export const useToastContainerStyles_unstable = (state)=>{\n const rootBaseClassName = useRootBaseClassName();\n const styles = useStyles();\n state.root.className = mergeClasses(toastContainerClassNames.root, rootBaseClassName, state.visible ? styles.enter : styles.exit, state.root.className);\n return state;\n};\n"],"mappings":"AAAA,SAAAA,aAAA,EAAAC,QAAA,EAAsCC,YAAY,EAAEC,UAAU,QAAQ,gBAAgB;AACtF,SAASC,MAAM,QAAQ,uBAAuB;AAC9C,SAASC,+BAA+B,QAAQ,yBAAyB;AACzE,OAAO,MAAMC,wBAAwB,GAAG;EACpCC,IAAI,EAAE,oBAAoB;EAC1BC,KAAK,EAAE;AACX,CAAC;AACD,MAAMC,oBAAoB,gBAAGT,aAAA,i6BAU5B,CAAC;AACF;AACA;AACA;AAAI,MAAMU,SAAS,gBAAGT,QAAA;EAAAU,KAAA;IAAAC,MAAA;IAAAC,MAAA;IAAAC,OAAA;EAAA;EAAAC,IAAA;IAAAH,MAAA;IAAAC,MAAA;IAAAC,OAAA;EAAA;AAAA;EAAAE,CAAA;EAAAC,CAAA;AAAA,CAmDrB,CAAC;AACF;AACA;AACA;AAAI,OAAO,MAAMC,gCAAgC,GAAIC,KAAK,IAAG;EACzD,MAAMC,iBAAiB,GAAGX,oBAAoB,CAAC,CAAC;EAChD,MAAMY,MAAM,GAAGX,SAAS,CAAC,CAAC;EAC1BS,KAAK,CAACZ,IAAI,CAACe,SAAS,GAAGpB,YAAY,CAACI,wBAAwB,CAACC,IAAI,EAAEa,iBAAiB,EAAED,KAAK,CAACI,OAAO,GAAGF,MAAM,CAACV,KAAK,GAAGU,MAAM,CAACN,IAAI,EAAEI,KAAK,CAACZ,IAAI,CAACe,SAAS,CAAC;EACvJ,OAAOH,KAAK;AAChB,CAAC"}
|
|
@@ -16,7 +16,7 @@ import { useToastContainerContext } from '../../contexts/toastContainerContext';
|
|
|
16
16
|
const child = getTriggerChild(children);
|
|
17
17
|
const handleClick = useEventCallback((e)=>{
|
|
18
18
|
var _child_props, _child_props_onClick;
|
|
19
|
-
(_child_props_onClick =
|
|
19
|
+
(_child_props_onClick = _child_props = child === null || child === void 0 ? void 0 : child.props.onClick) === null || _child_props_onClick === void 0 ? void 0 : _child_props_onClick.call(_child_props, e);
|
|
20
20
|
if (!e.isDefaultPrevented()) {
|
|
21
21
|
close();
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useToastTrigger.ts"],"sourcesContent":["import * as React from 'react';\nimport { applyTriggerPropsToChildren, getTriggerChild, useEventCallback } from '@fluentui/react-utilities';\nimport { useARIAButtonProps } from '@fluentui/react-aria';\nimport type { ToastTriggerProps, ToastTriggerState } from './ToastTrigger.types';\nimport { useToastContainerContext } from '../../contexts/toastContainerContext';\n\n/**\n * A non-visual component that wraps its child\n * and configures them to be the trigger that will close a `Toast`.\n * This component should only accept one child.\n *\n * This component sole purpose is to avoid opting out of the internal controlled open state of a `Toast`\n * Besides being a trigger that closes a toast through context this component doesn't do much,\n * making it basically unnecessary in cases where the trigger is outside of a toast.\n */\nexport const useToastTrigger_unstable = (props: ToastTriggerProps): ToastTriggerState => {\n const { children, disableButtonEnhancement = false } = props;\n const { close } = useToastContainerContext();\n\n const child = getTriggerChild(children);\n\n const handleClick = useEventCallback(\n (e: React.MouseEvent<HTMLButtonElement & HTMLAnchorElement & HTMLDivElement>) => {\n child?.props.onClick?.(e);\n if (!e.isDefaultPrevented()) {\n close();\n }\n },\n );\n\n const triggerChildProps = {\n ...child?.props,\n ref: child?.ref,\n onClick: handleClick,\n };\n\n const ariaButtonTriggerChildProps = useARIAButtonProps(\n child?.type === 'button' || child?.type === 'a' ? child.type : 'div',\n {\n ...triggerChildProps,\n type: 'button',\n },\n );\n\n return {\n children: applyTriggerPropsToChildren(\n children,\n disableButtonEnhancement ? triggerChildProps : ariaButtonTriggerChildProps,\n ),\n };\n};\n"],"names":["React","applyTriggerPropsToChildren","getTriggerChild","useEventCallback","useARIAButtonProps","useToastContainerContext","useToastTrigger_unstable","props","children","disableButtonEnhancement","close","child","handleClick","e","onClick","isDefaultPrevented","triggerChildProps","ref","ariaButtonTriggerChildProps","type"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,2BAA2B,EAAEC,eAAe,EAAEC,gBAAgB,QAAQ,4BAA4B;AAC3G,SAASC,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,wBAAwB,QAAQ,uCAAuC;AAEhF;;;;;;;;CAQC,GACD,OAAO,MAAMC,2BAA2B,CAACC,QAAgD;IACvF,MAAM,EAAEC,SAAQ,EAAEC,0BAA2B,KAAK,CAAA,EAAE,GAAGF;IACvD,MAAM,EAAEG,MAAK,EAAE,GAAGL;IAElB,MAAMM,QAAQT,gBAAgBM;IAE9B,MAAMI,cAAcT,iBAClB,CAACU,IAAgF;YAC/EF,cAAAA;QAAAA,CAAAA,uBAAAA,
|
|
1
|
+
{"version":3,"sources":["useToastTrigger.ts"],"sourcesContent":["import * as React from 'react';\nimport { applyTriggerPropsToChildren, getTriggerChild, useEventCallback } from '@fluentui/react-utilities';\nimport { useARIAButtonProps } from '@fluentui/react-aria';\nimport type { ToastTriggerProps, ToastTriggerState } from './ToastTrigger.types';\nimport { useToastContainerContext } from '../../contexts/toastContainerContext';\n\n/**\n * A non-visual component that wraps its child\n * and configures them to be the trigger that will close a `Toast`.\n * This component should only accept one child.\n *\n * This component sole purpose is to avoid opting out of the internal controlled open state of a `Toast`\n * Besides being a trigger that closes a toast through context this component doesn't do much,\n * making it basically unnecessary in cases where the trigger is outside of a toast.\n */\nexport const useToastTrigger_unstable = (props: ToastTriggerProps): ToastTriggerState => {\n const { children, disableButtonEnhancement = false } = props;\n const { close } = useToastContainerContext();\n\n const child = getTriggerChild(children);\n\n const handleClick = useEventCallback(\n (e: React.MouseEvent<HTMLButtonElement & HTMLAnchorElement & HTMLDivElement>) => {\n child?.props.onClick?.(e);\n if (!e.isDefaultPrevented()) {\n close();\n }\n },\n );\n\n const triggerChildProps = {\n ...child?.props,\n ref: child?.ref,\n onClick: handleClick,\n };\n\n const ariaButtonTriggerChildProps = useARIAButtonProps(\n child?.type === 'button' || child?.type === 'a' ? child.type : 'div',\n {\n ...triggerChildProps,\n type: 'button',\n },\n );\n\n return {\n children: applyTriggerPropsToChildren(\n children,\n disableButtonEnhancement ? triggerChildProps : ariaButtonTriggerChildProps,\n ),\n };\n};\n"],"names":["React","applyTriggerPropsToChildren","getTriggerChild","useEventCallback","useARIAButtonProps","useToastContainerContext","useToastTrigger_unstable","props","children","disableButtonEnhancement","close","child","handleClick","e","onClick","isDefaultPrevented","triggerChildProps","ref","ariaButtonTriggerChildProps","type"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,2BAA2B,EAAEC,eAAe,EAAEC,gBAAgB,QAAQ,4BAA4B;AAC3G,SAASC,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,wBAAwB,QAAQ,uCAAuC;AAEhF;;;;;;;;CAQC,GACD,OAAO,MAAMC,2BAA2B,CAACC,QAAgD;IACvF,MAAM,EAAEC,SAAQ,EAAEC,0BAA2B,KAAK,CAAA,EAAE,GAAGF;IACvD,MAAM,EAAEG,MAAK,EAAE,GAAGL;IAElB,MAAMM,QAAQT,gBAAgBM;IAE9B,MAAMI,cAAcT,iBAClB,CAACU,IAAgF;YAC/EF,cAAAA;QAAAA,CAAAA,uBAAAA,eAAAA,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAOJ,KAAK,CAACO,OAAO,cAApBH,kCAAAA,KAAAA,IAAAA,qBAAAA,KAAAA,cAAuBE;QACvB,IAAI,CAACA,EAAEE,kBAAkB,IAAI;YAC3BL;QACF,CAAC;IACH;IAGF,MAAMM,oBAAoB;QACxB,GAAGL,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAOJ,KAAK;QACfU,KAAKN,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAOM,GAAG;QACfH,SAASF;IACX;IAEA,MAAMM,8BAA8Bd,mBAClCO,CAAAA,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAOQ,IAAI,AAAD,MAAM,YAAYR,CAAAA,kBAAAA,mBAAAA,KAAAA,IAAAA,MAAOQ,IAAI,AAAD,MAAM,MAAMR,MAAMQ,IAAI,GAAG,KAAK,EACpE;QACE,GAAGH,iBAAiB;QACpBG,MAAM;IACR;IAGF,OAAO;QACLX,UAAUP,4BACRO,UACAC,2BAA2BO,oBAAoBE,2BAA2B;IAE9E;AACF,EAAE"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { isHTMLElement } from '@fluentui/react-utilities';
|
|
3
|
+
/**
|
|
4
|
+
* Wraps an aria live announcement function.
|
|
5
|
+
* Aria live announcements can be detrimental once the user is already navigating
|
|
6
|
+
* multiple toasts. Once the user is focused inside the toaster, the announecments should be disabled.
|
|
7
|
+
* @param announce
|
|
8
|
+
* @returns A function to announce a toast and a ref to attach to the toaster element
|
|
9
|
+
*/ export function useToastAnnounce(announce) {
|
|
10
|
+
const activeRef = React.useRef(true);
|
|
11
|
+
const cleanupRef = React.useRef(()=>undefined);
|
|
12
|
+
const announceToast = React.useCallback((message, options)=>{
|
|
13
|
+
if (activeRef.current) {
|
|
14
|
+
announce(message, options);
|
|
15
|
+
}
|
|
16
|
+
}, [
|
|
17
|
+
announce
|
|
18
|
+
]);
|
|
19
|
+
const toasterRef = React.useCallback((el)=>{
|
|
20
|
+
if (!el) {
|
|
21
|
+
cleanupRef.current();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const onFocusIn = (e)=>{
|
|
25
|
+
if (isHTMLElement(e.currentTarget) && e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
activeRef.current = false;
|
|
29
|
+
};
|
|
30
|
+
const onFocusOut = (e)=>{
|
|
31
|
+
if (isHTMLElement(e.currentTarget) && e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
activeRef.current = true;
|
|
35
|
+
};
|
|
36
|
+
el.addEventListener('focusin', onFocusIn);
|
|
37
|
+
el.addEventListener('focusout', onFocusOut);
|
|
38
|
+
cleanupRef.current = ()=>{
|
|
39
|
+
el.removeEventListener('focusin', onFocusIn);
|
|
40
|
+
el.removeEventListener('focusout', onFocusOut);
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
43
|
+
return {
|
|
44
|
+
announceToast,
|
|
45
|
+
toasterRef
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useToastAnnounce.ts"],"sourcesContent":["import * as React from 'react';\nimport { Announce } from '../AriaLive';\nimport { isHTMLElement } from '@fluentui/react-utilities';\n\n/**\n * Wraps an aria live announcement function.\n * Aria live announcements can be detrimental once the user is already navigating\n * multiple toasts. Once the user is focused inside the toaster, the announecments should be disabled.\n * @param announce\n * @returns A function to announce a toast and a ref to attach to the toaster element\n */\nexport function useToastAnnounce(announce: Announce) {\n const activeRef = React.useRef(true);\n const cleanupRef = React.useRef<() => void>(() => undefined);\n const announceToast = React.useCallback<Announce>(\n (message, options) => {\n if (activeRef.current) {\n announce(message, options);\n }\n },\n [announce],\n );\n\n const toasterRef = React.useCallback((el: HTMLDivElement | null) => {\n if (!el) {\n cleanupRef.current();\n return;\n }\n\n const onFocusIn = (e: FocusEvent) => {\n if (\n isHTMLElement(e.currentTarget) &&\n e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)\n ) {\n return;\n }\n\n activeRef.current = false;\n };\n\n const onFocusOut = (e: FocusEvent) => {\n if (\n isHTMLElement(e.currentTarget) &&\n e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)\n ) {\n return;\n }\n\n activeRef.current = true;\n };\n\n el.addEventListener('focusin', onFocusIn);\n el.addEventListener('focusout', onFocusOut);\n\n cleanupRef.current = () => {\n el.removeEventListener('focusin', onFocusIn);\n el.removeEventListener('focusout', onFocusOut);\n };\n }, []);\n\n return {\n announceToast,\n toasterRef,\n };\n}\n"],"names":["React","isHTMLElement","useToastAnnounce","announce","activeRef","useRef","cleanupRef","undefined","announceToast","useCallback","message","options","current","toasterRef","el","onFocusIn","e","currentTarget","contains","relatedTarget","onFocusOut","addEventListener","removeEventListener"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAE/B,SAASC,aAAa,QAAQ,4BAA4B;AAE1D;;;;;;CAMC,GACD,OAAO,SAASC,iBAAiBC,QAAkB,EAAE;IACnD,MAAMC,YAAYJ,MAAMK,MAAM,CAAC,IAAI;IACnC,MAAMC,aAAaN,MAAMK,MAAM,CAAa,IAAME;IAClD,MAAMC,gBAAgBR,MAAMS,WAAW,CACrC,CAACC,SAASC,UAAY;QACpB,IAAIP,UAAUQ,OAAO,EAAE;YACrBT,SAASO,SAASC;QACpB,CAAC;IACH,GACA;QAACR;KAAS;IAGZ,MAAMU,aAAab,MAAMS,WAAW,CAAC,CAACK,KAA8B;QAClE,IAAI,CAACA,IAAI;YACPR,WAAWM,OAAO;YAClB;QACF,CAAC;QAED,MAAMG,YAAY,CAACC,IAAkB;YACnC,IACEf,cAAce,EAAEC,aAAa,KAC7BD,EAAEC,aAAa,CAACC,QAAQ,CAACjB,cAAce,EAAEG,aAAa,IAAIH,EAAEG,aAAa,GAAG,IAAI,GAChF;gBACA;YACF,CAAC;YAEDf,UAAUQ,OAAO,GAAG,KAAK;QAC3B;QAEA,MAAMQ,aAAa,CAACJ,IAAkB;YACpC,IACEf,cAAce,EAAEC,aAAa,KAC7BD,EAAEC,aAAa,CAACC,QAAQ,CAACjB,cAAce,EAAEG,aAAa,IAAIH,EAAEG,aAAa,GAAG,IAAI,GAChF;gBACA;YACF,CAAC;YAEDf,UAAUQ,OAAO,GAAG,IAAI;QAC1B;QAEAE,GAAGO,gBAAgB,CAAC,WAAWN;QAC/BD,GAAGO,gBAAgB,CAAC,YAAYD;QAEhCd,WAAWM,OAAO,GAAG,IAAM;YACzBE,GAAGQ,mBAAmB,CAAC,WAAWP;YAClCD,GAAGQ,mBAAmB,CAAC,YAAYF;QACrC;IACF,GAAG,EAAE;IAEL,OAAO;QACLZ;QACAK;IACF;AACF,CAAC"}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { getNativeElementProps,
|
|
2
|
+
import { getNativeElementProps, resolveShorthand, useEventCallback, useMergedRefs } from '@fluentui/react-utilities';
|
|
3
3
|
import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
|
|
4
|
+
import { useFocusableGroup } from '@fluentui/react-tabster';
|
|
5
|
+
import { Escape } from '@fluentui/keyboard-keys';
|
|
4
6
|
import { TOAST_POSITIONS, useToaster } from '../../state';
|
|
5
7
|
import { ToastContainer } from '../ToastContainer';
|
|
8
|
+
import { useToasterFocusManagement_unstable } from './useToasterFocusManagement';
|
|
9
|
+
import { useToastAnnounce } from './useToastAnnounce';
|
|
6
10
|
/**
|
|
7
11
|
* Create the state required to render Toaster.
|
|
8
12
|
*
|
|
@@ -10,45 +14,43 @@ import { ToastContainer } from '../ToastContainer';
|
|
|
10
14
|
*/ export const useToaster_unstable = (props)=>{
|
|
11
15
|
const { offset , announce: announceProp , ...rest } = props;
|
|
12
16
|
const announceRef = React.useRef(()=>null);
|
|
13
|
-
const { toastsToRender , isToastVisible , pauseAllToasts , playAllToasts , tryRestoreFocus } = useToaster(rest);
|
|
17
|
+
const { toastsToRender , isToastVisible , pauseAllToasts , playAllToasts , tryRestoreFocus , closeAllToasts } = useToaster(rest);
|
|
14
18
|
const announce = React.useCallback((message, options)=>announceRef.current(message, options), []);
|
|
15
19
|
const { dir } = useFluent();
|
|
16
20
|
const rootProps = getNativeElementProps('div', rest);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
el.addEventListener('focusin', (e)=>{
|
|
22
|
-
if (isHTMLElement(e.currentTarget) && !e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)) {
|
|
23
|
-
pauseAllToasts();
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
el.addEventListener('focusout', (e)=>{
|
|
27
|
-
if (isHTMLElement(e.currentTarget) && !e.currentTarget.contains(isHTMLElement(e.relatedTarget) ? e.relatedTarget : null)) {
|
|
28
|
-
playAllToasts();
|
|
29
|
-
tryRestoreFocus();
|
|
30
|
-
}
|
|
31
|
-
});
|
|
21
|
+
const focusableGroupAttr = useFocusableGroup({
|
|
22
|
+
tabBehavior: 'limited-trap-focus',
|
|
23
|
+
ignoreDefaultKeydown: {
|
|
24
|
+
Escape: true
|
|
32
25
|
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
});
|
|
27
|
+
const onKeyDown = useEventCallback((e)=>{
|
|
28
|
+
var _props_onKeyDown;
|
|
29
|
+
if (e.key === Escape) {
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
closeAllToasts();
|
|
32
|
+
}
|
|
33
|
+
(_props_onKeyDown = props.onKeyDown) === null || _props_onKeyDown === void 0 ? void 0 : _props_onKeyDown.call(props, e);
|
|
34
|
+
});
|
|
35
|
+
const usePositionSlot = (toastPosition)=>{
|
|
39
36
|
var _toastsToRender_get;
|
|
37
|
+
const focusManagementRef = useToasterFocusManagement_unstable(pauseAllToasts, playAllToasts);
|
|
38
|
+
const { announceToast , toasterRef } = useToastAnnounce(announceProp !== null && announceProp !== void 0 ? announceProp : announce);
|
|
40
39
|
return resolveShorthand(toastsToRender.has(toastPosition) ? rootProps : null, {
|
|
41
40
|
defaultProps: {
|
|
42
|
-
ref:
|
|
41
|
+
ref: useMergedRefs(focusManagementRef, toasterRef),
|
|
43
42
|
children: (_toastsToRender_get = toastsToRender.get(toastPosition)) === null || _toastsToRender_get === void 0 ? void 0 : _toastsToRender_get.map((toast)=>/*#__PURE__*/ React.createElement(ToastContainer, {
|
|
44
43
|
...toast,
|
|
45
44
|
tryRestoreFocus: tryRestoreFocus,
|
|
46
45
|
intent: toast.intent,
|
|
47
|
-
announce:
|
|
46
|
+
announce: announceToast,
|
|
48
47
|
key: toast.toastId,
|
|
49
48
|
visible: isToastVisible(toast.toastId)
|
|
50
49
|
}, toast.content)),
|
|
51
|
-
|
|
50
|
+
onKeyDown,
|
|
51
|
+
...focusableGroupAttr,
|
|
52
|
+
'data-toaster-position': toastPosition,
|
|
53
|
+
role: 'list'
|
|
52
54
|
}
|
|
53
55
|
});
|
|
54
56
|
};
|
|
@@ -64,10 +66,10 @@ import { ToastContainer } from '../ToastContainer';
|
|
|
64
66
|
root: resolveShorthand(rootProps, {
|
|
65
67
|
required: true
|
|
66
68
|
}),
|
|
67
|
-
bottomStart:
|
|
68
|
-
bottomEnd:
|
|
69
|
-
topStart:
|
|
70
|
-
topEnd:
|
|
69
|
+
bottomStart: usePositionSlot(TOAST_POSITIONS.bottomStart),
|
|
70
|
+
bottomEnd: usePositionSlot(TOAST_POSITIONS.bottomEnd),
|
|
71
|
+
topStart: usePositionSlot(TOAST_POSITIONS.topStart),
|
|
72
|
+
topEnd: usePositionSlot(TOAST_POSITIONS.topEnd),
|
|
71
73
|
announceRef,
|
|
72
74
|
offset,
|
|
73
75
|
announce: announceProp !== null && announceProp !== void 0 ? announceProp : announce,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useToaster.tsx"],"sourcesContent":["import * as React from 'react';\nimport {\n ExtractSlotProps,\n Slot,\n getNativeElementProps,\n
|
|
1
|
+
{"version":3,"sources":["useToaster.tsx"],"sourcesContent":["import * as React from 'react';\nimport {\n ExtractSlotProps,\n Slot,\n getNativeElementProps,\n resolveShorthand,\n useEventCallback,\n useMergedRefs,\n} from '@fluentui/react-utilities';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { useFocusableGroup } from '@fluentui/react-tabster';\nimport { Escape } from '@fluentui/keyboard-keys';\nimport type { ToasterProps, ToasterState } from './Toaster.types';\nimport { TOAST_POSITIONS, ToastPosition, useToaster } from '../../state';\nimport { Announce } from '../AriaLive';\nimport { ToastContainer } from '../ToastContainer';\nimport { useToasterFocusManagement_unstable } from './useToasterFocusManagement';\nimport { useToastAnnounce } from './useToastAnnounce';\n\n/**\n * Create the state required to render Toaster.\n *\n * @param props - props from this instance of Toaster\n */\nexport const useToaster_unstable = (props: ToasterProps): ToasterState => {\n const { offset, announce: announceProp, ...rest } = props;\n const announceRef = React.useRef<Announce>(() => null);\n const { toastsToRender, isToastVisible, pauseAllToasts, playAllToasts, tryRestoreFocus, closeAllToasts } =\n useToaster<HTMLDivElement>(rest);\n const announce = React.useCallback<Announce>((message, options) => announceRef.current(message, options), []);\n const { dir } = useFluent();\n\n const rootProps = getNativeElementProps('div', rest);\n const focusableGroupAttr = useFocusableGroup({\n tabBehavior: 'limited-trap-focus',\n ignoreDefaultKeydown: { Escape: true },\n });\n\n const onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLDivElement>) => {\n if (e.key === Escape) {\n e.preventDefault();\n closeAllToasts();\n }\n\n props.onKeyDown?.(e);\n });\n\n const usePositionSlot = (toastPosition: ToastPosition) => {\n const focusManagementRef = useToasterFocusManagement_unstable(pauseAllToasts, playAllToasts);\n const { announceToast, toasterRef } = useToastAnnounce(announceProp ?? announce);\n return resolveShorthand(toastsToRender.has(toastPosition) ? rootProps : null, {\n defaultProps: {\n ref: useMergedRefs(focusManagementRef, toasterRef),\n children: toastsToRender.get(toastPosition)?.map(toast => (\n <ToastContainer\n {...toast}\n tryRestoreFocus={tryRestoreFocus}\n intent={toast.intent}\n announce={announceToast}\n key={toast.toastId}\n visible={isToastVisible(toast.toastId)}\n >\n {toast.content as React.ReactNode}\n </ToastContainer>\n )),\n onKeyDown,\n ...focusableGroupAttr,\n 'data-toaster-position': toastPosition,\n role: 'list',\n // Explicitly casting because our slot types can't handle data attributes\n } as ExtractSlotProps<Slot<'div'>>,\n });\n };\n\n return {\n dir,\n components: {\n root: 'div',\n bottomStart: 'div',\n bottomEnd: 'div',\n topStart: 'div',\n topEnd: 'div',\n },\n root: resolveShorthand(rootProps, { required: true }),\n bottomStart: usePositionSlot(TOAST_POSITIONS.bottomStart),\n bottomEnd: usePositionSlot(TOAST_POSITIONS.bottomEnd),\n topStart: usePositionSlot(TOAST_POSITIONS.topStart),\n topEnd: usePositionSlot(TOAST_POSITIONS.topEnd),\n announceRef,\n offset,\n announce: announceProp ?? announce,\n renderAriaLive: !announceProp,\n };\n};\n"],"names":["React","getNativeElementProps","resolveShorthand","useEventCallback","useMergedRefs","useFluent_unstable","useFluent","useFocusableGroup","Escape","TOAST_POSITIONS","useToaster","ToastContainer","useToasterFocusManagement_unstable","useToastAnnounce","useToaster_unstable","props","offset","announce","announceProp","rest","announceRef","useRef","toastsToRender","isToastVisible","pauseAllToasts","playAllToasts","tryRestoreFocus","closeAllToasts","useCallback","message","options","current","dir","rootProps","focusableGroupAttr","tabBehavior","ignoreDefaultKeydown","onKeyDown","e","key","preventDefault","usePositionSlot","toastPosition","focusManagementRef","announceToast","toasterRef","has","defaultProps","ref","children","get","map","toast","intent","toastId","visible","content","role","components","root","bottomStart","bottomEnd","topStart","topEnd","required","renderAriaLive"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAGEC,qBAAqB,EACrBC,gBAAgB,EAChBC,gBAAgB,EAChBC,aAAa,QACR,4BAA4B;AACnC,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAClF,SAASC,iBAAiB,QAAQ,0BAA0B;AAC5D,SAASC,MAAM,QAAQ,0BAA0B;AAEjD,SAASC,eAAe,EAAiBC,UAAU,QAAQ,cAAc;AAEzE,SAASC,cAAc,QAAQ,oBAAoB;AACnD,SAASC,kCAAkC,QAAQ,8BAA8B;AACjF,SAASC,gBAAgB,QAAQ,qBAAqB;AAEtD;;;;CAIC,GACD,OAAO,MAAMC,sBAAsB,CAACC,QAAsC;IACxE,MAAM,EAAEC,OAAM,EAAEC,UAAUC,aAAY,EAAE,GAAGC,MAAM,GAAGJ;IACpD,MAAMK,cAAcpB,MAAMqB,MAAM,CAAW,IAAM,IAAI;IACrD,MAAM,EAAEC,eAAc,EAAEC,eAAc,EAAEC,eAAc,EAAEC,cAAa,EAAEC,gBAAe,EAAEC,eAAc,EAAE,GACtGjB,WAA2BS;IAC7B,MAAMF,WAAWjB,MAAM4B,WAAW,CAAW,CAACC,SAASC,UAAYV,YAAYW,OAAO,CAACF,SAASC,UAAU,EAAE;IAC5G,MAAM,EAAEE,IAAG,EAAE,GAAG1B;IAEhB,MAAM2B,YAAYhC,sBAAsB,OAAOkB;IAC/C,MAAMe,qBAAqB3B,kBAAkB;QAC3C4B,aAAa;QACbC,sBAAsB;YAAE5B,QAAQ,IAAI;QAAC;IACvC;IAEA,MAAM6B,YAAYlC,iBAAiB,CAACmC,IAA2C;YAM7EvB;QALA,IAAIuB,EAAEC,GAAG,KAAK/B,QAAQ;YACpB8B,EAAEE,cAAc;YAChBb;QACF,CAAC;QAEDZ,CAAAA,mBAAAA,MAAMsB,SAAS,cAAftB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