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

use-autoscroll

v1.1.0

Published

This is a React custom hook. It allows you to easily add functionality to your site to programatically vertical scroll through a series of items of interest. A typical scenario would be a vertical stack of "card" type divs, and having an arrow button that

Downloads

11

Readme

useAutoScroll: a React Hook

This is a React custom hook. It allows you to easily add functionality to your site to programatically vertical scroll through a series of items of interest. A typical scenario would be a vertical stack of "card" type divs, and having an arrow button that you click, to scroll through them.

A codesandbox showing the usage can be seen here.

Getting Started

The basic functionality is relatively straightforward. You call useAutoScroll, and pass in a DOM node (presumably obtained as a ref), and a CSS-type selector string, as would be used in document.querySelector(e.g., 'h2', '.card' 'h2.card', etc.). useAutoScroll will search within the DOM node using the query selector, and make each item a scrollable destination, in addition to the top of the page. It then returns an object with the following signature: { scrollNext: func, scrollBack: func, position: number, length: number }. The first two properties are functions, and will programatically move through the scroll targets in question. They will wrap around (i.e., if you have 5 elements, and click to go the 6th, you will be taken back to the first). It is up to you to programatically disable this activity if you desire. You can do this using the last two arguments, position and length. The position property tells the current index in the array of scroll-points (including the top of the page). The length property tells you how many such scroll points there currently are. You could use these to selectively deactivate the scrolling, hide the scroll buttons, etc.

Prerequisites

This is meant to be used with React 16.8+ and is designed for React DOM only.

Installing

This can be installed using yarn add use-autoscroll or npm install use-autoscroll. It can then be imported as the default import for the package: import useAutoScroll from 'use-autoscroll.

Usage Example

The basic semantics are as follows:

const { scrollNext, scrollBack, position, length } = useAutoScroll(ref.current, '.card')

A more elaborate example is as follows:

import React, { useEffect, useState, useRef } from "react";
import ReactDOM from "react-dom";
import useAutoScroll from "use-autoscroll";
const data = [0, 1, 2, 3, 4, 5];

function App() {
  const rootNode = useRef(null);

  const { scrollNext, scrollBack, position, length } = useAutoScroll(
    rootNode.current,
    ".card"
  );
  console.log(position, length);
  return (
    <div className="App" ref={rootNode}>
      <h1>useAutoScroll Example</h1>
      <div className="controls">
        <button onClick={scrollBack}>
          up
        </button>
        <button onClick={scrollNext}>
          down
        </button>
      </div>
      {data.map(datum => (
        <div className="card" key={datum}>
          {datum}
        </div>
      ))}
    </div>
  );
}

Contributing

Contributions and suggestions are welcome! Please open an Issue with any ideas!

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details