Last active
March 14, 2024 00:58
-
-
Save github0013/7bba37c108cb8983ff3f0dd2f7102cc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const { awake, request, release } = useWakeLock() | |
... | |
... | |
return ( | |
... | |
onClick={() => { | |
// can't mix with other functions to make sure | |
// the video is played under a user interaction? | |
awake ? release() : request() | |
}} | |
... | |
) |
This file contains hidden or 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
import NoSleep from "nosleep.js" | |
import React from "react" | |
const useWakeLock = () => { | |
const noSleep = React.useMemo(() => new NoSleep(), []) | |
const [awake, set] = React.useState(noSleep.isEnabled) | |
const request = React.useCallback(() => { | |
noSleep.enable().then(() => set(noSleep.isEnabled)) | |
}, []) | |
const release = React.useCallback(() => { | |
noSleep.disable() | |
set(noSleep.isEnabled) | |
}, []) | |
return { | |
awake, | |
request, | |
release, | |
} | |
} | |
export { useWakeLock } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment