[Help]: How can I track the selected index using ShadcnUI? #919
-
Summaryhow i can achieve this behavior with embla i tried so hard and this what i got so far but no luck in determining which slide is active right now
If applicable, which variants of Embla Carousel are relevant to this question?
Additional informationi'm using shadcn which is completely based on embla on it's core CodeSandbox exampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
Hi @Adamkaram, Thanks for your question. You're mixing the Shadcn carousel logic with the bare Embla library logic. Shadcn is a layer on top of Embla so you should use their API instead. I would recommend you to access the Embla API as demonstrated here in the Shadcn docs using the "use client"
import React, { useState, useEffect, useCallback } from "react";
import { Card, CardContent } from "@/components/ui/card";
import {
Carousel,
CarouselContent,
CarouselItem,
} from "@/components/ui/carousel";
import "animate.css"; // Import animate.css styles
import Autoplay from "embla-carousel-autoplay";
import Image from 'next/image'
export default function Home() {
const [api, setApi] = useState()
const [selectedIndex, setSelectedIndex] = useState(0)
const updateSelectedIndex = useCallback((api) => {
setSelectedIndex(api.selectedScrollSnap())
}, [])
useEffect(() => {
if (!api) return
updateSelectedIndex() // Update index on mount
api
.on("select", updateSelectedIndex) // Update index when it changes: https://www.embla-carousel.com/api/events/#select
.on("reInit", updateSelectedIndex) // Update index when the carousel re-mounts: https://www.embla-carousel.com/api/events/#reinit
}, [api])
return (
<Carousel
setApi={setApi /* Set the carousel API when it's ready */}
orientation="vertical"
opts = {{
align: "start",
loop: true,
skipSnaps: false,
}}
plugins={[
Autoplay({ delay: 1000 }),
]}
>
<CarouselContent className="-mt-1 h-[570px]">
{Array.from({ length: 5 }).map((_, index) => (
<CarouselItem key={index} className="pt-1 md:w-[90%]">
<div>
<CardContent className={`p-1 flex items-center justify-center p-6${
selectedIndex === index
? "animate__animated animate__bounce animate__faster"
: ""
}`}>
<Image
src="/card-front.webp"
width={500}
height={500}
alt="Picture of the author"
/>
</CardContent>
</div>
</CarouselItem>
))}
</CarouselContent>
</Carousel>
);
} |
Beta Was this translation helpful? Give feedback.
-
yes i mixed between them , igot this error |
Beta Was this translation helpful? Give feedback.
-
yes i did it there was a space typo
should be
code 1 lead to this class p6animate__animated ..etc hence what come after dosen't matter and it not will be working |
Beta Was this translation helpful? Give feedback.
-
full code
|
Beta Was this translation helpful? Give feedback.
-
can we also add new feature (Virtual slides) |
Beta Was this translation helpful? Give feedback.
Hi @Adamkaram,
Thanks for your question. You're mixing the Shadcn carousel logic with the bare Embla library logic. Shadcn is a layer on top of Embla so you should use their API instead.
I would recommend you to access the Embla API as demonstrated here in the Shadcn docs using the
setApi
prop: