Skip to content

Commit b07544c

Browse files
committed
Initial commit
0 parents  commit b07544c

6 files changed

+103
-0
lines changed

LICENSE

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright (c) 2023, Jonny Buchanan
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Astro Lazy YouTube Embed
2+
3+
Embed YouTube videos with a static placeholder which only embeds when you click.
4+
5+
This component uses [an `<iframe>` with `srcdoc` attribute to provide a static placeholder](https://css-tricks.com/lazy-load-embedded-youtube-videos/), which mimics the style of a real YouTube embed (at the time of writing).
6+
7+
## Install
8+
9+
```sh
10+
npm i astro-lazy-youtube-embed
11+
```
12+
13+
## Example
14+
15+
This component uses a `default` export, so you can import it as whichever name you prefer:
16+
17+
```astro
18+
---
19+
import YouTubeVideo from 'astro-lazy-youtube-embed'
20+
---
21+
<YouTubeVideo videoCode="FfTT7mxGw8I" title="Just Curious - Limmy's Homemade Show"/>
22+
23+
<div class="my-8">Provide your own class for additional styling:</div>
24+
25+
<YouTubeVideo
26+
class="rounded"
27+
videoCode="L0C5nyOVTzc"
28+
title="Limmy Teaches Techno - Limmy's Homemade Show"
29+
/>
30+
```
31+
32+
![Rendered version of the above example code](/example.jpg)

YouTubeVideo.astro

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
export interface Props {
3+
class?: string
4+
title: string
5+
videoCode: string
6+
}
7+
8+
let {class: className, title, videoCode} = Astro.props
9+
let playButtonSvg = `<svg height="100%" version="1.1" viewBox="0 0 68 48" width="100%"><path d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#f00"></path><path d="M 45,24 27,14 27,34" fill="#fff"></path></svg>`
10+
let gradientStyle = `.gradient{width:100%;height:49px;padding-bottom:50px;position:absolute;top:0;background-repeat:repeat-x;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAADGCAYAAAAT+OqFAAAAdklEQVQoz42QQQ7AIAgEF/T/D+kbq/RWAlnQyyazA4aoAB4FsBSA/bFjuF1EOL7VbrIrBuusmrt4ZZORfb6ehbWdnRHEIiITaEUKa5EJqUakRSaEYBJSCY2dEstQY7AuxahwXFrvZmWl2rh4JZ07z9dLtesfNj5q0FU3A5ObbwAAAABJRU5ErkJggg==);pointer-events:none;}`
11+
let srcdoc = `<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img{position:absolute;width:100%;top:0;bottom:0;margin:auto}.button{position:absolute;left:50%;top:50%;width:68px;height:48px;margin-left:-34px;margin-top:-24px;}.top{position:absolute;top:18px;left:18px;right:18px;display:flex;flex-wrap:nowrap}.title{color:#fff;font-size:18px;white-space:nowrap;word-wrap:normal;text-shadow:0 0 2px rgba(0,0,0,.5);font-family:"YouTube Noto",Roboto,Arial,Helvetica,sans-serif;line-height:1.3;text-overflow:ellipsis;overflow:hidden;}${gradientStyle}</style><a href="https://www.youtube.com/embed/${videoCode}?autoplay=1"><img src="https://img.youtube.com/vi/${videoCode}/sddefault.jpg" alt="${title}"><div class="gradient"></div><div class="top"><div class="title">${title}</div></div><div class="button">${playButtonSvg}</div></a>`
12+
---
13+
<iframe
14+
class={className}
15+
style="width: 100%; aspect-ratio: 16/9;"
16+
src={`https://www.youtube.com/embed/${videoCode}`}
17+
title={title}
18+
srcdoc={srcdoc}
19+
frameborder="0"
20+
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
21+
allowfullscreen
22+
></iframe>

example.jpg

58 KB
Loading

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './YouTubeVideo.astro'

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "astro-lazy-youtube-embed",
3+
"version": "0.0.1",
4+
"description": "Embed YouTube videos with a static placeholder which only embeds when you click",
5+
"type": "module",
6+
"license": "MIT",
7+
"author": "Jonny Buchanan <[email protected]>",
8+
"exports": {
9+
".": "./index.js"
10+
},
11+
"files": [
12+
"index.js",
13+
"LICENSE",
14+
"YouTubeVideo.astro"
15+
],
16+
"keywords": [
17+
"astro",
18+
"astro-component",
19+
"lazy",
20+
"youtube",
21+
"embed"
22+
],
23+
"peerDependencies": {
24+
"astro": "^1.0.0 || ^2.0.0-beta"
25+
},
26+
"repository": {
27+
"type": "git",
28+
"url": "https://github.com/insin/astro-lazy-youtube-embed.git"
29+
}
30+
}

0 commit comments

Comments
 (0)