Skip to content

Commit 1913f75

Browse files
committed
Improve sidebar toggle performance
1 parent ff1324c commit 1913f75

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/_layouts/DashboardLayout/DashboardLayout.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef } from 'react'
1+
import React, { useState, useEffect, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import clsx from 'clsx'
44
import useComponentSize from '@rehooks/component-size'
@@ -19,19 +19,23 @@ const DashboardLayout = (
1919
},
2020
) => {
2121
const refHeaderContainer = useRef(null)
22-
// const refSidebarContainer = useRef(null)
23-
// const refFooterContainer = useRef(null)
2422

2523
const classes = useStyles()
2624
const theme = useTheme()
2725
const isDesktop = useMediaQuery(theme.breakpoints.up('md'))
2826
const isMobile = !isDesktop
2927

28+
const [headerHeight, setHeaderHeight] = useState(0)
3029
const [isSidebarOpenMobile, setIsSidebarOpenMobile] = useState(false)
3130
const [isSidebarOpenDesktop, setIsSidebarOpenDesktop] = useState(true)
3231
const [isSidebarCollapsed, setIsSidebarCollapsed] = useState(false)
3332

34-
const headerSize = useComponentSize(refHeaderContainer)
33+
useEffect(() => {
34+
// code to run on component mount
35+
setHeaderHeight(refHeaderContainer.current.offsetHeight)
36+
}, [])
37+
38+
// const headerSize = useComponentSize(refHeaderContainer)
3539
// const sidebarSize = useComponentSize(refSidebarContainer)
3640
// const footerSize = useComponentSize(refFooterContainer)
3741

@@ -116,7 +120,7 @@ const DashboardLayout = (
116120
<main
117121
className={classes.mainContainer}
118122
style={{
119-
paddingTop: headerSize.height,
123+
paddingTop: headerHeight, // || headerSize.height,
120124
}}
121125
>
122126
<div className={classes.contentContainer}>{children}</div>
@@ -184,10 +188,10 @@ const useStyles = makeStyles(theme => ({
184188
drawer: {
185189
width: '100%',
186190
position: 'absolute',
187-
// [theme.breakpoints.up('md')]: {
188-
// width: theme.sidebar.width,
189-
// flexShrink: 0,
190-
// },
191+
[theme.breakpoints.down('sm')]: {
192+
width: theme.sidebar.width,
193+
flexShrink: 0,
194+
},
191195
},
192196
mainContainer: {
193197
flexGrow: 1,

0 commit comments

Comments
 (0)