Overlay transition
This commit is contained in:
1 parent
a597a0d061
commit
f96b495b38
2 files changed
+57
-31
No files matched your search
@@ -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>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -98,32 +98,30 @@ export const Menu: React.FC<IMenuProps> = props => {
|
|||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{!open ? null : (
|
<Overlay
|
||||||
<Overlay
|
show={open}
|
||||||
opacity={1}
|
onClickOverlay={onClick}
|
||||||
onClickOverlay={onClick}
|
>
|
||||||
>
|
<div style={{
|
||||||
<div style={{
|
width: '50%',
|
||||||
width: '50%',
|
margin: '50px auto',
|
||||||
margin: '50px auto',
|
}}>
|
||||||
}}>
|
{props.items.map((item, index) => (
|
||||||
{props.items.map((item, index) => (
|
<li key={index}
|
||||||
<li key={index}
|
className={cx(
|
||||||
className={cx(
|
item.color, {
|
||||||
item.color, {
|
selected: item.highlighted,
|
||||||
selected: item.highlighted,
|
})}
|
||||||
})}
|
>
|
||||||
>
|
<MenuItem
|
||||||
<MenuItem
|
url={itemUrl(item.url)}
|
||||||
url={itemUrl(item.url)}
|
text={item.text}
|
||||||
text={item.text}
|
active={item.active}
|
||||||
active={item.active}
|
/>
|
||||||
/>
|
</li>
|
||||||
</li>
|
))}
|
||||||
))}
|
</div>
|
||||||
</div>
|
</Overlay>
|
||||||
</Overlay>
|
|
||||||
)}
|
|
||||||
</menu>
|
</menu>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user