This component allows you to easily embed Youtube videos in a much more performant way.
When using the standard Youtube embed code, around 500kb of code is downloaded and executed whether the user views the video or not. The Youtube component defers this process so it only happens if the user clicks explicitly to start the video.
You can modify youtube component functionality by adding the below data attribute. Unless stated otherwise, these must be placed on the top level block component (eg:-
This is a required attribute, and must contain the ID of the Youtube video you wish to display. You can grab this from the standard embed code, or from the URL of the YouTube video itself. For example, for
This component has methods which allow you to programatically trigger actions. You can trigger these using the
You can access the YouTube player API object by retrieving the component's state using the
This component has bespoke events which trigger automatically on certain actions. You can hook into these using event listeners to run your own custom code.
Typically, most events are fired from the block level class (eg:-
c-tabs), however some may fire from element level classes (eg:-
c-tabs__item). In practice, this usually doesn't matter as these element level events will bubble upwards and trigger any listeners on the block element. When this happens, you can get a reference to the element via the
target property of the event object.
Fires when the user plays the video.
Fires when the user pauses the video.
Fires when the video ends.
Example code snippet showing how you can set up an event listener.