use-fullscreen (use-fullscreen)
Enter and exit fullscreen mode for a given element or the entire page.
v0.1.0
Usage
import { use-fullscreen } from "@/hooks"1"use client";
2
3import React, { useRef } from "react";
4
5import { Button } from "@/components/ui/button";
6import {
7  Card,
8  CardContent,
9  CardDescription,
10  CardFooter,
11  CardHeader,
12  CardTitle,
13} from "@/components/ui/card";
14
15import { useFullscreen } from "@/components/hooks/use-fullscreen";
16
17export function UseFullscreenDemo() {
18  const boxRef = useRef<HTMLDivElement>(null);
19  const [isFullscreen, enter, exit] = useFullscreen(
20    boxRef as React.RefObject<HTMLElement>
21  );
22
23  return (
24    <Card className="relative max-w-md w-full">
25      <CardHeader>
26        <CardTitle>useFullscreen</CardTitle>
27        <CardDescription>
28          This component uses the <code>useFullscreen</code> hook to toggle
29          fullscreen mode for a specific element.
30        </CardDescription>
31      </CardHeader>
32      <CardContent>
33        <p className="text-lg font-semibold text-primary">
34          {isFullscreen ? "In Fullscreen" : "Not Fullscreen"}
35        </p>
36      </CardContent>
37      <CardFooter className="flex gap-2">
38        <Button onClick={enter} disabled={isFullscreen} variant="secondary">
39          Enter Fullscreen
40        </Button>
41        <Button onClick={exit} disabled={!isFullscreen} variant="destructive">
42          Exit Fullscreen
43        </Button>
44      </CardFooter>
45    </Card>
46  );
47}
48Install
npx wkkkis-hooks add use-fullscreenOptions
| Name | Type | Default | Description | 
|---|---|---|---|
| ref | RefObject<HTMLElement> | null | null | The ref of the element to enter/exit fullscreen. If null, uses the document. | 
Returns — Properties
| Name | Type | Default | Description | 
|---|---|---|---|
| Returns a tuple | — | — | [isFullscreen, enter, exit] . | 
| isFullscreen | — | — | is true if the element (or page) is in fullscreen. | 
| enter | — | — | and exit are functions to enter/exit fullscreen mode. | 
Cross-Browser Support
- The hook uses the appropriate vendor-prefixed Fullscreen API methods for compatibility with all major browsers.
 - Listeners for fullscreen changes are also vendor-prefixed.
 
Ref Behavior
- If a ref is provided, the hook will enter/exit fullscreen for that element.
 - If null is passed, the hook will use the entire document ( document.documentElement ).
 
State Updates & Event Handling
- The hook tracks whether the referenced element (or the page) is currently in fullscreen.
 - Listens for fullscreen change events and updates state accordingly.
 - The state is updated immediately on mount and whenever fullscreen changes.
 
Returned Values
- Returns a tuple: [isFullscreen, enter, exit] .
 - isFullscreen is true if the element (or page) is in fullscreen.
 - enter and exit are functions to enter/exit fullscreen mode.
 
Cleanup
- All event listeners are removed automatically when the component unmounts.
 
Caveats & Best Practices
- Only works in the browser (client-side). On the server, the functions are no-ops.
 - If the Fullscreen API is not available, the functions will be no-ops.
 - The element must be in the DOM and visible to enter fullscreen.
 - Some browsers may block fullscreen requests unless triggered by a user gesture.
 
Files in this hook
| Source | Destination | 
|---|---|
| files/use-fullscreen.ts | src/hooks/use-fullscreen.ts |