Skip to main content

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 — true while the target is fullscreen.
  • isSupported — true if 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 enter or toggle from an event handler.
  • The returned promises reject if the browser refuses the request; handle that with try / catch if needed.
  • On the server, isSupported is false and all actions are no-ops.