saleor-dashboard/src/components/AppLayout/MenuNested.tsx

192 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-08-09 11:14:35 +00:00
import Hidden from "@material-ui/core/Hidden";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import Typography from "@material-ui/core/Typography";
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-08-09 11:14:35 +00:00
import SVG from "react-inlinesvg";
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
import menuArrowIcon from "@assets/images/menu-arrow-icon.svg";
import useTheme from "@saleor/hooks/useTheme";
import { createHref } from "@saleor/misc";
2019-11-13 12:57:46 +00:00
import { drawerNestedMenuWidth, drawerWidthExpandedMobile } from "./consts";
2019-08-09 11:14:35 +00:00
import { IActiveSubMenu } from "./MenuList";
2019-06-19 14:40:52 +00:00
import { IMenuItem } from "./menuStructure";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(theme => ({
menuListNested: {
background: theme.palette.background.paper,
height: "100vh",
position: "absolute",
right: 0,
top: 0,
transition: `right ${theme.transitions.duration.shorter}ms ease`,
2019-11-13 12:56:51 +00:00
width: drawerNestedMenuWidth,
2019-10-30 14:34:24 +00:00
zIndex: -1
},
menuListNestedClose: {
"& svg": {
fill: theme.palette.primary.main,
2019-11-04 19:44:17 +00:00
left: 11,
2019-10-30 14:34:24 +00:00
position: "relative",
2019-11-04 19:44:17 +00:00
top: 1
2019-08-09 11:14:35 +00:00
},
2019-10-30 14:34:24 +00:00
border: `solid 1px #EAEAEA`,
borderRadius: "100%",
cursor: "pointer",
height: 32,
position: "absolute",
right: 32,
top: 35,
transform: "rotate(180deg)",
width: 32
},
menuListNestedCloseDark: {
border: `solid 1px #252728`
},
menuListNestedHide: {
opacity: 0
},
menuListNestedIcon: {
"& path": {
fill: "initial"
2019-08-09 11:14:35 +00:00
},
2019-10-30 14:34:24 +00:00
"& svg": { height: 32, position: "relative", top: 7, width: 32 }
},
menuListNestedIconDark: {
"& path": {
fill: theme.palette.common.white
}
},
menuListNestedItem: {
"&:hover": {
"& p": {
color: theme.palette.primary.main
2019-08-09 11:14:35 +00:00
}
},
2019-10-30 14:34:24 +00:00
display: "block",
marginBottom: theme.spacing(2),
padding: "0px 30px",
textDecoration: "none"
},
menuListNestedOpen: {
[theme.breakpoints.down("sm")]: {
right: 0,
2019-11-04 19:44:17 +00:00
width: drawerWidthExpandedMobile,
2019-10-30 14:34:24 +00:00
zIndex: 2
2019-08-09 11:14:35 +00:00
},
2019-11-13 12:56:51 +00:00
right: -drawerNestedMenuWidth,
width: drawerNestedMenuWidth,
2019-10-30 14:34:24 +00:00
zIndex: -1
},
subHeader: {
borderBottom: "solid 1px #EAEAEA",
margin: "30px",
marginBottom: 39,
paddingBottom: 22
},
subHeaderDark: {
borderBottom: "solid 1px #252728"
},
subHeaderTitle: {
[theme.breakpoints.up("md")]: {
paddingLeft: 0
2019-08-09 11:14:35 +00:00
},
2019-10-30 14:34:24 +00:00
display: "inline",
paddingLeft: 10
}
}));
2019-08-09 11:14:35 +00:00
2019-06-19 14:40:52 +00:00
export interface MenuNestedProps {
2019-08-09 11:14:35 +00:00
activeItem: IActiveSubMenu;
ariaLabel: string;
closeSubMenu: ({ isActive, label }: IActiveSubMenu) => void;
icon: string;
2019-06-19 14:40:52 +00:00
menuItem: IMenuItem;
2019-08-09 11:14:35 +00:00
title: string;
handleSubMenu: (itemLabel: string) => void;
2019-06-19 14:40:52 +00:00
onMenuItemClick: (url: string, event: React.MouseEvent<any>) => void;
}
2019-10-30 14:34:24 +00:00
const MenuNested: React.FC<MenuNestedProps> = props => {
const {
2019-08-09 11:14:35 +00:00
activeItem,
ariaLabel,
2019-10-30 14:34:24 +00:00
2019-08-09 11:14:35 +00:00
closeSubMenu,
icon,
menuItem,
onMenuItemClick,
title
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
const menuItems = menuItem.children;
const { isDark } = useTheme();
const closeMenu = (menuItemUrl, event) => {
onMenuItemClick(menuItemUrl, event);
closeSubMenu({
isActive: false,
label: null
});
event.stopPropagation();
event.preventDefault();
};
return (
<>
<div
className={classNames(classes.menuListNested, {
[classes.menuListNestedOpen]:
activeItem.label === ariaLabel && activeItem.isActive
})}
>
<Typography
className={classNames(classes.subHeader, {
[classes.subHeaderDark]: isDark
2019-08-09 11:14:35 +00:00
})}
2019-10-30 14:34:24 +00:00
variant="h5"
2019-08-09 11:14:35 +00:00
>
2019-10-30 14:34:24 +00:00
<Hidden mdUp>
<SVG
className={classNames(classes.menuListNestedIcon, {
[classes.menuListNestedIconDark]: isDark
})}
src={icon}
/>
</Hidden>
<div className={classes.subHeaderTitle}>{title}</div>
<Hidden mdUp>
<div
className={classNames(classes.menuListNestedClose, {
[classes.menuListNestedCloseDark]: isDark
})}
data-tc={ariaLabel}
onClick={() =>
closeSubMenu({
isActive: false,
label: null
})
}
>
<SVG src={menuArrowIcon} />
</div>
</Hidden>
</Typography>
{menuItems.map(item => (
<a
className={classNames(classes.menuListNestedItem)}
href={createHref(item.url)}
data-tc={ariaLabel}
onClick={event => closeMenu(item.url, event)}
key={item.label}
2019-08-09 11:14:35 +00:00
>
2019-10-30 14:34:24 +00:00
<Typography aria-label={item.ariaLabel}>{item.label}</Typography>
</a>
))}
</div>
</>
);
};
MenuNested.displayName = "MenuNested";
2019-06-19 14:40:52 +00:00
export default MenuNested;