Skip to content

Instantly share code, notes, and snippets.

@raym
Created December 21, 2023 16:32
Show Gist options
  • Save raym/4feb3cc53b8b27d50ab703034b93d888 to your computer and use it in GitHub Desktop.
Save raym/4feb3cc53b8b27d50ab703034b93d888 to your computer and use it in GitHub Desktop.
Multiple Layer Page with React Router Outlets
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