npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-carousel-hook

v0.0.5

Published

Simple and easy to use carousel for react apps

Downloads

10

Readme

React useCarousel hook 🎠

This npm package provides react components to display a carousel. The components can be loaded through a useCarousel hook. The carousel is quite simple but still supports a wide set of requirements and is easy to use.

Should I use a carousel?

Short answer, no. See shouldiuseacarousel.com for more information on this topic. If you want or have to use a carousel anyway you're more than welcome to use this one. :-)

Key features

  • Load multiple carousels on one page without conflicts
  • Easy to use Carousel, Slides, Control and Pagination components
  • Lightweight and zero dependencies (besides react)
  • Supports multiple slides per view without any custom configurations
  • Realized with vanilla JavaScript and CSS Scroll Snap
  • Accessible and automatically applied aria attributes

Installation

npm install react-use-carousel-hook
# or
yarn add react-use-carousel-hook

Usage

With react-use-carousel-hook you can easily display a carousel by importing the hook and use the returned components. There are four available components. The first one is the Carousel component which is needed to support the aria attributes. The second one is the Slides component which is used to wrap your slides. The third one is the Control component which can be used to display previous / next slide buttons. The last one is the Pagination component which can be used to display a pagination representing the carousel slides.

import { useCarousel } from 'react-use-carousel-hook';
import "react-use-carousel-hook/dist/index.css";

const YourComponent = ({ items = [] }) => {
  const { Carousel, Slides, Control, Pagination } = useCarousel();

  return (
    <Carousel>
      <Slides>
        {items.map((item) => (
          <div key={item.id}>
            <h3>{item.title}</h3>
            <p>{item.content}</p>
          </div>
        ))}
      </Slides>

      <Control direction="prev" />

      <Pagination buttonClassName="bg-gray aria-selected:bg-black" />
      {/* OR */}
      <Pagination>
        {(index) => <button className="bg-gray aria-selected:bg-black">Slide number {index}</button>}
      </Pagination>

      <Control direction="next" />
    </Carousel>
  );
};

Hook

Usage

The useCarousel hook returns four components and accpets some options as function parameter.

const { Slides } = useCarousel({ loop: false });

Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | loop | boolean | false | If set to false the Control prev/next button will be disabled when start/end of carousel is reached. If set to true buttons can always be clicked and carousel jumps to start if at end and vice versa. |

Components

There are four components that can be used via the useCarousel hook. Those components are Carousel, Slides, Control and Pagination. Each of them supports or even requires a number of props.

Carousel

This component is needed to support the aria attributes. Its only purpose is to make the carousel accessible. You can pass anything as children.

<Carousel>
  <h2>FooBar</h2>
  <Slides>
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </Slides>
</Carousel>

| Prop | Type | Default | Description | | ---------- | ----------- | ----------- | --------------------------- | | children | ReactNode | undefined | Any elements. Used as wrapper for Slides, Control, Pagination. | | | | | All props that are supported by the <div/> element. |

Slides

Wrapper component for the slides. It will add styles to make the slides look and feel like a carousel.

<Slides>
  {[1, 2, 3, 4].map((item) => (
    <div key={item}>Slide {item}</div>
  ))}
</Slides>

| Prop | Type | Default | Description | | ---------- | ---------------- | ----------- | --------------------------- | | children* | ReactElement[] | undefined | The slides of the carousel. | | | | | All props that are supported by the <div/> element. |

* required

Control

This component can be used to display a previous or next slide button. To style a disabled control button use the :disabled pseudo-class in CSS.

<Control direction="prev"/>
<Control direction="next"/>

| Prop | Type | Default | Description | | --- | --- | --- | --- | | direction* | "prev" \| "next" | undefined | The direction in which the carousel should scroll/slide when clicked. | | | | | All props that are supported by the <button/> element. |

* required

Pagination

This component can be used to display a pagination to control the carousel slides. To style the active pagination button use the &[aria-selected=“true”] attribute selector in CSS.

Usage of basic pagination with bullets:

<Pagination buttonClassName="bg-gray aria-selected:bg-black" />

If you need more control and want to display a numbered pagination you can implement it like this:

<Pagination>
  {(index) => (
    <button className="bg-gray aria-selected:bg-black">Slide number {index}</button>
  )}
</Pagination>

| Prop | Type | Default | Description | | --- | --- | --- | --- | | buttonClassName | string | undefined | Can be used to set classNames to the pagination buttons. | | children | (index: number) => HTMLButtonElement | undefined | Will return basic button as pagination point if undefined. Button can be customized by returning a function returning a button. | | | | | All props that are supported by the <div/> element. |

Contributors

Big thanks to all our contributors who helped with this project.