ContainerMixin is a mixin factory that adds media discovery and auto-attachment to a custom element class. The resulting element consumes the player store from context and uses a MutationObserver to watch for <video> and <audio> descendants, calling store.attach() whenever the media target changes.
ContainerMixin is returned by createPlayer pre-bound to the player context.
Signature
Usage
Creating a container element
index.html
<video-region> element automatically discovers the <video> child and attaches it to the store owned by the nearest <video-player> provider above it in the DOM tree.
Split provider and container
The most common pattern is to have the store owner (ProviderMixin) at a higher level in the tree and one or more container elements deeper:
Composed element
Apply both mixins to a single element when the store owner and media container should be the same node:Properties
The resolved player store, or
null if the element is not connected to a provider. Updated automatically when the context value changes.Media discovery
OnconnectedCallback, ContainerMixin sets up a MutationObserver on its subtree. When a <video> or <audio> element is added or removed, it calls store.attach({ media, container }) to update the store’s media target. The observer is torn down on disconnectedCallback.
Media discovery checks, in order:
- A direct
<video>or<audio>child element. - A custom element whose tag name ends in
-videoor-audio. - An element assigned to a
<slot name="media">slot.
Custom media elements (tag names ending in
-video or -audio) are upgraded via customElements.upgrade() before attachment to ensure they are fully initialized.Lifecycle
connectedCallback— starts theMutationObserver, listens forslotchangeevents, and callsstore.attach()with the current media element if one is found.disconnectedCallback— stops the observer, removes theslotchangelistener, and calls the attachment cleanup function.