You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/mdx-pages/guides/components.mdx
+100-2Lines changed: 100 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,10 +95,108 @@ There are a few used for creating and registering components:
95
95
96
96
*`videojs.getComponent(String name)`: Retrieves component constructors from Video.js.
97
97
*`videojs.registerComponent(String name, Function Comp)`: Registers component constructors with Video.js.
98
-
*`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.
98
+
*`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.*Deprecated, will be removed in Video.js 8 in favor of ES6 classes.
99
99
100
-
For a working example, [we have a JSBin](https://jsbin.com/vobacas/edit?html,css,js,output) demonstrating the creation of a component for displaying a title across the top of the player.
100
+
This example creates a title bar component, as a class extending `Component`.
101
101
102
+
```js
103
+
// Get the Component base class from Video.js
104
+
constComponent=videojs.getComponent('Component');
105
+
106
+
classTitleBarextendsComponent {
107
+
108
+
// The constructor of a component receives two arguments: the
109
+
// player it will be associated with and an object of options.
110
+
constructor(player, options= {}) {
111
+
112
+
// It is important to invoke the superclass before anything else,
113
+
// to get all the features of components out of the box!
114
+
super(player, options);
115
+
116
+
// If a `text` option was passed in, update the text content of
117
+
// the component.
118
+
if (options.text) {
119
+
this.updateTextContent(options.text);
120
+
}
121
+
}
122
+
123
+
// The `createEl` function of a component creates its DOM element.
124
+
createEl() {
125
+
returnvideojs.dom.createEl('div', {
126
+
127
+
// Prefixing classes of elements within a player with "vjs-"
128
+
// is a convention used in Video.js.
129
+
className:'vjs-title-bar'
130
+
});
131
+
}
132
+
133
+
// This function could be called at any time to update the text
134
+
// contents of the component.
135
+
updateTextContent(text) {
136
+
137
+
// If no text was provided, default to "Title Unknown"
138
+
if (typeof text !=='string') {
139
+
text ='Title Unknown';
140
+
}
141
+
142
+
// Use Video.js utility DOM methods to manipulate the content
143
+
// of the component's element.
144
+
videojs.emptyEl(this.el());
145
+
videojs.appendContent(this.el(), text);
146
+
}
147
+
}
148
+
```
149
+
150
+
This creates the same component without using classes, using the `videojs.extend()` helper instead.
151
+
152
+
```js
153
+
var Component =videojs.getComponent('Component');
154
+
155
+
// The videojs.extend function can be used instead of ES6 classes.
156
+
var TitleBar =videojs.extend(Component, {
157
+
158
+
constructor:function(player, options) {
159
+
160
+
// Equivalent of `super(this, arguments)`
161
+
Component.apply(this, arguments);
162
+
163
+
if (options.text) {
164
+
this.updateTextContent(options.text);
165
+
}
166
+
},
167
+
168
+
createEl:function() {
169
+
returnvideojs.dom.createEl('div', {
170
+
className:'vjs-title-bar'
171
+
});
172
+
},
173
+
174
+
updateTextContent:function(text) {
175
+
if (typeof text !=='string') {
176
+
text ='Title Unknown';
177
+
}
178
+
179
+
videojs.emptyEl(this.el());
180
+
videojs.appendContent(this.el(), text);
181
+
}
182
+
});
183
+
```
184
+
185
+
Either way, the component can be registered, and used in a player.
186
+
187
+
```js
188
+
// Register the component with Video.js, so it can be used in players.
189
+
videojs.registerComponent('TitleBar', TitleBar);
190
+
191
+
// Create a player.
192
+
var player =videojs('my-player');
193
+
194
+
// Add the TitleBar as a child of the player and provide it some text
195
+
// in its options.
196
+
player.addChild('TitleBar', {text:'The Title of The Video!'});
197
+
```
198
+
199
+
A live example is in [this JSBin](https://jsbin.com/vobacas/edit?html,css,js,output).
0 commit comments