|
| 1 | +"use client" |
| 2 | + |
| 3 | +import * as React from "react" |
| 4 | +import { LabCTA } from "@/components/mdx/lab-cta" |
| 5 | +import { Button } from "@/components/ui/button" |
| 6 | +import { ChevronLeft, ChevronRight } from "lucide-react" |
| 7 | + |
| 8 | +interface LabItem { |
| 9 | + id: string |
| 10 | + title: string |
| 11 | + description: string |
| 12 | + href: string |
| 13 | +} |
| 14 | + |
| 15 | +interface LabsCarouselProps { |
| 16 | + labs: LabItem[] |
| 17 | +} |
| 18 | + |
| 19 | +export function LabsCarousel({ labs }: LabsCarouselProps) { |
| 20 | + const [index, setIndex] = React.useState(0) |
| 21 | + const hasMultiple = labs?.length > 1 |
| 22 | + |
| 23 | + const goPrev = () => setIndex((prev) => (prev - 1 + labs.length) % labs.length) |
| 24 | + const goNext = () => setIndex((prev) => (prev + 1) % labs.length) |
| 25 | + |
| 26 | + if (!labs || labs.length === 0) return null |
| 27 | + |
| 28 | + const current = labs[index] |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="mt-10"> |
| 32 | + <div className="relative"> |
| 33 | + {hasMultiple && ( |
| 34 | + <div className="absolute -top-10 right-0 flex items-center gap-2"> |
| 35 | + <Button variant="outline" size="icon" onClick={goPrev} aria-label="Previous lab"> |
| 36 | + <ChevronLeft className="h-4 w-4" /> |
| 37 | + </Button> |
| 38 | + <Button variant="outline" size="icon" onClick={goNext} aria-label="Next lab"> |
| 39 | + <ChevronRight className="h-4 w-4" /> |
| 40 | + </Button> |
| 41 | + </div> |
| 42 | + )} |
| 43 | + |
| 44 | + <LabCTA title={current.title} description={current.description} href={current.href} /> |
| 45 | + </div> |
| 46 | + |
| 47 | + {hasMultiple && ( |
| 48 | + <div className="mt-4 flex justify-center gap-2"> |
| 49 | + {labs.map((_, i) => ( |
| 50 | + <button |
| 51 | + key={i} |
| 52 | + aria-label={`Go to slide ${i + 1}`} |
| 53 | + onClick={() => setIndex(i)} |
| 54 | + className={`h-2 w-2 rounded-full transition-colors ${i === index ? 'bg-primary' : 'bg-muted-foreground/30 hover:bg-muted-foreground/60'}`} |
| 55 | + /> |
| 56 | + ))} |
| 57 | + </div> |
| 58 | + )} |
| 59 | + </div> |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +export default LabsCarousel |
| 64 | + |
| 65 | + |
0 commit comments