@@ -7,46 +7,40 @@ export default class Scrollchor extends React.Component {
7
7
super ( props ) ;
8
8
this . _to = props . to && props . to . replace ( / ^ # / , '' ) || '' ;
9
9
const {
10
- offset = 0 , duration = 400 , easing = easeOutQuad ,
11
- beforeHook = ( ) => { } ,
12
- afterHook = ( ) => { }
10
+ offset = 0 , duration = 400 , easing = easeOutQuad
13
11
} = props . animate || { } ;
14
- this . _beforeHook = beforeHook ;
15
- this . _afterHook = afterHook ;
16
12
this . _animate = { offset, duration, easing } ;
13
+ this . _beforeAnimate = props . beforeAnimate || function ( ) { } ;
14
+ this . _afterAnimate = props . afterAnimate || function ( ) { } ;
17
15
}
18
16
19
17
static propTypes = {
20
18
to : PropTypes . string . isRequired ,
21
19
animate : PropTypes . shape ( {
22
20
offset : PropTypes . number ,
23
21
duration : PropTypes . number ,
24
- easing : PropTypes . func ,
25
- beforeHook : PropTypes . func ,
26
- afterHook : PropTypes . func
27
- } )
22
+ easing : PropTypes . func
23
+ } ) ,
24
+ beforeAnimate : PropTypes . func ,
25
+ afterAnimate : PropTypes . func
28
26
} ;
29
27
30
28
handleClick = ( event ) => {
31
- this . _beforeHook ( event ) ;
32
-
29
+ this . _beforeAnimate ( event ) ;
33
30
event . preventDefault ( ) ;
34
31
animateScroll ( this . _to , this . _animate ) ;
35
-
36
- this . _afterHook ( event ) ;
32
+ this . _afterAnimate ( event ) ;
37
33
}
38
34
39
35
render ( ) {
40
- const { to, animate, ...props } = this . props ; // eslint-disable-line no-unused-vars
36
+ const { to, animate, beforeAnimate , afterAnimate , ...props } = this . props ; // eslint-disable-line no-unused-vars
41
37
return < a { ...props } href = { '#' + this . _to } onClick = { this . handleClick } /> ;
42
38
}
43
39
}
44
40
45
41
function animateScroll ( id , animate ) {
46
42
const element = id ? document . getElementById ( id ) : document . body ;
47
-
48
43
warning ( element , `Cannot find element: #${ id } ` ) ;
49
-
50
44
scrollTo ( element , animate ) ;
51
45
}
52
46
@@ -59,9 +53,7 @@ function scrollTo(element, { offset, duration, easing }) {
59
53
function animate ( elapsedTime ) {
60
54
const elapsed = elapsedTime + increment ;
61
55
const position = easing ( null , elapsed , start , change , duration ) ;
62
-
63
56
setScrollTop ( position ) ;
64
-
65
57
if ( elapsed < duration ) {
66
58
setTimeout ( function ( ) {
67
59
animate ( elapsed ) ;
0 commit comments