Gathering detailed insights and metrics for react-router-hash-link
Gathering detailed insights and metrics for react-router-hash-link
Gathering detailed insights and metrics for react-router-hash-link
Gathering detailed insights and metrics for react-router-hash-link
@types/react-router-hash-link
TypeScript definitions for react-router-hash-link
@xzar90/react-router-hash-link
Hash link scroll functionality for React Router v6
react-router
Declarative routing for React
react-router-redux
Ruthlessly simple bindings to keep react-router and redux in sync
Hash link scroll functionality for React Router
npm install react-router-hash-link
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
734 Stars
111 Commits
62 Forks
10 Watching
4 Branches
7 Contributors
Updated on 01 Nov 2024
Minified
Minified + Gzipped
JavaScript (97.74%)
TypeScript (2.26%)
Cumulative downloads
Total Downloads
Last day
1.5%
36,891
Compared to previous day
Last week
3.5%
186,800
Compared to previous week
Last month
2.4%
798,918
Compared to previous month
Last year
0%
10,407,203
Compared to previous year
1
2
6
This is a solution to React Router's issue of not scrolling to #hash-fragments
when using the <Link>
component to navigate.
When you click on a link created with react-router-hash-link
it will scroll to the element on the page with the id
that matches the #hash-fragment
in the link. This will also work for elements that are created after an asynchronous data load. Note that you must use React Router's BrowserRouter
for this to work.
Code is in the /demo
folder, or open the demo in CodeSandbox
1npm install --save react-router-hash-link
react-router-dom
is a peer dependency.
<HashLink>
1import { HashLink } from 'react-router-hash-link'; 2 3... 4 5// use it just like a RRv4/5 <Link> 6// the `to` prop can be a string or an object, see RRv4/5 api for details 7<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
<NavHashLink>
1import { NavHashLink } from 'react-router-hash-link'; 2 3... 4 5// use it just like a RRv4/5 <NavLink> (see RRv4/5 api for details) 6// it will be active only if both the path and hash fragment match 7<NavHashLink 8 to="/some/path#with-hash-fragment" 9 activeClassName="selected" 10 activeStyle={{ color: 'red' }} 11 // etc... 12>Link to Hash Fragment</NavHashLink>
smooth: boolean
element.scrollIntoView()
for scrolling, and when the smooth
prop is present it will call it with the smooth option, element.scrollIntoView({ behavior: 'smooth' })
scrollIntoView
- see MDN and Can I Use - there is also a browser polyfill for smooth scrolling which you can install separately so smooth
will work in all browsers1import { HashLink } from 'react-router-hash-link'; 2 3... 4 5<HashLink smooth to="/path#hash"> 6 Link to Hash Fragment 7</HashLink>;
scroll: function
const myScrollFn = element => {...}
scrollIntoView
1import { HashLink } from 'react-router-hash-link'; 2 3... 4 5<HashLink 6 to="/path#hash" 7 scroll={(el) => el.scrollIntoView({ behavior: 'auto', block: 'end' })} 8> 9 Link to Hash Fragment 10</HashLink>;
#
(empty) or #top
1import { HashLink } from 'react-router-hash-link'; 2 3... 4 5<HashLink to="/path#top">Link to Top of Page</HashLink> 6// or 7<HashLink to="#top">Link to Top of Page</HashLink>
elementId: string
to
prop, if both are present then the elementId
will override the to
prop's hash fragmentto
prop's hash fragment instead of the elementId
Link
The exported components are wrapped versions of the Link
and NavLink
exports of react-router-dom. In some cases you may need to provide a custom Link
implementation.
For example, the gatsby static site generator requires you to use its implementation of Link
. You can wrap it with the genericHashLink
function of this package.
1import { genericHashLink } from 'react-router-hash-link'; 2import GatsbyLink from 'gatsby-link'; 3 4const MyHashLink = genericHashLink(GatsbyLink); 5 6const MyComponent = () => ( 7 <div> 8 The default wont work for you? 9 <MyHashLink to="/faq#how-to-use-custom-link">No problem!</MyHashLink> 10 </div> 11);
react-router-hash-link
attempts to recreate the native browser focusing behavior as closely as possible.
The browser native behavior when clicking a hash link is:
tabindex
), then the target element is focused.To recreate this react-router-hash-link
does the following:
element.focus()
followed by element.blur()
(using a temporary tabindex
to ensure that the element can be focused programmatically) so that focus moves to the target element but does not remain on it or trigger any style changes.element.focus()
and leaves focus on the target element.Note that you may find it useful to leave focus on non-interactive elements (by adding a tabindex
of -1
) to augment the navigation action with a visual focus indicator.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/23 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
117 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More