A React component for scroll to
#hash
links with smooth animations. Scrollchor is a mix ofScroll
andAnchor
, a joke name for a useful component.
See it in action:
- demo video
- example page and source code
hash
is the id
of a HTML tag on current page
npm install react-scrollchor --save
- User should provide its own
React
package
fbjs is a collection of utility libraries created by React Team. It include useful modules like warning
and invariant
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>
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
}
Animated behavior can be customize on each #hash link
instance.
<Scrollchor to="#aboutus" animate={{offset: 20, duration: 600}} className="nav-link">Home</Scrollchor>
{ offset: 0, duration: 400, easing: easeOutQuad }
This setting is equivalent to default jQuery.animate easing: swing
Use this callbacks to trigger behaviours like, for example, update state, load async stuffs, etc.
<Scrollchor to="#aboutus" afterAnimate={() => updateState(this)}>Home</Scrollchor>
- Documentation improvement
- Feel free to send any PR