zachcd / react-scrollchor

A React component for scroll to `#hash` links with smooth animations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-scrollchor (React Scrollchor)

npm version npm downloads bitHound Overall Score Donate

A React component for scroll to #hash links with smooth animations. Scrollchor is a mix of Scroll and Anchor, a joke name for a useful component.

See it in action:

hash

hash is the id of a HTML tag on current page

Installation

npm

npm install react-scrollchor --save

Dependencies

  • User should provide its own React package

fbjs package

fbjs is a collection of utility libraries created by React Team. It include useful modules like warning and invariant

Usage

import Scrollchor from 'react-scrollchor';
export default (props) => (
  <Page>

    <Navbar brand={brand} className="navbar-fixed-top">
      <NavItem><Scrollchor to="" className="nav-link">Home</Scrollchor></NavItem>
      <NavItem><Scrollchor to="#sample-code" className="nav-link">Sample</Scrollchor></NavItem>
      <NavItem><Scrollchor to="#features" className="nav-link">Features</Scrollchor></NavItem>
      <NavItem><Scrollchor to="footer" className="nav-link">SignUp</Scrollchor></NavItem>
    </Navbar>


  <Section id="sample-code">

  </Section>

  <div id="features">

  </div>

  <footer id="footer">

  </footer>

</Page>

Prop types

  propTypes: {

    /**
     * id of tag scroll target node
     * - starting `#` can be omited
     * - this prop is required
     * - let it blank, `to = ''`, for scroll to page top
     */
    to: PropTypes.string.isRequired,

    /**
     * scroll smooth animation can be customize
     * Accepted options:
     *  { offset: 0, duration: 400, easing: easeOutQuad }
     */
    animate: PropTypes.object,

    /**
     * callback function triggered before scroll to #hash
     */
    beforeAnimate: PropTypes.func,

    /**
     * callback function triggered after scroll to #hash
     */
    afterAnimate: PropTypes.func
}

Custom animation

Animated behavior can be customize on each #hash link instance.

<Scrollchor to="#aboutus" animate={{offset: 20, duration: 600}} className="nav-link">Home</Scrollchor>

default animation config

{ offset: 0, duration: 400, easing: easeOutQuad }

This setting is equivalent to default jQuery.animate easing: swing

more Easing functions

before and after animate callbacks

Use this callbacks to trigger behaviours like, for example, update state, load async stuffs, etc.

<Scrollchor to="#aboutus" afterAnimate={() => updateState(this)}>Home</Scrollchor>

Example

react-scrollchor--example

Contributing

  • Documentation improvement
  • Feel free to send any PR

License

ISC

About

A React component for scroll to `#hash` links with smooth animations

License:ISC License


Languages

Language:JavaScript 100.0%