-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.ts
118 lines (110 loc) · 3.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* External dependencies
*/
import type { RefCallback, SyntheticEvent } from 'react';
/**
* WordPress dependencies
*/
import { useRef, useEffect, useCallback } from '@wordpress/element';
import { ESCAPE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import useConstrainedTabbing from '../use-constrained-tabbing';
import useFocusOnMount from '../use-focus-on-mount';
import useFocusReturn from '../use-focus-return';
import useFocusOutside from '../use-focus-outside';
import useMergeRefs from '../use-merge-refs';
type DialogOptions = {
/**
* Determines whether focus should be automatically moved to the popover
* when it mounts. `false` causes no focus shift, `true` causes the popover
* itself to gain focus, and `firstElement` focuses the first focusable
* element within the popover.
*
* @default 'firstElement'
*/
focusOnMount?: Parameters< typeof useFocusOnMount >[ 0 ];
/**
* Determines whether tabbing is constrained to within the popover,
* preventing keyboard focus from leaving the popover content without
* explicit focus elsewhere, or whether the popover remains part of the
* wider tab order.
* If no value is passed, it will be derived from `focusOnMount`.
*
* @see focusOnMount
* @default `focusOnMount` !== false
*/
constrainTabbing?: boolean;
onClose?: () => void;
/**
* Use the `onClose` prop instead.
*
* @deprecated
*/
__unstableOnClose?: (
type: string | undefined,
event: SyntheticEvent
) => void;
};
type useDialogReturn = [
RefCallback< HTMLElement >,
ReturnType< typeof useFocusOutside > & Pick< HTMLElement, 'tabIndex' >,
];
/**
* Returns a ref and props to apply to a dialog wrapper to enable the following behaviors:
* - constrained tabbing.
* - focus on mount.
* - return focus on unmount.
* - focus outside.
*
* @param options Dialog Options.
*/
function useDialog( options: DialogOptions ): useDialogReturn {
const currentOptions = useRef< DialogOptions | undefined >();
const { constrainTabbing = options.focusOnMount !== false } = options;
useEffect( () => {
currentOptions.current = options;
}, Object.values( options ) );
const constrainedTabbingRef = useConstrainedTabbing();
const focusOnMountRef = useFocusOnMount( options.focusOnMount );
const focusReturnRef = useFocusReturn();
const focusOutsideProps = useFocusOutside( ( event ) => {
// This unstable prop is here only to manage backward compatibility
// for the Popover component otherwise, the onClose should be enough.
if ( currentOptions.current?.__unstableOnClose ) {
currentOptions.current.__unstableOnClose( 'focus-outside', event );
} else if ( currentOptions.current?.onClose ) {
currentOptions.current.onClose();
}
} );
const closeOnEscapeRef = useCallback( ( node: HTMLElement ) => {
if ( ! node ) {
return;
}
node.addEventListener( 'keydown', ( event: KeyboardEvent ) => {
// Close on escape.
if (
event.keyCode === ESCAPE &&
! event.defaultPrevented &&
currentOptions.current?.onClose
) {
event.preventDefault();
currentOptions.current.onClose();
}
} );
}, [] );
return [
useMergeRefs( [
constrainTabbing ? constrainedTabbingRef : null,
options.focusOnMount !== false ? focusReturnRef : null,
options.focusOnMount !== false ? focusOnMountRef : null,
closeOnEscapeRef,
] ),
{
...focusOutsideProps,
tabIndex: -1,
},
];
}
export default useDialog;