@kryezy/headless-react-carousel
v0.0.3
Published
Headless React component for carousel with scrolling support
Readme
Headless React Carousel
Install
pnpm add @kryezy/headless-react-carousel
# or
npm install @kryezy/headless-react-carousel
# or
yarn add @kryezy/headless-react-carousel
# or other package manager equivalentAPI
Context
CarouselContext
React.Context<CarouselContextObject>
Hooks
useCarousel
Parameters:
carouselCount: number, the number ofCarouselItemwill be used.loop?: boolean = true, move back to the firstCarouselItemwhen theCarouselNextis clicked at the lastCarouselItem, or move the lastCarouselItemwhenCarouselPrevis clicked at the firstCarouselItem.
Returns:
sliderItemsRef: React.RefObject<HTMLElement>, thereffor theCarouselItemselement.currentCarousel: number, the state of theCarouselItemindex shown on the screen.setCurrentCarousel: React.Dispatch<React.SetStateAction<number>>, to set thecurrentCarouselstate.scrollToPrevious: () => void, the function is called when theCarouselPrevis clicked.scrollToNext: () => void, the function is called when theCarouselNextis clicked.scrollPosition: ScrollPosition, the state of theCarouselItemsscroll position, it willstartwhen the firstCarouselItemis fully shown,endwhen the lastCarouselItemis fully shown, andmiddlefor the rest.handleScroll: () => void, the function is called when theCarouselItemsis scrolled.
Components
Carousel
Props:
children?: React.ReactNode, theCarouselchildren.carouselCount?: number, see theuseCarouselfirst parameter.mode: CarouselMode:mode="loop", see theuseCarouselsecond parameter.auto?: boolean, make the carousel auto scroll.interval?: number = 2000, the interval for the carousel scroll automatically.
mode="stop", see theCarouselPrevor theCarouselNextdescription.
direction: CarouselDirection:direction: "vertical", the items will move verticallydirection: "horizontal"the items will move horizontally
CarouselItems
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.
CarouselItem
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,truewhen theindexis equal to thecurrentCarousel,falseotherwise.
CarouselItemButton
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 theCarouselItemButtonthat represents theCarouselItem.
data-attribute:
data-active,truewhen theindexis equal to thecurrentCarousel,falseotherwise.
CarouselCount
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.
CarouselMax
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.
CarouselPrev
It will disabled when the Carousel mode is "stop" 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.
CarouselNext
It will disabled when the Carousel mode is "stop" 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.
Types
ScrollPosition
"start" | "middle" | "end"
CarouselMode
"stop" | "loop"
CarouselContextObject
{
carouselCount: number;
currentCarousel: number;
mode: CarouselMode;
setCurrentCarousel: (carousel: number) => void;
handleScroll: () => void;
scrollToNext: () => void;
scrollToPrevious: () => void;
scrollPosition: ScrollPosition;
}Example
import {
Carousel,
CarouselCount,
CarouselMax,
CarouselItems,
CarouselItem,
CarouselItemButton,
CarouselPrev,
CarouselNext,
} from "@kryezy/headless-react-carousel";
export function App() {
return (
<Carousel carouselCount={3} mode="loop">
<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",
};