Install screenfull via npm
mainInstall the screenfull package using npm. Note that this package is ESM.
If you cannot use ESM or need to support older browsers without transpilation, use version 5.2.0.
npm install screenfullrepository·main·Indexed 27 days ago
https://github.com/sindresorhus/screenfullA lightweight, cross-browser wrapper for the JavaScript Fullscreen API (version 6.0.2). It provides a consistent interface to request, exit, or toggle fullscreen mode for the page or specific DOM elements, and includes utilities to check fullscreen status via isFullscreen and isEnabled, as well as event listeners for state changes and errors.
Install the screenfull package using npm. Note that this package is ESM.
If you cannot use ESM or need to support older browsers without transpilation, use version 5.2.0.
npm install screenfullThe .raw property exposes the underlying browser properties used internally, including vendor prefixes where necessary:
requestFullscreenexitFullscreenfullscreenElementfullscreenEnabledfullscreenchangefullscreenerror.exit() to bring the browser out of fullscreen mode. Returns a promise that resolves after the element exits fullscreen.Use .request(element, options?) to make an element fullscreen. If no element is provided, it defaults to <html> (the page).
Important: Browsers only allow entering fullscreen when initiated by user events (e.g., click, touch, keydown).
If your page is inside an <iframe>, you must add the allowfullscreen attribute (plus webkitallowfullscreen and mozallowfullscreen).
Returns a promise that resolves after the element enters fullscreen.
import screenfull from 'screenfull';
// Fullscreen the page
if (screenfull.isEnabled) {
screenfull.request();
}
// Fullscreen a specific element
const element = document.getElementById('target');
if (screenfull.isEnabled) {
screenfull.request(element, {navigationUI: 'hide'});
}Use .on('change', callback) or the alias .onchange(callback) to detect when the browser switches in and out of fullscreen mode. Use .off('change', callback) or .onchange to remove the listener.
Use .on('error', callback) or the alias .onerror(callback) to detect errors during fullscreen transitions.
import screenfull from 'screenfull';
if (screenfull.isEnabled) {
screenfull.on('change', () => {
console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
});
}
// Remove listener
screenfull.off('change', callback);Use .toggle(element, options?) to request fullscreen if it is not currently active, or exit if it is. Accepts a DOM element and FullscreenOptions. Returns a promise that resolves after the state change.
import screenfull from 'screenfull';
const element = document.getElementById('target');
// Toggle fullscreen on an element
if (screenfull.isEnabled) {
screenfull.toggle(element);
}Use the following properties to inspect the current state:
isEnabled: Returns a boolean indicating if the browser allows entering fullscreen (check if your page is in an <iframe> with allowfullscreen).isFullscreen: Returns a boolean indicating if fullscreen is currently active.element: Returns the DOM element currently in fullscreen, or undefined if not in fullscreen.request(element, options) to enter fullscreen mode. By default, it targets document.documentElement. It returns a Promise that resolves when the fullscreen transition is complete. You can pass an optional options object (e.g., for allowPointerLock).exit() to exit fullscreen mode. It returns a Promise that resolves when the exit transition is complete. If the document is not currently in fullscreen mode, the promise resolves immediately.raw property provides access to the underlying browser-specific fullscreen methods (e.g., requestFullscreen, webkitRequestFullscreen, mozRequestFullScreen, etc.) that screenfull abstracts.You can subscribe to fullscreen state changes and errors using the following methods:
onchange(callback): Invokes the callback when the fullscreen state changes.onerror(callback): Invokes the callback when a fullscreen error occurs.on(event, callback): A generic method to listen for 'change' or 'error' events.off(event, callback): Removes a previously registered event listener.toggle(element, options) to switch between fullscreen and normal mode. If the document is currently in fullscreen, it calls exit(). Otherwise, it calls request(element, options).