-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a full screen button for the sakura view
- Loading branch information
1 parent
8ba951a
commit e3a34c5
Showing
2 changed files
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* Simple fullscreen hook implementation, does not work together with F11 => https://stackoverflow.com/a/21118401 | ||
*/ | ||
export function useFullscreen(): [boolean, () => Promise<void>] { | ||
const [isFullscreen, setIsFullscreen] = useState(false); | ||
|
||
const close = async () => { | ||
if ( | ||
isFullscreen && | ||
document.fullscreenElement === document.documentElement | ||
) { | ||
await document.exitFullscreen(); | ||
} | ||
}; | ||
|
||
const open = async () => { | ||
if (document.fullscreenEnabled && !isFullscreen) { | ||
await document.documentElement.requestFullscreen(); | ||
} | ||
}; | ||
|
||
const setFullscreen = async (state: boolean) => { | ||
return state ? await open() : await close(); | ||
}; | ||
|
||
const toggleFullscreen = async () => { | ||
await setFullscreen(!isFullscreen); | ||
}; | ||
|
||
// Update the state of the hook if the full screenstate is changed outside of the React application. | ||
useEffect(() => { | ||
const handler = () => { | ||
setIsFullscreen(document.fullscreenElement === document.documentElement); | ||
}; | ||
|
||
document.addEventListener("fullscreenchange", handler); | ||
|
||
return () => { | ||
document.removeEventListener("fullscreenchange", handler); | ||
}; | ||
}, []); | ||
|
||
return [isFullscreen, toggleFullscreen]; | ||
} |