fscreen: Cross-Browser Fullscreen API
fscreen is a small, vendor-agnostic JavaScript utility library that provides a unified interface to the browser's Fullscreen API. It abstracts away browser-specific prefixes (like `webkit` or `moz`) for methods and properties such as `requestFullscreen`, `exitFullscreen`, `fullscreenElement`, and event listeners like `fullscreenchange` and `fullscreenerror`. The current stable version is 1.2.0. As a lightweight wrapper, fscreen aims to simplify cross-browser fullscreen implementation without introducing significant overhead or polyfills. It doesn't have a strict release cadence, typically releasing new versions as needed to adapt to browser changes or fix issues. Its primary differentiator is its simplicity and focus on providing a direct, standardized access layer to the native Fullscreen API, ensuring consistent behavior across different browsers without requiring developers to manage vendor prefixes manually.
Common errors
-
Uncaught (in promise) DOMException: The request is not allowed by the user agent or the platform in the current context.
cause `fscreen.requestFullscreen()` was called without a direct user gesture.fixTrigger `fscreen.requestFullscreen()` only from within an event handler directly tied to user interaction, such as a button's `click` event listener. -
TypeError: Cannot read properties of null (reading 'requestFullscreen') or TypeError: element.requestFullscreen is not a function
cause The Fullscreen API (even with vendor prefixes) is not supported by the browser, or `fscreen` was not properly initialized/loaded.fixVerify browser support by checking `fscreen.fullscreenEnabled` before attempting fullscreen operations. Ensure `fscreen` is correctly imported and available in the scope where it's being used.
Warnings
- gotcha The Fullscreen API, and by extension `fscreen.requestFullscreen()`, requires a direct user gesture (e.g., a click event) to initiate fullscreen mode. Calling it programmatically without user interaction will generally result in a security error or a rejected promise.
- gotcha Fullscreen operations (`requestFullscreen`, `exitFullscreen`) are asynchronous. The state of the fullscreen element (`fscreen.fullscreenElement`) does not change immediately after calling these methods. Always rely on the `fullscreenchange` event to confirm the actual fullscreen state.
- gotcha fscreen acts as a wrapper to unify vendor prefixes for the native Fullscreen API; it does not polyfill the API itself. If a browser completely lacks support for the Fullscreen API, fscreen will report `fscreen.fullscreenEnabled` as `false`, and its methods will not function.
Install
-
npm install fscreen -
yarn add fscreen -
pnpm add fscreen
Imports
- fscreen
import { fscreen } from 'fscreen';import fscreen from 'fscreen';
- fscreen
const fscreen = require('fscreen');import fscreen from 'fscreen';
- fscreen.addEventListener
document.addEventListener('fullscreenchange', handler, false);fscreen.addEventListener('fullscreenchange', handler, false);
Quickstart
import fscreen from 'fscreen';
const initFullscreen = () => {
if (fscreen.fullscreenEnabled) {
// Store a reference to the handler to remove it later if needed
const fullscreenChangeHandler = () => {
if (fscreen.fullscreenElement !== null) {
console.log('Entered fullscreen mode');
// Example: add a class to the body when in fullscreen
document.body.classList.add('is-fullscreen');
} else {
console.log('Exited fullscreen mode');
// Example: remove the class when exiting
document.body.classList.remove('is-fullscreen');
// Clean up the event listener if no longer needed
// fscreen.removeEventListener('fullscreenchange', fullscreenChangeHandler, false);
}
};
fscreen.addEventListener('fullscreenchange', fullscreenChangeHandler, false);
const myElement = document.getElementById('my-fullscreen-target');
const enterFullscreenButton = document.getElementById('enter-fullscreen-btn');
if (myElement && enterFullscreenButton) {
enterFullscreenButton.addEventListener('click', () => {
fscreen.requestFullscreen(myElement);
});
} else {
console.warn("Required elements (my-fullscreen-target or enter-fullscreen-btn) not found.");
}
} else {
console.log('Fullscreen API is not supported by this browser.');
const statusDiv = document.getElementById('fullscreen-status');
if (statusDiv) {
statusDiv.textContent = 'Fullscreen is not available.';
}
}
};
// Call initFullscreen after the DOM is loaded
document.addEventListener('DOMContentLoaded', initFullscreen);
// Example HTML for context:
// <div id="my-fullscreen-target" style="background: lightblue; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;">Content</div>
// <button id="enter-fullscreen-btn">Enter Fullscreen</button>
// <div id="fullscreen-status"></div>