alanwsmith.com ~ source code ~ other projects ~ socials

Alan's YouTube Player Web Component

Overview

An enhanced YouTube embed experience. It started as a way to clean up the preview thumbnail images and evolved from there.

Usage

  1. Grab the component file here: youtube-player-v1.js
  2. Include it on your page with:
    <script 
      type="module" 
      src="/path/to/youtube-player-v1.js"
    ></script>
  3. Use it with the ID from a YouTube video like this:
    <youtube-player :video="dQw4w9WgXcQ"><youtube-player>

Features

Video Player

Multiple Players

The component is designed to accommodate multiple video players on a page.

Page Level Styling

Examples

Default Player

Videos are added via a custom youtube-player element with a :video attribute for the ID of the video to play. For example:

HTML

<youtube-player
  :video="LT3cERVRoQo"
><youtube-player>

Output

The component limits what's shown to the thumbnail, title, and player controls. That's compared to the default YouTube embed which puts the play button in the middle of the image. The YouTube player also adds "Watch later", "Share", etc... elements based on how much room is available. Here's an example:

Thumbnails look nicer without the play button covering them. They also load much faster than the default YouTube embed which pulls in the entire player before showing an image.

Full Video URLs

The :video attribute accepts full YouTube URLs as well to make them easier to copy/paste:

HTML

<youtube-player
  :video="https://www.youtube.com/watch?v=DvJRdS1CaYQ"
><youtube-player>

Output

Chapters

Chapters can be added via the optional :chapters attribute. Chapter skip buttons slot into the player controls to move between them:

HTML

<youtube-player
  :video="-91vymvIH0c"
  :chapters="18, 32, 84, 176, 276, 407, 577"
><youtube-player>

Output

Custom Thumbnails

YouTube thumbnails can be replaced with custom images

HTML

<youtube-player
  :video="fiShsfvbFUA"
  :thumbnail="/images/rage-against-thumbnail.jpg"
><youtube-player>

Output

Custom Start Time

The component accepts custom start times. For example, this videos doesn't really get going until 54 seconds in:

HTML

<youtube-player
  :video="NoMqvniiEkk" 
  :start="54"
><youtube-player>

Output

Custom End Time

An end time can be set as well. For example, this one only plays the first 11 seconds of the video:

HTML

<youtube-player
  :video="REPPgPcw4hk" 
  :end="11"
><youtube-player>

Output

Custom Title

YouTube titles are used by default. They can be overridden with a custom title. Makes it nice for removing "[Official Video]" type stuff which is what was done for this one:

HTML

<youtube-player
  :video="K7xzmkpwNoA" 
  :title="Little Simz - Gorilla"
><youtube-player>

Output

Content Advisory

The component provides for adding content advisories that can be used for flashing lights, language, sensitive topics, etc...

HTML

<youtube-player
  :video="tRwHpyOq4P4" 
  :advisory="Warning: contains flashing lights"
><youtube-player>

Output

Restarting From The Beginning

Switching between multiple videos on a page stops the one that was left and returns it to the thumbnail. If you go back to it, it picks up where it left off by default. The component also provides a :restart attribute that sends it back to the beginning instead. (Note that it doesn't go back just by pausing. You have to switch to another video to see the behaviour)

HTML

<youtube-player
  :video="Cz8cbwR_6ms" 
  :restart="on"
><youtube-player>

Output

Custom Skip Times

The skip forward and back buttons default to +7sec. and -10sec. respectively. Those times can be customized with :skip-forward and :skip-back attributes.

HTML

<youtube-player
  :video="dAwLMS8fgoA" 
  :skip-forward="3"
  :skip-back="3"
><youtube-player>

Output

Details

Player Styling

The player includes default styles for the text, border, and buttons. They can be adjusted with CSS custom property variables on the parent page (either globally in something like :root or via .class or #id selectors).

Text Styling

Text styles are controlled via:

--youtube-player--text-color
--youtube-player--text-background-color
--youtube-player--font-size

They default to:

--youtube-player--text-color: #ccc
--youtube-player--text-background-color: rgb(0 0 0 / 0.3);
--youtube-player--font-size: 0.9rem;

Border Styling

The border can be styled for three different states:

--youtube-player--faded-border
--youtube-player--playing-border
--youtube-player--stopped-border

The defaults are:

--youtube-player--faded-border: 1px solid #222;
--youtube-player--playing--border: 1px solid #aaa;
--youtube-player--stopped--border: 1px solid #999;

Button Color Styling

The colors for the buttons are set with:

--youtube-player--base-button-background
--youtube-player--base-button-foreground
--youtube-player--faded-button-background
--youtube-player--faded-button-foreground

They default to:

--youtube-player--base-button-background: rgb(255 255 255 / 0.4); 
--youtube-player--base-button-foreground: rgb(0 0 0 / 0.8);
--youtube-player--faded-button-background: rgb(255 255 255 / 0.2);
--youtube-player--faded-button-foreground: rgb(0 0 0 / 0.3);

Hovering over a button causes the foreground and background to reverse.

Button Icons

The buttons use SVGs for their icons. The varialbes are:

--youtube-player--fast-forward-icon
--youtube-player--mute-icon
--youtube-player--next-chapter-icon
--youtube-player--pause-icon
--youtube-player--play-icon
--youtube-player--previous-chapter-icon
--youtube-player--restart-icon
--youtube-player--rewind-icon
--youtube-player--unmute-icon

They can be updated with an embedded URL for another SVG. For example:

--youtube-player--play-icon: url("data:image/svg+xml;utf8,%3Csvg%0A%20%20viewBox%3D%220%200%20100%20100%22%0A%20%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%0A%20%20stroke%3D%22red%22%0A%20%20fill%3D%22grey%22%3E%0A%20%20%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2240%22%20%2F%3E%0A%3C%2Fsvg%3E%0A");

The buttons use mask-image for the SVGs. That allows them to be styled with the custom CSS property variables. The masking is done as an alpha channel. Colors aren't used.

Note that you'll need to properly escape the SVG string for things to work. See: Optimizing SVGs in data URIs for guidance.

Transition Time

The transition time for the fades can be updated with:

--youtube-player--transition-time

It defaults to:

--youtube-player--transition-time: 0.3s

Page Styling

The player adds a data-youtube-player-state attribute to the <body> tag of the page the first time a video is played. The value is playing whenever a video is playing. This attribute can be used to adjust styles on the page outside the player's custom element.

For example, this page has the following styles which fade the background to black and the text colors to gray via my custom CSS properties when the video is playing:

body {
  transition: background-color 0.6s ease-in, color 0.6s ease-in;
}

body[data-youtube-player-state="playing"] {
  --background-color: black;
  --primary-color: #444;
  --secondary-color: #444;
}

Known Bugs

Someday/Maybe TODOs

Notes

References

github repo