|
| 1 | +import { useRef, useState, useEffect } from 'react'; |
| 2 | +import { usePopper } from 'react-popper'; |
| 3 | +import { uniqueId } from 'lodash'; |
| 4 | + |
| 5 | +/** |
| 6 | + *@description Hook that returns the necessary refs and event handlers to make a popper.js menu component. |
| 7 | + * @param {popperOptions} param.popperOptions - Options to pass to the popper instance |
| 8 | + * @link https://popper.js.org/docs/v2/constructors/ |
| 9 | + */ |
| 10 | +const useMenu = ({ popperOptions }) => { |
| 11 | + /** Ref of the menu */ |
| 12 | + const [menuRef, setMenu] = useState(null); |
| 13 | + |
| 14 | + /** Ref of the trigger (button, select, etc.) */ |
| 15 | + const [triggerRef, setTrigger] = useState(null); |
| 16 | + |
| 17 | + /** Ref of the optional arrow element */ |
| 18 | + const [arrowRef, setArrowRef] = useState(null); |
| 19 | + |
| 20 | + const [closeOnSelect, setCloseOnSelect] = useState(true); |
| 21 | + |
| 22 | + const { current: menuId } = useRef(uniqueId(`menu-`)); |
| 23 | + const { current: menuTriggerId } = useRef(uniqueId(`menu-trigger-`)); |
| 24 | + const { current: menuArrowId } = useRef(uniqueId(`menu-arrow-`)); |
| 25 | + |
| 26 | + const popperModifiers = [ |
| 27 | + { |
| 28 | + name: 'flip', |
| 29 | + enabled: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'arrow', |
| 33 | + enabled: !!arrowRef, |
| 34 | + options: { |
| 35 | + element: arrowRef, |
| 36 | + offset: [0, 6], |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'offset', |
| 41 | + options: { |
| 42 | + offset: [0, 6], |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'preventOverflow', |
| 47 | + options: { |
| 48 | + rootBoundary: 'document', |
| 49 | + padding: 0, |
| 50 | + }, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + |
| 54 | + const { styles, attributes, update } = usePopper(triggerRef, menuRef, { |
| 55 | + placement: 'bottom-start', |
| 56 | + modifiers: popperModifiers, |
| 57 | + strategy: 'absolute', |
| 58 | + ...popperOptions, |
| 59 | + }); |
| 60 | + |
| 61 | + // Focus on a node |
| 62 | + const focusOnNode = node => () => { |
| 63 | + if (node) node.focus(); |
| 64 | + }; |
| 65 | + |
| 66 | + const focusMenu = focusOnNode(menuRef); |
| 67 | + const focusTrigger = focusOnNode(triggerRef); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + // Update the menu after a click or keydown event to ensure the menu is positioned correctly after the DOM has updated |
| 71 | + const handleUpdate = () => { |
| 72 | + if (update) setTimeout(update, 10); |
| 73 | + }; |
| 74 | + |
| 75 | + document.addEventListener('click', handleUpdate, true); |
| 76 | + document.addEventListener('keydown', handleUpdate, true); |
| 77 | + document.addEventListener('scroll', handleUpdate, true); |
| 78 | + document.addEventListener('resize', handleUpdate, true); |
| 79 | + |
| 80 | + return () => { |
| 81 | + document.removeEventListener('click', handleUpdate, true); |
| 82 | + document.removeEventListener('keydown', handleUpdate, true); |
| 83 | + document.removeEventListener('scroll', handleUpdate, true); |
| 84 | + document.removeEventListener('resize', handleUpdate, true); |
| 85 | + }; |
| 86 | + }, [triggerRef, update]); |
| 87 | + |
| 88 | + return { |
| 89 | + menuRef: setMenu, |
| 90 | + currentMenuRef: menuRef, |
| 91 | + triggerRef: setTrigger, |
| 92 | + arrowRef: setArrowRef, |
| 93 | + focusMenu, |
| 94 | + focusTrigger, |
| 95 | + setCloseOnSelect, |
| 96 | + closeOnSelect, |
| 97 | + styles, |
| 98 | + attributes, |
| 99 | + update, |
| 100 | + /** Id to be set on menu container. Needed for closing menu on click outside */ |
| 101 | + menuId, |
| 102 | + /** Id to be set on menu trigger. Needed for aria-controls */ |
| 103 | + menuTriggerId, |
| 104 | + /** Id to be set on menu arrow. Needed for aria-controls */ |
| 105 | + menuArrowId, |
| 106 | + }; |
| 107 | +}; |
| 108 | + |
| 109 | +export default useMenu; |
0 commit comments