MCPcopy Index your code
hub / github.com/YIZHUANG/react-multi-carousel

github.com/YIZHUANG/react-multi-carousel @2.5.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release 2.5.4 ↗ · + Follow
179 symbols 440 edges 75 files 0 documented · 0% 4 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-multi-carousel 👋

All Contributors

Production-ready, lightweight fully customizable React carousel component that rocks supports multiple items and SSR(Server-side rendering).

Package Quality npm version download per month Build Status Documentation Maintenance License: MIT FOSSA Status David Dependancy Status Known Vulnerabilities

demo

demo

Features.

  • Server-side rendering
  • Infinite mode
  • Dot mode
  • Custom animation
  • AutoPlay mode
  • Auto play interval
  • Supports images, videos, everything.
  • Responsive
  • Swipe to slide
  • Mouse drag to slide
  • Keyboard control to slide
  • Multiple items
  • Show / hide arrows
  • Custom arrows / control buttons
  • Custom dots
  • Custom styling
  • Accessibility support
  • Center mode.
  • Show next/previous set of items partially

Shoutouts 🙏

BrowserStack Logo

Big thanks to BrowserStack for letting the maintainers use their service to debug browser issues.

Documentation

Other important links.

Bundle size

Bundle-size. 2.5kB

Demo.

Documentation is here.

Demo for the SSR https://react-multi-carousel.now.sh/

Try to disable JavaScript to test if it renders on the server-side.

Codes for SSR at github.

Codes for the documentation at github.

Install

$ npm install react-multi-carousel --save

import Carousel from 'react-multi-carousel';
import 'react-multi-carousel/lib/styles.css';

How the SSR mode works?

Codes for SSR at github.

  • Demo for the SSR are at here
  • Try to disable JavaScript to test if it renders on the server-side.

Here is a lighter version of the library for detecting the user's device type alternative

You can choose to only bundle it on the server-side.

Minimum working set up.

import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
const responsive = {
  superLargeDesktop: {
    // the naming can be any, depends on you.
    breakpoint: { max: 4000, min: 3000 },
    items: 5,
  },
  desktop: {
    breakpoint: { max: 3000, min: 1024 },
    items: 3,
  },
  tablet: {
    breakpoint: { max: 1024, min: 464 },
    items: 2,
  },
  mobile: {
    breakpoint: { max: 464, min: 0 },
    items: 1,
  },
};
<Carousel responsive={responsive}>


Item 1




Item 2




Item 3




Item 4


</Carousel>;

Common Usage

import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";

const responsive = {
  desktop: {
    breakpoint: { max: 3000, min: 1024 },
    items: 3,
    slidesToSlide: 3, // optional, default to 1.
  },
  tablet: {
    breakpoint: { max: 1024, min: 464 },
    items: 2,
    slidesToSlide: 2, // optional, default to 1.
  },
  mobile: {
    breakpoint: { max: 464, min: 0 },
    items: 1,
    slidesToSlide: 1, // optional, default to 1.
  },
};
<Carousel
  swipeable={false}
  draggable={false}
  showDots={true}
  responsive={responsive}
  ssr={true} // means to render carousel on server-side.
  infinite={true}
  autoPlay={this.props.deviceType !== "mobile" ? true : false}
  autoPlaySpeed={1000}
  keyBoardControl={true}
  customTransition="all .5"
  transitionDuration={500}
  containerClass="carousel-container"
  removeArrowOnDeviceType={["tablet", "mobile"]}
  deviceType={this.props.deviceType}
  dotListClass="custom-dot-list-style"
  itemClass="carousel-item-padding-40-px"
>


Item 1




Item 2




Item 3




Item 4


</Carousel>;

Custom Arrows.

You can pass your own custom arrows to make it the way you want, the same for the position. For example, add media query for the arrows to go under when on smaller screens.

You custom arrows will receive a list of props/state that's passed back by the carousel such as the currentSide, is dragging or swiping in progress.

