-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
89 lines (83 loc) · 2.56 KB
/
index.js
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
/**
* 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';
/* eslint-disable jsdoc/valid-types */
/**
* @typedef DialogOptions
* @property {Parameters<useFocusOnMount>[0]} focusOnMount Focus on mount arguments.
* @property {() => void} onClose Function to call when the dialog is closed.
*/
/* eslint-enable jsdoc/valid-types */
/**
* 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 {DialogOptions} options Dialog Options.
*/
function useDialog( options ) {
/**
* @type {import('react').MutableRefObject<DialogOptions | undefined>}
*/
const currentOptions = useRef();
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.
// @ts-ignore unstable property
if ( currentOptions.current?.__unstableOnClose ) {
// @ts-ignore unstable property
currentOptions.current.__unstableOnClose( 'focus-outside', event );
} else if ( currentOptions.current?.onClose ) {
currentOptions.current.onClose();
}
} );
const closeOnEscapeRef = useCallback( ( node ) => {
if ( ! node ) {
return;
}
node.addEventListener( 'keydown', (
/** @type {KeyboardEvent} */ event
) => {
// Close on escape.
if (
event.keyCode === ESCAPE &&
! event.defaultPrevented &&
currentOptions.current?.onClose
) {
event.preventDefault();
currentOptions.current.onClose();
}
} );
}, [] );
return [
useMergeRefs( [
options.focusOnMount !== false ? constrainedTabbingRef : null,
options.focusOnMount !== false ? focusReturnRef : null,
options.focusOnMount !== false ? focusOnMountRef : null,
closeOnEscapeRef,
] ),
{
...focusOutsideProps,
tabIndex: '-1',
},
];
}
export default useDialog;