File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 1
1
import React , { PropTypes } from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
4
- function hashLinkScroll ( ) {
4
+ function hashLinkScroll ( hashFragment ) {
5
5
// Push onto callback queue so it runs after the DOM is updated
6
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
- }
7
+ const element = document . getElementById ( hashFragment ) ;
8
+ if ( element ) element . scrollIntoView ( ) ;
13
9
} , 0 ) ;
14
10
}
15
11
16
12
export function HashLink ( props ) {
17
13
function handleClick ( e ) {
18
14
if ( props . onClick ) props . onClick ( e ) ;
19
- hashLinkScroll ( ) ;
15
+ let hashFragment = '' ;
16
+ if ( typeof props . to === 'string' ) {
17
+ hashFragment = props . to . split ( '#' ) . slice ( 1 ) . join ( '#' ) ;
18
+ } else if ( typeof props . to === 'object' && props . to . hash ) {
19
+ hashFragment = props . to . hash . replace ( '#' , '' ) ;
20
+ }
21
+ if ( hashFragment ) hashLinkScroll ( hashFragment ) ;
20
22
}
21
23
return < Link { ...props } onClick = { handleClick } > { props . children } </ Link > ;
22
24
}
23
25
24
26
HashLink . PropTypes = {
25
27
onClick : PropTypes . func ,
26
28
children : PropTypes . node ,
29
+ to : PropTypes . oneOfType ( [
30
+ PropTypes . string ,
31
+ PropTypes . object ,
32
+ ] ) ,
27
33
} ;
You can’t perform that action at this time.
0 commit comments