Code

const CustomRightArrow = ({ onClick, ...rest }) => {
  const {
    onMove,
    carouselState: { currentSlide, deviceType },
  } = rest;
  // onMove means if dragging or swiping in progress.
  return <button onClick={() => onClick()} />;
};
<Carousel customRightArrow={<CustomRightArrow />} />;

Custom button group.

This is very useful if you don't want the dots, or arrows and you want to fully customize the control functionality and styling yourself.

Code

const ButtonGroup = ({ next, previous, goToSlide ...rest }) => {
  const { carouselState: { currentSlide } } = rest;
  return (


 // remember to give it position:absolute
      <ButtonOne className={currentSlide === 0 : 'disable' : ''} onClick={() => previous()} />
      <ButtonTwo onClick={() => next()} />
      <ButtonThree onClick={() => goToSlide(currentSlide + 1)}> Go to any slide </ButtonThree>



  );
};
<Carousel arrows={false} customButtonGroup={<ButtonGroup />}>
  <ItemOne>
  <ItemTwo>
</Carousel>

renderButtonGroupOutside

Passing this props would render the button group outside of the Carousel container. This is done using React.fragment




  <Carousel arrows={false} renderButtonGroupOutside={true} customButtonGroup={<ButtonGroup />}>
    <ItemOne>
    <ItemTwo>
  </Carousel>



Custom dots.

You can pass your own custom dots to replace the default one.

Custom dots can also be a copy or an image of your carousel item. See example in this one

The codes for this example

You custom dots will receive a list of props/state that's passed back by the carousel such as the currentSide, is dragging or swiping in progress.

Code

const CustomDot = ({ onClick, ...rest }) => {
  const { onMove, index, active, carouselState: { currentSlide, deviceType }  } = rest;
  const carouselItems = [CarouselItem1, CaourselItem2, CarouselItem3];
  // onMove means if dragging or swiping in progress.
  // active is provided by this lib for checking if the item is active or not.
  return (
    <button className={active ? 'active' : 'inactive'} onClick={() => onClick()}>
      {React.Children.toArray(carouselItems)[index]}
    </button>
    )
}
<Carousel showDots customDot={<CustomDot />}>
{carouselItems}
</Carousel>

renderDotsOutside

Passing this props would render the dots outside of the Carousel container. This is done using React.fragment




  <Carousel arrows={false} showDots={true} renderDotsOutside={renderButtonGroupOutside}>
    <ItemOne>
    <ItemTwo>
  </Carousel>



partialVisible props.

Shows the next items partially, this is very useful if you want to indicate to the users that this carousel component is swipable, has more items behind it.

This is different from the "centerMode" prop, as it only shows the next items. For the centerMode, it shows both.

const responsive = {
  desktop: {
    breakpoint: { max: 3000, min: 1024 },
    items: 3,
    partialVisibilityGutter: 40 // this is needed to tell the amount of px that should be visible.
  },
  tablet: {
    breakpoint: { max: 1024, min: 464 },
    items: 2,
    partialVisibilityGutter: 30 // this is needed to tell the amount of px that should be visible.
  },
  mobile: {
    breakpoint: { max: 464, min: 0 },
    items: 1,
    partialVisibilityGutter: 30 // this is needed to tell the amount of px that should be visible.
  }
}
<Carousel partialVisible={true} responsive={responsive}>
  <ItemOne />
  <ItemTwo />
</Carousel>

centerMode

Shows the next items and previous items partially.

<Carousel centerMode={true} />

afterChange callback.

This is a callback function that is invoked each time when there has been a sliding.

<Carousel
  afterChange={(previousSlide, { currentSlide, onMove }) => {
    doSpeicalThing();
  }}
/>

beforeChange call back

This is a callback function that is invoked each time before a sliding.

<Carousel
  beforeChange={(nextSlide, { currentSlide, onMove }) => {
    doSpeicalThing();
  }}
/>

Combine beforeChange and nextChange, real usage.

