Core Concepts
Components
In this section, we'll go through the basics of working with the custom elements in Vidstack Player.
Elements
We provide a variety of components out of the box that help enhance the player.
Some are concerned with layout such as <media-player>
and <media-outlet>
, some provide visual controls such as <media-play-button>
or <media-time-slider>
, and others manage player instances.
We recommend either searching (cmd + k
) for what you're looking for or browsing the sidebar. Each element contains documentation on how to use/style it, and an API reference.
You can register all elements like so:
Or, individually like so:
Next, we can use the defined elements and the browser will "upgrade" them once the script above has loaded and run.
Attach Hook
Vidstack elements go through a two-step process in which they're defined then attached before they're finally ready to be interacted with:
You can await the defineCustomElements
call to ensure all elements are defined:
Keep Alive
By default, Vidstack elements will be destroyed when they've disconnected from the DOM and have not re-connected after an animation frame tick. You can specify that an element should be kept alive like so:
Now, you can manually destroy the element instance and all children by calling the destroy()
method on the element that you specified to keep alive like so:
You don't need to worry about keeping elements alive if you're using a framework integration such as React. Elements will be disposed of correctly based on the framework lifecycle.
Attributes
Most component props can be set directly in HTML via attributes like so:
All attributes in Vidstack are the kebab-case variant of the property name. For example, the fooBar
property would be the attribute foo-bar
.
Events
Events can be listened to by obtaining a reference to the element instance and attaching an event listener like so:
Instance
Obtaining a reference to the element instance enables you to manipulate the custom element itself and directly call properties/methods like so:
Element Types
All element types are classes named using PascalCase and suffixed with the word Element
(e.g., MediaPlayerElement
).
Provider Types
The following utilities can be useful for narrowing the type of a media provider: