Coder Social home page Coder Social logo

zachcd / react-scrollchor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from some-react-components/react-scrollchor

0.0 2.0 0.0 5.81 MB

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

License: ISC License

JavaScript 100.00%

react-scrollchor's Introduction

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

react-scrollchor's People

Contributors

bysabi avatar jeanchung avatar

Watchers

James Cloos avatar Zachary Chay-Dolan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.