Improve dom nesting

This commit is contained in:
dominik-zeglen 2020-09-08 13:11:01 +02:00
parent 3d571e3f4f
commit 4fa7730611
3 changed files with 40 additions and 23 deletions

View file

@ -43,6 +43,18 @@ const useStyles = makeStyles(
opacity: 1, opacity: 1,
transition: theme.transitions.duration.shortest + "ms" transition: theme.transitions.duration.shortest + "ms"
}, },
menuItemBtn: {
"&:focus": {
color: theme.palette.primary.main,
outline: 0
},
background: "none",
border: "none",
color: "inherit",
display: "inline-flex",
margin: 0,
padding: 0
},
paper: { paper: {
borderRadius: 16, borderRadius: 16,
cursor: "default", cursor: "default",
@ -58,8 +70,6 @@ const useStyles = makeStyles(
color: theme.palette.primary.main, color: theme.palette.primary.main,
outline: 0 outline: 0
}, },
background: "none",
border: "none",
borderBottomRightRadius: 100, borderBottomRightRadius: 100,
borderTopRightRadius: 100, borderTopRightRadius: 100,
color: fade(theme.palette.text.primary, 0.6), color: fade(theme.palette.text.primary, 0.6),
@ -89,7 +99,8 @@ const useStyles = makeStyles(
} }
}, },
"&:hover, &:focus": { "&:hover, &:focus": {
color: theme.palette.primary.main color: theme.palette.primary.main,
outline: 0
}, },
background: "none", background: "none",
border: "none", border: "none",
@ -109,7 +120,7 @@ const MenuItem: React.FC<MenuItemProps> = ({
}) => { }) => {
const classes = useStyles({}); const classes = useStyles({});
const [open, setOpen] = React.useState(false); const [open, setOpen] = React.useState(false);
const anchor = React.useRef<HTMLButtonElement>(null); const anchor = React.useRef<HTMLDivElement>(null);
const handleClick = (event: React.MouseEvent, menuItem: IMenuItem) => { const handleClick = (event: React.MouseEvent, menuItem: IMenuItem) => {
if (menuItem.children) { if (menuItem.children) {
@ -121,7 +132,7 @@ const MenuItem: React.FC<MenuItemProps> = ({
}; };
return ( return (
<button <div
className={classNames(classes.root, { className={classNames(classes.root, {
[classes.rootActive]: active, [classes.rootActive]: active,
[classes.rootExpanded]: !isMenuShrunk [classes.rootExpanded]: !isMenuShrunk
@ -129,6 +140,7 @@ const MenuItem: React.FC<MenuItemProps> = ({
ref={anchor} ref={anchor}
onClick={event => handleClick(event, menuItem)} onClick={event => handleClick(event, menuItem)}
> >
<button className={classes.menuItemBtn}>
{menuItem.icon && <SVG className={classes.icon} src={menuItem.icon} />} {menuItem.icon && <SVG className={classes.icon} src={menuItem.icon} />}
<Typography <Typography
aria-label={menuItem.ariaLabel} aria-label={menuItem.ariaLabel}
@ -141,6 +153,7 @@ const MenuItem: React.FC<MenuItemProps> = ({
> >
{menuItem.label} {menuItem.label}
</Typography> </Typography>
</button>
{menuItem.children && ( {menuItem.children && (
<Popper <Popper
className={classes.popper} className={classes.popper}
@ -176,7 +189,7 @@ const MenuItem: React.FC<MenuItemProps> = ({
</ClickAwayListener> </ClickAwayListener>
</Popper> </Popper>
)} )}
</button> </div>
); );
}; };

View file

@ -93,6 +93,7 @@ const SideBar: React.FC<SideBarProps> = ({
isMenuShrunk={isShrunk} isMenuShrunk={isShrunk}
menuItem={menuItem} menuItem={menuItem}
onClick={onMenuItemClick} onClick={onMenuItemClick}
key={menuItem.ariaLabel}
/> />
); );
})} })}

View file

@ -11,11 +11,14 @@ export function isMenuActive(location: string, menuItem: IMenuItem) {
); );
} }
return location.split("?")[0] === orderDraftListUrl().split("?")[0] && const activeUrl = location.split("?")[0];
menuItem.url.split("?")[0] === orderListUrl().split("?")[0] const menuItemUrl = menuItem.url.split("?")[0];
return activeUrl === orderDraftListUrl().split("?")[0] &&
menuItemUrl === orderListUrl().split("?")[0]
? false ? false
: !!matchPath(location.split("?")[0], { : !!matchPath(activeUrl, {
exact: menuItem.url.split("?")[0] === "/", exact: menuItemUrl === "/",
path: menuItem.url.split("?")[0] path: menuItemUrl
}); });
} }