Skip to content

Commit dbbfb9c

Browse files
committed
change to props beforeAnimate/afterAnimate callbacks
1 parent 4ab1776 commit dbbfb9c

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## master (unreleased)
22

3+
## 2.2.0
4+
5+
- Add beforeAnimate/afterAnimate hooks to scroll handler
6+
37
## 2.1.3
48

59
- Fix incorrect passed props to tag

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,24 @@ export default (props) => (
7676
* - this prop is required
7777
* - let it blank, `to = ''`, for scroll to page top
7878
*/
79-
to: PropTypes.string.isRequired
79+
to: PropTypes.string.isRequired,
8080

8181
/**
8282
* scroll smooth animation can be customize
8383
* Accepted options:
8484
* { offset: 0, duration: 400, easing: easeOutQuad }
8585
*/
86-
animate: PropTypes.object
86+
animate: PropTypes.object,
87+
88+
/**
89+
* callback function triggered before scroll to #hash
90+
*/
91+
beforeAnimate: PropTypes.func,
92+
93+
/**
94+
* callback function triggered after scroll to #hash
95+
*/
96+
afterAnimate: PropTypes.func
8797
}
8898
```
8999
@@ -107,6 +117,12 @@ This setting is equivalent to default jQuery.animate `easing: swing`
107117
* [Robert Penner's Easing Functions](http://robertpenner.com/easing/)
108118
* [Javascript source code](https://github.com/danro/jquery-easing/blob/master/jquery.easing.js)
109119
120+
## `before` and `after` animate callbacks
121+
Use this callbacks to trigger behaviours like, for example, update state, load async stuffs, etc.
122+
```javascript
123+
<Scrollchor to="#aboutus" afterAnimate={() => updateState(this)}>Home</Scrollchor>
124+
```
125+
110126
## Example
111127
112128
[react-scrollchor--example](https://github.com/bySabi/react-scrollchor/tree/example)

src/scrollchor.jsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,40 @@ export default class Scrollchor extends React.Component {
77
super(props);
88
this._to = props.to && props.to.replace(/^#/, '') || '';
99
const {
10-
offset = 0, duration = 400, easing = easeOutQuad,
11-
beforeHook = () => {},
12-
afterHook = () => {}
10+
offset = 0, duration = 400, easing = easeOutQuad
1311
} = props.animate || {};
14-
this._beforeHook = beforeHook;
15-
this._afterHook = afterHook;
1612
this._animate = { offset, duration, easing };
13+
this._beforeAnimate = props.beforeAnimate || function() {};
14+
this._afterAnimate = props.afterAnimate || function() {};
1715
}
1816

1917
static propTypes = {
2018
to: PropTypes.string.isRequired,
2119
animate: PropTypes.shape({
2220
offset: PropTypes.number,
2321
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
2826
};
2927

3028
handleClick = (event) => {
31-
this._beforeHook(event);
32-
29+
this._beforeAnimate(event);
3330
event.preventDefault();
3431
animateScroll(this._to, this._animate);
35-
36-
this._afterHook(event);
32+
this._afterAnimate(event);
3733
}
3834

3935
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
4137
return <a {...props} href={'#' + this._to} onClick={this.handleClick} />;
4238
}
4339
}
4440

4541
function animateScroll(id, animate) {
4642
const element = id ? document.getElementById(id) : document.body;
47-
4843
warning(element, `Cannot find element: #${id}`);
49-
5044
scrollTo(element, animate);
5145
}
5246

@@ -59,9 +53,7 @@ function scrollTo(element, { offset, duration, easing }) {
5953
function animate(elapsedTime) {
6054
const elapsed = elapsedTime + increment;
6155
const position = easing(null, elapsed, start, change, duration);
62-
6356
setScrollTop(position);
64-
6557
if (elapsed < duration) {
6658
setTimeout(function() {
6759
animate(elapsed);

0 commit comments

Comments
 (0)