Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

Commit

Permalink
fix: simplify and fix list item selection
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Murray <hellomikemurray@gmail.com>
  • Loading branch information
mikemurray committed Apr 7, 2020
1 parent d1bb593 commit 10c6fb6
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions package/src/NavigationDrawer/NavigationDrawer.js
@@ -1,5 +1,5 @@
import React from "react";
import { NavLink, useHistory } from "react-router-dom";
import { useHistory, useRouteMatch } from "react-router-dom";
import PropTypes from "prop-types";
import {
AppBar,
Expand All @@ -19,8 +19,6 @@ import { useTranslation } from "react-i18next";
import ShopLogo from "../ShopLogo";
import useRoutes from "../hooks/useRoutes";

const activeClassName = "nav-item-active";

const useStyles = makeStyles((theme) => ({
closeButton: {
"color": theme.palette.colors.white,
Expand All @@ -47,12 +45,21 @@ const useStyles = makeStyles((theme) => ({
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(2)
},
listItem: {
listItemRoot: {
"paddingLeft": theme.spacing(2),
"paddingRight": theme.spacing(2),
"&:hover": {
backgroundColor: theme.palette.colors.darkBlue600,
transition: `background-color ${theme.transitions.duration.shortest} ${theme.transitions.easing.easeInOut}`
"&$focusVisible": {
color: theme.palette.text.secondaryActive,
fontWeight: theme.typography.fontWeightSemiBold,
backgroundColor: theme.palette.colors.darkBlue600
},
"&$selected, &$selected:hover": {
color: theme.palette.text.secondaryActive,
fontWeight: theme.typography.fontWeightSemiBold,
backgroundColor: theme.palette.colors.darkBlue600
},
"&$selected $icon, &$selected:hover $icon": {
color: theme.palette.text.active
}
},
listItemText: {
Expand All @@ -62,24 +69,15 @@ const useStyles = makeStyles((theme) => ({
letterSpacing: 0.5,
color: theme.palette.colors.black15
},
listItemNested: {
"paddingTop": 0,
"paddingBottom": 0,
"paddingLeft": theme.spacing(8),
listItemButton: {
"borderRadius": theme.shape.borderRadius,
"&:hover": {
backgroundColor: theme.palette.colors.darkBlue600,
transition: `background-color ${theme.transitions.duration.shortest} ${theme.transitions.easing.easeInOut}`
textDecoration: "none",
backgroundColor: theme.palette.colors.darkBlue600
}
},
link: {
[`&.${activeClassName} span`]: {
color: theme.palette.text.secondaryActive,
fontWeight: theme.typography.fontWeightSemiBold
},
[`&.${activeClassName} $icon`]: {
color: theme.palette.text.active
}
}
/* Pseudo-class applied to the root element if `selected={true}`. */
selected: {}
}));

/**
Expand All @@ -97,9 +95,11 @@ function NavigationDrawer(props) {

const classes = useStyles();
const history = useHistory();
const routeMatch = useRouteMatch("/:any");
const primaryRoutes = useRoutes({ groups: ["navigation"] });
const { t } = useTranslation();


let drawerProps = {
classes: {
paper: classes.drawerPaper
Expand Down Expand Up @@ -141,26 +141,36 @@ function NavigationDrawer(props) {
</Toolbar>
</AppBar>
<List disablePadding>
{primaryRoutes.map((route) => (

{primaryRoutes.map(({
IconComponent,
href,
path,
navigationItemLabel
}) => (
<ListItem
button
className={classes.listItem}
key={route.path}
classes={{
root: classes.listItemRoot,
selected: classes.selected,
button: classes.listItemButton
}}
key={path}
selected={(href && href.startsWith(routeMatch.url)) || path.startsWith(routeMatch.url)}
onClick={() => {
history.push(route.href || route.path);
history.push(href || path);
setIsSettingsOpen(false);
onDrawerClose();
}}
>
<ListItemIcon className={classes.icon}>
{route.IconComponent && <route.IconComponent />}
{IconComponent && <IconComponent />}
</ListItemIcon>
<ListItemText
disableTypography
className={classes.listItemText}
>
{t(route.navigationItemLabel)}
{t(navigationItemLabel)}
</ListItemText>
</ListItem>
))}
Expand All @@ -185,19 +195,3 @@ NavigationDrawer.defaultProps = {
};

export default NavigationDrawer;

/*
<NavLink
activeClassName={activeClassName}
className={classes.link}
to={route.href || route.path}
key={route.path}
onClick={() => {
setIsSettingsOpen(false);
onDrawerClose();
}}
>
</NavLink>
*/

0 comments on commit 10c6fb6

Please sign in to comment.