mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-04 22:40:29 +03:00
* add buttons to codeblocks * add functionality * Document functions * Improve accessibility * Remove pointless DefaultReset * implement some requested changes * fix content shift when expanding or collapsing --------- Co-authored-by: Ajay Bura <32841439+ajbura@users.noreply.github.com>
37 lines
913 B
TypeScript
37 lines
913 B
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
/**
|
|
* Temporarily sets a boolean state.
|
|
*
|
|
* @param duration - Duration in milliseconds before resetting (default: 1500)
|
|
* @param initial - Initial value (default: false)
|
|
*/
|
|
export function useTimeoutToggle(duration = 1500, initial = false): [boolean, () => void] {
|
|
const [active, setActive] = useState(initial);
|
|
const timeoutRef = useRef<number | null>(null);
|
|
|
|
const clear = () => {
|
|
if (timeoutRef.current !== null) {
|
|
clearTimeout(timeoutRef.current);
|
|
timeoutRef.current = null;
|
|
}
|
|
};
|
|
|
|
const trigger = useCallback(() => {
|
|
setActive(!initial);
|
|
clear();
|
|
timeoutRef.current = window.setTimeout(() => {
|
|
setActive(initial);
|
|
timeoutRef.current = null;
|
|
}, duration);
|
|
}, [duration, initial]);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
clear();
|
|
},
|
|
[]
|
|
);
|
|
|
|
return [active, trigger];
|
|
}
|