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

react-yt

v0.1.2

Published

A YouTube player component for React following the render props pattern

Downloads

257

Readme

react-yt

A full-fledged wrapper for the Youtube Player API created with the render props pattern.

Example

https://btmpl.github.io/react-yt/

Features

  • playback of single video or supported lists (playlist, user uploads, search results),
  • full access to the Youtube Player API inside the render prop,
  • access to the player instance as an escape hatch,
  • ability to control selected features as props to the component

Installation

$ yarn add react-yt

Minimal usage example

/**
 * We are using `module` field to provide an ES module format
 * and `main` field for an CommonJS fallback
 */
import YouTube from "react-yt";
<YouTube
  videoId={'SKGzIhOSQVY'}
  autoplay={true}
/>

Accepted props

The player accepts all the official player parameters as props.

Prop name|TypeScript type|Accepted values ---|---|--- autoplay|YT.AutoPlay|0 \| 1 ccLoadPolicy|YT.ClosedCaptionsLoadPolicy|0 \| 1 color | YT.ProgressBarColor|"white" \| "red" controls | YT.Controls|0 \| 1 disablekb | YT.KeyboardControls|0 \| 1 enableJsApi | YT.JsApi|0 \| 1 end | number|number fs | YT.FullscreenButton|0 \| 1 hl | string|ISO 639-1 languag code ivLoadPolicy | YT.IvLoadPolicy|0 \| 1 list | string|string listType | ListType|"playlist" \| "user_uploads" \| "search" loop | YT.Loop|0 \| 1 modestbranding | YT.ModestBranding|0 \| 1 origin | string|string playlist | string|string playsinline | YT.PlaysInline|0 \| 1 rel | YT.RelatedVideos|0 \| 1 showinfo | YT.ShowInfo|0 \| 1 start | number|number videoId | string|string

Additionally it's possible to subscribe to the Player events by providing an events prop with following keys:

Key name|Event signature ---|--- onReady|YT.PlayerEventHandler<YT.PlayerEvent> onStateChange|YT.PlayerEventHandler<YT.OnStateChangeEvent> onPlaybackQualityChange|YT.PlayerEventHandler<YT.OnPlaybackQualityChangeEvent> onPlaybackRateChange|YT.PlayerEventHandler<YT.OnPlaybackRateChangeEvent> onError|YT.PlayerEventHandler<YT.OnErrorEvent> onApiChange|YT.PlayerEventHandler<YT.PlayerEvent>

Using the render prop

The render prop (render) will be called with an object exposing:

Field name|Content ---|--- iframe|The iframe React Element containing the player player|The player instance, allowing access to all internal mechanics

And all the internal Youtube player functions:

Function name|Parameters|Return type ---|---|--- loadVideoById|videoId: string[, startSeconds: number, suggestedQuality: string]|void cueVideoByUrl|videoId: string[, startSeconds: number, suggestedQuality: string]|void loadVideoByUrl|videoUrl: string[, startSeconds: number, suggestedQuality: string]|void loadPlaylist|playlist: string\|Array[, index: number, startSeconds: number, suggestedQuality: string]|void cuePlaylist|playlist: string\|Array[, index: number, startSeconds: number, suggestedQuality: string]|void pauseVideo|void|void playVideo|void|void mute|void|void unMute|void|void isMuted|void|boolean setVolume|number|void getVolume|void|number stopVideo|void|void clearVideo|void|void nextVideo|void|void previousVideo|void|void playVideoAt|number|void seekTo|number|void getPlaybackRate|void|number setPlaybackRate|number|void getAvailablePlaybackRates|void|Array<number> setLoop|boolean|void setShuffle|boolean|void getPlayerState|void|number getCurrentTime|void|number getPlaybackQuality|void|string setPlaybackQuality|string|void getVideoLoadedFraction|void|float getDuration|void|number getVideoUrl|void|string getVideoEmbedCode|void|string getPlaylist|void|Array<string> getPlaylistIndex|void|number addEventListener|string, Function|void removeEventListener|string, Function|void

Rendering with render props

<YouTube
  videoId={'SKGzIhOSQVY'}
  render={({
    iframe,
    playVideo,
    pauseVideo,
    getPlayerState
  }) => (
    <div>
      {iframe}
      {getPlayerState() !== 1 && <button onClick={(event) => playVideo()}>Play video</button>}
      {getPlayerState() === 1 && <button onClick={(event) => pauseVideo()}>Pause video</button>}
    </div>
  )}
/>

Controlling the player from outside

While the recommended way to control the playback is from inside the render prop function, it is also possible to control the component from outside by changing the props. Developers are able to provide the following props in order to control the component without having to remount it.

Prop|Result ---|--- videoId|When given a non-falsy value, the selected video will play list + listType |When given a non-falsy value, the selected list will play autoplay|Controls the playback state, false to pause the playback, or true to start / resume

The logic uses componentWillReceiveProps to control the playback, so passing the props in a given order will override older props. Passing new values to both props at the same time will give priority to the videoId prop.

License

MIT