Overlay refactoring and improvement
This commit is contained in:
1 parent
04d6c8d223
commit
30f1274405
3 files changed
+70
-44
No files matched your search
@@ -22,6 +22,10 @@ class MyApp extends App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.noscroll {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
#__next {
|
#__next {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import * as React from 'react'
|
||||||
|
|
||||||
|
export interface ITransitionProps {
|
||||||
|
show: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Transition: React.FC<ITransitionProps> = 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 && (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
opacity,
|
||||||
|
transition: 'all 250ms',
|
||||||
|
}}
|
||||||
|
onTransitionEnd={onTransitionEnd}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,66 +1,45 @@
|
|||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
|
import { Transition } from './Transition'
|
||||||
|
|
||||||
export interface IOverlayProps {
|
export interface IOverlayProps {
|
||||||
show: boolean
|
show: boolean
|
||||||
onClickOverlay: () => void
|
onClickOverlay: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Overlay: React.FC<IOverlayProps> = ({ children, ...props }) => {
|
export const Overlay: React.FC<IOverlayProps> = props => {
|
||||||
const [mounted, setMounted] = React.useState(props.show)
|
|
||||||
const [opacity, setOpacity] = React.useState(0)
|
|
||||||
const onClick = React.useCallback((event: React.MouseEvent<HTMLDivElement>) => {
|
const onClick = React.useCallback((event: React.MouseEvent<HTMLDivElement>) => {
|
||||||
if (event.target === event.currentTarget) {
|
if (event.target === event.currentTarget) {
|
||||||
props.onClickOverlay()
|
props.onClickOverlay()
|
||||||
}
|
}
|
||||||
}, [props.onClickOverlay])
|
}, [props.onClickOverlay])
|
||||||
const onTransitionEnd = React.useCallback(() => {
|
|
||||||
console.log('transition end', mounted, opacity)
|
|
||||||
if (opacity === 0) {
|
|
||||||
setMounted(false)
|
|
||||||
}
|
|
||||||
}, [opacity])
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!props.show) {
|
document.body.classList.toggle('noscroll', props.show)
|
||||||
setOpacity(0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setMounted(true)
|
|
||||||
let t = setTimeout(() => {
|
|
||||||
setOpacity(1)
|
|
||||||
}, 16)
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearTimeout(t)
|
|
||||||
}
|
|
||||||
}, [props.show])
|
}, [props.show])
|
||||||
|
|
||||||
return mounted && (
|
return (
|
||||||
<div
|
<Transition
|
||||||
style={{
|
show={props.show}
|
||||||
opacity,
|
|
||||||
}}
|
|
||||||
onClick={onClick}
|
|
||||||
onTransitionEnd={onTransitionEnd}
|
|
||||||
>
|
>
|
||||||
<style jsx>{`
|
<div
|
||||||
div {
|
onClick={onClick}
|
||||||
position: fixed;
|
>
|
||||||
width: 100%;
|
<style jsx>{`
|
||||||
height: 100%;
|
div {
|
||||||
top: 0;
|
position: fixed;
|
||||||
left: 0;
|
width: 100%;
|
||||||
z-index: 1000000;
|
height: 100%;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
z-index: 1000000;
|
||||||
|
|
||||||
background-color: rgb(255, 255, 255);
|
background-color: rgb(255, 255, 255);
|
||||||
opacity: 0;
|
}
|
||||||
|
`}</style>
|
||||||
|
|
||||||
transition: all 200ms;
|
{props.children}
|
||||||
}
|
</div>
|
||||||
`}</style>
|
</Transition>
|
||||||
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user