2019-11-18 23:37:36 +00:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
|
|
|
export function useIntermittentClass(
|
|
|
|
classname: string,
|
|
|
|
activeMilliseconds: number,
|
|
|
|
inactiveMillisecond: number,
|
2021-01-02 10:58:24 +00:00
|
|
|
startMillisecond?: number,
|
|
|
|
) {
|
2019-11-18 23:37:36 +00:00
|
|
|
const [currentClass, setCurrentClass] = useState("");
|
|
|
|
const [firstTime, setFirstTime] = useState(true);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let timeout: NodeJS.Timeout;
|
|
|
|
|
|
|
|
if (firstTime) {
|
|
|
|
if (startMillisecond && startMillisecond > 0) {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setCurrentClass(classname);
|
|
|
|
setFirstTime(false);
|
|
|
|
}, startMillisecond);
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setCurrentClass(classname);
|
|
|
|
setFirstTime(false);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (currentClass === "") {
|
|
|
|
timeout = setTimeout(() => setCurrentClass(classname), inactiveMillisecond);
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(() => setCurrentClass(""), activeMilliseconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
}, [currentClass, classname, activeMilliseconds, inactiveMillisecond, startMillisecond, firstTime]);
|
|
|
|
|
|
|
|
return currentClass;
|
2021-01-02 10:58:24 +00:00
|
|
|
}
|