nuka-carousel

A React carousel component.

NcNUKA CAROUSEL

Maintenance Status

nuka-carousel

A Pure ReactJS Carousel Component

Maintained by @sarmeyer

Nuka Carousel Animated Example

Install

To add nuka-carousel to your project run the following command in your project folder.

$ yarn add nuka-carousel

Example

import React from 'react';
import Carousel from 'nuka-carousel';

export default class extends React.Component {
  render() {
    return (
      <Carousel>
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide1" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide2" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide3" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide4" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide5" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide6" />
      </Carousel>
    );
  }
}

Running demo locally

The demo can be launched on your local machine via webpack-dev-server. Make sure you are running node version 9.11 or earlier. Once you have cloned this repo locally, run the following:

yarn
yarn build
yarn start

You can access the application on your localhost at the following url: Local Demo

Or on CodeSandBox: Edit currying-smoke-c8v2n

Keyboard Controls

Key CombinationFunction
Right/Up Arrow or D/W keyNext slide
Left/Down Arrow or A/S keyPrevious slide
Q keyFirst slide
E keyLast slide
SpaceBarWhen autoplay={true} pauses and unpauses carousel
  • Keyboard shortcuts are disabled as a default. To enable them set enableKeyboardControls prop to true.
  • keyCodeConfig prop can be used to configure the default keyCodes

You can control the state of the carousel from your parent component as shown below:

import React from 'react';
import Carousel from 'nuka-carousel';

export default class extends React.Component {
  state = {
    slideIndex: 0
  };

  render() {
    return (
      <Carousel
        slideIndex={this.state.slideIndex}
        afterSlide={slideIndex => this.setState({ slideIndex })}
      >
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide1" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide2" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide3" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide4" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide5" />
        <img src="https://via.placeholder.com/400/ffffff/c0392b/&text=slide6" />
      </Carousel>
    );
  }
}

TypeScript

TypeScript type definitions are now shipped with nuka-carousel. You can use them directly from the library.

Resizing Height

How resizing works

In componentDidMount, the initial dimensions are assigned to each slide:

  • Width: initialSlidewidth || slideWidth || (slidesToShow / width of container)
  • Height: initialSlideHeight

After the component completes mounting with the accurate width, it tries to calculate the desired height of the content (current, first, max). If that calculation fails (perhaps because slide images are still loading), it'll wait a bit and try again. Once successful, that measurement then replaces initialSlideHeight with the measured height in pixels.

Contributing

See the Contribution Docs.

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

Work With Us

We partner with our clients to build all manner of digital products. Wherever you are in your product lifecycle, from concept to launch, our team of design and engineering experts will ensure you meet your web or mobile app's goals.