1.1.1 • Published 2 years ago

@computeruniverse/react-carousel v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@computeruniverse/react-carousel

Last commit license PRs Welcome
Downloads Activity Minified npm

Usage

import React, { useState } from 'react';
import { ReactCarousel } from '@computeruiverse/react-carousel';
import '@computeruiverse/react-carousel/lib/index.css';

const MyCarousel: React.VFC = () => {
    const [activeSlide, setActiveSlide] = useState(0);
    const items = [];
    for (let i = 0; i < 5; i++) {
        items.push(i);
    }

    return (
        <>
            <ReactCarousel
                isStatic={true}
                slidesToShow={4}
                activeSlide={activeSlide}
                itemCount={items.length}
                autoplayInterval={5000}
                movementTension={50}
                movementFriction={20}
                onChange={(activeIndex) => {
                    console.log('activeIndex', activeIndex);
                    setActiveSlide(activeIndex);
                }}
                itemFactory={(index, preloading) => {
                    return (
                        <div
                            key={index}
                            style={{
                                height: '200px',
                                fontSize: '60px',
                                backgroundColor: (index + 1) % 2 === 0 ? 'green' : 'yellow',
                            }}
                        >
                            {index}
                        </div>
                    );
                }}
            />

            <button onClick={() => setActiveSlide(activeSlide - 1)}>Prev</button>
            <button onClick={() => setActiveSlide(activeSlide + 1)}>Next</button>
        </>
    );
};

export default MyCarousel;

Props

NameTypeDefaultDescription
itemFactory(index: number, preloading: boolean) => React.ReactElement(required)undefinedIs called at the creation of the carousel and basically returns an array of slides to show. Important the return value is cached
isStaticboolean(required)undefined
itemCountnumber(required)undefinedCount of how many Slides are in the carousel
activeSlidenumber(required)`undefined | Index of the currently active slide, if the value is changed the carousel slides to this index
slidesToShownumber1Value of how many slides the carousel shows at the same time
defaultSlidenumber0Position of the default slide, you're probably don't need this value
onChange(activeIndex: number) => voidundefinedTriggers every time the carousel moves
autoplayIntervalnumber (ms)undefinedSets the interval time for the carousel in milliseconds
movementTensionnumber50
movementFrictionnumber20
slideCssClasses{ default: string, active: string }undefinedSets a default and/or active class to the slides so that you can style your slides in an easier way