Socket
Socket
Sign inDemoInstall

@farfetch/react-carousel

Package Overview
Dependencies
16
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @farfetch/react-carousel

A carousel component, with support for Swipe, Scroll and Snap Points


Version published
Weekly downloads
11
increased by266.67%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

A carousel component, with support for Swipe, Scroll and Snap Points.

Installing

$ npm install --save @farfetch/react-carousel

...and include it in your project

import { Carousel } from '@farfetch/react-carousel';

We also include css styles out-of-the-box, to use them just import the css by doing the following:

import '@farfetch/react-carousel/styles.css'

If you want to style the carousel by yourself, ignore this step.

Screenshots

Example

Infinite scroll demo

Infinite Scroll

Concepts

The Carousel is fully operated by Context, this gives the flexibility to extend it without having to change the core, as all the state of the Carousel is already exposed in the context.

To create a new carousel, you basically need two things: The wrapper (Carousel), a movement engine (one of the sliders) and optional navigation components.

The base structure would look like this:

import { Carousel, SwipeSlider, Bullets } from '@farfetch/react-carousel';

/* The mandatory wrapper of the Carousel */
<Carousel>
    /* One of the available movement engines */
    <SwipeSlider>
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
        <div>Item 4</div>
        <div>Item 5</div>
    </SwipeSlider>
    
    /* Optional navigation component */
    <Bullets />
</Carousel>

You have full control of what you can put inside the Carousel wrapper, including divs and others components.

The wrapper is nothing more than a <div> and the instance of the CarouselProvider, so all children already have access to the Context by default.

Architecture overview

react-carousel Architecture

Usage examples

In the examples below we demonstrate the flexibility of the components, passing all the available props, just for demonstration purposes.

Except for the movement engines, you can mix all the examples to have the more adequate Carousel for your needs.

SwipeSlider

import { Carousel, SwipeSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <SwipeSlider className={ Style.slider } disableSwipe hasKeysNavigation>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </SwipeSlider>
    </Carousel>
);

export default MyComponent;

ScrollSlider

import { Carousel, ScrollSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <ScrollSlider className={ Style.slider } limitScroll>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </ScrollSlider>
    </Carousel>
);

export default MyComponent;

SnapSlider

import { Carousel, SnapSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <SnapSlider className={ Style.slider }>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </SnapSlider>
    </Carousel>
);

export default MyComponent;

With Bullets

import { Carousel, Bullets, SwipeSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <SwipeSlider className={ Style.slider } disableSwipe hasKeysNavigation>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </SwipeSlider>
        
        <Bullets theme={ BulletsTheme } />
    </Carousel>
);

export default MyComponent;

With Arrows

import { Carousel, Bullets, SwipeSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const handleArrowClick = () => {
    console.log('Clicked on the arrow');
}

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <SwipeSlider className={ Style.slider } disableSwipe hasKeysNavigation>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </SwipeSlider>
        
        <Arrow flow={ "prev" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Previous</button> }
        </Arrow>
        
        <Arrow flow={ "next" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Next</button> }
        </Arrow>
    </Carousel>
);

