YouTube
The <YouTube>
component generates an embed using the lite-youtube-embed
custom element that will load YouTube’s <iframe>
only when a user clicks play.
YouTube embeds will always require some JavaScript, but this is one of the most minimal and performant ways to embed a YouTube video.
Usage
You can also pass in a URL in one of the various YouTube formats:
The above code produces the following result:
Optional props
In addition to the required id
prop, the following props are available to customise how the <YouTube>
component renders:
poster
Type: string
You can provide an alternative poster image by passing in a URL to the poster
prop.
For example, this is the same video as above but with a custom poster image:
posterQuality
Type: 'max' | 'high' | 'default' | 'low'
Default: 'default'
When using the default YouTube poster image, set the posterQuality
to change the size of the placeholder image.
This can be useful if displaying the embed at very large or very small sizes.
posterQuality | resolution |
---|---|
'low' | 120px |
'default' | 480px |
'high' | 640px |
'max' | 1280px |
params
You can pass in a params
prop to set the YouTube player parameters. This looks like a series of URL search params.
For example, the following params
value sets the start and end times of the video playback:
playlabel
Type: string
Default: 'Play'
By default, the play button in the embed has an accessible label set to “Play”.
If you want to customise this, for example to match the language of your website, you can set the playlabel
prop:
title
Type: string
Set a visible title to overlay on the video.
Standalone installation
If you only need the <YouTube>
component, you can install the package directly instead of the main astro-embed
package:
The <YouTube>
component can then be imported as: