-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change the interaction of MenuBar in horizontal mode (#636)
- Loading branch information
Showing
3 changed files
with
167 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { | ||
getBEMElement, | ||
prefixClaName, | ||
getBEMModifier, | ||
} from 'mo/common/className'; | ||
import { IMenuBarItem } from 'mo/model/workbench/menuBar'; | ||
import { Menu, MenuMode, MenuRef, IMenuProps } from 'mo/components/menu'; | ||
import Logo from './logo'; | ||
|
||
export const defaultClassName = prefixClaName('menuBar'); | ||
export const actionClassName = getBEMElement(defaultClassName, 'action'); | ||
export const horizontalClassName = getBEMModifier( | ||
defaultClassName, | ||
'horizontal' | ||
); | ||
export const logoClassName = getBEMElement(horizontalClassName, 'logo'); | ||
export const logoContentClassName = getBEMElement(logoClassName, 'content'); | ||
|
||
export interface IHorizontalViewProps { | ||
data?: IMenuProps[]; | ||
onClick?: (event: React.MouseEvent<any, any>, item: IMenuBarItem) => void; | ||
logo?: React.ReactNode; | ||
} | ||
|
||
export function HorizontalView(props: IHorizontalViewProps) { | ||
const { data, onClick, logo } = props; | ||
const menuRef = useRef<MenuRef>(null); | ||
const [autoDisplayMenu, setAutoDisplayMenu] = useState(false); | ||
|
||
const checkIsRootLiElem = (e: MouseEvent) => { | ||
const target = e.target as HTMLElement; | ||
const liElem = target.closest('li'); | ||
const menuBarElem = liElem?.parentElement?.parentElement; | ||
const isRootLiElem = | ||
!!menuBarElem && | ||
menuBarElem.classList.contains(horizontalClassName); | ||
return isRootLiElem; | ||
}; | ||
|
||
useEffect(() => { | ||
const menuBarElem = document.getElementsByClassName( | ||
horizontalClassName | ||
)[0] as HTMLElement; | ||
|
||
const handleClickMenuBar = (e: MouseEvent) => { | ||
const isRootLiElem = checkIsRootLiElem(e); | ||
if (!isRootLiElem) return; | ||
if (autoDisplayMenu) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
menuRef.current?.dispose?.(); | ||
} | ||
// Delay the execution of setAutoDisplayMenu to ensure that the menu can be displayed. | ||
setTimeout(() => setAutoDisplayMenu(!autoDisplayMenu)); | ||
}; | ||
|
||
const clearAutoDisplay = (e: MouseEvent) => { | ||
if (!autoDisplayMenu) return; | ||
const isRootLiElem = checkIsRootLiElem(e); | ||
const target = e.target as HTMLElement; | ||
const liElem = target.closest('li'); | ||
if (!isRootLiElem && !liElem?.dataset.submenu) { | ||
setAutoDisplayMenu(false); | ||
} | ||
}; | ||
|
||
document.addEventListener('click', clearAutoDisplay); | ||
menuBarElem?.addEventListener('click', handleClickMenuBar); | ||
|
||
return () => { | ||
document.removeEventListener('click', clearAutoDisplay); | ||
menuBarElem?.removeEventListener('click', handleClickMenuBar); | ||
}; | ||
}, [autoDisplayMenu]); | ||
|
||
const trigger = autoDisplayMenu ? 'hover' : 'click'; | ||
|
||
const handleClickMenu = (e: React.MouseEvent, item: IMenuBarItem) => { | ||
onClick?.(e, item); | ||
menuRef.current!.dispose(); | ||
}; | ||
|
||
return ( | ||
<div className={horizontalClassName}> | ||
<div className={logoClassName}> | ||
{logo || <Logo className={logoContentClassName} />} | ||
</div> | ||
<Menu | ||
ref={menuRef} | ||
role="menu" | ||
mode={MenuMode.Horizontal} | ||
trigger={trigger} | ||
onClick={handleClickMenu} | ||
style={{ width: '100%' }} | ||
data={data} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters