Auto Height in React #287
-
hello, Can anyone help me to get auto height in my carrousel with next JS please ? here is my code :
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hi @Tambouil, Thanks for your question. It seems like you've misunderstood the documentation. Here's what the documentation say:
Here are some problems I found with your setup:
Try this: import React, { useState, useEffect, useCallback, useRef } from "react";
import { PrevButton, NextButton } from "./CarouselButtons";
import useEmblaCarousel from "embla-carousel-react";
import Autoplay from "embla-carousel-autoplay";
import AutoHeight from "embla-carousel-auto-height";
const CarouselDetails = ({
carouselOptions = { loop: true },
}) => {
const autoheight = useRef(AutoHeight());
const autoplay = useRef(
Autoplay(
{ delay: 5000, stopOnInteraction: false },
(emblaRoot) => emblaRoot.parentElement
)
);
const [emblaRef, emblaApi] = useEmblaCarousel(
carouselOptions,
[autoheight.current, autoplay.current],
);
const [prevBtnEnabled, setPrevBtnEnabled] = useState(false);
const [nextBtnEnabled, setNextBtnEnabled] = useState(false);
const scrollNext = useCallback(() => {
if (!emblaApi) return;
emblaApi.scrollNext();
autoplay.current.reset();
}, [emblaApi]);
const scrollPrev = useCallback(() => {
if (!emblaApi) return;
emblaApi.scrollPrev();
autoplay.current.reset();
}, [emblaApi]);
const onSelect = useCallback(() => {
if (!emblaApi) return;
setPrevBtnEnabled(emblaApi.canScrollPrev());
setNextBtnEnabled(emblaApi.canScrollNext());
}, [emblaApi]);
useEffect(() => {
if (!emblaApi) return;
onSelect();
emblaApi.on("select", onSelect);
}, [emblaApi, onSelect]); Now this might work, but you haven't provided a CodeSandbox with your full setup. So there might be other problems with your JS or CSS setup that I can't help you with. Kindly, |
Beta Was this translation helpful? Give feedback.
-
thanks a ton david |
Beta Was this translation helpful? Give feedback.
Hi @Tambouil,
Thanks for your question. It seems like you've misunderstood the documentation. Here's what the documentation say:
Here are some problems I found with your setup:
autoheight.current
when passing theAutoHeight
plugin to Embla.auto
so no need to pass any options to it.Try this: