-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from musehq/dev
v2.12.1
- Loading branch information
Showing
4 changed files
with
21 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { useCallback, useRef, useState } from "react"; | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import Timeout = NodeJS.Timeout; | ||
|
||
/** | ||
* A hook that allows you to toggle a boolean value. | ||
* @param delay time in ms | ||
*/ | ||
export const useDelayedToggle = (delay = 1000) => { | ||
const lastTimeSet = useRef(0); | ||
const timeoutId = useRef<Timeout | null>(null); | ||
const [active, setActiveState] = useState(false); | ||
|
||
if (active && Date.now() - lastTimeSet.current > delay) { | ||
lastTimeSet.current = Date.now(); | ||
setActiveState(!active); | ||
} | ||
|
||
const setActive = useCallback(() => { | ||
if (timeoutId.current !== null) { | ||
clearTimeout(timeoutId.current); | ||
} | ||
|
||
setActiveState(true); | ||
lastTimeSet.current = Date.now(); | ||
setTimeout(() => { | ||
if (Date.now() - lastTimeSet.current > delay) setActiveState(false); | ||
|
||
timeoutId.current = setTimeout(() => { | ||
setActiveState(false); | ||
}, delay); | ||
}, [delay]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (timeoutId.current !== null) { | ||
clearTimeout(timeoutId.current); | ||
} | ||
}; | ||
}, []); | ||
|
||
return { active, setActive }; | ||
}; |
14bb8bf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
spacesvr – ./
spacesvr.vercel.app
spacesvr-git-master-muse-hq.vercel.app
spacesvr-muse-hq.vercel.app
www.spacesvr.io
spacesvr.io