0.1.2 • Published 6 years ago

react-yt v0.1.2

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

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 nameTypeScript typeAccepted values
autoplayYT.AutoPlay0 \| 1
ccLoadPolicyYT.ClosedCaptionsLoadPolicy0 \| 1
colorYT.ProgressBarColor"white" \| "red"
controlsYT.Controls0 \| 1
disablekbYT.KeyboardControls0 \| 1
enableJsApiYT.JsApi0 \| 1
endnumbernumber
fsYT.FullscreenButton0 \| 1
hlstringISO 639-1 languag code
ivLoadPolicyYT.IvLoadPolicy0 \| 1
liststringstring
listTypeListType"playlist" \| "user_uploads" \| "search"
loopYT.Loop0 \| 1
modestbrandingYT.ModestBranding0 \| 1
originstringstring
playliststringstring
playsinlineYT.PlaysInline0 \| 1
relYT.RelatedVideos0 \| 1
showinfoYT.ShowInfo0 \| 1
startnumbernumber
videoIdstringstring

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

Key nameEvent signature
onReadyYT.PlayerEventHandler<YT.PlayerEvent>
onStateChangeYT.PlayerEventHandler<YT.OnStateChangeEvent>
onPlaybackQualityChangeYT.PlayerEventHandler<YT.OnPlaybackQualityChangeEvent>
onPlaybackRateChangeYT.PlayerEventHandler<YT.OnPlaybackRateChangeEvent>
onErrorYT.PlayerEventHandler<YT.OnErrorEvent>
onApiChangeYT.PlayerEventHandler<YT.PlayerEvent>

Using the render prop

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

Field nameContent
iframeThe iframe React Element containing the player
playerThe player instance, allowing access to all internal mechanics

And all the internal Youtube player functions:

Function nameParametersReturn type
loadVideoByIdvideoId: string[, startSeconds: number, suggestedQuality: string]void
cueVideoByUrlvideoId: string[, startSeconds: number, suggestedQuality: string]void
loadVideoByUrlvideoUrl: string[, startSeconds: number, suggestedQuality: string]void
loadPlaylistplaylist: string\|Array[, index: number, startSeconds: number, suggestedQuality: string]void
cuePlaylistplaylist: string\|Array[, index: number, startSeconds: number, suggestedQuality: string]void
pauseVideovoidvoid
playVideovoidvoid
mutevoidvoid
unMutevoidvoid
isMutedvoidboolean
setVolumenumbervoid
getVolumevoidnumber
stopVideovoidvoid
clearVideovoidvoid
nextVideovoidvoid
previousVideovoidvoid
playVideoAtnumbervoid
seekTonumbervoid
getPlaybackRatevoidnumber
setPlaybackRatenumbervoid
getAvailablePlaybackRatesvoidArray<number>
setLoopbooleanvoid
setShufflebooleanvoid
getPlayerStatevoidnumber
getCurrentTimevoidnumber
getPlaybackQualityvoidstring
setPlaybackQualitystringvoid
getVideoLoadedFractionvoidfloat
getDurationvoidnumber
getVideoUrlvoidstring
getVideoEmbedCodevoidstring
getPlaylistvoidArray<string>
getPlaylistIndexvoidnumber
addEventListenerstring, Functionvoid
removeEventListenerstring, Functionvoid

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.

PropResult
videoIdWhen given a non-falsy value, the selected video will play
list + listTypeWhen given a non-falsy value, the selected list will play
autoplayControls 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