Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.
1import { useRef } from 'react';
2import { useLayoutEffect } from './utils/react';
3import { contains, getRoot } from './utils/element';
4import { makePriorityHook } from './usePriority';
5import { Ref } from './types';
6
7const usePriority = makePriorityHook();
8
9export interface DismissableOptions {
10 focusLoss?: boolean;
11 disabled?: boolean;
12}
13
14export function useDismissable<T extends HTMLElement>(
15 ref: Ref<T>,
16 onDismiss: (event: Event) => void,
17 options?: DismissableOptions
18) {
19 const focusLoss = !!(options && options.focusLoss);
20 const disabled = !!(options && options.disabled);
21 const hasPriority = usePriority(ref, disabled);
22 const onDismissRef = useRef(onDismiss);
23
24 useLayoutEffect(() => {
25 onDismissRef.current = onDismiss;
26 }, [onDismiss]);
27
28 useLayoutEffect(() => {
29 const { current: element } = ref;
30 if (!element || disabled) return;
31
32 const root = getRoot(element);
33 let willLoseFocus = false;
34
35 function onFocusOut(event: FocusEvent) {
36 const { target, relatedTarget } = event;
37 if (
38 !event.defaultPrevented &&
39 (relatedTarget || willLoseFocus) &&
40 contains(element, target) &&
41 !contains(element, relatedTarget)
42 ) {
43 willLoseFocus = false;
44 onDismissRef.current(event);
45 }
46 }
47
48 function onFocusIn(event: FocusEvent) {
49 const { target } = event;
50 if (!event.defaultPrevented && !contains(element, target)) {
51 onDismissRef.current(event);
52 }
53 }
54
55 function onKey(event: KeyboardEvent) {
56 if (event.isComposing) {
57 return;
58 }
59
60 if (event.code === 'Escape' && hasPriority.current) {
61 // The current dialog can be dismissed by pressing escape if it either has focus
62 // or it has priority
63 event.preventDefault();
64 onDismissRef.current(event);
65 } else if (event.code === 'Tab') {
66 willLoseFocus = true;
67 }
68 }
69
70 function onClick(event: MouseEvent | TouchEvent) {
71 const { target } = event;
72 if (event.defaultPrevented) {
73 return;
74 } else if (contains(element, target)) {
75 willLoseFocus = false;
76 return;
77 } else if (hasPriority.current) {
78 // The current dialog can be dismissed by pressing outside of it if it either has
79 // focus or it has priority
80 event.preventDefault();
81 onDismissRef.current(event);
82 }
83 }
84
85 if (focusLoss) {
86 root.addEventListener('focusout', onFocusOut, true);
87 root.addEventListener('focusin', onFocusIn, true);
88 }
89
90 root.addEventListener('click', onClick, true);
91 root.addEventListener('touchstart', onClick, true);
92 root.addEventListener('keydown', onKey, true);
93
94 return () => {
95 if (focusLoss) {
96 root.removeEventListener('focusout', onFocusOut, true);
97 root.removeEventListener('focusin', onFocusIn, true);
98 }
99
100 root.removeEventListener('click', onClick, true);
101 root.removeEventListener('touchstart', onClick, true);
102 root.removeEventListener('keydown', onKey, true);
103 };
104 }, [ref.current, hasPriority, disabled, focusLoss]);
105}