@react-aria/toast 3.0.0-nightly.1954

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.
@@ -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
+ }