Skip to content

Commit b994fa3

Browse files
committed
Add custom scroll function
1 parent 1566c25 commit b994fa3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Link, NavLink } from 'react-router-dom';
55
let hashFragment = '';
66
let observer = null;
77
let asyncTimerId = null;
8+
let scrollFunction = null;
89

910
function reset() {
1011
hashFragment = '';
@@ -18,7 +19,7 @@ function reset() {
1819
function getElAndScroll() {
1920
const element = document.getElementById(hashFragment);
2021
if (element !== null) {
21-
element.scrollIntoView();
22+
scrollFunction(element);
2223
reset();
2324
return true;
2425
}
@@ -60,7 +61,13 @@ export function genericHashLink(props, As) {
6061
) {
6162
hashFragment = props.to.hash.replace('#', '');
6263
}
63-
if (hashFragment !== '') hashLinkScroll();
64+
if (hashFragment !== '') {
65+
scrollFunction =
66+
props.scroll ||
67+
(el =>
68+
el.scrollIntoView(props.smooth ? { behavior: 'smooth' } : undefined));
69+
hashLinkScroll();
70+
}
6471
}
6572
return (
6673
<As {...props} onClick={handleClick}>
@@ -80,6 +87,7 @@ export function NavHashLink(props) {
8087
const propTypes = {
8188
onClick: PropTypes.func,
8289
children: PropTypes.node,
90+
scroll: PropTypes.func,
8391
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
8492
};
8593

0 commit comments

Comments
 (0)