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-snap-slider

v0.1.3

Published

A headless React hook that allows you to control a CSS `scroll-snap` slider.

Downloads

1

Readme

react-use-snap-slider

This is simple React hook for creating a CSS Scroll Snap slider. It allows you to create basic sliders with a few lines of code, and gives you full control over the styling.

  • Native scrolling and snapping
  • Supports SSR, since all the layout is done in CSS - No JS layout calculations
  • Uses React IntersectionObserver to detect when slides are visible
  • MutationObserver to detect when slides are added or removed

It's still early. There are likely to be bugs and missing features.

Installation

npm install react-use-snap-slider

Usage

useSnapSlider hook

The useSnapSlider hook handles the logic for a CSS Scroll Snap slider.

import { useSnapSlider } from "react-snap-slider";

function App() {
  const { ref, next, previous, activeIndex, totalSlides } = useSnapSlider({
    threshold: 0,
  });

  return (
    <div>
      <div
        ref={ref}
        className="flex snap-x snap-mandatory overflow-x-scroll scrollbar-hide"
      >
        <div className="flex-none basis-full snap-start">Slide 1</div>
        <div className="flex-none basis-full snap-start">Slide 2</div>
        <div className="flex-none basis-full snap-start">Slide 3</div>
      </div>
      <p>
        Slide {activeIndex + 1} / {totalSlides}
      </p>
      <button>Previous</button>
      <button>Next</button>
    </div>
  );
}

API

Options

Provide these values as the options argument in the useSnapSlider hook:

| Name | Type | Default | Description | | ------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------- | | selector | string | | A CSS selector to use to find the slides inside the root element. If not defined, all children will be used | | threshold | number | 0 | Number between 0 and 1 indicating the percentage of a slide that should be visible, before it's considered visible. | | disabled | boolean | false | Disable the snap slider logic | | loop | boolean | true | Loop the slides with the next and previous functions. |

Return values

The useSnapSlider hook returns an object with the following values:

| Name | Type | Description | | --------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------- | | ref | Ref | The ref function to pass to the slider container. This is the element that can scroll to show the slides. | | activeIndex | number | The index of the first visible slide. | | totalSlides | number | The total number of slides that's been registered. This will be 0 until the ref is set. | | next | () => void | A function to scroll to the next slide. | | previous | () => void | A function to scroll to the previous slide. | | scrollTo | (index: number) => void | A function to scroll to a specific slide. |

Styling

The useSnapSlider hook handles the logic for a snap slider, giving you full control over the styling.

Tailwind CSS

Example of a snap slider with three slides, styled with Tailwind CSS.

The important parts are:

  • snap-x snap-mandatory overflow-x-scroll on the container. This enables the native scrolling and snapping.
  • snap-start on each slide. This makes the slides snap to the start of the container.
  • flex-none basis-full on each slide. This makes the slides take up the full width of the container.
<div class="flex snap-x snap-mandatory overflow-x-scroll">
  <div class="flex-none basis-full snap-start">Slide 1</div>
  <div class="flex-none basis-full snap-start">Slide 2</div>
  <div class="flex-none basis-full snap-start">Slide 3</div>
</div>

Hiding the scrollbar

You can use the following CSS to hide the scrollbar on the snap slider.

@layer components {
  .scrollbar-hide {
    scrollbar-width: none;
  }
  .scrollbar-hide::-webkit-scrollbar {
    display: none;
  }
}

Apply it to the snap slider container.

<div class="snap-x snap-mandatory overflow-x-scroll scrollbar-hide">...</div>