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-simple-horizontal-scroll

v1.1.12

Published

React Simple Horizontal Scroll

Downloads

19

Readme

React Simple Horizontal Scroll

This is a simple horizontal scroll React component.

The intention to build this component was to have a lightweigt horizontal scroll component with snapping and some basic features for mobile devices. A usual slider or carousel was way to heavy for my needs. It works well on all touch devices and desktop devices who has the ability to scroll horzizontal by mouse or touchpad.

The scrolling is native browser scroll, the scrollbar will be hidden by CSS. It has support for CSS Snapping. You can scroll initialy to a given item index or you can scroll programmaticly with the use of a reference. You can also set a data attribute by your own. The active item will have a value of "true" otherwise "false". So you can style the active item by your own.

Usage

Install component

npm i react-simple-horizontal-scroll

Use in your component

import { HorizontalScroll } from 'react-simple-horizontal-scroll';
import type { HorizontalScrollHandle } from 'react-simple-horizontal-scroll/dist/types';

<HorizontalScroll gap={10} scrollToItemIndex={2} activeDataAttribute="data-active" offset={'2rem'}>
  /** children here */
</HorizontalScroll>;

Props

| Prop | Description | Type | mandatory | default | | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | --------- | -------- | | gap | the gap between the items | string, number | false | 0 | | offset | offset of the first and last item to the border of the wrapper | string, number | false | 0 | | scrollToItemIndex | initial scroll to the given item index starting at 0 | number | false | 0 | | activeDataAttribute | a data attribute that can be used to style the active item | string | false | | | layout | layout option to render the scroller SCROLL -> renders the items in a row with a scroll FLOAT -> renders the items in float layout without scroll | 'SCROLL', 'FLOAT' | false | 'SCROLL' |

Usage with ref

The component returns 2 methods to a given ref.

Methods

| method | description | return value | | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | | scrollToIndex | method to scroll programmaticly to an item index | void | | hasScrollContent | handler function to evaluate if the scroller has overflowing content can be used in combination with window.resize to toggle a showAll button for example | boolean |

Sample code

import React from 'react';
import { HorizontalScroll } from 'react-simple-horizontal-scroll';
import type { HorizontalScrollHandle } from 'react-simple-horizontal-scroll/dist/types';

function Component() {
  const ref = React.useRef<HorizontalScrollHandle | null>(null);
  const [showAll, setShowAll] = React.useState(false);

  React.useEffect(() => {
    const handler = () => {
      if (ref?.current) {
        setShowAll(ref.current.hasScrollContent());
      }
    };

    window.addEventListener('resize', handler);

    return () => {
      window.removeEventListener('resize', handler);
    };
  }, [ref, setShowAll]);

  function onClickHandler(index: number) {
    if (ref?.current) {
      ref.current.scrollToIndex(index);
    }
  }

  return (
    <>
      {showAll && <button>Show All</button>}
      <HorizontalScroll gap={10} scrollToItemIndex={2} activeDataAttribute="data-active" offset={'2rem'} ref={ref}>
        <div onClick={() => onClickHandler(0)}>1</div>
        <div onClick={() => onClickHandler(1)}>2</div>
        <div onClick={() => onClickHandler(2)}>3</div>
      </HorizontalScroll>
    </>
  );
}

Usage - for Github only

Checkout project

$ git clone https://github.com/DerWebschreiner/react-simple-horizontal-scroll.git

install

$ npm install

start demo

$ npm run start

run tests

$ npm run test