2022-02-13 18:49:03 +01:00
|
|
|
import { useFade } from "hooks/useFade";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
interface BackdropProps {
|
|
|
|
onClick?: (e: MouseEvent) => void;
|
|
|
|
onBackdropHide?: () => void;
|
|
|
|
active?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useBackdrop(): [
|
|
|
|
(state: boolean) => void,
|
|
|
|
BackdropProps,
|
|
|
|
{ style: any }
|
|
|
|
] {
|
|
|
|
const [backdrop, setBackdropState] = useState(false);
|
|
|
|
const [isHighlighted, setisHighlighted] = useState(false);
|
|
|
|
|
|
|
|
const setBackdrop = (state: boolean) => {
|
|
|
|
setBackdropState(state);
|
|
|
|
if (state) setisHighlighted(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const backdropProps: BackdropProps = {
|
|
|
|
active: backdrop,
|
|
|
|
onBackdropHide() {
|
|
|
|
setisHighlighted(false);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const highlightedProps = {
|
|
|
|
style: isHighlighted
|
|
|
|
? {
|
|
|
|
zIndex: "1000",
|
|
|
|
position: "relative",
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
return [setBackdrop, backdropProps, highlightedProps];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Backdrop(props: BackdropProps) {
|
2022-03-06 14:41:51 +01:00
|
|
|
const clickEvent = props.onClick || (() => {});
|
2022-02-13 18:49:03 +01:00
|
|
|
const animationEvent = props.onBackdropHide || (() => {});
|
|
|
|
const [isVisible, setVisible, fadeProps] = useFade();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setVisible(!!props.active);
|
2022-02-13 19:29:25 +01:00
|
|
|
/* eslint-disable-next-line */
|
|
|
|
}, [props.active, setVisible]);
|
2022-02-13 18:49:03 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isVisible) animationEvent();
|
2022-02-13 19:29:25 +01:00
|
|
|
/* eslint-disable-next-line */
|
2022-02-13 18:49:03 +01:00
|
|
|
}, [isVisible]);
|
|
|
|
|
|
|
|
if (!isVisible) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2022-02-13 19:29:25 +01:00
|
|
|
className={`fixed top-0 left-0 right-0 z-[999] h-screen bg-black bg-opacity-50 opacity-100 transition-opacity ${
|
2022-02-13 18:49:03 +01:00
|
|
|
!isVisible ? "opacity-0" : ""
|
|
|
|
}`}
|
|
|
|
{...fadeProps}
|
|
|
|
onClick={(e) => clickEvent(e.nativeEvent)}
|
2022-03-06 14:41:51 +01:00
|
|
|
/>
|
2022-02-13 18:49:03 +01:00
|
|
|
);
|
|
|
|
}
|