export default MyComponent;
import { ResponsiveConsumer } from '@farfetch/react-context-responsive';
import { Carousel, Bullets, SwipeSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const handleArrowClick = () => {
    console.log('Clicked on the arrow');
}
const ResponsiveCarousel = (props) => (
    const { children } = props;
    
    <Carousel className={ Style.container } itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <ResponsiveConsumer>
            { ({ lessThan }) => {
                lessThan.md ? (
                    <ScrollSlider className={ Style.slider }>
                        { children }
                    </SwipeSlider>
                ) : 
                (
                    <SwipeSlider className={ Style.slider } disableSwipe hasKeysNavigation>
                        { children }
                    </SwipeSlider>
                )
            } }
        </ResponsiveConsumer>
        
        <Bullets theme={ BulletsTheme } />
        
        <Arrow flow={ "prev" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Previous</button> }
        </Arrow>
        
        <Arrow flow={ "next" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Next</button> }
        </Arrow>
    </Carousel>
);

export default ResponsiveCarousel;

With custom component

import { Carousel, CarouselContext, Bullets, SwipeSlider } from '@farfetch/react-carousel';

const handleAfterChange = (e) => {
    console.log(e.index);
    console.log(e.dir);
};

const handleArrowClick = () => {
    console.log('Clicked on the arrow');
}

const Pagination = (props) => {
    <CarouselContext.Consumer>
        { ({ activeItem, itemsLength }) => <div>{ activeItem } of { itemsLength } items</div> }
    </CarouselContext.Consumer>
}

const MyComponent = (props) => (
    <Carousel className={ Style.container } isInfinite isRTL itemsToShow={ 2 } onAfterChange={ handleAfterChange }>
        <SwipeSlider className={ Style.slider } disableSwipe hasKeysNavigation>
            <div className={ Style.customItem }>Item 1</div>
            <div className={ Style.moreCustomItem }>Item 2</div>
            <div>Item 3</div>
            <div>Item 4</div>
            <div>Item 5</div>
        </SwipeSlider>
        
        <Pagination />
        
        <Arrow flow={ "prev" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Previous</button> }
        </Arrow>
        
        <Arrow flow={ "next" } onClick={ handleArrowClick }>
            { ({ onClick }) => <button onClick={ onClick }>Next</button> }
        </Arrow>
    </Carousel>
);

export default MyComponent;

Components

The available components for the Carousel are:

The wrapper for the entire Carousel, it creates a wrapping <div> and an implementation of the CarouselProvider.

Properties
NameTypeDefaultDescription
classNameStringDesired theme's class names
childrenNodeThe components to construct the carousel
isInfiniteBoolfalseEnables the behaviour to go back to the start/end in the carousel edges
isRTLBoolfalseSets the carousel in RTL mode
itemsToShowNumber1Number of visible items in the carousel
onAfterChangeFuncCallback when changing the active item. Callback object: { index: Number - The new active index, dir: String - The movement direction ('prev' or 'next') }
itemsToScrollNumber1The number of items to scroll when using Arrow

It also accepts additional props to be passed to the container.

CarouselContext

A React Context for the Carousel, following the Context API.

Context object
NameTypeDescription
activeItemNumberThe active carousel item index (0 based)
isRTLBoolIs the carousel in RTL mode
isInfiniteBoolIs the carousel in infinite mode
itemsToShowNumberNumber of visible items in the carousel
itemsLengthNumberTotal items length in the carousel
setItemsLengthFuncSetter for the itemsLength value. Arguments: (itemsLength: Number - The new items length)
goToFuncMove the carousel to an specific index. Arguments: (newIndex: Number - The new active item, options: Object - To be passed the onAfterChange callback)
goNextFuncMove to the next item, if possible. Arguments: (options: Object - To be passed the onAfterChange callback)
goPrevFuncMove to the previous item, if possible. Arguments: (options: Object - To be passed the onAfterChange callback)
containerRefObjectComponent reference that can be used throughout the Carousel from the context

CarouselProvider

A provider for the CarouselContext, already handling all the business logic and the state management for the Carousel.

Properties
NameTypeDefaultDescription
isInfiniteBoolfalseEnables the behaviour to go back to the start/end in the carousel edges
childrenNodeChildren to have the context available
isRTLBoolfalseSets the carousel in RTL mode
itemsToShowNumber1Number of visible items in the carousel
onAfterChangeFuncCallback for after the change of the active item. Callback object: { index: Number - The new active index, dir: String - The movement direction ('prev' or 'next') }
startItemNumber0Force to display a item by index

Arrow

A render prop component that returns the onClick function, based on the props.

Properties
NameTypeDefaultDescription
flowStringThe movement flow for the arrow. Allowed values: "next" and "prev"
childrenNodeFunction to render the component. Callback object: { onClick: Func - The onClick handler for the component }
onClickFuncCallback for the onClick

Bullets

The pagination of the carousel, with infinite bullets support.

Properties
NameTypeDefaultDescription
themeObjectThe theme object, as described below
Theming
NameDescription
container
containerDefault
containerInfinite
moveInfinite
bullet
bulletInfinite
isSecondary
isActive

SwipeSlider

A slider that uses Javascript swipe navigation and keyboard navigation.

Properties
NameTypeDefaultDescription
classNameStringDesired theme's class names
childrenNodeThe items for the carousel
disableSwipeBoolfalseDisables the swipe navigation
hasKeysNavigationBooltrueEnables the keyboard navigation for the carousel
Support for navigation components

The SwipeSlider has full support for all the navigation components.

ScrollSlider

A slider using native scroll navigation. Variable-width items are NOT supported.

Properties
NameTypeDefaultDescription
classNameStringDesired theme's class names
childrenNodeThe items for the carousel
limitScrollBoolfalseLimits the scroll depth to just one item (mobile only)
Support for navigation components

The ScrollSlider works with the Arrow component using the requestAnimationFrame, to have a smooth scroll for the item.

It also has support for the Bullets.

UnevenItemsScrollSlider

A slider using native scroll navigation. itemsToScroll are NOT supported. Other Carousel props not supported include itemsToShow, and isInfinite.

Properties
NameTypeDefaultDescription
classNameStringDesired theme's class names
childrenNodeThe items for the carousel
ratioToScrollNumber1Range ratioToScroll > 0 && ratioToScroll <= 1. The percentage (in decimal form) of the carousel viewport to scroll when using Arrow
itemOnSizeChangeNumberThe item to go to when length of carousel changes. If not specified, the carousel will remain where it is when resizing occurs
Support for navigation components

The UnevenItemsScrollSlider works with the Arrow component using the requestAnimationFrame, to have a smooth scroll for the item.

It also has support for Bullets.

SnapSlider

A slider using native scroll navigation, with snap points, on browsers with support.

It fallback to the SwipeSlider for browser with no support.

NOTE: The Snap Points support can be laggy on Android and RTL mobile browsers. For those cases, prefer the SwipeSlider.

Properties
NameTypeDefaultDescription
classNameStringDesired theme's class names
childrenNodeThe items for the carousel
limitScrollBooltrueLimits the scroll depth to just one item (mobile only)
Support for navigation components

The ScrollSlider doesn't work with the Arrow component, due to snap point support.

It supports the Bullets component.

Creating your own component

As the Carousel state is managed by a context, we can easily create new components extending the carousel logic.

Example:

import { CarouselContext } from '@farfetch/react-carousel';

const Pagination = (props) => {
    <CarouselContext.Consumer>
        { ({ activeItem, itemsLength }) => <div>{ activeItem } of { itemsLength } items</div> }
    </CarouselContext.Consumer>
}

export default Pagination;

You can easily consume any information from the CarouselContext.Consumer and also even create a new movement engine with a custom animation, for example, as all the functions are already exposed in the Context.

React compatibility

React >= 16.8.0 is required to use this package.

Contributing

Read the Contributing guidelines

Disclaimer

By sending us your contributions, you are agreeing that your contribution is made subject to the terms of our Contributor Ownership Statement

Maintainers

License

MIT

Keywords

FAQs

Last updated on 13 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc