[Help]: Is their a way to run a function after the animation is finished #814
-
SummarySo I have a carousel that I control with the arrow keys and updates the query params in the URL on slide change using the nextJS replace function. Issue is that changing the query params causes a bit of stutter at the end of the animation. I fixed this when I was using nuka-carousel by using the Snippet of how I am doing it currently: const { replace, asPath } = useRouter();
const pathWithoutQuery = asPath.split("?")[0];
const debouncedReplace = useRef(
debounce((debouncedItems, endSlideIndex) => {
replace(
`${pathWithoutQuery}?item=` + debouncedItems[endSlideIndex]!.id,
undefined,
{ shallow: true, scroll: false },
);
}, 500),
);
useEffect(() => {
return () => {
debouncedReplace.current.cancel();
};
}, []);
const [emblaRef, emblaApi] = useEmblaCarousel({
startIndex: initialIndex,
containScroll: "keepSnaps",
watchDrag: false,
duration: fullscreen ? 0 : 25,
});
const slideChange = () => {
const newIndex = emblaApi?.selectedScrollSnap();
if (newIndex) {
if (newIndex == newBufferedItems.length - 1) {
fetchNextPage();
}
if (newBufferedItems[newIndex] != undefined) {
markRead(newBufferedItems[newIndex]);
}
debouncedReplace.current(newBufferedItems, newIndex);
}
};
useHotkeys("LeftArrow", () => {
if (emblaApi) {
emblaApi.scrollPrev();
slideChange();
}
});
useHotkeys("RightArrow", () => {
if (emblaApi) {
emblaApi?.scrollNext();
slideChange();
}
}); output.webmIf applicable, which variants of Embla Carousel are relevant to this question?
Additional informationNo response CodeSandbox exampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@michaelkremenetsky thanks for your question. Do you want your function to trigger after the animation ends or as soon as a new active slide has been selected? |
Beta Was this translation helpful? Give feedback.
@michaelkremenetsky I see. I think you're looking for these events:
select
settle
And here is an example of how to make use of these events in the docs. Remember to switch to the
React
tab for a relevant code example.