Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.
at v0.1.1 3.1 kB view raw
1import { useRef } from 'react'; 2import { useLayoutEffect } from './utils/react'; 3import { contains } 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 let willLoseFocus = false; 33 34 function onFocusOut(event: FocusEvent) { 35 const { target, relatedTarget } = event; 36 if ( 37 !event.defaultPrevented && 38 (relatedTarget || willLoseFocus) && 39 contains(element, target) && 40 !contains(element, relatedTarget) 41 ) { 42 willLoseFocus = false; 43 onDismissRef.current(event); 44 } 45 } 46 47 function onFocusIn(event: FocusEvent) { 48 const { target } = event; 49 if (!event.defaultPrevented && !contains(element, target)) { 50 onDismissRef.current(event); 51 } 52 } 53 54 function onKey(event: KeyboardEvent) { 55 if (event.isComposing) { 56 return; 57 } 58 59 if (event.code === 'Escape' && hasPriority.current) { 60 // The current dialog can be dismissed by pressing escape if it either has focus 61 // or it has priority 62 event.preventDefault(); 63 onDismissRef.current(event); 64 } else if (event.code === 'Tab') { 65 willLoseFocus = true; 66 } 67 } 68 69 function onClick(event: MouseEvent | TouchEvent) { 70 const { target } = event; 71 if (event.defaultPrevented) { 72 return; 73 } else if (contains(element, target)) { 74 willLoseFocus = false; 75 return; 76 } else if (hasPriority.current) { 77 // The current dialog can be dismissed by pressing outside of it if it either has 78 // focus or it has priority 79 event.preventDefault(); 80 onDismissRef.current(event); 81 } 82 } 83 84 if (focusLoss) { 85 document.body.addEventListener('focusout', onFocusOut, true); 86 document.body.addEventListener('focusin', onFocusIn, true); 87 } 88 89 document.addEventListener('click', onClick, true); 90 document.addEventListener('touchstart', onClick, true); 91 document.addEventListener('keydown', onKey, true); 92 93 return () => { 94 if (focusLoss) { 95 document.body.removeEventListener('focusout', onFocusOut, true); 96 document.body.removeEventListener('focusin', onFocusIn, true); 97 } 98 99 document.removeEventListener('click', onClick, true); 100 document.removeEventListener('touchstart', onClick, true); 101 document.removeEventListener('keydown', onKey, true); 102 }; 103 }, [ref.current, hasPriority, disabled, focusLoss]); 104}