Fullscreen
A fullscreen button and commands, with a fallback for iPhones.
import { fullscreen } from "react-video-kit/fullscreen";Adds 860 B to a player2.0 KB on its ownSource
Adds a fullscreen button to the controls. It takes the whole player fullscreen, so your controls and overlays come with it. Press the button below and the label in the corner changes to active. Press Escape and it follows that too. The gestures plugin adds double-click, and the hotkeys plugin adds F.
Supported: no. Active: no.
"use client";import { useVideoState } from "react-video-kit";import { fullscreen } from "react-video-kit/fullscreen";import { VideoPlayer } from "react-video-kit/player";const plugins = [fullscreen()];function FullscreenLabel() { const supported = useVideoState(fullscreen, (state) => state.supported); const active = useVideoState(fullscreen, (state) => state.active); return ( <p className="pointer-events-none absolute top-3 left-1/2 -translate-x-1/2 rounded-full bg-black/45 px-3 py-1 text-xs whitespace-nowrap text-white backdrop-blur-md"> Supported: {supported ? "yes" : "no"}. Active: {active ? "yes" : "no"}. </p> );}export default function Fullscreen() { return ( <VideoPlayer src="/media/sample-720.mp4" poster="/media/poster.jpg" plugins={plugins} load="visible"> <FullscreenLabel /> </VideoPlayer> );}Usage
import { fullscreen } from "react-video-kit/fullscreen";import { VideoPlayer } from "react-video-kit/player";const plugins = [fullscreen()];<VideoPlayer src="/video.mp4" plugins={plugins} />;On touch devices, entering fullscreen also tries to turn the screen to suit the video: landscape, or upright for a vertical video. To turn that off:
fullscreen({ lockOrientation: false });When the button shows
The button renders nothing while supported is false. The plugin works it out in two steps:
- When the player mounts,
supportedisdocument.fullscreenEnabled. If the browser doesn't define that, it's whether the player's root haswebkitRequestFullscreen. - When the video attaches, if
supportedis still false,document.fullscreenEnabledisn'tfalse, and the video haswebkitEnterFullscreen, it becomes true. This is how an iPhone gets the button.
So the button hides where fullscreen isn't available, such as an iframe whose permissions deny it.
supported is false on the server, so the button isn't in the server-rendered HTML. It appears once the player mounts.
What goes fullscreen
enter uses the first of these that exists:
requestFullscreen()on the player's root.webkitRequestFullscreen()on the player's root.webkitEnterFullscreen()on the video. This is the iPhone path. Only the video goes fullscreen, so the player's controls and overlays aren't part of it. The captions plugin hands its cues to the browser while this is active.
Build your own control
enter and toggle return a promise that rejects if the browser refuses, for example outside a click or key press. Catch that in your own control.
"use client";import { useCommands, useVideoState } from "react-video-kit";import { fullscreen } from "react-video-kit/fullscreen";function FullscreenToggle() { const { toggle } = useCommands(fullscreen); const active = useVideoState(fullscreen, (state) => state.active); const supported = useVideoState(fullscreen, (state) => state.supported); if (!supported) return null; return <button onClick={() => toggle().catch(() => {})}>{active ? "Exit" : "Fullscreen"}</button>;}Fullscreen is the button from the controls, which the plugin adds to the top-start slot. It catches a refusal itself, and nothing changes. In your own layout, render the slot or the button itself.
Behaviour
activeis true only while this player is the fullscreen element, or while its video is fullscreen on the iPhone path. It follows exits that the browser performs, such as Escape, and ignores another element going fullscreen.- The plugin sets
data-fullscreenon the player's root whileactiveis true, for your own CSS. - The lock only runs where
(pointer: coarse)matches. If the browser has no lock, or refuses it, nothing happens. The lock is released on exit.
API
Options
| Option | Type | Description |
|---|---|---|
| lockOrientation? | boolean | Lock touch devices to landscape while in fullscreen. Defaults to true. |
State
Read it with useVideoState(fullscreen, (state) => …).
| Field | Type | Description |
|---|---|---|
| active | boolean | True while the player is in fullscreen. |
| supported | boolean | True when the browser and page allow fullscreen, known once the player mounts. |
Commands
Call them with useCommands(fullscreen).
| Command | Type | Description |
|---|---|---|
| enter | () => Promise<void> | Puts the player in fullscreen, or just the video on iPhones. |
| exit | () => Promise<void> | Leaves fullscreen and releases any orientation lock. |
| toggle | () => Promise<void> | Enters fullscreen, or exits it if already active. |
Parts
| Part | Description |
|---|---|
| Fullscreen | Renders nothing where fullscreen isn't available. |