MediaAttachMixin
Mixin for custom media elements to register themselves with the provider via context
Import
import { MediaAttachMixin } from "@videojs/html";MediaAttachMixin creates a class that registers itself with the player on connect. Apply it to custom media elements so they automatically wire to the store.
Built-in elements, like <hlsjs-video>, <dash-video>, <hls-video>, and <background-video>, all use this mixin internally.
import { MediaAttachMixin } from '@videojs/html';
import { MyCustomMedia } from './my-custom-media';
export class MyMedia extends MediaAttachMixin(MyCustomMedia) {
// inherits context registration
}
customElements.define('my-media', MyMedia);Who needs this
You only need MediaAttachMixin when your base class doesn’t already register as a media element with the provider. For example, elements that wrap a third-party player or use a canvas renderer.
For plain <video> and <audio> elements, the player tracks them automatically as they are added, removed, or replaced — no mixin needed.
Overriding the media target
By default, MediaAttachMixin registers this as the media element. Override getMediaTarget() if the actual media element is something other than the element itself — for example, an inner <video> in a shadow root:
import { MediaAttachMixin } from '@videojs/html';
export class BackgroundVideo extends MediaAttachMixin(HTMLElement) {
getMediaTarget() {
return this.shadowRoot?.querySelector('video') ?? null;
}
}Factory function
MediaAttachMixin is pre-configured with the default mediaContext published by PlayerElement. Call createMediaAttachMixin only when a custom player publishes a matching custom MediaContext:
import { createMediaAttachMixin } from '@videojs/html';
import { myCustomContext } from './my-player/context';
export const MyMediaAttachMixin = createMediaAttachMixin(myCustomContext);API Reference
Parameters
| Parameter | Type | Default | Details |
|---|---|---|---|
context* | Context<typeof MEDIA_CONTEXT_KEY, MediaContextValue> | — | |
| |||
Return Value
| Type | Details |
|---|---|
function | |
| |