I suspect that my Theme is not being applied (specifically the CSS transition) to a styled component
that I have wrapped in another function, however I am unsure why. I downloaded the Basic MUI Dashboard theme found here and here. I pulled this into my project and it worked just fine. To clean up the implementation of the Dashboard.tsx
file I refactored the Drawer
and AppBar
styled components out into their own files. The code below is my implementation for the 'Drawer' in the original demo; other than wrapping this code in a function called Sidebar
the code is the same and I have a similar implementation for the AppBar
.
function Sidebar(open: boolean, drawerWidth: number, toggleDrawer: any){
const Drawer = styled(MuiDrawer, { shouldForwardProp: (prop) => prop !== 'open' })(
({ theme, open }) => ({
'& .MuiDrawer-paper': {
position: 'relative',
whiteSpace: 'nowrap',
width: drawerWidth,
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
boxSizing: 'border-box',
...(!open && {
overflowX: 'hidden',
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
width: theme.spacing(7),
[theme.breakpoints.up('sm')]: {
width: theme.spacing(9),
},
}),
},
}),
);
return (
<Drawer variant="permanent" open={open}>
<Toolbar
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
px: [1],
}}
>
<IconButton onClick={toggleDrawer}>
<ChevronLeftIcon />
</IconButton>
</Toolbar>
<Divider />
<List component="nav">
{NavItems }
<Divider sx={{ my: 1 }} />
</List>
</Drawer>
);
}
export default Sidebar;
And then on the Dashboard.tsx
file I have this:
<ThemeProvider theme={mdTheme}>
//other components omitted
{Sidebar(open, drawerWidth, toggleDrawer)};
//other components omitted
</ThemeProvider>
The three props that are passed to Sidebar
are from the original demo (open is a state value, drawerWidth is the width of the sidebar, and toggleDrawer is a function that updates state). When I run the run the application with these changes, the user can still toggle the drawer open and closed like in the original demo, however the smooth transition that is present in the original demo is completely gone, instead the drawer just 'slams' open and shut.
I have tried different implementations using withTheme
, useTheme
, etc. but nothing I have done seems to work.
One of the similar questions suggested that position:relative
needed to be added to the Drawer container, but that is being applied as part of the call to styled
(as far as I can tell). Any help is appreciated.