Skip to content

Commit cbe91a4

Browse files
committed
Create HashLink component
1 parent 3831edb commit cbe91a4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { PropTypes } from 'react';
2+
import { Link } from 'react-router-dom';
3+
4+
function hashLinkScroll() {
5+
// Push onto callback queue so it runs after the DOM is updated
6+
setTimeout(() => {
7+
const { hash } = window.location;
8+
if (hash !== '') {
9+
const id = hash.replace('#', '');
10+
const element = document.getElementById(id);
11+
if (element) element.scrollIntoView();
12+
}
13+
}, 0);
14+
}
15+
16+
export function HashLink(props) {
17+
function handleClick(e) {
18+
if (props.onClick) props.onClick(e);
19+
hashLinkScroll();
20+
}
21+
return <Link {...props} onClick={handleClick}>{props.children}</Link>;
22+
}
23+
24+
HashLink.PropTypes = {
25+
onClick: PropTypes.func,
26+
children: PropTypes.node,
27+
};

0 commit comments

Comments
 (0)