Skip to content

Commit 85ee66c

Browse files
committed
add shorts
1 parent 9e2590a commit 85ee66c

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

components/youtube-short-embed.tsx

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
"use client";
22

33
import { youtubeParser } from "@/lib/utils";
4-
import { useState } from "react";
4+
import { useState, useEffect } from "react";
55

66
export function YouTubeShortEmbed(props: {
77
youtube: string;
88
children?: React.ReactNode;
9+
loadEmbed?: boolean;
910
}) {
10-
const { youtube, children } = props;
11+
const { youtube, children, loadEmbed: load } = props;
1112
const [loadEmbed, setLoadEmbed] = useState(false);
1213
const id = youtubeParser(youtube);
1314

15+
useEffect(() => {
16+
if (load) {
17+
setLoadEmbed(true);
18+
} else {
19+
setLoadEmbed(false);
20+
}
21+
}, [load]);
22+
1423
return (
1524
<div
1625
style={{
@@ -57,14 +66,32 @@ export function YouTubeShortEmbed(props: {
5766
</button>
5867
</div>
5968
{loadEmbed && (
60-
<iframe
61-
style={{ height: "100%", width: "100%", border: 0 }}
62-
src={`https://www.youtube-nocookie.com/embed/${id}?autoplay=1&fs=0`}
63-
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
64-
allowFullScreen
65-
title="YouTube video player"
66-
/>
69+
<>
70+
<iframe
71+
style={{ height: "100%", width: "100%", border: 0 }}
72+
src={`https://www.youtube-nocookie.com/embed/${id}?fs=0`}
73+
allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
74+
allowFullScreen
75+
title="YouTube video player"
76+
/>
77+
<button
78+
type="button"
79+
onClick={() => setLoadEmbed(false)}
80+
aria-label="Close"
81+
style={{
82+
border: "0",
83+
background: "none",
84+
position: "absolute",
85+
top: 0,
86+
right: 0,
87+
color: "white",
88+
padding: "8px"
89+
}}
90+
>
91+
X
92+
</button>
93+
</>
6794
)}
6895
</div>
6996
);
70-
}
97+
}

components/youtube-short.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export function YouTubeShort(props: {
88
youtube: string;
99
image?: CloudinaryAsset | null | undefined;
1010
className?: string;
11+
isActive?: boolean;
1112
}) {
12-
const { youtube, image, className } = props;
13+
const { youtube, image, className, isActive } = props;
1314
const id = youtubeParser(youtube);
1415

1516
return (
16-
<YouTubeShortEmbed youtube={youtube}>
17+
<YouTubeShortEmbed youtube={youtube} loadEmbed={isActive}>
1718
{image?.public_id ? (
1819
<CoverImage image={image} priority={true} className={className} />
1920
) : (
@@ -37,4 +38,4 @@ export function YouTubeShort(props: {
3738
)}
3839
</YouTubeShortEmbed>
3940
);
40-
}
41+
}

components/youtube-shorts.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
11
'use client'
22

3-
import React from 'react'
3+
import React, { useState, useEffect } from 'react'
44
import { Card, CardContent } from "@/components/ui/card"
55
import {
66
Carousel,
77
CarouselContent,
88
CarouselItem,
99
CarouselNext,
1010
CarouselPrevious,
11+
type CarouselApi,
1112
} from "@/components/ui/carousel"
1213
import { YouTubeShort } from './youtube-short'
1314
import Autoplay from "embla-carousel-autoplay"
1415

1516
export function YouTubeShorts(props: { shorts: string[] }) {
1617
const { shorts } = props
18+
const [api, setApi] = useState<CarouselApi>()
19+
const [current, setCurrent] = useState(0)
20+
21+
useEffect(() => {
22+
if (!api) {
23+
return
24+
}
25+
26+
setCurrent(api.selectedScrollSnap())
27+
28+
api.on("select", () => {
29+
setCurrent(api.selectedScrollSnap())
30+
})
31+
}, [api])
1732

1833
if (shorts.length === 1) {
1934
return (
2035
<div className='flex items-center justify-center w-full'>
2136
<div className="aspect-w-9 aspect-h-16 w-full max-w-xs">
22-
<YouTubeShort youtube={shorts[0]} />
37+
<YouTubeShort youtube={shorts[0]} isActive={true} />
2338
</div>
2439
</div>
2540
)
2641
}
2742

2843
return (
2944
<div className='flex items-center justify-center w-full'>
30-
3145
<Carousel
46+
setApi={setApi}
3247
className="w-full max-w-xs"
33-
plugins={[
34-
Autoplay({
35-
delay: 2000,
36-
}),
37-
]}
48+
// plugins={[
49+
// Autoplay({
50+
// delay: 2000,
51+
// }),
52+
// ]}
3853
>
3954
<CarouselContent>
4055
{shorts.map((short, index) => (
4156
<CarouselItem key={index}>
4257

43-
<YouTubeShort youtube={short} />
58+
<YouTubeShort youtube={short} isActive={index === current} />
4459

4560
</CarouselItem>
4661
))}

0 commit comments

Comments
 (0)