npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bentoproject/youtube

v1.2302271541.0

Published

Embeds Youtube videos.

Downloads

533

Readme

Bento Youtube

Embeds Youtube videos.

Web Component

You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See Layout and style.

Import via npm

npm install @bentoproject/youtube
import {defineElement as defineBentoYoutube} from '@bentoproject/youtube';
defineBentoYoutube();

Include via <script>

<script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-youtube-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-youtube-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-youtube-1.0.css" crossorigin="anonymous">

Example

<!DOCTYPE html>
<html>
  <head>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-youtube-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-youtube-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-youtube-1.0.css"
    />
    <style>
      bento-youtube {
        width: 320px;
        height: 180px;
      }
    </style>
  </head>
  <body>
    <bento-youtube data-videoid="dQw4w9WgXcQ"></bento-youtube>
  </body>
</html>

Layout and style

Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.

<link
  rel="stylesheet"
  type="text/css"
  href="https://cdn.ampproject.org/v0/bento-youtube-1.0.css"
/>

Alternatively, you may also make the light-weight pre-upgrade styles available inline:

<style>
  bento-youtube {
    display: block;
    overflow: hidden;
    position: relative;
  }
</style>

Container type

The bento-youtube component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height, width, aspect-ratio, or other such properties):

bento-youtube {
  height: 100px;
  width: 100%;
}

Attributes

autoplay

If this attribute is present, and the browser supports autoplay:

loop

If this attribute is present, the video or playlist will play again (from the beginning) once it ends.

data-videoid

The YouTube video id found in every YouTube video page URL.

For example, in this URL: https://www.youtube.com/watch?v=Z1q71gFeRqM, Z1q71gFeRqM is the video id.

data-live-channelid

The Youtube channel id that provides a stable livestream url. For example, in this URL: https://www.youtube.com/embed/live_stream?channel=UCB8Kb4pxYzsDsHxzBfnid4Q, UCB8Kb4pxYzsDsHxzBfnid4Q is the channel id. You can provide a data-live-channelid instead of a data-videoid attribute to embed a stable url for a live stream instead of a video. Channels do not come with default placeholders. You can provide a placeholder for the video per example 2 above.

data-param-*

All data-param-* attributes (with the exception of data-param-autoplay and data-param-loop) will be added as query parameter to the YouTube iframe src. This may be used to pass custom values through to YouTube plugins, such as whether to show controls.

Keys and values will be URI encoded. Keys will be camel cased.

See YouTube Embedded Player Parameters for more parameter options for YouTube.

credentials (optional)

Defines a credentials option as specified by the Fetch API (omit or include).

If you want to use the YouTube player in privacy-enhanced mode, pass the value of omit.

Usually YouTube sets its cookies when the player is loaded. In privacy-enhanced mode cookies are set when the user has clicked on the player.

title (optional)

Defines a title attribute for the component to propagate to the underlying <iframe> element. The default value is "YouTube video".

Interactivity and API usage

The bento-youtube component API is accessible by including the following script tag in your document:

await customElements.whenDefined('bento-youtube');
const api = await video.getApi();
<!DOCTYPE html>
<html>
  <head>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-youtube-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-youtube-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-youtube-1.0.css"
    />
    <style>
      bento-youtube {
        width: 320px;
        height: 180px;
      }
    </style>
  </head>
  <body>
    <bento-youtube id="my-video" data-videoid="dQw4w9WgXcQ">
    </bento-youtube>
    <p>
      <button id="play">Play</button>
      <button id="pause">Pause</button>
      <button id="mute">Mute</button>
      <button id="unmute">Unmute</button>
    </p>
    <script>
      (async () => {
        await customElements.whenDefined('youtube');

        const video = document.querySelector('#my-video');
        const api = await video.getApi();

        document.querySelector('#play').onclick = () => {
          api.play();
        };
        document.querySelector('#pause').onclick = () => {
          api.pause();
        };
        document.querySelector('#mute').onclick = () => {
          api.mute();
        };
        document.querySelector('#unmute').onclick = () => {
          api.unmute();
        };
      })();
    </script>
  </body>
</html>

Actions

The bento-youtube API allows you to perform the following actions:

