Overlay components -> ui
This commit is contained in:
1 parent
6c71a78fe6
commit
e8a4ba054b
4 files changed
+1
-1
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { MenuToggle } from "../MenuToggle"
|
||||
import { useMobile } from "src/hooks/useMobile"
|
||||
import { Menu } from "../Menu"
|
||||
import { Overlay } from "../Overlay"
|
||||
import { Overlay } from "@/ui/Overlay"
|
||||
import { useContext } from "react"
|
||||
import { ConfigContext } from "src/context/ConfigContext"
|
||||
import { Block } from "../Block"
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react"
|
||||
|
||||
export type TransitionProps = {
|
||||
children?: React.ReactNode
|
||||
duration: number
|
||||
extraStyle?: React.CSSProperties
|
||||
show: boolean
|
||||
}
|
||||
|
||||
export const Transition: React.FC<TransitionProps> = props => {
|
||||
const [mounted, setMounted] = useState(props.show)
|
||||
const [opacity, setOpacity] = useState(0)
|
||||
const onTransitionEnd = useCallback(() => {
|
||||
if (opacity === 0) {
|
||||
setMounted(false)
|
||||
}
|
||||
}, [opacity])
|
||||
const transition = useMemo(
|
||||
() => `all ${props.duration}ms`,
|
||||
[props.duration]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.show) {
|
||||
setOpacity(0)
|
||||
return
|
||||
}
|
||||
|
||||
setMounted(true)
|
||||
let t = setTimeout(() => {
|
||||
setOpacity(1)
|
||||
}, 16)
|
||||
|
||||
return () => {
|
||||
clearTimeout(t)
|
||||
}
|
||||
}, [props.show])
|
||||
|
||||
if (!mounted) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
...props.extraStyle,
|
||||
opacity,
|
||||
transition,
|
||||
}}
|
||||
onTransitionEnd={onTransitionEnd}
|
||||
>
|
||||
{props.children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import s from "./styles.module.css"
|
||||
|
||||
import { Transition } from "./Transition"
|
||||
import { useReducedMotion } from "../../hooks/useReducedMotion"
|
||||
import { useLockBodyScroll } from "react-use"
|
||||
|
||||
export type OverlayProps = {
|
||||
children?: React.ReactNode
|
||||
show: boolean
|
||||
onClickOverlay: () => void
|
||||
style?: React.CSSProperties
|
||||
duration: number
|
||||
}
|
||||
|
||||
export const Overlay: React.FC<OverlayProps> = props => {
|
||||
const motionDisabled = useReducedMotion()
|
||||
const duration = motionDisabled ? 0 : props.duration
|
||||
|
||||
useLockBodyScroll(props.show)
|
||||
|
||||
return (
|
||||
<Transition
|
||||
duration={duration}
|
||||
show={props.show}
|
||||
>
|
||||
<div
|
||||
onClick={(event) => {
|
||||
if (event.target === event.currentTarget) {
|
||||
props.onClickOverlay()
|
||||
}
|
||||
}}
|
||||
className={s.overlay}
|
||||
style={props.style}
|
||||
>
|
||||
{props.children}
|
||||
</div>
|
||||
</Transition>
|
||||
)
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
.overlay {
|
||||
position: fixed;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
top: 0;
|
||||
left: 0;
|
||||
overflow-y: auto;
|
||||
z-index: 1000000;
|
||||
|
||||
background-color: var(--color-back-main);
|
||||
}
|
||||
Reference in new issue
Block a user