- reduces initial load size by ~1.1MB per video
- built with a11y in mind β fully accessible via keyboard and to screen readers
.webp
thumbnail format for modern browsers that support it, with.jpg
fallback for browsers that don't- fully customizable through
props
andslots
$ npm install vue-lazy-youtube-video --save
$ yarn add vue-lazy-youtube-video
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue-lazy-youtube-video@1.x"></script>
<script>
// as a plugin
Vue.use(VueLazyYoutubeVideo.Plugin)
// as a component
Vue.component('LazyYoutubeVideo', VueLazyYoutubeVideo.default)
</script>
β οΈ It must be called beforenew Vue()
.
import Vue from 'vue'
import LazyYoutubeVideo from 'vue-lazy-youtube-video'
Vue.component('LazyYoutubeVideo', LazyYoutubeVideo)
import LazyYoutubeVideo from 'vue-lazy-youtube-video'
export default {
name: 'YourAwesomeComponent',
components: {
LazyYoutubeVideo,
},
}
There is special bundle for SSR users:
import Vue from 'vue'
import LazyYoutubeVideo, {
Plugin,
} from 'vue-lazy-youtube-video/dist/vue-lazy-youtube-video.ssr.common'
// have to import ejected style directly, see this issue https://github.com/vuejs/rollup-plugin-vue/issues/266
import 'vue-lazy-youtube-video/dist/style.css'
// as a global component
Vue.component('LazyYoutubeVideo', LazyYoutubeVideo)
// or as a plugin
Vue.use(Plugin)
// or as a local component
export default {
// ...
components: {
LazyYoutubeVideo,
},
// ...
}
β οΈ It must be called beforenew Vue()
.
import Vue from 'vue'
import { Plugin } from 'vue-lazy-youtube-video'
Vue.use(Plugin)
<template>
<LazyYoutubeVideo url="https://www.youtube.com/watch?v=[VIDEO_ID]" />
</template>
π¨ Value of the
url
property must be a DIRECT link to the video (e.g. https://www.youtube.com/watch?v=4JS70KB9GS0), but not the EMBED link (e.g. https://www.youtube.com/embed/4JS70KB9GS0). Component itself will generate appropriatesrc
attribute for the<iframe />
element
The list of available props
(with their types, default values and descriptions) is listed below:
Property | Required | Type | Default | Description |
---|---|---|---|---|
url |
true |
string |
Video URL in https://www.youtube.com/watch?v=[VIDEO_ID] format. Supported URL s are listed here |
|
query |
false |
string |
?autoplay=1 |
Query string which will be appended to the generated embed URL |
alt |
false |
string |
Video thumbnail |
Value of the alt attribute of the thumbnail <img /> element |
buttonLabel |
false |
string |
Play video |
Value of the aria-label attribute of the play <button></button> element. Improves a11y |
aspectRatio |
false |
string |
16:9 |
Aspect ratio of the video. This prop helps to save proportions of the video on different container sizes. Should match the number:number pattern |
previewImageSize |
false |
string |
maxresdefault |
Size of the thumbnail, generated by YouTube. Available variants: default , mqdefault , sddefault , hqdefault , maxresdefault . More info |
thumbnail |
false |
{ webp: string, jpg: string} |
Custom thumbnail object, which should contain two keys: webp and jpg . Value of the key is the path to the custom thumbnail image |
|
noCookie |
false |
boolean |
false |
Whether or not to enable privacy-enhanced mode. If true β component will insert -nocookie part into the generated embed link |
iframeAttributes |
false |
object |
Custom attributes that will be assigned to the <iframe /> element |
|
autoplay |
false |
boolean |
false |
Whether or not to play video as soon as component mounts into the DOM |
Component provides some slots
.
The list of available slots is listed below:
Slot | Description |
---|---|
button |
Slot gives an ability to provide custom play button |
icon |
Slot gives an ability to provide custom icon of the play button |
β οΈ Note, that whenbutton
slot is passed and this slot contains<button></button>
, ones should not to forget to addaria-label
(if this button contains only icon) andtype="button"
attributes. Also, if that button do not contain.y-video-button
class, all default styles will be lost, so style concerns it's up to developer.
Name | Type | Usage |
---|---|---|
videoLoaded | () => void |
The event that is triggered when the <iframe> is inserted into the DOM. |
Jest is used for unit-tests.
Jest
and VueTestUtils
is used for unit tests.
You can run unit tests by running the next command:
npm run test:unit
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
npm run dev
script
To build the production ready bundle simply run npm run build
:
After the successful build the following files will be generated in the dist
folder:
βββ vue-lazy-youtube-video.common.js
βββ vue-lazy-youtube-video.esm.js
βββ vue-lazy-youtube-video.js
βββ vue-lazy-youtube-video.min.js
βββ vue-lazy-youtube-video.ssr.common.js
Vue
Rollup
(and plugins)Babel
Jest
Vue Test Utils
TypeScript
Inspired by Vadim Makeev. Vadim created a comprehensive tutorial in which he shows how to lazyload YouTube videos properly.