Components

The architecture of the Video.js player is centered around components. The Player class and all classes representing player controls and other UI elements inherit from the Component class. This architecture makes it easy to construct the user interface of the Video.js player in a tree-like structure that mirrors the DOM.

Table of Contents

What is a Component?

A component is a JavaScript object that has the following features:

  • An associated DOM element, in almost all cases.
  • An association to a Player object.
  • The ability to manage any number of child components.
  • The ability to listen for and trigger events.
  • A lifecycle of initialization and disposal.

For more specifics on the programmatic interface of a component, see the component API docs.

Creating a Component

Video.js components can be inherited and registered with Video.js to add new features and UI to the player.

For a working example, we have a JSBin demonstrating the creation of a component for displaying a title across the top of the player.

In addition, there are a couple methods worth recognizing:

  • videojs.getComponent(String name): Retrieves component constructors from Video.js.
  • videojs.registerComponent(String name, Function Comp): Registers component constructors with Video.js.
  • videojs.extend(Function component, Object properties): Provides prototype inheritance. Can be used to extend a component’s constructor, returning a new constructor with the given properties.

Creation:

  1. // adding a button to the player
  2. var player = videojs('some-video-id');
  3. var Button = videojs.getComponent('Button');
  4. var button = new Button(player, {
  5. clickHandler: function(event) {
  6. videojs.log('Clicked');
  7. }
  8. });
  9. console.log(button.el());

The above code will output

  1. <div class="video-js">
  2. <div class="vjs-button">Button</div>
  3. </div>

Adding the new button to the player

  1. // adding a button to the player
  2. var player = videojs('some-video-id');
  3. var button = player.addChild('button');
  4. console.log(button.el());
  5. // will have the same html result as the previous example

Component Children

Again, refer to the component API docs for complete details on methods available for managing component structures.

Basic Example

When child component is added to a parent component, Video.js inserts the element of the child into the element of the parent. For example, adding a component like this:

  1. // Add a "BigPlayButton" component to the player. Its element will be appended to the player's element.
  2. player.addChild('BigPlayButton');

Results in a DOM that looks like this:

  1. <!-- Player Element -->
  2. <div class="video-js">
  3. <!-- BigPlayButton Element -->
  4. <div class="vjs-big-play-button"></div>
  5. </div>

Conversely, removing child components will remove the child component’s element from the DOM:

  1. player.removeChild('BigPlayButton');

Results in a DOM that looks like this:

  1. <!-- Player Element -->
  2. <div class="video-js">
  3. </div>

Using Options

Pass in options for child constructors and options for children of the child.

  1. var player = videojs('some-vid-id');
  2. var Component = videojs.getComponent('Component');
  3. var myComponent = new Component(player);
  4. var myButton = myComponent.addChild('MyButton', {
  5. text: 'Press Me',
  6. buttonChildExample: {
  7. buttonChildOption: true
  8. }
  9. });

Children can also be added via options when a component is initialized.

Note: Include a ‘name’ key which will be used if two child components of the same type that need different options.

  1. // MyComponent is from the above example
  2. var myComp = new MyComponent(player, {
  3. children: ['button', {
  4. name: 'button',
  5. someOtherOption: true
  6. }, {
  7. name: 'button',
  8. someOtherOption: false
  9. }]
  10. });

Event Listening

Using on

  1. var player = videojs('some-player-id');
  2. var Component = videojs.getComponent('Component');
  3. var myComponent = new Component(player);
  4. var myFunc = function() {
  5. var myComponent = this;
  6. console.log('myFunc called');
  7. };
  8. myComponent.on('eventType', myFunc);
  9. myComponent.trigger('eventType');
  10. // logs 'myFunc called'

The context of myFunc will be myComponent unless it is bound. You can add a listener to another element or component.

  1. var otherComponent = new Component(player);
  2. // myComponent/myFunc is from the above example
  3. myComponent.on(otherComponent.el(), 'eventName', myFunc);
  4. myComponent.on(otherComponent, 'eventName', myFunc);
  5. otherComponent.trigger('eventName');
  6. // logs 'myFunc called' twice

Using off

  1. var player = videojs('some-player-id');
  2. var Component = videojs.getComponent('Component');
  3. var myComponent = new Component(player);
  4. var myFunc = function() {
  5. var myComponent = this;
  6. console.log('myFunc called');
  7. };
  8. myComponent.on('eventType', myFunc);
  9. myComponent.trigger('eventType');
  10. // logs 'myFunc called'
  11. myComponent.off('eventType', myFunc);
  12. myComponent.trigger('eventType');
  13. // does nothing

If myFunc gets excluded, all listeners for the event type will get removed. If eventType gets excluded, all listeners will get removed from the component. You can use off to remove listeners that get added to other elements or components using:

