@brainhubeu/react-carousel

中文文档
2.0.4发布于 2021/05/26MITCommonJS零依赖近 30 天下载 4.5万

Carousel component for React

原始使用文档
文档目录20 节
  1. Table of Contents
  2. Why?
  3. Installation
  4. Basic
  5. Typescript
  6. CDN
  7. SSR
  8. Usage
  9. Showing dots or thumbnails
  10. Props
  11. Carousel props
  12. Plugins
  13. Dots props
  14. Setting up local development which means running the docs/d…
  15. Tests
  16. Workflow
  17. Labels
  18. Decision log
  19. License
  20. About


react-carousel

A pure extendable React carousel, powered by Brainhub (craftsmen who ❤️ JS)

Live code demo | v1 migration guide | Hire us

CircleCI Last commit license PRs Welcome Renovate enabled

Coveralls github Downloads Activity Minified npm Contributors

Table of Contents

Why?

There are some great carousels (like slick) that do not have real React implementations. This library provides you with carousel that is not merely a wrapper for some jQuery solution, can be used as controlled or uncontrolled element (similar to inputs), and has tons of useful features.

Installation

Basic

npm i @brainhubeu/react-carousel

Typescript

npm i @types/brainhubeu__react-carousel -D

CDN

If you don't use any bundler like Webpack, you can add these scripts to your HTML file, body section:

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin type="text/javascript" src="https://unpkg.com/@brainhubeu/[email protected]/lib/react-carousel.js"></script>

Make sure to use a version ending with -cdn.

Then, you can use the following global variables:

  • BrainhubeuReactCarousel
  • BrainhubeuReactCarouselDots
  • BrainhubeuReactCarouselItem
  • BrainhubeuReactCarouselWrapper

SSR

When using @brainhubeu/react-carousel with SSR (Server-side Rendering), we recommend Next.js as @brainhubeu/react-carousel currently doesn't work on the server side so it must be rendered on the client side (maybe we'll provide server-side working in the future).

import dynamic from 'next/dynamic';

const { default: Carousel, Dots } = dynamic(
 () => require('@brainhubeu/react-carousel'),
 { ssr: false },
);

Usage

By default, the component does not need anything except children to render a simple carousel. Remember that styles do not have to be imported every time you use carousel, you can do it once in an entry point of your bundle.

import React from 'react';
import Carousel from '@brainhubeu/react-carousel';
import '@brainhubeu/react-carousel/lib/style.css';

const MyCarousel = () => (
  <Carousel plugins={['arrows']}>
    <img src={imageOne} />
    <img src={imageTwo} />
    <img src={imageThree} />
  </Carousel>
);

export default MyCarousel;

gif

Showing dots or thumbnails

There is a separate Dots component that can be used to fully control navigation dots or add thumbnails.

import Carousel, { Dots } from '@brainhubeu/react-carousel';
import '@brainhubeu/react-carousel/lib/style.css'; import { useState } from 'react';

const MyCarouselWithDots = () => {
  const [value, setValue] = useState(0);

  const onChange = value => {
  setValue(value);
  }

  return (
    <div>
      <Carousel
        value={value}
        onChange={onChange}
      >
        <img className="img-example" src={someImage} />
        ...
        <img className="img-example" src={anotherImage} />
      </Carousel>
      <Dots
        value={this.state.value}
        onChange={this.onChange}
        thumbnails={[
          (<img key={1} className="img-example-small" src={abstractImage} />),
          ...
          (<img key={12} className="img-example-small" src={transportImage} />),
        ]}
      />
    </div>
  );
};

export default MyCarouselWithDots;

gif

Props

You can access a clickable demo with many examples and a live code editor by clicking on a Prop name.

PropTypeDefaultDescription
valueNumberundefinedCurrent slide's index (zero based, depends on the elements order)
onChangeFunctionundefinedHandler triggered when current slide is about to change (e.g. on arrow click or on swipe)
slidesArrayundefinedAlternative way to pass slides. This prop expects an array of JSX elements
itemWidthNumberundefinedDetermines custom width for every slide in the carousel
offsetNumber0Padding between items
animationSpeedNumber500Determines transition duration in milliseconds
draggableBooleantrueMakes it possible to drag to the next slide with mouse cursor
breakpointsObjectundefinedAll props can be set to different values on different screen resolutions

Plugins

You can extend react-carousel default behavior by applying plugins shipped within carousel or by creating and applying your own ones

Plugins documentation

Dots props

PropTypeDefaultDescription
valueNumberslide position in the slides ArrayCurrent Carousel value
onChangeFunctionundefinedonChange callback (works the same way as onChange in Carousel component)
numberNumberAmount of slidesNumber of slides in the carousel you want to control
thumbnailsArray of ReactElementsundefinedArray of thumbnails to show. If not provided, default dots will be shown
rtlBooleanfalseIndicating if the dots should have direction from Right to Left

Setting up local development which means running the docs/demo locally:

  • git clone https://github.com/brainhubeu/react-carousel
  • cd react-carousel
  • yarn
  • yarn start-demo
  • open http://localhost:8000/

Tests

Each test command should be run from the root directory.

Unit tests

yarn test:unit:coverage

E2E tests

yarn test:e2e

Workflow

See the Workflow subsection in our docs

Labels

See the Labels subsection in our docs

Decision log

See the Decision log subsection in our docs

License

react-carousel is copyright © 2018-2020 Brainhub. It is free software and may be redistributed under the terms specified in the license.

About

react-carousel is maintained by the Brainhub development team. It is funded by Brainhub and the names and logos for Brainhub are trademarks of Brainhub Sp. z o.o.. You can check other open-source projects supported/developed by our teammates here.

Brainhub

We love open-source JavaScript software! See our other projects or hire us to build your next web, desktop and mobile application with JavaScript.

版本与发布记录

@brainhubeu/react-carousel 共发布 390 个版本,首次发布于 2018/02/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2021/05/265 年前
2.0.32021/04/135 年前
2.0.2master2020/11/205 年前
1.19.262020/07/316 年前
1.19.26-cdn2020/07/316 年前
2.0.12020/07/296 年前

其他发布通道:v2-beta → 2.0.0-v2-beta.11,rc → 2.0.0-rc.3,master → 2.0.2。

关于 @brainhubeu/react-carousel 的常见问题

@brainhubeu/react-carousel 是什么?

Carousel component for React

如何安装 @brainhubeu/react-carousel?

在项目目录运行 npm install @brainhubeu/react-carousel,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0)、react-dom(^16.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@brainhubeu/react-carousel 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/brainhubeu__react-carousel 是否存在。

@brainhubeu/react-carousel 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@brainhubeu/react-carousel 可以商用吗?

@brainhubeu/react-carousel 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brainhubeu/react-carousel 的下载量有多少?

根据 npm 官方下载统计,@brainhubeu/react-carousel 在 2026/10/09 – 2026/11/07 这 30 天内被下载 44,644 次,日均约 1,488 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brainhubeu/react-carousel 还在维护吗?

最新版本 2.0.4 发布于 2021/05/26(5 年前);至今共发布 390 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/02/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。