|
| 1 | +import './style.scss'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { classNames, prefixClaName } from 'mo/common/className'; |
| 4 | +import { Icon } from '../icon'; |
| 5 | +import { Menu } from './menu'; |
| 6 | +import { useEffect } from 'react'; |
| 7 | +import { findParentByClassName, getRelativePosition, TriggerEvent } from 'mo/common/dom'; |
| 8 | +import { defaultMenuItemClassName, IMenuItem } from './menuItem'; |
| 9 | +import { em2Px } from 'mo/common/css'; |
| 10 | + |
| 11 | +export enum MenuMode { |
| 12 | + Vertical = 'vertical', |
| 13 | + Horizontal = 'horizontal' |
| 14 | +}; |
| 15 | + |
| 16 | +export function isHorizontal(mode: MenuMode) { |
| 17 | + return mode === MenuMode.Horizontal; |
| 18 | +} |
| 19 | + |
| 20 | +export function isVertical(mode: MenuMode) { |
| 21 | + return mode === MenuMode.Horizontal; |
| 22 | +} |
| 23 | + |
| 24 | +export interface ISubMenu extends IMenuItem { |
| 25 | + /** |
| 26 | + * The event of show subMenu, default value is 'hover' |
| 27 | + */ |
| 28 | + trigger?: TriggerEvent; |
| 29 | + icon?: string; |
| 30 | + data?: ISubMenu[]; |
| 31 | + mode?: MenuMode; |
| 32 | +} |
| 33 | + |
| 34 | +const defaultSubMenuClassName = prefixClaName('sub-menu'); |
| 35 | + |
| 36 | +function hideSubMenu(target?: HTMLElement) { |
| 37 | + const container = target || document.body; |
| 38 | + const all = container.querySelectorAll<HTMLMenuElement>('.'+defaultSubMenuClassName); |
| 39 | + all?.forEach(ele => { |
| 40 | + ele.style.visibility = 'hidden'; |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +const hideAll = () => { |
| 45 | + hideSubMenu(); |
| 46 | +}; |
| 47 | + |
| 48 | +const hideAfterLeftWindow = () => { |
| 49 | + if (document.hidden) { |
| 50 | + hideSubMenu(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +let timer; |
| 55 | + |
| 56 | +export function SubMenu(props: React.PropsWithChildren<ISubMenu>) { |
| 57 | + const { className, name, render, data = [], mode = MenuMode.Vertical, icon, children, ...others } = props; |
| 58 | + const cNames = classNames(defaultSubMenuClassName, mode, className); |
| 59 | + const isAlignHorizontal = isHorizontal(mode); |
| 60 | + |
| 61 | + const events = { |
| 62 | + onMouseOver: (event: React.MouseEvent<any, any>) => { |
| 63 | + clearTimeout(timer); |
| 64 | + |
| 65 | + const nextMenuItem = findParentByClassName<HTMLLIElement>(event.target, defaultMenuItemClassName); |
| 66 | + const nextSubMenu = nextMenuItem?.querySelector<HTMLMenuElement>('.' + defaultSubMenuClassName); |
| 67 | + if (!nextMenuItem || !nextSubMenu) return; |
| 68 | + |
| 69 | + const prevMenuItem = findParentByClassName<HTMLLIElement>(event.relatedTarget, defaultMenuItemClassName); |
| 70 | + const prevSubMenu = prevMenuItem?.querySelector<HTMLMenuElement>('.' + defaultSubMenuClassName); |
| 71 | + |
| 72 | + if (prevMenuItem && prevSubMenu && !prevMenuItem.contains(nextMenuItem)) { |
| 73 | + hideAll(); |
| 74 | + } |
| 75 | + |
| 76 | + const domRect = nextMenuItem.getBoundingClientRect(); |
| 77 | + nextSubMenu.style.visibility = 'visible'; |
| 78 | + const pos = getRelativePosition(nextSubMenu, domRect); |
| 79 | + |
| 80 | + if (isAlignHorizontal) pos.y = pos.y + domRect.height; |
| 81 | + else { |
| 82 | + pos.x = pos.x + domRect.width; |
| 83 | + // The vertical menu default has padding 0.5em so that need reduce the padding |
| 84 | + const fontSize = getComputedStyle(nextSubMenu).getPropertyValue('font-size'); |
| 85 | + const paddingTop = em2Px(0.5, parseInt(fontSize.replace('px', ''), 10)); |
| 86 | + pos.y = pos.y - paddingTop; |
| 87 | + } |
| 88 | + |
| 89 | + nextSubMenu.style.cssText = ` |
| 90 | + left: ${pos.x}px; |
| 91 | + top: ${pos.y}px; |
| 92 | + `; |
| 93 | + }, |
| 94 | + onMouseOut: function(event: React.MouseEvent) { |
| 95 | + const nextMenuItem = findParentByClassName<HTMLLIElement>(event.relatedTarget, defaultMenuItemClassName) ; |
| 96 | + if (!nextMenuItem) return; |
| 97 | + |
| 98 | + const prevMenuItem = event.currentTarget as HTMLLIElement; |
| 99 | + const prevSubMenu = prevMenuItem?.querySelector('.' + defaultSubMenuClassName); |
| 100 | + const nextSubMenu = nextMenuItem?.querySelector('.' + defaultSubMenuClassName); |
| 101 | + // Hide the prev subMenu when the next menuItem hasn't subMenu and the prev MenuItem |
| 102 | + // subMenu not contains it. |
| 103 | + if (!nextSubMenu && prevSubMenu && !prevMenuItem.contains(nextMenuItem)) { |
| 104 | + hideAll(); |
| 105 | + // delayHide(prevMenuItem); |
| 106 | + } |
| 107 | + }, |
| 108 | + onClick: function(event: React.MouseEvent) { |
| 109 | + event.stopPropagation(); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + window.addEventListener('contextmenu', hideAll); |
| 115 | + window.addEventListener('click', hideAll); |
| 116 | + window.addEventListener('visibilitychange', hideAfterLeftWindow); |
| 117 | + return () => { |
| 118 | + document.removeEventListener('contextmenu', hideAll); |
| 119 | + window.removeEventListener('click', hideAll); |
| 120 | + window.removeEventListener('visibilitychange', hideAfterLeftWindow); |
| 121 | + clearTimeout(timer); |
| 122 | + } |
| 123 | + }, []) |
| 124 | + |
| 125 | + const chevronType = isAlignHorizontal ? 'down' : 'right'; |
| 126 | + const subMenuContent = data.length > 0 ? |
| 127 | + <Menu className={cNames} style={{ visibility: 'hidden' }} data={data}/> : |
| 128 | + <Menu className={cNames} style={{ visibility: 'hidden' }}> { children } </Menu>; |
| 129 | + |
| 130 | + console.log('mode', mode, isAlignHorizontal) |
| 131 | + |
| 132 | + return ( |
| 133 | + <li className={defaultMenuItemClassName} {...events} {...others}> |
| 134 | + <a className="menu-item-container"> |
| 135 | + <Icon className="menu-item-check" type={icon || ''} /> |
| 136 | + <span className="menu-item-label">{ render ? render(props) : name }</span> |
| 137 | + <Icon className="menu-item-indicator" type={`chevron-${chevronType}`}/> |
| 138 | + </a> |
| 139 | + { subMenuContent } |
| 140 | + </li> |
| 141 | + ) |
| 142 | +} |
0 commit comments