@react-aria/toast 3.0.10 → 3.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/dist/import.mjs +2 -4
- package/dist/main.js +3 -5
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -4
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/package.json +15 -18
- package/src/index.ts +2 -4
- package/dist/ar-AE.main.js +0 -10
- package/dist/ar-AE.main.js.map +0 -1
- package/dist/ar-AE.mjs +0 -12
- package/dist/ar-AE.module.js +0 -12
- package/dist/ar-AE.module.js.map +0 -1
- package/dist/bg-BG.main.js +0 -10
- package/dist/bg-BG.main.js.map +0 -1
- package/dist/bg-BG.mjs +0 -12
- package/dist/bg-BG.module.js +0 -12
- package/dist/bg-BG.module.js.map +0 -1
- package/dist/cs-CZ.main.js +0 -10
- package/dist/cs-CZ.main.js.map +0 -1
- package/dist/cs-CZ.mjs +0 -12
- package/dist/cs-CZ.module.js +0 -12
- package/dist/cs-CZ.module.js.map +0 -1
- package/dist/da-DK.main.js +0 -10
- package/dist/da-DK.main.js.map +0 -1
- package/dist/da-DK.mjs +0 -12
- package/dist/da-DK.module.js +0 -12
- package/dist/da-DK.module.js.map +0 -1
- package/dist/de-DE.main.js +0 -10
- package/dist/de-DE.main.js.map +0 -1
- package/dist/de-DE.mjs +0 -12
- package/dist/de-DE.module.js +0 -12
- package/dist/de-DE.module.js.map +0 -1
- package/dist/el-GR.main.js +0 -10
- package/dist/el-GR.main.js.map +0 -1
- package/dist/el-GR.mjs +0 -12
- package/dist/el-GR.module.js +0 -12
- package/dist/el-GR.module.js.map +0 -1
- package/dist/en-US.main.js +0 -10
- package/dist/en-US.main.js.map +0 -1
- package/dist/en-US.mjs +0 -12
- package/dist/en-US.module.js +0 -12
- package/dist/en-US.module.js.map +0 -1
- package/dist/es-ES.main.js +0 -10
- package/dist/es-ES.main.js.map +0 -1
- package/dist/es-ES.mjs +0 -12
- package/dist/es-ES.module.js +0 -12
- package/dist/es-ES.module.js.map +0 -1
- package/dist/et-EE.main.js +0 -10
- package/dist/et-EE.main.js.map +0 -1
- package/dist/et-EE.mjs +0 -12
- package/dist/et-EE.module.js +0 -12
- package/dist/et-EE.module.js.map +0 -1
- package/dist/fi-FI.main.js +0 -10
- package/dist/fi-FI.main.js.map +0 -1
- package/dist/fi-FI.mjs +0 -12
- package/dist/fi-FI.module.js +0 -12
- package/dist/fi-FI.module.js.map +0 -1
- package/dist/fr-FR.main.js +0 -10
- package/dist/fr-FR.main.js.map +0 -1
- package/dist/fr-FR.mjs +0 -12
- package/dist/fr-FR.module.js +0 -12
- package/dist/fr-FR.module.js.map +0 -1
- package/dist/he-IL.main.js +0 -10
- package/dist/he-IL.main.js.map +0 -1
- package/dist/he-IL.mjs +0 -12
- package/dist/he-IL.module.js +0 -12
- package/dist/he-IL.module.js.map +0 -1
- package/dist/hr-HR.main.js +0 -10
- package/dist/hr-HR.main.js.map +0 -1
- package/dist/hr-HR.mjs +0 -12
- package/dist/hr-HR.module.js +0 -12
- package/dist/hr-HR.module.js.map +0 -1
- package/dist/hu-HU.main.js +0 -10
- package/dist/hu-HU.main.js.map +0 -1
- package/dist/hu-HU.mjs +0 -12
- package/dist/hu-HU.module.js +0 -12
- package/dist/hu-HU.module.js.map +0 -1
- package/dist/intlStrings.main.js +0 -108
- package/dist/intlStrings.main.js.map +0 -1
- package/dist/intlStrings.mjs +0 -110
- package/dist/intlStrings.module.js +0 -110
- package/dist/intlStrings.module.js.map +0 -1
- package/dist/it-IT.main.js +0 -10
- package/dist/it-IT.main.js.map +0 -1
- package/dist/it-IT.mjs +0 -12
- package/dist/it-IT.module.js +0 -12
- package/dist/it-IT.module.js.map +0 -1
- package/dist/ja-JP.main.js +0 -10
- package/dist/ja-JP.main.js.map +0 -1
- package/dist/ja-JP.mjs +0 -12
- package/dist/ja-JP.module.js +0 -12
- package/dist/ja-JP.module.js.map +0 -1
- package/dist/ko-KR.main.js +0 -10
- package/dist/ko-KR.main.js.map +0 -1
- package/dist/ko-KR.mjs +0 -12
- package/dist/ko-KR.module.js +0 -12
- package/dist/ko-KR.module.js.map +0 -1
- package/dist/lt-LT.main.js +0 -10
- package/dist/lt-LT.main.js.map +0 -1
- package/dist/lt-LT.mjs +0 -12
- package/dist/lt-LT.module.js +0 -12
- package/dist/lt-LT.module.js.map +0 -1
- package/dist/lv-LV.main.js +0 -10
- package/dist/lv-LV.main.js.map +0 -1
- package/dist/lv-LV.mjs +0 -12
- package/dist/lv-LV.module.js +0 -12
- package/dist/lv-LV.module.js.map +0 -1
- package/dist/nb-NO.main.js +0 -10
- package/dist/nb-NO.main.js.map +0 -1
- package/dist/nb-NO.mjs +0 -12
- package/dist/nb-NO.module.js +0 -12
- package/dist/nb-NO.module.js.map +0 -1
- package/dist/nl-NL.main.js +0 -10
- package/dist/nl-NL.main.js.map +0 -1
- package/dist/nl-NL.mjs +0 -12
- package/dist/nl-NL.module.js +0 -12
- package/dist/nl-NL.module.js.map +0 -1
- package/dist/pl-PL.main.js +0 -12
- package/dist/pl-PL.main.js.map +0 -1
- package/dist/pl-PL.mjs +0 -14
- package/dist/pl-PL.module.js +0 -14
- package/dist/pl-PL.module.js.map +0 -1
- package/dist/pt-BR.main.js +0 -10
- package/dist/pt-BR.main.js.map +0 -1
- package/dist/pt-BR.mjs +0 -12
- package/dist/pt-BR.module.js +0 -12
- package/dist/pt-BR.module.js.map +0 -1
- package/dist/pt-PT.main.js +0 -10
- package/dist/pt-PT.main.js.map +0 -1
- package/dist/pt-PT.mjs +0 -12
- package/dist/pt-PT.module.js +0 -12
- package/dist/pt-PT.module.js.map +0 -1
- package/dist/ro-RO.main.js +0 -10
- package/dist/ro-RO.main.js.map +0 -1
- package/dist/ro-RO.mjs +0 -12
- package/dist/ro-RO.module.js +0 -12
- package/dist/ro-RO.module.js.map +0 -1
- package/dist/ru-RU.main.js +0 -10
- package/dist/ru-RU.main.js.map +0 -1
- package/dist/ru-RU.mjs +0 -12
- package/dist/ru-RU.module.js +0 -12
- package/dist/ru-RU.module.js.map +0 -1
- package/dist/sk-SK.main.js +0 -11
- package/dist/sk-SK.main.js.map +0 -1
- package/dist/sk-SK.mjs +0 -13
- package/dist/sk-SK.module.js +0 -13
- package/dist/sk-SK.module.js.map +0 -1
- package/dist/sl-SI.main.js +0 -12
- package/dist/sl-SI.main.js.map +0 -1
- package/dist/sl-SI.mjs +0 -14
- package/dist/sl-SI.module.js +0 -14
- package/dist/sl-SI.module.js.map +0 -1
- package/dist/sr-SP.main.js +0 -10
- package/dist/sr-SP.main.js.map +0 -1
- package/dist/sr-SP.mjs +0 -12
- package/dist/sr-SP.module.js +0 -12
- package/dist/sr-SP.module.js.map +0 -1
- package/dist/sv-SE.main.js +0 -10
- package/dist/sv-SE.main.js.map +0 -1
- package/dist/sv-SE.mjs +0 -12
- package/dist/sv-SE.module.js +0 -12
- package/dist/sv-SE.module.js.map +0 -1
- package/dist/tr-TR.main.js +0 -10
- package/dist/tr-TR.main.js.map +0 -1
- package/dist/tr-TR.mjs +0 -12
- package/dist/tr-TR.module.js +0 -12
- package/dist/tr-TR.module.js.map +0 -1
- package/dist/types.d.ts +0 -42
- package/dist/types.d.ts.map +0 -1
- package/dist/uk-UA.main.js +0 -10
- package/dist/uk-UA.main.js.map +0 -1
- package/dist/uk-UA.mjs +0 -12
- package/dist/uk-UA.module.js +0 -12
- package/dist/uk-UA.module.js.map +0 -1
- package/dist/useToast.main.js +0 -83
- package/dist/useToast.main.js.map +0 -1
- package/dist/useToast.mjs +0 -78
- package/dist/useToast.module.js +0 -78
- package/dist/useToast.module.js.map +0 -1
- package/dist/useToastRegion.main.js +0 -184
- package/dist/useToastRegion.main.js.map +0 -1
- package/dist/useToastRegion.mjs +0 -179
- package/dist/useToastRegion.module.js +0 -179
- package/dist/useToastRegion.module.js.map +0 -1
- package/dist/zh-CN.main.js +0 -10
- package/dist/zh-CN.main.js.map +0 -1
- package/dist/zh-CN.mjs +0 -12
- package/dist/zh-CN.module.js +0 -12
- package/dist/zh-CN.module.js.map +0 -1
- package/dist/zh-TW.main.js +0 -10
- package/dist/zh-TW.main.js.map +0 -1
- package/dist/zh-TW.mjs +0 -12
- package/dist/zh-TW.module.js +0 -12
- package/dist/zh-TW.module.js.map +0 -1
- package/src/useToast.ts +0 -102
- package/src/useToastRegion.ts +0 -201
package/dist/zh-TW.main.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAAA,iBAAiB;IAAG,SAAS,CAAC,gBAAE,CAAC;IAC/B,iBAAiB,CAAC,MAAM,YAAc,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE;YAAC,KAAK,IAAM,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE,yBAAI,CAAC;YAAE,OAAO,IAAM,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE,yBAAI,CAAC;QAAA,GAAG,QAAC,CAAC;AAClL","sources":["packages/@react-aria/toast/intl/zh-TW.json"],"sourcesContent":["{\n \"close\": \"關閉\",\n \"notifications\": \"{count, plural, one {# 個通知} other {# 個通知}}。\"\n}\n"],"names":[],"version":3,"file":"zh-TW.main.js.map"}
|
package/dist/zh-TW.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
var $52e8b1b00094e5a3$exports = {};
|
|
2
|
-
$52e8b1b00094e5a3$exports = {
|
|
3
|
-
"close": `\u{95DC}\u{9589}`,
|
|
4
|
-
"notifications": (args, formatter)=>`${formatter.plural(args.count, {
|
|
5
|
-
one: ()=>`${formatter.number(args.count)} \u{500B}\u{901A}\u{77E5}`,
|
|
6
|
-
other: ()=>`${formatter.number(args.count)} \u{500B}\u{901A}\u{77E5}`
|
|
7
|
-
})}\u{3002}`
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export {$52e8b1b00094e5a3$exports as default};
|
|
12
|
-
//# sourceMappingURL=zh-TW.module.js.map
|
package/dist/zh-TW.module.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
var $52e8b1b00094e5a3$exports = {};
|
|
2
|
-
$52e8b1b00094e5a3$exports = {
|
|
3
|
-
"close": `\u{95DC}\u{9589}`,
|
|
4
|
-
"notifications": (args, formatter)=>`${formatter.plural(args.count, {
|
|
5
|
-
one: ()=>`${formatter.number(args.count)} \u{500B}\u{901A}\u{77E5}`,
|
|
6
|
-
other: ()=>`${formatter.number(args.count)} \u{500B}\u{901A}\u{77E5}`
|
|
7
|
-
})}\u{3002}`
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export {$52e8b1b00094e5a3$exports as default};
|
|
12
|
-
//# sourceMappingURL=zh-TW.module.js.map
|
package/dist/zh-TW.module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";AAAA,4BAAiB;IAAG,SAAS,CAAC,gBAAE,CAAC;IAC/B,iBAAiB,CAAC,MAAM,YAAc,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE;YAAC,KAAK,IAAM,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE,yBAAI,CAAC;YAAE,OAAO,IAAM,GAAG,UAAU,MAAM,CAAC,KAAK,KAAK,EAAE,yBAAI,CAAC;QAAA,GAAG,QAAC,CAAC;AAClL","sources":["packages/@react-aria/toast/intl/zh-TW.json"],"sourcesContent":["{\n \"close\": \"關閉\",\n \"notifications\": \"{count, plural, one {# 個通知} other {# 個通知}}。\"\n}\n"],"names":[],"version":3,"file":"zh-TW.module.js.map"}
|
package/src/useToast.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {AriaButtonProps} from '@react-types/button';
|
|
14
|
-
import {AriaLabelingProps, DOMAttributes, FocusableElement, RefObject} from '@react-types/shared';
|
|
15
|
-
import {filterDOMProps, useId, useLayoutEffect, useSlotId} from '@react-aria/utils';
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
import intlMessages from '../intl/*.json';
|
|
18
|
-
import {QueuedToast, ToastState} from '@react-stately/toast';
|
|
19
|
-
import {useEffect, useState} from 'react';
|
|
20
|
-
import {useLocalizedStringFormatter} from '@react-aria/i18n';
|
|
21
|
-
|
|
22
|
-
export interface AriaToastProps<T> extends AriaLabelingProps {
|
|
23
|
-
/** The toast object. */
|
|
24
|
-
toast: QueuedToast<T>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface ToastAria {
|
|
28
|
-
/** Props for the toast container, non-modal dialog element. */
|
|
29
|
-
toastProps: DOMAttributes,
|
|
30
|
-
/** Props for the toast content alert message. */
|
|
31
|
-
contentProps: DOMAttributes,
|
|
32
|
-
/** Props for the toast title element. */
|
|
33
|
-
titleProps: DOMAttributes,
|
|
34
|
-
/** Props for the toast description element, if any. */
|
|
35
|
-
descriptionProps: DOMAttributes,
|
|
36
|
-
/** Props for the toast close button. */
|
|
37
|
-
closeButtonProps: AriaButtonProps
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Provides the behavior and accessibility implementation for a toast component.
|
|
42
|
-
* Toasts display brief, temporary notifications of actions, errors, or other events in an application.
|
|
43
|
-
*/
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
-
export function useToast<T>(props: AriaToastProps<T>, state: ToastState<T>, ref: RefObject<FocusableElement | null>): ToastAria {
|
|
46
|
-
let {
|
|
47
|
-
key,
|
|
48
|
-
timer,
|
|
49
|
-
timeout
|
|
50
|
-
} = props.toast;
|
|
51
|
-
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
if (timer == null || timeout == null) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
timer.reset(timeout);
|
|
58
|
-
return () => {
|
|
59
|
-
timer.pause();
|
|
60
|
-
};
|
|
61
|
-
}, [timer, timeout]);
|
|
62
|
-
|
|
63
|
-
let titleId = useId();
|
|
64
|
-
let descriptionId = useSlotId();
|
|
65
|
-
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/toast');
|
|
66
|
-
|
|
67
|
-
// This is required for NVDA announcements, without it NVDA will NOT announce the toast when it appears.
|
|
68
|
-
// Originally was tied to animationStart/End via https://github.com/adobe/react-spectrum/pull/6223/commits/e22e319df64958e822ab7cd9685e96818cae9ba5
|
|
69
|
-
// but toasts don't always have animations.
|
|
70
|
-
let [isVisible, setIsVisible] = useState(false);
|
|
71
|
-
useLayoutEffect(() => {
|
|
72
|
-
setIsVisible(true);
|
|
73
|
-
}, []);
|
|
74
|
-
|
|
75
|
-
let toastProps = filterDOMProps(props, {labelable: true});
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
toastProps: {
|
|
79
|
-
...toastProps,
|
|
80
|
-
role: 'alertdialog',
|
|
81
|
-
'aria-modal': 'false',
|
|
82
|
-
'aria-labelledby': props['aria-labelledby'] || titleId,
|
|
83
|
-
'aria-describedby': props['aria-describedby'] || descriptionId,
|
|
84
|
-
tabIndex: 0
|
|
85
|
-
},
|
|
86
|
-
contentProps: {
|
|
87
|
-
role: 'alert',
|
|
88
|
-
'aria-atomic': 'true',
|
|
89
|
-
'aria-hidden': isVisible ? undefined : 'true'
|
|
90
|
-
},
|
|
91
|
-
titleProps: {
|
|
92
|
-
id: titleId
|
|
93
|
-
},
|
|
94
|
-
descriptionProps: {
|
|
95
|
-
id: descriptionId
|
|
96
|
-
},
|
|
97
|
-
closeButtonProps: {
|
|
98
|
-
'aria-label': stringFormatter.format('close'),
|
|
99
|
-
onPress: () => state.close(key)
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
package/src/useToastRegion.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
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 {AriaLabelingProps, DOMAttributes, FocusableElement, RefObject} from '@react-types/shared';
|
|
14
|
-
import {focusWithoutScrolling, mergeProps, useLayoutEffect} from '@react-aria/utils';
|
|
15
|
-
import {getInteractionModality, useFocusWithin, useHover} from '@react-aria/interactions';
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
import intlMessages from '../intl/*.json';
|
|
18
|
-
import {ToastState} from '@react-stately/toast';
|
|
19
|
-
import {useCallback, useEffect, useRef} from 'react';
|
|
20
|
-
import {useLandmark} from '@react-aria/landmark';
|
|
21
|
-
import {useLocalizedStringFormatter} from '@react-aria/i18n';
|
|
22
|
-
|
|
23
|
-
export interface AriaToastRegionProps extends AriaLabelingProps {
|
|
24
|
-
/**
|
|
25
|
-
* An accessibility label for the toast region.
|
|
26
|
-
* @default "Notifications"
|
|
27
|
-
*/
|
|
28
|
-
'aria-label'?: string
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface ToastRegionAria {
|
|
32
|
-
/** Props for the landmark region element. */
|
|
33
|
-
regionProps: DOMAttributes
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Provides the behavior and accessibility implementation for a toast region containing one or more toasts.
|
|
38
|
-
* Toasts display brief, temporary notifications of actions, errors, or other events in an application.
|
|
39
|
-
*/
|
|
40
|
-
export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState<T>, ref: RefObject<HTMLElement | null>): ToastRegionAria {
|
|
41
|
-
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/toast');
|
|
42
|
-
let {landmarkProps} = useLandmark({
|
|
43
|
-
role: 'region',
|
|
44
|
-
'aria-label': props['aria-label'] || stringFormatter.format('notifications', {count: state.visibleToasts.length})
|
|
45
|
-
}, ref);
|
|
46
|
-
|
|
47
|
-
let isHovered = useRef(false);
|
|
48
|
-
let isFocused = useRef(false);
|
|
49
|
-
let updateTimers = useCallback(() => {
|
|
50
|
-
if (isHovered.current || isFocused.current) {
|
|
51
|
-
state.pauseAll();
|
|
52
|
-
} else {
|
|
53
|
-
state.resumeAll();
|
|
54
|
-
}
|
|
55
|
-
}, [state]);
|
|
56
|
-
|
|
57
|
-
let {hoverProps} = useHover({
|
|
58
|
-
onHoverStart: () => {
|
|
59
|
-
isHovered.current = true;
|
|
60
|
-
updateTimers();
|
|
61
|
-
},
|
|
62
|
-
onHoverEnd: () => {
|
|
63
|
-
isHovered.current = false;
|
|
64
|
-
updateTimers();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Manage focus within the toast region.
|
|
69
|
-
// If a focused containing toast is removed, move focus to the next toast, or the previous toast if there is no next toast.
|
|
70
|
-
let toasts = useRef<FocusableElement[]>([]);
|
|
71
|
-
let prevVisibleToasts = useRef(state.visibleToasts);
|
|
72
|
-
let focusedToast = useRef<number | null>(null);
|
|
73
|
-
useLayoutEffect(() => {
|
|
74
|
-
// If no toast has focus, then don't do anything.
|
|
75
|
-
if (focusedToast.current === -1 || state.visibleToasts.length === 0 || !ref.current) {
|
|
76
|
-
toasts.current = [];
|
|
77
|
-
prevVisibleToasts.current = state.visibleToasts;
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
toasts.current = [...ref.current.querySelectorAll('[role="alertdialog"]')] as FocusableElement[];
|
|
81
|
-
// If the visible toasts haven't changed, we don't need to do anything.
|
|
82
|
-
if (prevVisibleToasts.current.length === state.visibleToasts.length
|
|
83
|
-
&& state.visibleToasts.every((t, i) => t.key === prevVisibleToasts.current[i].key)) {
|
|
84
|
-
prevVisibleToasts.current = state.visibleToasts;
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
// Get a list of all toasts by index and add info if they are removed.
|
|
88
|
-
let allToasts = prevVisibleToasts.current
|
|
89
|
-
.map((t, i) => ({
|
|
90
|
-
...t,
|
|
91
|
-
i,
|
|
92
|
-
isRemoved: !state.visibleToasts.some(t2 => t.key === t2.key)
|
|
93
|
-
}));
|
|
94
|
-
|
|
95
|
-
let removedFocusedToastIndex = allToasts.findIndex(t => t.i === focusedToast.current && t.isRemoved);
|
|
96
|
-
|
|
97
|
-
// If the focused toast was removed, focus the next or previous toast.
|
|
98
|
-
if (removedFocusedToastIndex > -1) {
|
|
99
|
-
// In pointer modality, move focus out of the toast region.
|
|
100
|
-
// Otherwise auto-dismiss timers will appear "stuck".
|
|
101
|
-
if (getInteractionModality() === 'pointer' && lastFocused.current?.isConnected) {
|
|
102
|
-
focusWithoutScrolling(lastFocused.current);
|
|
103
|
-
} else {
|
|
104
|
-
let i = 0;
|
|
105
|
-
let nextToast;
|
|
106
|
-
let prevToast;
|
|
107
|
-
while (i <= removedFocusedToastIndex) {
|
|
108
|
-
if (!allToasts[i].isRemoved) {
|
|
109
|
-
prevToast = Math.max(0, i - 1);
|
|
110
|
-
}
|
|
111
|
-
i++;
|
|
112
|
-
}
|
|
113
|
-
while (i < allToasts.length) {
|
|
114
|
-
if (!allToasts[i].isRemoved) {
|
|
115
|
-
nextToast = i - 1;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
i++;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// in the case where it's one toast at a time, both will be undefined, but we know the index must be 0
|
|
122
|
-
if (prevToast === undefined && nextToast === undefined) {
|
|
123
|
-
prevToast = 0;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// prioritize going to newer toasts
|
|
127
|
-
if (prevToast >= 0 && prevToast < toasts.current.length) {
|
|
128
|
-
focusWithoutScrolling(toasts.current[prevToast]);
|
|
129
|
-
} else if (nextToast >= 0 && nextToast < toasts.current.length) {
|
|
130
|
-
focusWithoutScrolling(toasts.current[nextToast]);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
prevVisibleToasts.current = state.visibleToasts;
|
|
136
|
-
}, [state.visibleToasts, ref]);
|
|
137
|
-
|
|
138
|
-
let lastFocused = useRef<FocusableElement | null>(null);
|
|
139
|
-
let {focusWithinProps} = useFocusWithin({
|
|
140
|
-
onFocusWithin: (e) => {
|
|
141
|
-
isFocused.current = true;
|
|
142
|
-
lastFocused.current = e.relatedTarget as FocusableElement;
|
|
143
|
-
updateTimers();
|
|
144
|
-
},
|
|
145
|
-
onBlurWithin: () => {
|
|
146
|
-
isFocused.current = false;
|
|
147
|
-
lastFocused.current = null;
|
|
148
|
-
updateTimers();
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// When the number of visible toasts becomes 0 or the region unmounts,
|
|
153
|
-
// restore focus to the last element that had focus before the user moved focus
|
|
154
|
-
// into the region. FocusScope restore focus doesn't update whenever the focus
|
|
155
|
-
// moves in, it only happens once, so we correct it.
|
|
156
|
-
// Because we're in a hook, we can't control if the user unmounts or not.
|
|
157
|
-
useEffect(() => {
|
|
158
|
-
if (state.visibleToasts.length === 0 && lastFocused.current?.isConnected) {
|
|
159
|
-
if (getInteractionModality() === 'pointer') {
|
|
160
|
-
focusWithoutScrolling(lastFocused.current);
|
|
161
|
-
} else {
|
|
162
|
-
lastFocused.current.focus();
|
|
163
|
-
}
|
|
164
|
-
lastFocused.current = null;
|
|
165
|
-
}
|
|
166
|
-
}, [ref, state.visibleToasts.length]);
|
|
167
|
-
|
|
168
|
-
useEffect(() => {
|
|
169
|
-
return () => {
|
|
170
|
-
if (lastFocused.current?.isConnected) {
|
|
171
|
-
if (getInteractionModality() === 'pointer') {
|
|
172
|
-
focusWithoutScrolling(lastFocused.current);
|
|
173
|
-
} else {
|
|
174
|
-
lastFocused.current.focus();
|
|
175
|
-
}
|
|
176
|
-
lastFocused.current = null;
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}, [ref]);
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
regionProps: mergeProps(landmarkProps, hoverProps, focusWithinProps, {
|
|
183
|
-
tabIndex: -1,
|
|
184
|
-
// Mark the toast region as a "top layer", so that it:
|
|
185
|
-
// - is not aria-hidden when opening an overlay
|
|
186
|
-
// - allows focus even outside a containing focus scope
|
|
187
|
-
// - doesn’t dismiss overlays when clicking on it, even though it is outside
|
|
188
|
-
// @ts-ignore
|
|
189
|
-
'data-react-aria-top-layer': true,
|
|
190
|
-
// listen to focus events separate from focuswithin because that will only fire once
|
|
191
|
-
// and we need to follow all focus changes
|
|
192
|
-
onFocus: (e) => {
|
|
193
|
-
let target = e.target.closest('[role="alertdialog"]');
|
|
194
|
-
focusedToast.current = toasts.current.findIndex(t => t === target);
|
|
195
|
-
},
|
|
196
|
-
onBlur: () => {
|
|
197
|
-
focusedToast.current = -1;
|
|
198
|
-
}
|
|
199
|
-
})
|
|
200
|
-
};
|
|
201
|
-
}
|