|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import React from 'react' |
| 3 | +import React, { useState, useEffect } from 'react' |
4 | 4 | import { Card, CardContent } from "@/components/ui/card" |
5 | 5 | import { |
6 | 6 | Carousel, |
7 | 7 | CarouselContent, |
8 | 8 | CarouselItem, |
9 | 9 | CarouselNext, |
10 | 10 | CarouselPrevious, |
| 11 | + type CarouselApi, |
11 | 12 | } from "@/components/ui/carousel" |
12 | 13 | import { YouTubeShort } from './youtube-short' |
13 | 14 | import Autoplay from "embla-carousel-autoplay" |
14 | 15 |
|
15 | 16 | export function YouTubeShorts(props: { shorts: string[] }) { |
16 | 17 | 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]) |
17 | 32 |
|
18 | 33 | if (shorts.length === 1) { |
19 | 34 | return ( |
20 | 35 | <div className='flex items-center justify-center w-full'> |
21 | 36 | <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} /> |
23 | 38 | </div> |
24 | 39 | </div> |
25 | 40 | ) |
26 | 41 | } |
27 | 42 |
|
28 | 43 | return ( |
29 | 44 | <div className='flex items-center justify-center w-full'> |
30 | | - |
31 | 45 | <Carousel |
| 46 | + setApi={setApi} |
32 | 47 | className="w-full max-w-xs" |
33 | | - plugins={[ |
34 | | - Autoplay({ |
35 | | - delay: 2000, |
36 | | - }), |
37 | | - ]} |
| 48 | + // plugins={[ |
| 49 | + // Autoplay({ |
| 50 | + // delay: 2000, |
| 51 | + // }), |
| 52 | + // ]} |
38 | 53 | > |
39 | 54 | <CarouselContent> |
40 | 55 | {shorts.map((short, index) => ( |
41 | 56 | <CarouselItem key={index}> |
42 | 57 |
|
43 | | - <YouTubeShort youtube={short} /> |
| 58 | + <YouTubeShort youtube={short} isActive={index === current} /> |
44 | 59 |
|
45 | 60 | </CarouselItem> |
46 | 61 | ))} |
|
0 commit comments