From a53ff910331b8814d251e21a9add33be4a7267a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 29 Mar 2022 13:59:28 +0200 Subject: [PATCH] Fix hover menu behavior on ViewHeader component (#2662) --- webapp/src/components/viewMenu.tsx | 20 ++++++---- webapp/src/widgets/menu/menu.tsx | 47 +++++------------------ webapp/src/widgets/menu/subMenuOption.tsx | 12 +++--- 3 files changed, 30 insertions(+), 49 deletions(-) diff --git a/webapp/src/components/viewMenu.tsx b/webapp/src/components/viewMenu.tsx index 9b71b631b..6e8220266 100644 --- a/webapp/src/components/viewMenu.tsx +++ b/webapp/src/components/viewMenu.tsx @@ -253,23 +253,29 @@ const ViewMenu = (props: Props) => { />))} - {!props.readonly && + + {!props.readonly && + } onClick={handleDuplicateView} /> - } - {!props.readonly && views.length > 1 && + + } + {!props.readonly && views.length > 1 && + } onClick={handleDeleteView} /> - } - {!props.readonly && + + } + {!props.readonly && + { onClick={handleAddViewCalendar} /> - } - + + } ) } diff --git a/webapp/src/widgets/menu/menu.tsx b/webapp/src/widgets/menu/menu.tsx index ba787f2d0..aea7fbb87 100644 --- a/webapp/src/widgets/menu/menu.tsx +++ b/webapp/src/widgets/menu/menu.tsx @@ -6,7 +6,7 @@ import SeparatorOption from './separatorOption' import SwitchOption from './switchOption' import TextOption from './textOption' import ColorOption from './colorOption' -import SubMenuOption from './subMenuOption' +import SubMenuOption, {HoveringContext} from './subMenuOption' import LabelOption from './labelOption' import './menu.scss' @@ -27,7 +27,7 @@ export default class Menu extends React.PureComponent { static Label = LabelOption public state = { - hoveringIdx: -1, + hovering: null, } public render(): JSX.Element { @@ -36,16 +36,14 @@ export default class Menu extends React.PureComponent {
- {React.Children.map(children, (child, i) => { - return addChildMenuItem({ - child, - onMouseEnter: () => - this.setState({ - hoveringIdx: i, - }), - isHovering: () => i === this.state.hoveringIdx, - }) - })} + {React.Children.map(children, (child) => ( +
this.setState({hovering: child})} + > + + {child} + +
))}
@@ -67,28 +65,3 @@ export default class Menu extends React.PureComponent { // No need to do anything, as click bubbled up to MenuWrapper, which closes } } - -function addChildMenuItem(props: {child: React.ReactNode, onMouseEnter: () => void, isHovering: () => boolean}): JSX.Element | null { - const {child, onMouseEnter, isHovering} = props - if (child !== null) { - if (React.isValidElement(child)) { - const castedChild = child as React.ReactElement - - return ( -
- {castedChild.type === SubMenuOption ? ( - - ) : ( - - )} -
- ) - } - } - return (null) -} diff --git a/webapp/src/widgets/menu/subMenuOption.tsx b/webapp/src/widgets/menu/subMenuOption.tsx index 414c19793..15b71bacd 100644 --- a/webapp/src/widgets/menu/subMenuOption.tsx +++ b/webapp/src/widgets/menu/subMenuOption.tsx @@ -1,6 +1,6 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React, {useEffect, useState} from 'react' +import React, {useEffect, useState, useContext} from 'react' import SubmenuTriangleIcon from '../icons/submenuTriangle' @@ -8,25 +8,27 @@ import Menu from '.' import './subMenuOption.scss' +export const HoveringContext = React.createContext(false) + type SubMenuOptionProps = { id: string name: string position?: 'bottom' | 'top' | 'left' | 'left-bottom' icon?: React.ReactNode children: React.ReactNode - isHovering?: boolean } function SubMenuOption(props: SubMenuOptionProps): JSX.Element { const [isOpen, setIsOpen] = useState(false) + const isHovering = useContext(HoveringContext) const openLeftClass = props.position === 'left' || props.position === 'left-bottom' ? ' open-left' : '' useEffect(() => { - if (props.isHovering !== undefined) { - setIsOpen(props.isHovering) + if (isHovering !== undefined) { + setIsOpen(isHovering) } - }, [props.isHovering]) + }, [isHovering]) return (