If you're wondering how to react to the carousel actions, you're in the right section 🥳
import { useTransitionCarousel } from 'react-spring-carousel'
export function Component() {
const {
carouselFragment,
useListenToCustomEvent
} = useSpringCarousel({
items: mockedItems.map((i) => ({
id: i.id,
renderItem: (
<CarouselItem color={i.color}>
{i.title}
</CarouselItem>
),
})),
});
useListenToCustomEvent((event) => {
// Triggered during drag gestures
if (event.eventName === "onDrag") {
// Do something...
}
// Triggered when the slide is about to slide
else if (event.eventName === "onSlideStartChange") {
// Do something...
}
// Triggered when the slide animation is completed
else if (event.eventName === "onSlideChange") {
// Do something...
}
// Triggered on fullscreen change
else if (event.eventName === "onFullscreenChange") {
}
});
return (
<div>
{carouselFragment}
</div>
);
}
Every event will also came with some extra/useful data that you can eventually use; remember also that depending on the hook you're using, the events will slightly vary:
{
// Indicates where you're sliding to
slideActionType: 'prev' | 'next',
// Props of the next active item
nextItem: {
id: string
index: number
}
}
{
// Indicates where the slide came from
slideActionType: 'prev' | 'next',
// Props of the current active item
currentItem: {
id: string
index: number
}
}
{
isFullscreen: boolean
}
{
// Indicates the direction of the drag action
slideActionType: 'prev' | 'next',
/*
* For all other props you'll get,
* please refer to https://use-gesture.netlify.app/docs/state/
**/
...useGestureProps
}
{
// Indicates where you're sliding to
slideActionType: 'prev' | 'next',
// Props of the next active item
nextItem: {
id: string
index: number
}
}
{
// Indicates where the slide came from
slideActionType: 'prev' | 'next',
// Props of the current active item
currentItem: {
id: string
index: number
}
}
{
isFullscreen: boolean
}