Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

exitFullscreen

Exits fullscreen mode if currently in fullscreen.

TypeScript
Copied!
1/**
2 * Exits fullscreen mode if currently in fullscreen.
3 */
4export function exitFullscreen(): void {
5  const exit =
6    document.exitFullscreen ||
7    (document as any).webkitExitFullscreen ||
8    (document as any).mozCancelFullScreen ||
9    (document as any).msExitFullscreen;
10
11  if (exit) {
12    exit.call(document);
13  } else {
14    console.warn('Fullscreen API is not supported by this browser.');
15  }
16}
  • Cross-Browser Compatibility

    Supports standard and vendor-prefixed versions (webkit, moz, ms) of the Fullscreen API to ensure broad compatibility.

  • Graceful Fallback

    Includes a console warning when the Fullscreen API is not supported, aiding in debugging and progressive enhancement.

  • Clean Abstraction

    Encapsulates the exit logic in a single utility, reducing boilerplate and repetitive checks in application code.

  • Safe Invocation

    Uses .call(document) to maintain correct context when invoking vendor-prefixed methods, avoiding runtime errors.

Tests | Examples

TypeScript
Copied!
1test('exitFullscreen calls document.exitFullscreen if available', () => {
2  const mockExit = vi.fn();
3  Object.defineProperty(document, 'exitFullscreen', {
4    value: mockExit,
5    configurable: true
6  });
7
8  exitFullscreen();
9  expect(mockExit).toHaveBeenCalled();
10});
11
12test('exitFullscreen falls back to vendor-prefixed methods', () => {
13  const mockWebkitExit = vi.fn();
14  Object.defineProperty(document, 'webkitExitFullscreen', {
15    value: mockWebkitExit,
16    configurable: true
17  });
18
19  // Temporarily delete standard method
20  const original = document.exitFullscreen;
21  delete (document as any).exitFullscreen;
22
23  exitFullscreen();
24  expect(mockWebkitExit).toHaveBeenCalled();
25
26  // Restore
27  (document as any).exitFullscreen = original;
28});
29
30test('exitFullscreen warns if fullscreen is not supported', () => {
31  const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
32
33  // Remove all methods
34  const original = {
35    exitFullscreen: document.exitFullscreen,
36    webkitExitFullscreen: (document as any).webkitExitFullscreen
37  };
38  delete (document as any).exitFullscreen;
39  delete (document as any).webkitExitFullscreen;
40
41  exitFullscreen();
42  expect(warnSpy).toHaveBeenCalledWith(
43    'Fullscreen API is not supported by this browser.'
44  );
45
46  // Restore
47  Object.assign(document, original);
48  warnSpy.mockRestore();
49});

Common Use Cases

  • Video Player Controls

    Exit fullscreen when playback ends or the user exits fullscreen mode manually.

  • Presentation Apps

    Allow users to return to normal mode after finishing a slide deck or presentation.

  • Fullscreen Toggles

    Pair with an enterFullscreen utility for toggleable fullscreen functionality via a single button.

  • Game or Simulation Interfaces

    Let users exit immersive fullscreen sessions to return to standard navigation or UI.

Codebase: Utilities -> Browser & DOM -> exitFullscreen | Yevhen Klymentiev