diff --git a/pages/_app.tsx b/pages/_app.tsx index 76a2ef98..021c14a3 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -22,6 +22,10 @@ class MyApp extends App { } } + .noscroll { + overflow: hidden; + } + #__next { height: 100%; } diff --git a/src/components/Overlay/Transition.tsx b/src/components/Overlay/Transition.tsx new file mode 100644 index 00000000..f7f99a35 --- /dev/null +++ b/src/components/Overlay/Transition.tsx @@ -0,0 +1,43 @@ +import * as React from 'react' + +export interface ITransitionProps { + show: boolean +} + +export const Transition: React.FC = props => { + const [mounted, setMounted] = React.useState(props.show) + const [opacity, setOpacity] = React.useState(0) + const onTransitionEnd = React.useCallback(() => { + if (opacity === 0) { + setMounted(false) + } + }, [opacity]) + + React.useEffect(() => { + if (!props.show) { + setOpacity(0) + return + } + + setMounted(true) + let t = setTimeout(() => { + setOpacity(1) + }, 16) + + return () => { + clearTimeout(t) + } + }, [props.show]) + + return mounted && ( +
+ {props.children} +
+ ) +} diff --git a/src/components/Overlay/index.tsx b/src/components/Overlay/index.tsx index b91ea824..6669466d 100644 --- a/src/components/Overlay/index.tsx +++ b/src/components/Overlay/index.tsx @@ -1,66 +1,45 @@ import * as React from 'react' +import { Transition } from './Transition' export interface IOverlayProps { show: boolean onClickOverlay: () => void } -export const Overlay: React.FC = ({ children, ...props }) => { - const [mounted, setMounted] = React.useState(props.show) - const [opacity, setOpacity] = React.useState(0) +export const Overlay: React.FC = props => { const onClick = React.useCallback((event: React.MouseEvent) => { if (event.target === event.currentTarget) { props.onClickOverlay() } }, [props.onClickOverlay]) - const onTransitionEnd = React.useCallback(() => { - console.log('transition end', mounted, opacity) - if (opacity === 0) { - setMounted(false) - } - }, [opacity]) React.useEffect(() => { - if (!props.show) { - setOpacity(0) - return - } + document.body.classList.toggle('noscroll', props.show) - setMounted(true) - let t = setTimeout(() => { - setOpacity(1) - }, 16) - - return () => { - clearTimeout(t) - } }, [props.show]) - return mounted && ( -
- - transition: all 200ms; - } - `} - - {children} -
+ {props.children} + + ) }