nuka-carousel
repository·main·Indexed 25 days ago
https://github.com/formidablelabs/nuka-carouselA small, fast, and accessibility-first React carousel library. It features a highly customizable UI and behavior, supporting features such as autoplay, adaptive height, keyboard navigation, and custom easing functions. The library provides a <Carousel /> component with a comprehensive API for managing navigation arrows, paging dots, wrapping behavior, and slide events, and requires React 18 as a peer dependency.
What's inside nuka-carousel
- Nuka Carousel is a small, fast, and accessibility-first React carousel library. It provides an easily customizable UI and behavior designed to fit various brand requirements and site designs.
Migrate Changed Props in Nuka v8
mainThe following props have changed their names or signatures in v8. Update your implementation to use the new equivalents:
carouselId$\rightarrow$ Useidinstead.slidesToScroll$\rightarrow$ Renamed toscrollDistance.frameAriaLabel$\rightarrow$ Replaced by thetitleprop.afterSlideandbeforeSlide$\rightarrow$ The API signatures have changed. Refer to the callbacks documentation for the new signature.disableAnimation$\rightarrow$ Use theautoPlayproperty (see navigation docs).dragging$\rightarrow$ Now enabled by default. Use theswipingproperty to control this.enableKeyboardControls$\rightarrow$ Now enabled by default. Use thekeyboardproperty to control this.
Use HTML block elements in the Carousel
mainWhen using HTML block elements like
<div>as slides, you must set amin-widthin your CSS or inline styles so that Nuka can correctly measure the slide dimensions./* CSS */ .demo-slide { min-width: 300px; min-height: 100px; } /* JSX */ <Carousel showDots> <div className="demo-slide bg-green-500" /> <div className="demo-slide bg-red-500" /> <div className="demo-slide bg-blue-500" /> </Carousel>Install nuka-carousel via yarn
mainTo add
nuka-carouselto your React project, run the following command in your project folder:$ yarn add nuka-carouselUpgrade from Nuka Carousel v7 to v8
mainNuka v8 has been rewritten for simplicity and native support. This version includes significant API changes where many props have been renamed, replaced, or removed to streamline the interface. When upgrading, you must update your component props to match the new API surface.Use images in the Carousel
mainNuka will automatically calculate the width and height of images after they load. However, to prevent layout shifts and ensure compatibility with frameworks like
Next/image, it is recommended to explicitly set thewidthandheightattributes on your<img>tags.<Carousel showDots> <img src="pexels-01.jpg" width={300} height={100} /> <img src="pexels-02.jpg" width={300} height={100} /> <img src="pexels-03.jpg" width={300} height={100} /> </Carousel>Install nuka-carousel
mainInstall
nuka-carouselusing your preferred package manager. Note that Nuka Carousel has a peer dependency on React 18.npm install nuka-carousel # or yarn add nuka-carousel # or pnpm add nuka-carouselHandle Removed Props in Nuka v8
mainThe following props have been removed in v8. You must adjust your configuration to accommodate these changes:
Automatic Functionality (No prop needed)
adaptiveHeightandadaptiveHeightAnimation: The carousel now adapts to item height automatically.pauseOnHover: Enabled by default. UseautoPlaysettings to manage this.tabbed: Enabled by default.slidesToShow: Now determined by media queries and slide size.speed: Controlled by native browser settings.dragThreshold: Defaults to OS/browser settings.
Replaced by Other Props or Methods
slideIndex: UseinitialPageto set the starting page, or thegoToPagemethod to change indices programmatically.defaultControlsConfigandwithoutControls: Replaced by individual properties likeshowArrows,showDots,arrows, anddots.style,cellAlign, andcellSpacing: These are now controlled via CSS.
Removed (No direct replacement listed)
disableEdgeSwipingeasingedgeEasingkeyCodeConfiglandmarkonDragStart,onDrag, andonDragEndonUserNavigationrender{position}{direction}ControlsscrollMode(defaults toremainder)zoomScalezoomScale
Upgrade from v6 to v7
mainNuka Carousel v7 is a major release focused on aligning with the WCAG Aria Authoring Practices Guide. Upgrading requires awareness of new accessibility-related props and changes to default ARIA roles and behaviors.Get support for nuka-carousel
mainIf you have questions, submit an issue in the repository using the Question template. For inaccuracies or confusing documentation, you can open an issue or submit a pull request.Customize Default Controls
mainUse the
defaultControlsConfigprop to apply custom CSS classes, inline styles, or text to the default navigation buttons and paging dots. You can also attach customonClickhandlers to these elements.interface DefaultControlsConfig { containerClassName?: string nextButtonClassName?: string nextButtonStyle?: CSSProperties nextButtonText?: React.ReactNode pagingDotsClassName?: string pagingDotsContainerClassName?: string pagingDotsStyle?: CSSProperties prevButtonClassName?: string prevButtonStyle?: CSSProperties prevButtonText?: React.ReactNode prevButtonOnClick?(event: React.MouseEvent): void nextButtonOnClick?(event: React.MouseEvent): void pagingDotsOnClick?(event: React.MouseEvent): void }Configure Keyboard Controls and Key Codes
mainTo enable keyboard navigation, set
enableKeyboardControlstotrue. You can further customize which keys trigger specific actions using thekeyCodeConfigprop.interface KeyCodeConfig { firstSlide?: number[]; lastSlide?: number[]; nextSlide?: number[]; pause?: number[]; previousSlide?: number[]; }