Skip to content

Commit

Permalink
Add support for the options prop and autoplay
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-be committed Mar 21, 2020
1 parent dcb6987 commit 4f914f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "svelte-youtube",
"version": "0.0.1",
"svelte": "src/index.svelte",
"repository": {
"type": "git",
"url": "https://github.com/PandaWhisperer/svelte-youtube"
},
"homepage": "https://github.com/PandaWhisperer/svelte-youtube#README",
"module": "index.mjs",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 10 additions & 2 deletions src/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
export { className as class }; // HTML class names for container element (optional)
export let id = undefined; // HTML element ID for player (optional)
export let videoId; // Youtube video ID (required)
export let options = undefined; // YouTube player options (optional)
let className; // HTML class names for container element
let playerElem; // player DOM element reference
Expand All @@ -34,7 +35,7 @@
$: play(videoId);
function createPlayer() {
player = YoutubePlayer(playerElem);
player = YoutubePlayer(playerElem, options);
// Register event handlers
player.on('ready', onPlayerReady);
Expand All @@ -48,8 +49,15 @@
}
function play(videoId) {
// this is needed because the loadVideoById function always starts playing,
// even if you have set autoplay to 1 whereas the cueVideoById function
// never starts autoplaying
if (player && videoId) {
player.loadVideoById(videoId);
if (options && options.playerVars && options.playerVars.autoplay === 1) {
player.loadVideoById(videoId);
} else {
player.cueVideoById(videoId);
}
}
}
Expand Down

0 comments on commit 4f914f4

Please sign in to comment.