|
| 1 | +import { ReactNode, useState, useEffect, useCallback, useRef } from 'react'; |
| 2 | +import cx from 'clsx'; |
| 3 | + |
| 4 | +import { cloneChildren } from '../utils'; |
| 5 | +import { MenuItemExternalProps } from './MenuItem'; |
| 6 | + |
| 7 | +export interface SubMenuProps { |
| 8 | + label: string; |
| 9 | + children: ReactNode; |
| 10 | + className?: string; |
| 11 | + disabled?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +const CLOSE_DELAY = 150; |
| 15 | +const RIGHT_CLASS = 'react-context-menu__submenu-right'; |
| 16 | +const BOTTOM_CLASS = 'react-context-menu__submenu-bottom'; |
| 17 | + |
| 18 | +const SubMenu = ({ label, children, className, disabled, ...rest }: SubMenuProps) => { |
| 19 | + const [active, setActive] = useState(false); |
| 20 | + |
| 21 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 22 | + |
| 23 | + const itemRef = useRef<HTMLDivElement>(null); |
| 24 | + const subMenuRef = useRef<HTMLDivElement>(null); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + return () => { |
| 28 | + if (timeoutRef.current) clearTimeout(timeoutRef.current); |
| 29 | + }; |
| 30 | + }, []); |
| 31 | + |
| 32 | + const clearTimer = useCallback(() => { |
| 33 | + if (timeoutRef.current) clearTimeout(timeoutRef.current); |
| 34 | + }, []); |
| 35 | + |
| 36 | + const calculatePosition = useCallback(() => { |
| 37 | + if (subMenuRef.current && itemRef.current) { |
| 38 | + clearTimer(); |
| 39 | + setActive(true); |
| 40 | + |
| 41 | + // Reset position styling |
| 42 | + subMenuRef.current.style.top = '0'; |
| 43 | + subMenuRef.current.classList.remove(RIGHT_CLASS, BOTTOM_CLASS); |
| 44 | + |
| 45 | + const { height } = itemRef.current.getBoundingClientRect(); |
| 46 | + const { right, bottom } = subMenuRef.current.getBoundingClientRect(); |
| 47 | + |
| 48 | + if (right > window.innerWidth) subMenuRef.current.classList.add(RIGHT_CLASS); |
| 49 | + if (bottom - window.innerHeight > 0) { |
| 50 | + subMenuRef.current.style.top = `${window.innerHeight - bottom - height}px`; |
| 51 | + subMenuRef.current.classList.add(BOTTOM_CLASS); |
| 52 | + } |
| 53 | + } |
| 54 | + }, [subMenuRef, itemRef, clearTimer]); |
| 55 | + |
| 56 | + const onLeave = useCallback(() => { |
| 57 | + clearTimer(); |
| 58 | + |
| 59 | + timeoutRef.current = setTimeout(() => { |
| 60 | + setActive(false); |
| 61 | + }, CLOSE_DELAY); |
| 62 | + }, [clearTimer]); |
| 63 | + |
| 64 | + const classNames = cx('react-context-menu__item', className, { |
| 65 | + 'react-context-menu__item--disabled': disabled, |
| 66 | + }); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div |
| 70 | + ref={itemRef} |
| 71 | + className={classNames} |
| 72 | + aria-haspopup="true" |
| 73 | + role="menuitem" |
| 74 | + tabIndex={-1} |
| 75 | + onMouseEnter={calculatePosition} |
| 76 | + onMouseLeave={onLeave} |
| 77 | + onClick={(event: React.MouseEvent<HTMLElement>) => event.stopPropagation()} |
| 78 | + > |
| 79 | + <div className="react-context-menu__label"> |
| 80 | + {label} |
| 81 | + <span className="react-context-menu__arrow" /> |
| 82 | + </div> |
| 83 | + <div |
| 84 | + ref={subMenuRef} |
| 85 | + style={{ |
| 86 | + visibility: active ? 'visible' : 'hidden', |
| 87 | + }} |
| 88 | + className="react-context-menu__submenu" |
| 89 | + > |
| 90 | + {/* rest is sent from the ContextMenu element */} |
| 91 | + {cloneChildren(children, rest as MenuItemExternalProps)} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default SubMenu; |
0 commit comments