Created
December 21, 2023 16:32
-
-
Save raym/4feb3cc53b8b27d50ab703034b93d888 to your computer and use it in GitHub Desktop.
Multiple Layer Page with React Router Outlets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { disableBodyScroll, enableBodyScroll } from 'body-scroll-lock' | |
import { isNil, isNotNil } from 'ramda' | |
import React, { useEffect, useRef } from 'react' | |
import { useOutlet } from 'react-router-dom' | |
import renderIfElse from '../../lib/renderIfElse' | |
import Overlay from '../../portals/Overlay' | |
const MyMultiLevelPageComponent = () => { | |
const outletWrapperRef = useRef(null) | |
const outlet = useOutlet() | |
useEffect(() => { | |
const element = outletWrapperRef.current | |
if (isNil(element)) return | |
if (outlet) { | |
disableBodyScroll(element) | |
} else { | |
enableBodyScroll(element) | |
} | |
return () => enableBodyScroll(element) | |
}, [outlet]) | |
return ( | |
<> | |
{renderIfElse( | |
outlet, | |
<div ref={outletWrapperRef}> | |
<Overlay> | |
<div | |
style={{ | |
minHeight: '100vh', | |
minWidth: '100vw', | |
display: 'flex', | |
flexDirection: 'column', | |
}} | |
> | |
{outlet} | |
</div> | |
</Overlay> | |
</div>, | |
null | |
)} | |
<div | |
aria-hidden={isNotNil(outlet)} | |
style={isNotNil(outlet) ? { visibility: 'hidden' } : {}} | |
> | |
<RestOfPage /> | |
</div> | |
</> | |
) | |
} | |
export { MyMultiLevelPageComponent } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment