@react-aria/toast 3.0.0-alpha.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/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/import.mjs +430 -0
- package/dist/main.js +435 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +430 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +41 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +41 -0
- package/src/index.ts +16 -0
- package/src/useToast.ts +111 -0
- package/src/useToastRegion.ts +79 -0
package/src/useToast.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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} from '@react-types/shared';
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
import intlMessages from '../intl/*.json';
|
|
17
|
+
import {QueuedToast, ToastState} from '@react-stately/toast';
|
|
18
|
+
import {RefObject, useEffect, useRef} from 'react';
|
|
19
|
+
import {useId, useLayoutEffect, useSlotId} from '@react-aria/utils';
|
|
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 element. */
|
|
29
|
+
toastProps: DOMAttributes,
|
|
30
|
+
/** Props for the toast title element. */
|
|
31
|
+
titleProps: DOMAttributes,
|
|
32
|
+
/** Props for the toast description element, if any. */
|
|
33
|
+
descriptionProps: DOMAttributes,
|
|
34
|
+
/** Props for the toast close button. */
|
|
35
|
+
closeButtonProps: AriaButtonProps
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Provides the behavior and accessibility implementation for a toast component.
|
|
40
|
+
* Toasts display brief, temporary notifications of actions, errors, or other events in an application.
|
|
41
|
+
*/
|
|
42
|
+
export function useToast<T>(props: AriaToastProps<T>, state: ToastState<T>, ref: RefObject<FocusableElement>): ToastAria {
|
|
43
|
+
let {
|
|
44
|
+
key,
|
|
45
|
+
timer,
|
|
46
|
+
timeout,
|
|
47
|
+
animation
|
|
48
|
+
} = props.toast;
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!timer) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
timer.reset(timeout);
|
|
56
|
+
return () => {
|
|
57
|
+
timer.pause();
|
|
58
|
+
};
|
|
59
|
+
}, [timer, timeout]);
|
|
60
|
+
|
|
61
|
+
// Restore focus to the toast container on unmount.
|
|
62
|
+
// If there are no more toasts, the container will be unmounted
|
|
63
|
+
// and will restore focus to wherever focus was before the user
|
|
64
|
+
// focused the toast region.
|
|
65
|
+
let focusOnUnmount = useRef(null);
|
|
66
|
+
useLayoutEffect(() => {
|
|
67
|
+
let container = ref.current.closest('[role=region]') as HTMLElement;
|
|
68
|
+
return () => {
|
|
69
|
+
if (container && container.contains(document.activeElement)) {
|
|
70
|
+
// Focus must be delayed for focus ring to appear, but we can't wait
|
|
71
|
+
// until useEffect cleanup to check if focus was inside the container.
|
|
72
|
+
focusOnUnmount.current = container;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}, [ref]);
|
|
76
|
+
|
|
77
|
+
// eslint-disable-next-line
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
return () => {
|
|
80
|
+
if (focusOnUnmount.current) {
|
|
81
|
+
focusOnUnmount.current.focus();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}, [ref]);
|
|
85
|
+
|
|
86
|
+
let titleId = useId();
|
|
87
|
+
let descriptionId = useSlotId();
|
|
88
|
+
let stringFormatter = useLocalizedStringFormatter(intlMessages);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
toastProps: {
|
|
92
|
+
role: 'alert',
|
|
93
|
+
'aria-label': props['aria-label'],
|
|
94
|
+
'aria-labelledby': props['aria-labelledby'] || titleId,
|
|
95
|
+
'aria-describedby': props['aria-describedby'] || descriptionId,
|
|
96
|
+
'aria-details': props['aria-details'],
|
|
97
|
+
// Hide toasts that are animating out so VoiceOver doesn't announce them.
|
|
98
|
+
'aria-hidden': animation === 'exiting' ? 'true' : undefined
|
|
99
|
+
},
|
|
100
|
+
titleProps: {
|
|
101
|
+
id: titleId
|
|
102
|
+
},
|
|
103
|
+
descriptionProps: {
|
|
104
|
+
id: descriptionId
|
|
105
|
+
},
|
|
106
|
+
closeButtonProps: {
|
|
107
|
+
'aria-label': stringFormatter.format('close'),
|
|
108
|
+
onPress: () => state.close(key)
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {AriaLabelingProps, DOMAttributes} from '@react-types/shared';
|
|
2
|
+
import {focusWithoutScrolling, mergeProps} from '@react-aria/utils';
|
|
3
|
+
import {getInteractionModality, useFocusWithin, useHover} from '@react-aria/interactions';
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
import intlMessages from '../intl/*.json';
|
|
6
|
+
import {RefObject, useEffect, useRef} from 'react';
|
|
7
|
+
import {ToastState} from '@react-stately/toast';
|
|
8
|
+
import {useLandmark} from '@react-aria/landmark';
|
|
9
|
+
import {useLocalizedStringFormatter} from '@react-aria/i18n';
|
|
10
|
+
|
|
11
|
+
export interface AriaToastRegionProps extends AriaLabelingProps {
|
|
12
|
+
/**
|
|
13
|
+
* An accessibility label for the toast region.
|
|
14
|
+
* @default "Notifications"
|
|
15
|
+
*/
|
|
16
|
+
'aria-label'?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ToastRegionAria {
|
|
20
|
+
/** Props for the landmark region element. */
|
|
21
|
+
regionProps: DOMAttributes
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Provides the behavior and accessibility implementation for a toast region containing one or more toasts.
|
|
26
|
+
* Toasts display brief, temporary notifications of actions, errors, or other events in an application.
|
|
27
|
+
*/
|
|
28
|
+
export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState<T>, ref: RefObject<HTMLElement>): ToastRegionAria {
|
|
29
|
+
let stringFormatter = useLocalizedStringFormatter(intlMessages);
|
|
30
|
+
let {landmarkProps} = useLandmark({
|
|
31
|
+
role: 'region',
|
|
32
|
+
'aria-label': props['aria-label'] || stringFormatter.format('notifications')
|
|
33
|
+
}, ref);
|
|
34
|
+
|
|
35
|
+
let {hoverProps} = useHover({
|
|
36
|
+
onHoverStart: state.pauseAll,
|
|
37
|
+
onHoverEnd: state.resumeAll
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
let lastFocused = useRef(null);
|
|
41
|
+
let {focusWithinProps} = useFocusWithin({
|
|
42
|
+
onFocusWithin: (e) => {
|
|
43
|
+
state.pauseAll();
|
|
44
|
+
lastFocused.current = e.relatedTarget;
|
|
45
|
+
},
|
|
46
|
+
onBlurWithin: () => {
|
|
47
|
+
state.resumeAll();
|
|
48
|
+
lastFocused.current = null;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// When the region unmounts, restore focus to the last element that had focus
|
|
53
|
+
// before the user moved focus into the region.
|
|
54
|
+
// TODO: handle when the element has unmounted like FocusScope does?
|
|
55
|
+
// eslint-disable-next-line arrow-body-style
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
return () => {
|
|
58
|
+
if (lastFocused.current && document.body.contains(lastFocused.current)) {
|
|
59
|
+
if (getInteractionModality() === 'pointer') {
|
|
60
|
+
focusWithoutScrolling(lastFocused.current);
|
|
61
|
+
} else {
|
|
62
|
+
lastFocused.current.focus();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}, [ref]);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
regionProps: mergeProps(landmarkProps, hoverProps, focusWithinProps, {
|
|
70
|
+
tabIndex: -1,
|
|
71
|
+
// Mark the toast region as a "top layer", so that it:
|
|
72
|
+
// - is not aria-hidden when opening an overlay
|
|
73
|
+
// - allows focus even outside a containing focus scope
|
|
74
|
+
// - doesn’t dismiss overlays when clicking on it, even though it is outside
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
'data-react-aria-top-layer': true
|
|
77
|
+
})
|
|
78
|
+
};
|
|
79
|
+
}
|