useFullscreen
The useFullscreen hook wraps the browser Fullscreen API. Attach the returned ref to any element to make just that element fullscreen, or skip the ref to take over the whole page. The state stays in sync when the user leaves fullscreen with the Escape key.
Usage
Installation
import { useFullscreen } from "hookstorm";
Parameters
This hook does not take any parameters.
Return Values
- ref — A ref to attach to the target element. Optional: without it, the whole page is used.
- isFullscreen —
truewhile the target is fullscreen. - isSupported —
trueif the browser supports the Fullscreen API. - enter() — Requests fullscreen. Returns a promise.
- exit() — Leaves fullscreen. Returns a promise.
- toggle() — Enters or leaves fullscreen. Returns a promise.
Example Usage
import { ReactElement, RefObject } from "react";
import { useFullscreen } from "hookstorm";
export default function VideoPlayer(): ReactElement {
const { ref, isFullscreen, isSupported, toggle } = useFullscreen();
return (
<div ref={ref as RefObject<HTMLDivElement>}>
<video src="/movie.mp4" controls />
{isSupported && (
<button onClick={toggle}>
{isFullscreen ? "Exit fullscreen" : "Go fullscreen"}
</button>
)}
</div>
);
}
Notes
- Browsers only allow fullscreen requests from a user gesture such as a click, so call
enterortogglefrom an event handler. - The returned promises reject if the browser refuses the request; handle that with
try / catchif needed. - On the server,
isSupportedisfalseand all actions are no-ops.