@@ -4,11 +4,14 @@ import { keyframes } from '@emotion/react'
44import { Box , type BoxProps } from '@/components/Box'
55
66import { RemoveScroll } from 'react-remove-scroll'
7+ import { useEffect , useState } from 'react'
78
8- export interface OverlayProps extends BoxProps { }
9+ export interface OverlayProps extends BoxProps {
10+ lockScroll ?: boolean
11+ }
912
1013function OverlayWithRef (
11- { children, part, opacity = 0.5 , ...props } : OverlayProps ,
14+ { children, lockScroll = true , part, opacity = 0.5 , ...props } : OverlayProps ,
1215 ref : React . ForwardedRef < HTMLDivElement >
1316) {
1417 const fadeIn = keyframes `
@@ -20,8 +23,31 @@ function OverlayWithRef(
2023 }
2124 `
2225
26+ const [ hasScrolled , setHasScrolled ] = useState ( false )
27+
28+ useEffect ( ( ) => {
29+ if ( ! lockScroll ) {
30+ const handleScroll = ( ) => {
31+ if ( ! hasScrolled ) {
32+ setHasScrolled ( true )
33+ window . removeEventListener ( 'scroll' , handleScroll )
34+ }
35+ }
36+
37+ window . addEventListener ( 'scroll' , handleScroll )
38+
39+ return ( ) => {
40+ window . removeEventListener ( 'scroll' , handleScroll )
41+ }
42+ }
43+ } , [ lockScroll , hasScrolled ] )
44+
45+ if ( ! lockScroll && hasScrolled ) {
46+ return < > { children } </ >
47+ }
48+
2349 return (
24- < RemoveScroll forwardProps ref = { ref } >
50+ < RemoveScroll forwardProps ref = { ref } enabled = { lockScroll } >
2551 < Box
2652 animation = { `${ fadeIn } 300ms ease-out` }
2753 backgroundColor = "black"
0 commit comments