svelte-youtube

Simple Svelte component acting as a thin layer over the YouTube IFrame Player API. Based on react-youtube.

Features

NOTICE

I currently don't have the time to maintain this package, as I am not actively working on any Svelte app. If you are interested in maintaining the NPM package going forward, please send me an email.

Installation

$ npm install svelte-youtube

Usage

<script>
  import YouTube from 'svelte-youtube';
</script>

<YouTube
  videoId={string}                  // defaults -> null
  id={string}                       // defaults -> null
  class={string}                    // defaults -> null
  options={obj}                     // defaults -> {}
/>

Events

The following events are available:

  • on:ready: Player has finished loading and is ready to play
  • on:play: Playback has started
  • on:pause: Playback has been paused
  • on:end: Playback has ended
  • on:error: An error has occurred (see below)
  • on:stateChange: Player State has changed (see below)
  • on:playbackRateChange: Playback rate has changed (see below)
  • on:playbackQualityChange: Playback quality has changed (see below)

Each event's detail property contains a data and a target property (except for the ready event, which does not have a data property). The target is a reference to the player instance, while the data contains information specific to the event.

For details on the contents of the data property, and for a more detailed description of each event, refer to the YouTube IFrame Player API Events .

Player State

For convenience it is also possible to access the PlayerState constants through svelte-youtube.

The PlayerState named export contains the values that are used by the YouTube IFrame Player API.

Player Errors

Refer to YouTube IFrame Player API for an explanation of the error codes used in the on:error event.

Controlling the player

Each of the component's events contains a target property in the event's detail object. This property contains a reference to the underlying player instance. Once you have an instance of the player object, you may call any of the available functions on it.

Example

<script>
  import YouTube from 'svelte-youtube';

  const options = {
    height: '390',
    width: '640',
    //  see https://developers.google.com/youtube/player_parameters
    playerVars: {
      autoplay: 1
    }
  };

  function onReady(event) {
    // access to player in all event handlers via event.target
    event.target.pauseVideo();
  }
</script>

<YouTube videoId="2g811Eo7K8U" {options} on:ready={onReady} />

License

MIT

Top categories

Loading Svelte Themes