import classnames from 'classnames'; import * as React from 'react'; import { useEffect } from 'react'; import * as ReactDom from 'react-dom'; import { MenubarContext } from './context'; function stopPropagation(e: MouseEvent) { e.stopPropagation(); } export const MenubarItem = (props: MenubarItemProps) => { const context = React.useContext(MenubarContext); const menubarItemRef = React.useRef(null); const { text, children, hidden } = props; const { prefixCls, menubarActived } = context; const active = context.activeElementRef && context.activeElementRef.current && context.activeElementRef.current === menubarItemRef.current; const currentMenuActived = menubarActived && active; const baseCls = `${prefixCls}-item`; const popupClassName = `${context.prefixCls}-item-dropdown`; useEffect(()=>{ const element = menubarItemRef.current; if (element) { element.addEventListener('mousedown', stopPropagation); } return ()=>{ if (element) { element.removeEventListener('mousedown', stopPropagation); } } }, []) const doActive = () => { context.setActiveElementRef(menubarItemRef); }; const doDeactive = () => { context.setActiveElementRef(); }; const onToggleActive = (e:React.MouseEvent) => { e.stopPropagation(); if (!currentMenuActived) { context.activeMenubar(); doActive(); } }; const locatable = !!menubarItemRef.current; const getPortalItemStyle = () => { const node = menubarItemRef.current; if (!node) { return {}; } const { offsetHeight } = node; const { top, left } = node.getBoundingClientRect(); return { top: top + offsetHeight, left: left }; }; return (
{text}
{currentMenuActived && locatable && ReactDom.createPortal(
{children}
, document.body, )}
); }; // eslint-disable-next-line @typescript-eslint/no-redeclare export interface MenubarItemProps { text: string | React.ReactNode; hidden?: boolean; children?: React.ReactNode | React.ReactNode[]; }