close sidebar after route change

This commit is contained in:
Dawid Wysokiński 2021-01-01 14:47:05 +01:00
parent d62fb59503
commit 893b433d44
2 changed files with 15 additions and 6 deletions

View File

@ -26,11 +26,15 @@ function PageLayout({ children }: Props) {
const shouldOpenSidebar = isDesktop ? true : open; const shouldOpenSidebar = isDesktop ? true : open;
const openSidebar = () => { const openSidebar = () => {
if (!isDesktop) {
setOpen(true); setOpen(true);
}
}; };
const closeSidebar = () => { const closeSidebar = () => {
if (!isDesktop) {
setOpen(false); setOpen(false);
}
}; };
return ( return (

View File

@ -1,4 +1,5 @@
import React from 'react'; import React, { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import clsx from 'clsx'; import clsx from 'clsx';
import { TFunction } from 'i18next'; import { TFunction } from 'i18next';
import useServer from '@features/ServerPage/libs/ServerContext/useServer'; import useServer from '@features/ServerPage/libs/ServerContext/useServer';
@ -31,8 +32,8 @@ export interface Props {
t: TFunction; t: TFunction;
className?: string; className?: string;
open: boolean; open: boolean;
onClose: React.ReactEventHandler<{}>; onClose: () => void;
onOpen: React.ReactEventHandler<{}>; onOpen: () => void;
variant?: DrawerProps['variant']; variant?: DrawerProps['variant'];
} }
@ -40,7 +41,7 @@ const Sidebar = ({ t, className, open, variant, onClose, onOpen }: Props) => {
const classes = useStyles(); const classes = useStyles();
const { key } = useServer(); const { key } = useServer();
const theme = useTheme(); const theme = useTheme();
const { pathname } = useLocation();
const routes: Route[] = [ const routes: Route[] = [
{ {
name: t('pageLayout.sidebar.routes.dashboard'), name: t('pageLayout.sidebar.routes.dashboard'),
@ -136,6 +137,10 @@ const Sidebar = ({ t, className, open, variant, onClose, onOpen }: Props) => {
}, },
]; ];
useEffect(() => {
onClose(); // eslint-disable-next-line
}, [pathname]);
return ( return (
<SwipeableDrawer <SwipeableDrawer
anchor="left" anchor="left"