Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add liteYoutubeIframeLoaded event for iframe load #39

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ flexibility.
| `nocookie` | Use youtube-nocookie.com as iframe embed uri | `false` |
| `autoload` | Use Intersection Observer to load iframe when scrolled into view | `false` |
| `params` | Set YouTube query parameters | `` |

## Events

The web component allows certain attributes to be give a little additional
flexibility.

| Event Name | Description | Returns |
| -------------- | ---------------------------------------------------------------- | ------- |
| `liteYoutubeIframeLoaded` | When the iframe is loaded, allowing us of JS API | `detail: { videoId: this.videoId }` |
52 changes: 51 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ <h3>YouTube QueryParams</h3>
&lt;lite-youtube videoid=&quot;guJLfqTFfIw&quot; params=&quot;controls=0&amp;enablejsapi=1&quot;&gt;&lt;/lite-youtube&gt;
</pre>
<lite-youtube
id="test-jsapi"
videoid="guJLfqTFfIw"
params="controls=0&enablejsapi=1"
></lite-youtube>
Expand All @@ -124,6 +125,55 @@ <h3>YouTube QueryParams</h3>
<lite-youtube
videoid="guJLfqTFfIw"
nocookie
></lite-youtube
></lite-youtube>

<script type="text/javascript">
// From https://developers.google.com/youtube/iframe_api_reference#Examples
const tag = document.createElement('script');
tag.id = 'iframe-demo';
tag.src = 'https://www.youtube.com/iframe_api';
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

let player;

function onPlayerReady(event) {
document.querySelector('#test-jsapi').style.borderColor = '#FF6D00';
document.querySelector('#test-jsapi').style.borderWidth = '5px';
document.querySelector('#test-jsapi').style.borderStyle = 'solid';
}

function changeBorderColor(playerStatus) {
let color;
if (playerStatus == -1) {
color = "#37474F"; // unstarted = gray
} else if (playerStatus == 0) {
color = "#FFFF00"; // ended = yellow
} else if (playerStatus == 1) {
color = "#33691E"; // playing = green
} else if (playerStatus == 2) {
color = "#DD2C00"; // paused = red
} else if (playerStatus == 3) {
color = "#AA00FF"; // buffering = purple
} else if (playerStatus == 5) {
color = "#FF6DOO"; // video cued = orange
}
if (color) {
document.querySelector('#test-jsapi').style.borderColor = color;
}
}
function onPlayerStateChange(event) {
changeBorderColor(event.data);
}

document.addEventListener('liteYoutubeIframeLoaded', () => {
player = new YT.Player(document.querySelector('#test-jsapi').shadowRoot.querySelector('iframe'), {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}, false);
</script>
</body>
</html>
9 changes: 9 additions & 0 deletions lite-youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ export class LiteYTEmbed extends HTMLElement {
this.domRefFrame.insertAdjacentHTML('beforeend', iframeHTML);
this.domRefFrame.classList.add('lyt-activated');
this.iframeLoaded = true;
this.dispatchEvent(
new CustomEvent('liteYoutubeIframeLoaded', {
detail: {
videoId: this.videoId,
},
bubbles: true,
cancelable: true,
}),
);
}
}

Expand Down