flowbite-react icon indicating copy to clipboard operation
flowbite-react copied to clipboard

Setting Active Image In the Slider

Open technotip opened this issue 2 years ago • 11 comments

We can programatically set the active image: https://flowbite.com/docs/components/carousel/#default-slider

But the same is not available in flowbite react: https://www.flowbite-react.com/docs/components/carousel

Can we please get this in React too?

Expected Behaviour: When we set data-carousel-item="active" or something similar on any image, that image should be shown in the carousel and not from index 0.

I am using: import { Carousel } from 'flowbite-react';

technotip avatar Oct 30 '23 21:10 technotip

I see that setting data-carousel-item="active" does work partially. i.e., the indicator points to the correct active image. But the image itself still shows the 0th index image on the carousel instead of the image which is set as "active".

technotip avatar Oct 30 '23 21:10 technotip

Expected Changes:

The active={true | false} should be present on all the img tags, which are inside <Carousel></Carousel>. That way the library can pickup the active image to show on the canvas/Carousel and then change the indicators accordingly.

technotip avatar Nov 03 '23 08:11 technotip

Not sure if adding active={boolean} to every single img or div or whatever content is desirable, instead, the best approach, IMHO, would be to add the activeSlide={number} to the Carousel component.

rluders avatar Nov 03 '23 08:11 rluders

Just as a bump, i too was about to use the react comp and ended up making my own from the JS version. Underneath the carousel I wanted to show thumbnails and set state to jump to the active image.

activeSlide={number} would be the best approach here.

kurtisdunn avatar Dec 31 '23 10:12 kurtisdunn

what about a ref that we can use with ref.current.slideTo(index) ?

<Carousel carouselRef={ref} />

xegulon avatar Feb 11 '24 14:02 xegulon

@tulup-conner @rluders hey check this out #1214

cristiantiradob avatar Mar 23 '24 04:03 cristiantiradob

Any updates? Do you guys plan to review/approve #1214 ?

ArthurJahn avatar Apr 30 '24 17:04 ArthurJahn

Any updates guys?

enzoimola avatar Jun 20 '24 19:06 enzoimola

Any news?

wellitongervickas avatar Aug 13 '24 19:08 wellitongervickas

Guys, to fix that, I wrote the "REF" in the parent component, and scroll to left using the "item" size. Eg:

  useEffect(() => {
    const slider = carouselParentRef?.current?.children?.[0]?.children?.[0]
    if (slider) {
      slider.scrollLeft = activeSliderIndex * colsSize.width
    }
  }, [activeSliderIndex, colsSize.width])

wellitongervickas avatar Aug 13 '24 20:08 wellitongervickas

Please add slideTo as it's available in the original flowbite https://flowbite.com/docs/components/carousel/#methods

As a workaround I ended up with this...

...
const carouselRef = useRef(null);
const slideTo = (i: any) => {
    if(!carouselRef.current)
        return;
    const indicators = (carouselRef.current as any).querySelectorAll('[data-testid="carousel-indicator"]');
    if (indicators && indicators[i]) {
        indicators[i].click();
    }
}
return (
    <>
    <div className={"h-56 sm:h-64 xl:h-80 2xl:h-96"}>
        <Carousel
            ref={carouselRef}
            ...

dc-p8 avatar Feb 26 '25 16:02 dc-p8