They are very useful in the following cases:

  • The carousel item is clickable, but you don't want it to be clickable while the user is dragging it or swiping it.
<Carousel
  beforeChange={() => this.setState({ isMoving: true })}
  afterChange={() => this.setState({ isMoving: false })}
>
  <a
    onClick={e => {
      if (this.state.isMoving) {
        e.preventDefault();
      }
    }}
    href="https://w3js.com"
  >
    Click me
  </a>
</Carousel>
  • Preparing for the next slide.
<Carousel beforeChange={nextSlide => this.setState({ nextSlide: nextSlide })}>


Initial slide




 {
      if (this.state.nextSlide === 1) {
        doVerySpecialThing();
      }
    }}
  >
    Second slide



</Carousel>

Skipping callbacks

When calling the goToSlide function on a Carousel the callbacks will be run by default. You can skip all or individul callbacks by passing a second parameter to goToSlide.

this.Carousel.goToSlide(1, true); // Skips both beforeChange and afterChange
this.Carousel.goToSlide(1, { skipBeforeChange: true }); // Skips only beforeChange
this.Carousel.goToSlide(1, { skipAfterChange: true }); // Skips only afterChange

focusOnSelect

Go to slide on click and make the slide a current slide.

<Carousel focusOnSelect={true} />

Using ref.

<Carousel ref={(el) => (this.Carousel = el)} arrows={false} responsive={responsive}>
  <ItemOne />
  <ItemTwo />
</Carousel>
<button onClick={() => {
    const nextSlide = this.Carousel.state.currentSlide + 1;
     // this.Carousel.next()
     // this.Carousel.goToSlide(nextSlide)
  }}>Click me</button>

additionalTransfrom Props.

This is very useful when you are fully customizing the control functionality by yourself like this one

Code

For example if you give to your carousel item padding left and padding right 20px. And you have 5 items in total, you might want to do the following:

<Carousel ref={el => (this.Carousel = el)} additionalTransfrom={-20 * 5} /> // it needs to be a negative number

Specific Props

| Name | Type | Default | Description

Extension points exported contracts — how you extend this code

CarouselItemsProps (Interface)
(no doc)
src/CarouselItems.tsx
LeftArrowProps (Interface)
(no doc)
src/Arrows.tsx
DotsTypes (Interface)
(no doc)
src/Dots.tsx
ResponsiveType (Interface)
(no doc)
src/types.ts
NextSlidesTable (Interface)
(no doc)
src/utils/dots.ts
RightArrowProps (Interface)
(no doc)
src/Arrows.tsx
CarouselProps (Interface)
(no doc)
src/types.ts
Table (Interface)
(no doc)
src/utils/clones.ts

Core symbols most depended-on inside this repo

c
called by 15
coverage/lcov-report/prettify.js
i
called by 11
coverage/lcov-report/prettify.js
getState
called by 10
src/Carousel.tsx
notEnoughChildren
called by 9
src/utils/common.ts
isMouseMoveEvent
called by 7
src/types.ts
goToSlide
called by 5
src/Carousel.tsx
isInRightEnd
called by 5
src/utils/common.ts
getClones
called by 5
src/utils/clones.ts

Shape

Function 96
Method 47
Class 24
Interface 12

Languages

TypeScript100%

Modules by API surface

src/Carousel.tsx32 symbols
coverage/lcov-report/prettify.js23 symbols
coverage/lcov-report/sorter.js13 symbols
src/utils/common.ts9 symbols
src/types.ts9 symbols
stories/CopyOfChildAsDots.js6 symbols
src/utils/clones.ts6 symbols
examples/ssr/components/carousel-with-custom-dots.js6 symbols
examples/ssr/pages/_app.js5 symbols
dev/pages/_app.tsx5 symbols
dev/pages/HomePage/HomePage.tsx5 symbols
src/Arrows.tsx4 symbols

For agents

$ claude mcp add react-multi-carousel \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page