| 1 |
import { useEffect, useRef } from 'react'; |
| 2 |
import { onToggleArrowKeydownDefault, Popper } from '../../helpers'; |
| 3 |
import type { PopperOptions } from '../../helpers/Popper/Popper'; |
| 4 |
|
| 5 |
/** @deprecated Use PopperOptions instead */ |
| 6 |
export type MenuPopperProps = PopperOptions; |
| 7 |
|
| 8 |
export interface MenuContainerProps { |
| 9 |
/** Menu to be rendered */ |
| 10 |
menu: React.ReactElement<any, string | React.JSXElementConstructor<any>>; |
| 11 |
/** Reference to the menu */ |
| 12 |
menuRef: React.RefObject<any>; |
| 13 |
/** Toggle to be rendered */ |
| 14 |
toggle: React.ReactNode; |
| 15 |
/** Reference to the toggle */ |
| 16 |
toggleRef: React.RefObject<any>; |
| 17 |
/** Flag to indicate if menu is opened.*/ |
| 18 |
isOpen: boolean; |
| 19 |
/** Callback to change the open state of the menu. |
| 20 |
* Triggered by clicking outside of the menu, or by pressing any keys specified in onOpenChangeKeys. */ |
| 21 |
onOpenChange?: (isOpen: boolean) => void; |
| 22 |
/** Keys that trigger onOpenChange, defaults to tab and escape. It is highly recommended to include Escape in the array, while Tab may be omitted if the menu contains non-menu items that are focusable. */ |
| 23 |
onOpenChangeKeys?: string[]; |
| 24 |
/** Callback to override the toggle keydown behavior. By default, when the toggle has focus and the menu is open, pressing the up/down arrow keys will focus a valid non-disabled menu item - the first item for the down arrow key and last item for the up arrow key. */ |
| 25 |
onToggleKeydown?: (event: KeyboardEvent) => void; |
| 26 |
/** z-index of the dropdown menu */ |
| 27 |
zIndex?: number; |
| 28 |
/** Additional properties to pass to the Popper */ |
| 29 |
popperProps?: PopperOptions; |
| 30 |
/** @beta Flag indicating the first menu item should be focused after opening the dropdown. */ |
| 31 |
shouldFocusFirstItemOnOpen?: boolean; |
| 32 |
/** Flag indicating if scroll on focus of the first menu item should occur. */ |
| 33 |
shouldPreventScrollOnItemFocus?: boolean; |
| 34 |
/** Time in ms to wait before firing the toggles' focus event. Defaults to 0 */ |
| 35 |
focusTimeoutDelay?: number; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Container that links a menu and menu toggle together, to handle basic keyboard input and control the opening and closing of a menu. |
| 40 |
*/ |
| 41 |
export const MenuContainer: React.FunctionComponent<MenuContainerProps> = ({ |
| 42 |
menu, |
| 43 |
menuRef, |
| 44 |
isOpen, |
| 45 |
toggle, |
| 46 |
toggleRef, |
| 47 |
onOpenChange, |
| 48 |
onToggleKeydown, |
| 49 |
zIndex = 9999, |
| 50 |
popperProps, |
| 51 |
onOpenChangeKeys = ['Escape', 'Tab'], |
| 52 |
shouldFocusFirstItemOnOpen = false, |
| 53 |
shouldPreventScrollOnItemFocus = true, |
| 54 |
focusTimeoutDelay = 0 |
| 55 |
}: MenuContainerProps) => { |
| 56 |
const prevIsOpen = useRef<boolean>(isOpen); |
| 57 |
useEffect(() => { |
| 58 |
// menu was opened, focus on first menu item |
| 59 |
if (prevIsOpen.current === false && isOpen === true && shouldFocusFirstItemOnOpen) { |
| 60 |
setTimeout(() => { |
| 61 |
const firstElement = menuRef?.current?.querySelector( |
| 62 |
'li button:not(:disabled),li input:not(:disabled),li a:not([aria-disabled="true"])' |
| 63 |
); |
| 64 |
firstElement && (firstElement as HTMLElement).focus({ preventScroll: shouldPreventScrollOnItemFocus }); |
| 65 |
}, focusTimeoutDelay); |
| 66 |
} |
| 67 |
|
| 68 |
prevIsOpen.current = isOpen; |
| 69 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 70 |
}, [isOpen]); |
| 71 |
|
| 72 |
useEffect(() => { |
| 73 |
const handleMenuKeys = (event: KeyboardEvent) => { |
| 74 |
// Close the menu on tab or escape if onOpenChange is provided |
| 75 |
if ( |
| 76 |
(isOpen && onOpenChange && menuRef.current?.contains(event.target as Node)) || |
| 77 |
toggleRef.current?.contains(event.target as Node) |
| 78 |
) { |
| 79 |
if (onOpenChangeKeys.includes(event.key)) { |
| 80 |
onOpenChange(false); |
| 81 |
toggleRef.current?.focus(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (toggleRef.current?.contains(event.target as Node)) { |
| 86 |
if (onToggleKeydown) { |
| 87 |
onToggleKeydown(event); |
| 88 |
} else if (isOpen) { |
| 89 |
onToggleArrowKeydownDefault(event, menuRef); |
| 90 |
} |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
const handleClick = (event: MouseEvent) => { |
| 95 |
// If the event is not on the toggle and onOpenChange callback is provided, close the menu |
| 96 |
if (isOpen && onOpenChange && !toggleRef?.current?.contains(event.target as Node)) { |
| 97 |
if (isOpen && !menuRef.current?.contains(event.target as Node)) { |
| 98 |
onOpenChange(false); |
| 99 |
} |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
window.addEventListener('keydown', handleMenuKeys); |
| 104 |
window.addEventListener('click', handleClick); |
| 105 |
|
| 106 |
return () => { |
| 107 |
window.removeEventListener('keydown', handleMenuKeys); |
| 108 |
window.removeEventListener('click', handleClick); |
| 109 |
}; |
| 110 |
}, [ |
| 111 |
focusTimeoutDelay, |
| 112 |
isOpen, |
| 113 |
menuRef, |
| 114 |
onOpenChange, |
| 115 |
onOpenChangeKeys, |
| 116 |
onToggleKeydown, |
| 117 |
shouldPreventScrollOnItemFocus, |
| 118 |
toggleRef |
| 119 |
]); |
| 120 |
|
| 121 |
return ( |
| 122 |
<Popper |
| 123 |
trigger={toggle} |
| 124 |
triggerRef={toggleRef} |
| 125 |
popper={menu} |
| 126 |
popperRef={menuRef} |
| 127 |
isVisible={isOpen} |
| 128 |
zIndex={zIndex} |
| 129 |
{...popperProps} |
| 130 |
/> |
| 131 |
); |
| 132 |
}; |
| 133 |
MenuContainer.displayName = 'MenuContainer'; |