# video-player

The state boundary — creates a store and broadcasts it to all descendants.

The `<video-player>` element is the state boundary of your player. It creates a store and makes it available to every element inside it via context. Every player needs exactly one.

```html
<video-player>
  <!-- Everything inside can access the player store -->
  <media-container>
    <video src="video.mp4"></video>
  </media-container>
</video-player>
```

## How it’s created

For most users, importing from `@videojs/html/video/player` registers a ready-to-use `<video-player>` element with the standard video [features](../../guides/features.md) (bundled as a [preset](../../guides/presets.md)):

```ts
import '@videojs/html/video/player';
```

```html
<video-player>
  <media-container>
    <video src="video.mp4"></video>
  </media-container>
</video-player>
```

If you need a custom feature set or tag name, use `createPlayer()` to get the configured `PlayerElement`. Extend `ContainerElement` separately when you need custom container behavior:

**my-player.ts**

```ts
import { ContainerElement, createPlayer } from '@videojs/html';
import { videoFeatures } from '@videojs/html/video';

const { PlayerElement: MyPlayer } = createPlayer({ features: videoFeatures });

class MyContainer extends ContainerElement {}
customElements.define('my-player', MyPlayer);
customElements.define('my-container', MyContainer);
```

- [`createPlayer` reference](../api/html-create-player.md)

## What lives inside it

Everything that needs player state goes inside `<video-player>`: skins, containers, UI components, and your own custom components. Anything inside can access the store.

```html
<video-player>
  <video-skin>               <!-- skin — includes container + controls -->
    <video src="..."></video> <!-- media component -->
  </video-skin>
  <my-custom-overlay></my-custom-overlay> <!-- your own element — can use PlayerController -->
</video-player>
```

## No visual presence

Packaged skins make `<video-player>` boxless with `display: contents`. Width, height, positioning, transforms, and `getBoundingClientRect()` do not describe a visible player box there. Put layout and measurements on the [`<media-container>`](./player-container.md) instead.

If you omit `<video-skin>` and import only the player and media components, those skin styles are not loaded. The player element still owns state, not layout, so add and style a `<media-container>` instead of using the player itself as the layout surface.

## Accessing state

Use the preset’s typed `PlayerController` to subscribe to store state from any custom element inside the player:

**play-pause-button.ts**

```ts
import { UIElement, selectPlayback } from '@videojs/html';
import { PlayerController } from '@videojs/html/video';

class PlayPauseButton extends UIElement {
  readonly #playback = new PlayerController(this, selectPlayback);

  connectedCallback() {
    super.connectedCallback();
    this.addEventListener('click', this.#togglePlayback);
  }

  disconnectedCallback() {
    this.removeEventListener('click', this.#togglePlayback);
    super.disconnectedCallback();
  }

  readonly #togglePlayback = () => {
    const playback = this.#playback.value;
    if (!playback) return;

    playback.paused ? playback.play() : playback.pause();
  };
}

customElements.define('play-pause-button', PlayPauseButton);
```

- [PlayerController reference](../api/player-controller.md)

## Extended player layouts

The player’s scope can extend beyond the fullscreen target. Playlists, transcripts, sidebars, and other supplementary UI can live inside `<video-player>` but outside `<media-container>`. They still have full access to the store, but they won’t go fullscreen with the video.

```html
<video-player>
  <media-container>
    <video src="video.mp4"></video>
    <media-controls><media-controls-content>...</media-controls-content></media-controls>  <!-- goes fullscreen with the video -->
  </media-container>

  <media-transcript></media-transcript>     <!-- outside container — still has store access -->
  <playlist-sidebar></playlist-sidebar>     <!-- outside container — still has store access -->
</video-player>
```

## Lifecycle

Video.js elements clean up after themselves when you remove them from the page:

- `<video-player>` and the UI elements destroy themselves two animation frames after they’re removed. Destroying the player also destroys its store.
- Media components such as `<hlsjs-video>` tear down their playback engine in a microtask after they’re removed.

Moving an element synchronously, removing it and inserting it elsewhere with no `await` in between, triggers neither. To keep the player through a longer move, add the `keep-alive` attribute to it and to its media component:

```html
<video-player keep-alive>
  <media-container>
    <hlsjs-video src="/stream.m3u8" keep-alive></hlsjs-video>
  </media-container>
</video-player>
```

On the player, `keep-alive` covers the player and every UI element inside it, including the controls inside a packaged skin. Media components don’t inherit it, because destroying the player doesn’t release their engine: give each one its own `keep-alive`, or it tears down its engine as usual. An element you remove on its own from a kept-alive tree still destroys itself. `keep-alive` only stops an element from destroying itself: destroying an element, automatically or with `destroy()`, also destroys the UI elements inside it, including ones that carry `keep-alive`. When you’re finished, call `destroy()` on the player and `adapter.destroy()` on each media component to release its engine. A destroyed element can’t be reconnected; create a new one instead.