• Stars
    star
    642
  • Rank 70,096 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The fastest little YouTube web component on this side of the internet. The shadow dom web component version of Paul's lite-youtube-embed.

npm version min+gzip min+br

Statements Branches Functions Lines

<lite-youtube>

A web component that renders YouTube embeds faster. The ShadowDom web component version of Paul's lite-youtube-embed.

Features

  • No dependencies; it's just a vanilla web component.
  • It's fast yo.
  • It's Shadow Dom encapsulated!
  • It's responsive 16:9
  • It's accessible via keyboard and will set ARIA via the videotitle attribute
  • It's locale ready; you can set the videoplay to have a properly locale based label
  • Set the start attribute to start at a particular place in a video
  • You can set autoload to use Intersection Observer to load the iframe when scrolled into view.
  • Loads placeholder image as WebP with a Jpeg fallback
  • new in v1.1: Adds nocookie attr for use with use youtube-nocookie.com as iframe embed uri
  • new in v1.2: Adds playlistid for playlist loading interface support
  • new in v1.3: Adds loading=lazy to image placeholder for more perf with posterloading attr if you'd like to use eager
  • new in v1.4: Adds short attr for enabling experimental YouTube Shorts mobile interaction support. See (example video)[https://www.youtube.com/watch?v=aw7CRQTuRfo] for details.
  • new in v1.5: Adds support for nonce attribute via window.liteYouTubeNonce for CSP 2/3 support.

Install via package manager

This web component is built with ES modules in mind and is available on NPM:

To install, use your package manager of choice:

npm i @justinribeiro/lite-youtube
# or
yarn add @justinribeiro/lite-youtube

After install, import into your project:

import '@justinribeiro/lite-youtube';

Install with CDN

If you want the paste-and-go version, you can simply load it via CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/@justinribeiro/[email protected]/lite-youtube.js"></script>

Basic Usage

<lite-youtube videoid="guJLfqTFfIw"></lite-youtube>

Basic Usage with Fallback Link

A fallback appears in any of the following circumstances:

  1. Before the compontent is initialized
  2. When JS is disabled (like <noscript>)
  3. When JS fails or the lite-youtube script is not loaded/executed
  4. When the browser doesn't support web components
<lite-youtube videoid="guJLfqTFfIw">
  <a class="lite-youtube-fallback" href="https://www.youtube.com/watch?v=guJLfqTFfIw">Watch on YouTube: "Sample output of devtools-to-video cli tool"</a>
</lite-youtube>

Example CSS:

.lite-youtube-fallback {
	aspect-ratio: 16 / 9; /* matches YouTube player */
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	gap: 1em;
	padding: 1em;
	background-color: #000;
	color: #fff;
	text-decoration: none;
}

/* right-facing triangle "Play" icon */
.lite-youtube-fallback::before {
	display: block;
	content: '';
	border: solid transparent;
	border-width: 2em 0 2em 3em;
	border-left-color: red;
}

.lite-youtube-fallback:hover::before {
	border-left-color: #fff;
}

.lite-youtube-fallback:focus {
	outline: 2px solid red;
}

Playlist Usage

Setting the YouTube playlistid allows the playlist interface to load on interaction. Note, this still requires a videoid for to load a placeholder thumbnail as YouTube does not return a thumbnail for playlists in the API.

<lite-youtube
  videoid="VLrYOji75Vc"
  playlistid="PL-G5r6j4GptH5JTveoLTVqpp7w2oc27Q9"
></lite-youtube>

Add Video Title

<lite-youtube
  videotitle="This is a video title"
  videoid="guJLfqTFfIw"
></lite-youtube>

Update interface for Locale

<lite-youtube
  videoplay="Mirar"
  videotitle="Mis hijos se burlan de mi espaΓ±ol"
  videoid="guJLfqTFfIw"
>
</lite-youtube>

Style It

Height and Width are responsive in the component.

<style>
  .styleIt {
    width: 400px;
    margin: auto;
  }
</style>
<div class="styleIt">
  <lite-youtube videoid="guJLfqTFfIw"></lite-youtube>
</div>

Enable YouTube Shorts interaction on mobile

See the example video of how this feature works for additional details.

<lite-youtube videoid="vMImN9gghao" short></lite-youtube>

AutoLoad with IntersectionObserver

Uses Intersection Observer if available to automatically load the YouTube iframe when scrolled into view.

<lite-youtube videoid="guJLfqTFfIw" autoload> </lite-youtube>

Set a video start time

<!-- Start at 5 seconds -->
<lite-youtube videoid="guJLfqTFfIw" videoStartAt="5"></lite-youtube>

Fine tune the poster quality for a video

<lite-youtube
  videoid="guJLfqTFfIw"
  posterquality="maxresdefault"
></lite-youtube>

YouTube QueryParams

Use any YouTube Embedded Players and Player Parameters you like.

Note: the exception to this rule is the autoplay param; because of the nature of the performance loading and the inconsistency of usage, that parameter generally does not work. See this comment for details.

<lite-youtube videoid="guJLfqTFfIw" params="controls=0&enablejsapi=1">
</lite-youtube>

Attributes

The web component allows certain attributes to be give a little additional flexibility.

Name Description Default
videoid The YouTube videoid ``
playlistid The YouTube playlistid; requires a videoid for thumbnail ``
videotitle The title of the video Video
videoplay The title of the play button (for translation) Play
videoStartAt Set the point at which the video should start, in seconds 0
posterquality Set thumbnail poster quality (maxresdefault, sddefault, mqdefault, hqdefault) hqdefault
posterloading Set img lazy load attr loading for poster image lazy
nocookie Use youtube-nocookie.com as iframe embed uri false
autoload Use Intersection Observer to load iframe when scrolled into view false
short Show 9:16 YouTube Shorts-style interaction on mobile devices false
params Set YouTube query parameters ``

Events

The web component fires events to give the ability understand important lifecycle.

Event Name Description Returns
liteYoutubeIframeLoaded When the iframe is loaded, allowing us of JS API detail: { videoId: this.videoId }

More Repositories

1

speedometer-pwa

A tiny no-frills speedometer progressive web app based on Geolocation API, AmbientLightSensor API, and WakeLock API.
JavaScript
347
star
2

dockerfiles

Various dockerfiles including chrome-headless, lighthouse and other tooling.
Dockerfile
212
star
3

go-gopher-model

A 3D model of the Go Gopher mascot. We ❀️ Go.
188
star
4

pwa-firebase-functions-botrender

Rendering PWA metadata to link bots with a Firebase Function and Chrome headless
HTML
67
star
5

zotero-google-scholar-citation-count

Zotero plugin for fetching number of citations from Google Scholar.
JavaScript
60
star
6

lighthouse-mocha-example

Sample using lighthouse and lighthouse-core with Mocha to run tests on your project/site.
JavaScript
59
star
7

lighthouse-action

Audit deployed web sites with my artisanal blend of WPT Network Emulation Profiles, Puppeteer, Chrome headless, Lighthouse, and Github Actions.
JavaScript
55
star
8

blog-pwa

Combining Lit Web Components, Hugo, Service Worker, and Rollup into a progressive web app blog.
JavaScript
53
star
9

readinglight-pwa

A tiny no-frills screen reading light progressive web app based on WakeLock API.
JavaScript
52
star
10

android-hostcardemulation-sample

Android HostCardEmulation sample that implements NFC Forum Type 4 Tag Operation
Java
51
star
11

immersive-videoplayer-android

A demo project that incorporates a video player that hides unnecessary ui elements from the screen.
Java
37
star
12

lighthouse-jest-example

Sample using lighthouse and puppeteer with jest to run tests on your project/site.
JavaScript
32
star
13

ginger

A WebGL morph target progressive web application demo.
JavaScript
23
star
14

code-block

A web component that displays colorfully formatted code with Prism.js and LitElement.
JavaScript
23
star
15

stl-part-viewer

A lit-element web component that uses Three.js to display an STL model file.
JavaScript
19
star
16

onscreen-keyboard-html5-canvas-multitouch

Multitouch keyboard implemented with HTML5 canvas, touch events API and Magictouch.js
JavaScript
13
star
17

glass-asset-utils

Tooling to generate Google Glass related materials (icons, layouts, et cetera).
JavaScript
12
star
18

barcode-reader

A web component that reads barcodes via the Shape Detection API via a Web Worker.
JavaScript
12
star
19

resp-img-get-serving-url-appengine

Using Google Cloud Storage with Images Python API for responsive image fun
Python
11
star
20

devtools-to-video

Output a video file from screenshot frames within a Chrome DevTools JSON trace file.
JavaScript
11
star
21

lite-tiktok

A web component that lazy loads TikTok embeds. Currently experimental and a work in progress.
TypeScript
10
star
22

ga-dnt-analytics

Basic Google Analytics web component with Do Not Track support.
HTML
9
star
23

justinribeiro.com-examples

Examples and code from justinribeiro.com
JavaScript
9
star
24

webnfc-reader

A Polymer 3 / lit-element web component that uses WebNFC to read NFC tags.
JavaScript
9
star
25

hce-to-chromenfc-app

Chrome App that talks to Android host-based card emulation application.
JavaScript
9
star
26

Github2Glass

Listen for Github commits and send them to Google Glass
PHP
7
star
27

html5-dragdroptouch-shim

An opinionated shim that polyfills HTML5 drag and drop support on mobile devices with Event.ComposedPath() support
JavaScript
7
star
28

vscode-polymer2-snippets

LitElement, Polymer v2/v3, and Web Components code snippets for Visual Studio Code.
TypeScript
6
star
29

chrome-devtools-theme-atom-dark-green-cursor

An experimental Chrome DevTools theme.
CSS
5
star
30

dof-calculator-pwa

A tiny no-frills progressive web app depth of field calculator for film cameras.
HTML
5
star
31

share-to-mastodon

A small web component that uses a <dialog> to allow setting of a target Mastodon instance from the user to allow easier sharing.
JavaScript
5
star
32

ghost-gcs

Plugin that adds Google Cloud Storage support for Ghost 0.11.10.
JavaScript
3
star
33

service-worker-codelab-gdg-oakdale

Service Worker Codelab for Feb 2015 GDG Oakdale Meetup
JavaScript
3
star
34

glass-task-future

Sample Glassware application that shows the use of a App Engine push task queue to serve cards in the future.
Python
2
star
35

wan-snmp-to-firebase-bridge

Python script that uses Snimpy to extract SNMP data for WAN traffic and push to Firebase
Python
2
star
36

polymer-glass-timer-mqtt

<glass-timer> polymer element for feeding data from Google Glass > MQTT Broker > Web
JavaScript
2
star
37

toggle-tooltip

An accessible, fast, and WCAG 2.1 compliant toggle style tooltip web component using role=status.
TypeScript
2
star
38

chrome-extensions-codelab-gdg-oakdale

Getting started building Chrome extensions.
JavaScript
2
star
39

lighthouse-autocompletion

Command line auto-completion for Lighthouse (https://github.com/GoogleChrome/lighthouse)
Shell
2
star
40

glass-gdk-lowfreq-mqtt-live-card

Sample Glassware GDK application that shows the use of Eclipse Paho/MQTT for low frequency live card.
Java
2
star
41

paper-snackbar

Polymer 2 web component providing brief feedback about an operation through a message at the bottom of the screen that is loosely based on the Material Design spec.
HTML
2
star
42

we-model-500-phone-stand

3D model of Western Electric Model 500 base for a modern cell phone.
2
star
43

ReplicatorG-webstatus

Web status frontend for my ReplicatorG branch
JavaScript
1
star
44

jquery-websocket-callback

jQuery plugin that implements WebSockets with $.callbacks()
JavaScript
1
star
45

gen-labels-github

Bash script that calls GitHub API, remove default repo labels, and adds new labels.
Shell
1
star
46

ReplicatorG-websocketserver

WebSocket server based on pywebsocket for use with my ReplicatorG and ReplicatorG-webstatus branches
Python
1
star
47

4x5-film-masks-beseler

3D models for 4x5 film masks for a custom negative holder for my Beseler 45MX enlarger.
1
star
48

dotfiles

Bespoke dotfiles per my preferences. **There be dragons.**
Shell
1
star
49

glass-daily-card

Sample Glassware application that uses App Engine cron to send a daily card to users.
Python
1
star
50

is-justin-talking

Progressive Web App for use with my traveling beacon that offers my slides.
JavaScript
1
star
51

lit-web-components-vscode-extension-pack

A set of VS Code extensions useful for developing LitElement Web Components.
1
star