mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-26 17:51:57 +01:00
28 lines
766 B
TypeScript
28 lines
766 B
TypeScript
|
import React, { useEffect, useState } from "react";
|
||
|
import './useFade.css'
|
||
|
|
||
|
export const useFade = (initial: boolean = false): [boolean, React.Dispatch<React.SetStateAction<boolean>>, any] => {
|
||
|
const [show, setShow] = useState<boolean>(initial);
|
||
|
const [isVisible, setVisible] = useState<boolean>(show);
|
||
|
|
||
|
// Update visibility when show changes
|
||
|
useEffect(() => {
|
||
|
if (show) setVisible(true);
|
||
|
}, [show]);
|
||
|
|
||
|
// When the animation finishes, set visibility to false
|
||
|
const onAnimationEnd = () => {
|
||
|
if (!show) setVisible(false);
|
||
|
};
|
||
|
|
||
|
const style = { animation: `${show ? "fadeIn" : "fadeOut"} .3s` };
|
||
|
|
||
|
// These props go on the fading DOM element
|
||
|
const fadeProps = {
|
||
|
style,
|
||
|
onAnimationEnd
|
||
|
};
|
||
|
|
||
|
return [isVisible, setShow, fadeProps];
|
||
|
};
|