|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +import { PropsWithChildren, useState } from 'react'; |
| 8 | +import { NestedMenuItem, NestedMenuItemProps } from 'mui-nested-menu'; |
| 9 | +import { Box, MenuItem, MenuItemProps, SxProps, Theme } from '@mui/material'; |
| 10 | +import { mergeSx } from '../../utils'; |
| 11 | + |
| 12 | +const styles = { |
| 13 | + highlightedParentLine: { |
| 14 | + backgroundColor: 'action.hover', |
| 15 | + color: 'primary.main', |
| 16 | + transition: 'all 300ms ease', |
| 17 | + }, |
| 18 | + highlightedLine: { |
| 19 | + transition: 'all 300ms ease', |
| 20 | + '&:hover': { |
| 21 | + backgroundColor: 'action.hover', |
| 22 | + color: 'primary.main', |
| 23 | + }, |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +interface CustomNestedMenuItemProps extends PropsWithChildren, Omit<NestedMenuItemProps, 'parentMenuOpen'> { |
| 28 | + sx?: SxProps<Theme>; |
| 29 | +} |
| 30 | + |
| 31 | +export function CustomNestedMenuItem({ sx, children, ...other }: Readonly<CustomNestedMenuItemProps>) { |
| 32 | + const [subMenuActive, setSubMenuActive] = useState(false); |
| 33 | + |
| 34 | + return ( |
| 35 | + <NestedMenuItem |
| 36 | + {...other} |
| 37 | + parentMenuOpen |
| 38 | + sx={mergeSx(subMenuActive ? styles.highlightedParentLine : styles.highlightedLine, sx)} |
| 39 | + > |
| 40 | + <Box onMouseEnter={() => setSubMenuActive(true)} onMouseLeave={() => setSubMenuActive(false)}> |
| 41 | + {children} |
| 42 | + </Box> |
| 43 | + </NestedMenuItem> |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +export function CustomMenuItem({ sx, ...other }: Readonly<MenuItemProps>) { |
| 48 | + return <MenuItem sx={mergeSx(styles.highlightedLine, sx)} {...other} />; |
| 49 | +} |
0 commit comments