Files
misode.github.io/src/app/hooks/useActiveTimout.ts
Misode c6c52ca41a Projects (#192)
* Add file save UI and drafts project

* Fix build

* Create SearchList component as abstraction

* Add project page and file tree view

* Create Locale context

* Create Theme context

* Create Version context

* Create Title context

* Create Project context

* Store current file in project context

* Fix issues when renaming file and implement deleting

* Style improvements

* Make all project strings translatable

* Fix z-index
2022-01-18 01:02:19 +01:00

22 lines
641 B
TypeScript

import { useRef, useState } from 'preact/hooks'
interface ActiveTimeoutOptions {
cooldown?: number,
invert?: boolean,
initial?: boolean,
}
export function useActiveTimeout({ cooldown, invert, initial }: ActiveTimeoutOptions = {}): [boolean | undefined, () => unknown] {
const [active, setActive] = useState(initial)
const timeout = useRef<number | undefined>(undefined)
const trigger = () => {
setActive(invert ? false : true)
if (timeout.current !== undefined) clearTimeout(timeout.current)
timeout.current = setTimeout(() => {
setActive(invert ? true : false)
}, cooldown ?? 2000) as any
}
return [active, trigger]
}