Skip to content

Commit 85e445d

Browse files
authored
docs: Add examples of creating a component (#146)
1 parent 4937cbc commit 85e445d

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

src/mdx-pages/guides/components.mdx

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,108 @@ There are a few used for creating and registering components:
9595

9696
* `videojs.getComponent(String name)`: Retrieves component constructors from Video.js.
9797
* `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.
9999

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`.
101101

102+
```js
103+
// Get the Component base class from Video.js
104+
const Component = videojs.getComponent('Component');
105+
106+
class TitleBar extends Component {
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+
return videojs.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+
return videojs.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).
102200

103201
## Component Children
104202

0 commit comments

Comments
 (0)