Skip to main content
Similarly to <svelte:window>, this element allows you to add listeners to events on document, such as visibilitychange, which don’t fire on window. It also lets you use attachments on document.
As with <svelte:window>, this element may only appear at the top level of your component and must never be inside a block or element.

Event Listeners

You can add any valid document event listener:

Common document events

  • onvisibilitychange - Document visibility changes
  • onfullscreenchange - Fullscreen mode changes
  • onpointerlockchange - Pointer lock state changes
  • onselectionchange - Text selection changes
  • onkeydown - Key is pressed (anywhere in document)
  • onkeyup - Key is released
  • onclick - Click anywhere in document

Bindings

You can bind to the following document properties (all are readonly):
activeElement
Element
The element that currently has focus.
fullscreenElement
Element | null
The element currently displayed in fullscreen mode, or null if no element is in fullscreen.
pointerLockElement
Element | null
The element that currently has the pointer locked, or null if pointer is not locked.
visibilityState
string
The visibility state of the document. Possible values: "visible", "hidden".

Usage

Track document visibility

Track active element

Fullscreen detection

Global keyboard shortcuts

Using attachments

Pause video when page is hidden