pnpm add @sist/react-carousel
# or
npm install @sist/react-carousel
# or
yarn add @sist/react-carousel
# or other package manager equivalent
React.Context<CarouselContextObject>
Parameters:
-
carouselCount: number
, the number ofCarouselItem
will be used. -
loop?: boolean = true
, move back to the firstCarouselItem
when theCarouselNext
is clicked at the lastCarouselItem
, or move the lastCarouselItem
whenCarouselPrev
is clicked at the firstCarouselItem
.
Returns:
-
sliderItemsRef: React.RefObject<HTMLElement>
, theref
for theCarouselItems
element. -
currentCarousel: number
, the state of theCarouselItem
index shown on the screen. -
setCurrentCarousel: React.Dispatch<React.SetStateAction<number>>
, to set thecurrentCarousel
state. -
scrollToPrevious: () => void
, the function is called when theCarouselPrev
is clicked. -
scrollToNext: () => void
, the function is called when theCarouselNext
is clicked. -
scrollPosition: ScrollPosition
, the state of theCarouselItems
scroll position, it willstart
when the firstCarouselItem
is fully shown,end
when the lastCarouselItem
is fully shown, andmiddle
for the rest. -
handleScroll: () => void
, the function is called when theCarouselItems
is scrolled.
Props:
-
children?: React.ReactNode
, theCarousel
children. -
carouselCount?: number
, see theuseCarousel
first parameter. -
loop?: boolean
, see theuseCarousel
second parameter.
Props:
-
as?: React.ElementType = 'div'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc.
Props:
-
as?: React.ElementType = 'div'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc. -
index: number
, the index order of theCarouselItem
.
data-attribute:
-
data-active
,true
when theindex
is equal to thecurrentCarousel
,false
otherwise.
Props:
-
as?: React.ElementType = 'div'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc. -
index: number
, the index order of theCarouselItemButton
that represents theCarouselItem
.
data-attribute:
-
data-active
,true
when theindex
is equal to thecurrentCarousel
,false
otherwise.
Props:
-
as?: React.ElementType = 'span'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc. -
padStart?: number
, the first parameter ofString.padStart
, used to pad the counter number with zeros.
Props:
-
as?: React.ElementType = 'span'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc. -
padStart?: number
, the first parameter ofString.padStart
, used to pad the counter number with zeros.
It will disabled
when the Carousel
loop
is false
and the first CarouselItem
is shown.
Props:
-
as?: React.ElementType = 'button'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc.
It will disabled
when the Carousel
loop
is false
and the last CarouselItem
is shown.
Props:
-
as?: React.ElementType = 'button'
, the element that is used to render the component, the possible value is an HTML tag, like 'a', 'p', 'div', etc.
"start" | "middle" | "end"
{
carouselCount: number;
currentCarousel: number;
loop: boolean;
setCurrentCarousel: (carousel: number) => void;
handleScroll: () => void;
scrollToNext: () => void;
scrollToPrevious: () => void;
scrollPosition: ScrollPosition;
}
import {
Carousel,
CarouselCount,
CarouselMax,
CarouselItems,
CarouselItem,
CarouselItemButton,
CarouselPrev,
CarouselNext,
} from '@sist/react-carousel';
export function App() {
return (
<Carousel carouselCount={3} loop={true}>
<CarouselItems style={carouselItemsStyle}>
<CarouselItem index={0} style={carouselItemStyle}>
<img
src="https://picsum.photos/id/1/1000/700"
width={1000}
height={700}
/>
</CarouselItem>
<CarouselItem index={1} style={carouselItemStyle}>
<img
src="https://picsum.photos/id/2/1000/700"
width={1000}
height={700}
/>
</CarouselItem>
<CarouselItem index={2} style={carouselItemStyle}>
<img
src="https://picsum.photos/id/3/1000/700"
width={1000}
height={700}
/>
</CarouselItem>
</CarouselItems>
<div>
<CarouselCount /> / <CarouselMax />
<CarouselPrev>Before</CarouselPrev>
<CarouselItemButton index={0}>1</CarouselItemButton>
<CarouselItemButton index={1}>2</CarouselItemButton>
<CarouselItemButton index={2}>3</CarouselItemButton>
<CarouselNext>Next</CarouselNext>
</div>
</Carousel>
);
}
const carouselItemsStyle: React.CSSProperties = {
display: 'flex',
flexWrap: 'nowrap',
overflow: 'scroll',
width: '100%',
scrollSnapType: 'x mandatory',
};
const carouselItemStyle: React.CSSProperties = {
width: '100%',
height: 'auto',
scrollSnapAlign: 'start',
};