Skip to main content

Opens or closes an element in full-screen mode.

/**
 * Opens or closes an element in full-screen mode.
 *
 * - Use Document.querySelector() and Element.requestFullscreen() to open the given element in fullscreen.
 * - Use Document.exitFullscreen() to exit full-screen mode.
 * - Omit the second argument, el, to use body as the default element.
 * - Omit the first element, mode, to open the element in full-screen mode by default.
 *
 * https://www.30secondsofcode.org/js/s/fullscreen
 */
const fullscreen = (mode = true, el = "body") =>
  mode
    ? document.querySelector(el).requestFullscreen()
    : document.exitFullscreen();

// Examples

fullscreen(); // Opens `body` in fullscreen mode
fullscreen(false); // Exits fullscreen mode

// ----------------------------------------------------------------------
// ES5 version
// ----------------------------------------------------------------------

var fullscreen = function (mode, el) {
  if (mode === void 0) {
    mode = true;
  }

  if (el === void 0) {
    el = "body";
  }

  return mode
    ? document.querySelector(el).requestFullscreen()
    : document.exitFullscreen();
};