play()

Plays the video.

api.play();
pause()

Pauses the video.

api.pause();
mute()

Mutes the video.

api.mute();
unmute()

Unmutes the video.

api.unmute();
requestFullscreen()

Expands the video to fullscreen when possible.

api.requestFullscreen();

Properties

It also exposes the following read-only properties:

currentTime (number)

The current playback time in seconds.

console.log(api.currentTime);
duration (number)

The video's duration in seconds, when it's known (e.g. is not a livestream).

console.log(api.duration);
autoplay (boolean)

Whether the video autoplays.

console.log(api.autoplay);
controls (boolean)

Whether the video shows controls.

console.log(api.controls);
loop (boolean)

Whether the video loops.

console.log(api.loop);

Preact/React Component

Import via npm

npm install @bentoproject/youtube
import React from 'react';
import {BentoYoutube} from '@bentoproject/youtube/react';
import '@bentoproject/youtube/styles.css';

function App() {
  return <BentoYoutube videoid="dQw4w9WgXcQ" />;
}

Layout and style

Container type

The BentoYoutube component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height, width, aspect-ratio, or other such properties). These can be applied inline:

<BentoYoutube
  style={{width: 320, height: 180}}
  videoid="dQw4w9WgXcQ"
/>

Or via className:

<BentoYoutube
  className="custom-styles"
  videoid="dQw4w9WgXcQ"
/>
.custom-styles {
  height: 320px;
  width: 180px;
}

You may use aspect-ratio to size it responsively:

.custom-styles {
  aspect-ratio: 16/9;
}

Props

autoplay

If this prop is true, and the browser supports autoplay:

loop

If this prop is true, the video or playlist will play again (from the beginning) once it ends.

videoid

The YouTube video id found in every YouTube video page URL.

For example, in this URL: https://www.youtube.com/watch?v=Z1q71gFeRqM, Z1q71gFeRqM is the video id.

liveChannelid

The Youtube channel id that provides a stable livestream url. For example, in this URL: https://www.youtube.com/embed/live_stream?channel=UCB8Kb4pxYzsDsHxzBfnid4Q, UCB8Kb4pxYzsDsHxzBfnid4Q is the channel id. You can provide a liveChannelid prop instead of a videoid prop to embed a stable url for a live stream instead of a video. Channels do not come with default placeholders. You can provide a placeholder for the video per example 2 above.

params

All fields in this object (with the exception of autoplay and loop) will be added as query parameters to the YouTube iframe src. This may be used to pass custom values through to YouTube plugins, such as whether to show controls.

Keys and values will be URI encoded. Keys must be camel cased.

See YouTube Embedded Player Parameters for more parameter options for YouTube.

credentials (optional)

Defines a credentials option as specified by the Fetch API (omit or include).

If you want to use the YouTube player in privacy-enhanced mode, pass the value of omit.

Usually YouTube sets its cookies when the player is loaded. In privacy-enhanced mode cookies are set when the user has clicked on the player.

title (optional)

Defines a title attribute for the component to propagate to the underlying <iframe> element. The default value is "YouTube video".

Interactivity and API usage

The BentoYoutube component API is accessible by passing a ref:

import React, {createRef} from 'react';

function App() {
  const ref = createRef();
  return (
    <BentoYoutube ref={ref} />
  );
}

Actions

The BentoYoutube API allows you to perform the following actions:

play()

Plays the video.

ref.current.play();
pause()

Pauses the video.

ref.current.pause();
mute()

Mutes the video.

ref.current.mute();
unmute()

Unmutes the video.

ref.current.unmute();
requestFullscreen()

Expands the video to fullscreen when possible.

ref.current.requestFullscreen();

Properties

It also exposes the following read-only properties:

currentTime (number)

The current playback time in seconds.

console.log(ref.current.currentTime);
duration (number)

The video's duration in seconds, when it's known (e.g. is not a livestream).

console.log(ref.current.duration);
autoplay (boolean)

Whether the video autoplays.

console.log(ref.current.autoplay);
controls (boolean)

Whether the video shows controls.

console.log(ref.current.controls);
loop (boolean)

Whether the video loops.

console.log(ref.current.loop);