Skip to content

Commit

Permalink
[@mantine/carousel] Fix onSlideChange using stale props values (#6393)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Jun 28, 2024
1 parent 0c5e3bb commit 831041b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/@mantine/carousel/src/Carousel.story.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRef, useState } from 'react';
/* eslint-disable no-console */
import { useCallback, useRef, useState } from 'react';
import Autoplay from 'embla-carousel-autoplay';
import type { EmblaCarouselType } from 'embla-carousel-react';
import { Button, Modal } from '@mantine/core';
Expand All @@ -17,9 +18,14 @@ const slides = (
);

export function Usage() {
const [value, setValue] = useState(0);
const log = useCallback((index: any) => console.log({ value, index }), [value]);
return (
<div style={{ padding: 40, maxWidth: 500 }}>
<Carousel slideSize={70} slideGap="md" height={200} withIndicators>
<button type="button" onClick={() => setValue(Math.random())}>
random
</button>
<Carousel slideGap="md" height={200} withIndicators onSlideChange={log}>
{slides}
</Carousel>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/@mantine/carousel/src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ export const Carousel = factory<CarouselFactory>((_props, ref) => {
if (!embla) return;
const slide = embla.selectedScrollSnap();
setSelected(slide);
onSlideChange?.(slide);
}, [embla, setSelected]);
slide !== selected && onSlideChange?.(slide);
}, [embla, setSelected, onSlideChange, selected]);

const handlePrevious = useCallback(() => {
embla?.scrollPrev();
Expand Down Expand Up @@ -306,7 +306,7 @@ export const Carousel = factory<CarouselFactory>((_props, ref) => {
}

return undefined;
}, [embla, slidesToScroll]);
}, [embla, slidesToScroll, handleSelect]);

useEffect(() => {
if (embla) {
Expand Down

0 comments on commit 831041b

Please sign in to comment.