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-target-scroller

v0.1.14

Published

React component that scrolls smoothly to the target element passed in via props.

Downloads

71

Readme

🎯 react-target-scroller

npm npm npm Coveralls github CircleCI Snyk Vulnerabilities for GitHub Repo

React component that smoothly scrolls to the target element passed in via props.

Install

Via npm

npm install --save react-target-scroller

Via Yarn

yarn add react-target-scroller

How to use

The TargetScroller is very configurable and designed to do a single thing well. I have another project, react-hash-handler, that pairs well with this component, but can easily be used alone, or coupled with your own components.

Below are the properties and callbacks that can be set on the component, along with an example of some general use.

Properties

  • delay:Number - The time in milliseconds to delay the scroll after the target has been set/changed. (default: 0)

  • direction:String - The direction the scrollingElement will incremented/decremented in order to reach its target. The options are available via an export available from the component. Example: import TargetScroller, {Direction} from 'react-target-scroller; The options are, Direction.VERTICAL or Direction.HORIZONTAL. (default: Direction.VERTICAL)

  • duration:Number - The duration in milliseconds that the page will take to transition to the target. (default: 650)

  • ease:Func - The easing function used to define the tween of the transition. TargetScroller uses tweenkle and supports all the easing equations offered in that library, or you can write your own based on the ease spec. (default: Quad.InOut)

  • offset:Number - Number used to offset the scroll position. Useful for making sure fixed headers don’t cover up the target object.

  • scrollingElement:[String || Element] - The element that will be scrolled in order to reach the target. This is handy if you have a scrollable element in the page and want to target its children. (default: 'document.scrollingElement').

  • target:[String || Element] - The property that defines which element to scroll to. You can either supply an actual reference to the element, or a string that can target the element. (Note: If you supply a string, make sure it’s a string format that is supported by querySelector.)

Callbacks

  • onTweenComplete:Func - A function that is called when the scroll transition has finished.

  • onTweenTick:Func - A function that is called during the scroll transition on the way to the target.

Examples

import React, { Component } from 'react';
import TargetScroller from 'react-target-scroller';

...

class ExampleComponent extends Compnonent {
  constructor(props) {
    super(props);

    this.state = {
      scrollTarget: null,
    };

    this.onNavLinkClick = this.onNavLinkClick.bind(this);
  }

  onNavLinkClick(evt) {
    const hash = href.indexOf('#') > -1 ? href.split('#')[1] : null;

    if (!hash) {
      return;
    }

    this.setState({
      scrollTarget: `#${hash}`,
    });
  }

  render() {
    const {
      scrollTarget,
    } = this.state;

    return (
      <div className="page-wrapper">
        <nav>
          <ul>
            <li><a href="#overview" onClick={this.onNavLinkClick}>Overview</a></li>
            <li><a href="#about" onClick={this.onNavLinkClick}>About</a></li>
            <li><a href="#contact" onClick={this.onNavLinkClick}>Contact</a></li>
          </ul>
        </nav>
        <TargetScroller target={scrollTarget} />
        ...
        <section id="overview">
          ...
        </section>
        <section id="about">
          ...
        </section>
        <section id="contact">
          ...
        </section>
      </div>
    );
  }
}

License

MIT © Ryan Hefner