react-scrollable-anchor
Lightweight library for smooth scrolling anchors in React, tied to URL hash.
- Land on correct anchor when page is loaded, based on URL hash value.
- Scroll smoothly to anchors when URL hash changes. Easy links to sections with
<a href='#sectionId'>
. - URL hash updates automatically to reflect section in view
- Option to record history on hash changes
npm install --save react-scrollable-anchor
Examples
To run examples locally, npm run example
, then open your
browser to localhost:3210.
Usage
1. Creating a scrollable anchor
Use the ScrollableAnchor
tag to wrap any React element, making it a scrollable anchor.
{ return <div> <a href='#section1'> Go to section 1 </a> <a href='#section2'> Go to section 2 </a> <ScrollableAnchor id='section1'> <div> Hello World </div> </ScrollableAnchor> <ScrollableAnchor id='section2'> <div> How are you world? </div> </ScrollableAnchor> </div> ) }}
2. Configure
Access configureAnchors to customize scrolling and anchors.
Offset all scrollable anchors by a fixed amount
// Offset all anchors by -60 to account for a fixed header// and scroll more quickly than the default 400ms
Options:
option | default |
---|---|
offset |
0 |
scrollDuration |
400 |
keepLastAnchorHash |
false |
3. Utilities
A small toolkit of scrolling utilies for use with anchors
Jump to top of page in a way that plays nicely with scrollable anchors
// scroll to top of the page
Scroll to any scrollable anchor, with option to record history
// scroll to #section1 without saving that hash update in history // scroll to #section1, saving that hash update in history
Clear the URL hash without affecting scroll location at all
// clear URL hash
Issues and feature requests
Please open issues on Github. Issues are easier to address if you include context and code samples.
Contributing
Please contribute!
Feedback or contact
Feel free to contact me at gabergg@gmail.com.