I have Header with Toolbar. Clicking on left icon it will open Left sidebar like so
enter image description here
But if I open the right sidebar, when I open it, it shifts the icons on the left under the left open sidebar, but this shouldn’t happen, the icons should stay in place
And the second problem is that when I open the right sidebar the content inside the page is not narrowed on the right side.
enter image description here
How it should like
enter image description here
import { FC } from 'react';
import { styled, Theme, CSSObject } from '@mui/material/styles';
import MuiDrawer from '@mui/material/Drawer';
const drawerWidth = 256;
const openedMixin = (theme: Theme): CSSObject => ({
width: drawerWidth,
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
overflowX: 'hidden',
});
const closedMixin = (theme: Theme): CSSObject => ({
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: 'hidden',
width: 0,
[theme.breakpoints.up('sm')]: {
width: 0,
},
});
const Drawer = styled(MuiDrawer, {
shouldForwardProp: (prop) => prop !== 'open',
})(({ theme, open }) => ({
width: drawerWidth,
flexShrink: 0,
whiteSpace: 'nowrap',
boxSizing: 'border-box',
position: 'absolute',
right: 0,
top: 0,
...(open && {
...openedMixin(theme),
'& .MuiDrawer-paper': openedMixin(theme),
}),
...(!open && {
...closedMixin(theme),
'& .MuiDrawer-paper': closedMixin(theme),
}),
}));
interface RightSidebarProps {
open: boolean;
}
export const RightSidebar: FC<RightSidebarProps> = ({ open }) => {
return (
<Drawer variant="permanent" anchor="right" open={open}>
<div>Right sidebar</div>
</Drawer>
);
};
Header with sidebars
const drawerWidth = 256;
const closedMixin = (theme: Theme): CSSObject => ({
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: 'hidden',
width: `calc(${theme.spacing(7)} + 1px)`,
[theme.breakpoints.up('sm')]: {
width: `calc(${theme.spacing(8)} + 1px)`,
},
});
const DrawerHeader = styled('div')(({ theme }) => ({
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
padding: theme.spacing(0, 1),
...theme.mixins.toolbar,
}));
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== 'openLeft' && prop !== 'openRight',
})<AppBarProps>(({ theme, openLeft, openRight }) => {
const { isMedium } = useBreakpointsContext();
return {
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
...(openLeft &&
!isMedium && {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: `${drawerWidth}px`,
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
}),
...(!openLeft &&
!isMedium && {
width: `calc(100% - ${closedMixin(theme).width})`,
marginLeft: `${closedMixin(theme).width}`,
}),
...(openRight && {
width: `calc(100% - ${drawerWidth}px)`,
marginRight: `${drawerWidth}px`,
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
}),
};
});
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<AppBar
position="absolute"
openLeft={openLeftSidebar}
openRight={openRightSidebar}
color="transparent"
elevation={0}
sx={{
borderBottom: 1,
borderColor: isDarkMode
? 'rgba(255, 255, 255, 0.12)'
: '#E0E0E0',
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
}}
>
<Toolbar disableGutters sx={{ padding: '12px 31px' }}>
{!isMedium && (
<IconButton
color="inherit"
aria-label="open drawer"
onClick={toggleLeftSidebar}
edge="start"
disableRipple
sx={{
transform: 'rotate(180deg)',
marginRight: '8px',
}}
>
<ViewSidebarIcon />
</IconButton>
)}
{!isMedium && (
<Box>
<IconButton
disableRipple
onClick={handleBack}
disabled={currentIndex <= 0}
>
<ChevronLeftIcon
color="primary"
sx={{ width: 20, height: 20 }}
/>
</IconButton>
<IconButton
color="primary"
disableRipple
onClick={handleForward}
disabled={currentIndex >= history.length - 1}
>
<ChevronRightIcon
sx={{ width: 20, height: 20 }}
/>
</IconButton>
</Box>
)}
<BreadcrumbsMUI onMoveToPage={onMoveToPage} />
</Toolbar>
{isSmall && location.pathname === onboardingPath && pageTitle}
<Toolbar>
{isTablet && location.pathname === walletPath && pageTitle}
<IconButton
color="inherit"
disableRipple
onClick={toggleTheme}
>
{isDarkMode ? (
<LightModeOutlinedIcon />
) : (
<DarkModeOutlinedIcon />
)}
</IconButton>
<IconButton onClick={handleHelpDialogOpen} disableRipple>
<HelpOutlineIcon
sx={{ width: 24, height: 24 }}
color="primary"
/>
</IconButton>
{location.pathname !== onboardingPath && (
<IconButton color="inherit" disableRipple onClick={toggleRightSidebar}>
<Badge>
<NotificationsOutlinedIcon />
</Badge>
</IconButton>
)}
</Toolbar>
</AppBar>
{!isMedium && (
<LeftSidebar
open={openLeftSidebar}
openAccount={openAccount}
handleAccountClick={handleAccountClick}
onMoveToPage={onMoveToPage}
userData={userData}
dealsCount={dealsCount}
avatarImg={avatarImg}
/>
)}
{isMedium && (
<BottomNav
onMoveToPage={onMoveToPage}
onboardingComplete={userData?.onboarding_complete}
/>
)}
<RightSidebar open={openRightSidebar} />
<Box
component="main"
sx={{
width: '100%',
}}
>
<DrawerHeader />
<Snackbar open={!userData?.allowed_operations}>
<Alert
severity="warning"
variant="filled"
sx={{ width: '100%' }}
>
Your account is being reviewed for approval. If
approved, you will gain access to further details and
the ability to make an investment.
</Alert>
</Snackbar>
<Box sx={{ flexGrow: 1, padding: isMedium ? '24px' : '32px' }}>
{children}
</Box>
</Box>
<HelpDialog open={helpDialogOpen} onClose={handleHelpDialogClose} />
</Box>
I think it could because of the wrong margin calculation. To solve second problem I can just simply add margin when right sidebar is open, not sure it’s the best solution