1.0.0 • Published 9 months ago

scroll-carousel-react v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Scroll Carousel React

This is the react version of Scroll Carosel plugin, which is written in vanilla JavaScript.

NPM | Documentation | Demos

Note: This carousel only operates in browser.

Install

npm
npm install scroll-carousel-react

Usages

With React JS
import React from 'react';
import ScrollCarousel from 'scroll-carousel-react';


const MyComponent = () => {
  return (
    <>
      <h1>This is my component page</h1>
      <p>Now i am showing my creation scroll carousel</p>
      <ScrollCarousel
        autoplay
        autoplaySpeed={8}
        speed={7}
        onReady={() => console.log('I am ready')}
      >
        {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((item) => (
          <div key={item} className='bg-blue-300/20 border-2 border-blue-300/70 rounded h-36 w-48'>
            {item}
          </div>
        ))}
      </ScrollCarousel>
    </>
  );
};

export default MyComponent;
With Next JS
  1. Make a component with any name ScrollCarouselComponent with the following code. This is a wrapper component.

components/ScrollCarouselComponent.js

// 'use client'; // For Next JS 13 app router


import React from 'react';
import ScrollCarousel from 'scroll-carousel-react';


const ScrollCarouselComponent = () => {
  return (
    <ScrollCarousel
      autoplay
      autoplaySpeed={8}
      speed={7}
      onReady={() => console.log('I am ready')}
    >
      {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((item) => (
        <div key={item} className='bg-blue-300/20 border-2 border-blue-300/70 rounded h-36 w-48'>
          {item}
        </div>
      ))}
    </ScrollCarousel>
  );
};

export default ScrollCarouselComponent;
  1. Import this component where you need it. But it should be dynamic import. This is because the next js tries to run the plugin in its server side. But as it is only available for client side. That's why it does not find window or document
// 'use client'; // For Next JS 13 app router

import React from 'react';

import dynamic from 'next/dynamic';

const ScrollCarousel = dynamic(() => import('@/components/ScrollCarouselComponent'), { ssr: false });

const MyComponent = () => {
  return (
    <div className='flex min-h-screen flex-col items-center justify-between p-24'>
      <div className='text-center'>
        <h2 className='text-4xl font-bold'>This is our about page</h2>
      </div>

      <ScrollCarousel />
    </div>
  );
};

export default MyComponent;

Props

  • All props are optional
OptionTypeDefaultDescription
speednumber7The value given is how fast you want to move on the scroll. It needs to be greater than 0.
smartSpeedbooleanfalseTo calculate the speed of how fast or slow you are scrolling a website.
marginnumber10To make gap between two slide
slideSelectorstringnullSelect the slides with a class name you want to select for the carousel. Other elements will behave as simple.
autoplaybooleanfalseThe option will allow the slides move automatically and still you will have the ability to handle sliding speed on scroll.
autoplaySpeednumber5Control autoplay speed. It needs to be greater than 0
directionstring'rtl'Control direction left to right or right to left. Two possible option - ltr or rtl
onReady() => voidWhen the carousel is ready to perform its action, that time this event will be fired.
onMove(progress: number) => voidWhen the carousel is on move (i,e at the time of scrolling, when autoplay enabled), the event will be fired continuously.
onDestroy() => voidAt the time of destroy function, this event will be fired.
classNamestringFor using with extra class
elementTypestringdivTag, that will be used to create the carousel.
scRef(ref: ScrollCarousel) => voidThis will give you an instance of scroll carousel. You can use it for method call.

For better documentaion, please have a look on the website of Scroll Carousel

License

The code and the documentation are released under the MIT License.