myComponent.on(otherComponent...

In this case both the event type and listener function are REQUIRED.

  1. var otherComponent = new Component(player);
  2. // myComponent/myFunc is from the above example
  3. myComponent.on(otherComponent.el(), 'eventName', myFunc);
  4. myComponent.on(otherComponent, 'eventName', myFunc);
  5. otherComponent.trigger('eventName');
  6. // logs 'myFunc called' twice
  7. myComponent.off(ootherComponent.el(), 'eventName', myFunc);
  8. myComponent.off(otherComponent, 'eventName', myFunc);
  9. otherComponent.trigger('eventName');
  10. // does nothing

Using one

  1. var player = videojs('some-player-id');
  2. var Component = videojs.getComponent('Component');
  3. var myComponent = new Component(player);
  4. var myFunc = function() {
  5. var myComponent = this;
  6. console.log('myFunc called');
  7. };
  8. myComponent.one('eventName', myFunc);
  9. myComponent.trigger('eventName');
  10. // logs 'myFunc called'
  11. myComponent.trigger('eventName');
  12. // does nothing

You can also add a listener to another element or component that will get triggered only once.

  1. var otherComponent = new Component(player);
  2. // myComponent/myFunc is from the above example
  3. myComponent.one(otherComponent.el(), 'eventName', myFunc);
  4. myComponent.one(otherComponent, 'eventName', myFunc);
  5. otherComponent.trigger('eventName');
  6. // logs 'myFunc called' twice
  7. otherComponent.trigger('eventName');
  8. // does nothing

Using trigger

  1. var player = videojs('some-player-id');
  2. var Component = videojs.getComponent('Component');
  3. var myComponent = new Component(player);
  4. var myFunc = function(data) {
  5. var myComponent = this;
  6. console.log('myFunc called');
  7. console.log(data);
  8. };
  9. myComponent.one('eventName', myFunc);
  10. myComponent.trigger('eventName');
  11. // logs 'myFunc called' and 'undefined'
  12. myComponent.trigger({'type':'eventName'});
  13. // logs 'myFunc called' and 'undefined'
  14. myComponent.trigger('eventName', {data: 'some data'});
  15. // logs 'myFunc called' and "{data: 'some data'}"
  16. myComponent.trigger({'type':'eventName'}, {data: 'some data'});
  17. // logs 'myFunc called' and "{data: 'some data'}"

Default Component Tree

The default component structure of the Video.js player looks something like this:

  1. Player
  2. ├── MediaLoader (has no DOM element)
  3. ├── PosterImage
  4. ├── TextTrackDisplay
  5. ├── LoadingSpinner
  6. ├── BigPlayButton
  7. ├── LiveTracker (has no DOM element)
  8. ├─┬ ControlBar
  9. ├── PlayToggle
  10. ├── VolumePanel
  11. ├── CurrentTimeDisplay (hidden by default)
  12. ├── TimeDivider (hidden by default)
  13. ├── DurationDisplay (hidden by default)
  14. ├─┬ ProgressControl (hidden during live playback, except when liveui: true)
  15. └─┬ SeekBar
  16. ├── LoadProgressBar
  17. ├── MouseTimeDisplay
  18. └── PlayProgressBar
  19. ├── LiveDisplay (hidden during VOD playback)
  20. ├── SeekToLive (hidden during VOD playback)
  21. ├── RemainingTimeDisplay
  22. ├── CustomControlSpacer (has no UI)
  23. ├── PlaybackRateMenuButton (hidden, unless playback tech supports rate changes)
  24. ├── ChaptersButton (hidden, unless there are relevant tracks)
  25. ├── DescriptionsButton (hidden, unless there are relevant tracks)
  26. ├── SubtitlesButton (hidden, unless there are relevant tracks)
  27. ├── CaptionsButton (hidden, unless there are relevant tracks)
  28. ├── SubsCapsButton (hidden, unless there are relevant tracks)
  29. ├── AudioTrackButton (hidden, unless there are relevant tracks)
  30. ├── PictureInPictureToggle
  31. └── FullscreenToggle
  32. ├── ErrorDisplay (hidden, until there is an error)
  33. ├── TextTrackSettings
  34. └── ResizeManager (hidden)

Specific Component Details

Play Toggle

The PlayToggle has one option replay which can show or hide replay icon. This can be set by passing {replay: false} as the default behavior replay icon is shown after video end playback.

Example of how to hide a replay icon

  1. let player = videojs('myplayer', {
  2. controlBar: {
  3. playToggle: {
  4. replay: false
  5. }
  6. }
  7. });

Volume Panel

The VolumePanel includes the MuteToggle and the VolumeControl Components, which will be hidden if volume changes are not supported. There is one important option for the VolumePanel which can make your VolumeControl appear vertically over the MuteToggle. This can be set by passing VolumePanel {inline: false} as the default behavior is a horizontal VolumeControl with {inline: true}.

Example of a vertical VolumeControl

  1. let player = videojs('myplayer', {
  2. controlBar: {
  3. volumePanel: {
  4. inline: false
  5. }
  6. }
  7. });

Text Track Settings

The text track settings component is only available when using emulated text tracks.

Resize Manager

This new component is in charge of triggering a playerresize event when the player size changed. It uses the ResizeObserver if available or a polyfill was provided. It has no element when using the ResizeObserver. If a ResizeObserver is not available, it will fallback to an iframe element and listen to its resize event via a debounced handler.

A ResizeObserver polyfill can be passed in like so:

  1. var player = videojs('myplayer', {
  2. resizeManager: {
  3. ResizeObserver: ResizeObserverPoylfill
  4. }
  5. });

To force using the iframe fallback, pass in null as the ResizeObserver:

  1. var player = videojs('myplayer', {
  2. resizeManager: {
  3. ResizeObserver: null
  4. }
  5. });

The ResizeManager can also just be disabled like so:

  1. var player = videojs('myplayer', {
  2. resizeManager: false
  3. });