Overlay transition

This commit is contained in:
tmshv committed 2019-10-15 22:07:34 +03:00
1 parent a597a0d061
commit f96b495b38
2 files changed
+34 -8

No files matched your search

+33 -5
View File
@@ -1,23 +1,48 @@
import * as React from 'react' import * as React from 'react'
export interface IOverlayProps { export interface IOverlayProps {
opacity: number show: boolean
onClickOverlay: () => void onClickOverlay: () => void
} }
export const Overlay: React.FC<IOverlayProps> = props => { export const Overlay: React.FC<IOverlayProps> = ({ children, ...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])
return ( React.useEffect(() => {
if (!props.show) {
setOpacity(0)
return
}
setMounted(true)
let t = setTimeout(() => {
setOpacity(1)
}, 16)
return () => {
clearTimeout(t)
}
}, [props.show])
return mounted && (
<div <div
style={{ style={{
opacity: props.opacity opacity,
}} }}
onClick={onClick} onClick={onClick}
onTransitionEnd={onTransitionEnd}
> >
<style jsx>{` <style jsx>{`
div { div {
@@ -29,10 +54,13 @@ export const Overlay: React.FC<IOverlayProps> = props => {
z-index: 1000000; z-index: 1000000;
background-color: rgb(255, 255, 255); background-color: rgb(255, 255, 255);
opacity: 0;
transition: all 200ms;
} }
`}</style> `}</style>
{props.children} {children}
</div> </div>
) )
} }
+1 -3
View File
@@ -98,9 +98,8 @@ export const Menu: React.FC<IMenuProps> = props => {
</li> </li>
))} ))}
{!open ? null : (
<Overlay <Overlay
opacity={1} show={open}
onClickOverlay={onClick} onClickOverlay={onClick}
> >
<div style={{ <div style={{
@@ -123,7 +122,6 @@ export const Menu: React.FC<IMenuProps> = props => {
))} ))}
</div> </div>
</Overlay> </Overlay>
)}
</menu> </menu>
) )
} }