Skip to content

Commit 04c3ccc

Browse files
committed
add disableHistory pro
1 parent cf897b6 commit 04c3ccc

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-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+
## 4.1.0
4+
5+
- Add `disableHistory` prop for enable/disable update browser history with scroll behaviours. Default is `false`
6+
37
## 4.0.0
48

59
- Add support to changeable props. All props become responsive

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ export default (props) => (
9595
* @param1 Received click event
9696
*/
9797
afterAnimate: PropTypes.func
98+
99+
/**
100+
* enable/disable update browser history with scroll behaviours
101+
* Default to `false`
102+
*/
103+
disableHistory: PropTypes.bool
98104
}
99105
```
100106
### Reactive `props`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-scrollchor",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "A React component for scroll to #hash links with smooth animations",
55
"files": [
66
"lib"

src/scrollchor.jsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { animateScroll } from './utils';
3+
import { animateScroll, updateHistory } from './utils';
44

55
export default class Scrollchor extends React.Component {
6-
constructor(props) {
6+
constructor (props) {
77
super(props);
88
this._setup(props);
99
this.simulateClick = this._handleClick;
@@ -17,8 +17,9 @@ export default class Scrollchor extends React.Component {
1717
easing: PropTypes.func
1818
}),
1919
beforeAnimate: PropTypes.func,
20-
afterAnimate: PropTypes.func
21-
};
20+
afterAnimate: PropTypes.func,
21+
disableHistory: PropTypes.bool
22+
}
2223

2324
_setup = props => {
2425
this._to = (props.to && props.to.replace(/^#/, '')) || '';
@@ -30,23 +31,25 @@ export default class Scrollchor extends React.Component {
3031
} =
3132
props.animate || {};
3233
this._animate = { offset, duration, easing };
33-
this._beforeAnimate = props.beforeAnimate || function() {};
34-
this._afterAnimate = props.afterAnimate || function() {};
35-
};
34+
this._beforeAnimate = props.beforeAnimate || function () {};
35+
this._afterAnimate = props.afterAnimate || function () {};
36+
this._disableHistory = props.disableHistory;
37+
}
3638

3739
_handleClick = event => {
3840
this._beforeAnimate(event);
3941
event && event.preventDefault();
40-
animateScroll(this._to, this._animate);
42+
const id = animateScroll(this._to, this._animate);
43+
this._disableHistory || updateHistory(id);
4144
this._afterAnimate(event);
42-
};
45+
}
4346

44-
componentWillReceiveProps(props) {
47+
componentWillReceiveProps (props) {
4548
this._setup(props);
4649
}
4750

48-
render() {
49-
const { to, animate, beforeAnimate, afterAnimate, ...props } = this.props; // eslint-disable-line no-unused-vars
51+
render () {
52+
const { to, animate, beforeAnimate, afterAnimate, disableHistory, ...props } = this.props; // eslint-disable-line no-unused-vars
5053

5154
return !this.props.children
5255
? null
@@ -56,6 +59,6 @@ export default class Scrollchor extends React.Component {
5659

5760
// Default easing function
5861
// jQuery easing 'swing'
59-
function easeOutQuad(x, t, b, c, d) {
62+
function easeOutQuad (x, t, b, c, d) {
6063
return -c * (t /= d) * (t - 2) + b;
6164
}

src/utils.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warning from 'fbjs/lib/warning';
22

3-
export function animateScroll(id, animate) {
3+
export function animateScroll (id, animate) {
44
const element = id ? document.getElementById(id) : document.body;
55
warning(element, `Cannot find element: #${id}`);
66

@@ -9,31 +9,35 @@ export function animateScroll(id, animate) {
99
const to = getOffsetTop(element) + offset;
1010
const change = to - start;
1111

12-
function animateFn(elapsedTime = 0) {
12+
function animateFn (elapsedTime = 0) {
1313
const increment = 20;
1414
const elapsed = elapsedTime + increment;
1515
const position = easing(null, elapsed, start, change, duration);
1616
setScrollTop(position);
1717
elapsed < duration &&
18-
setTimeout(function() {
18+
setTimeout(function () {
1919
animateFn(elapsed);
2020
}, increment);
2121
}
2222

2323
animateFn();
24+
return id;
25+
}
26+
27+
export function updateHistory (id) {
2428
window.location.hash = id;
2529
}
2630

27-
function getScrollTop() {
31+
function getScrollTop () {
2832
// like jQuery -> $('html, body').scrollTop
2933
return document.documentElement.scrollTop || document.body.scrollTop;
3034
}
3135

32-
function setScrollTop(position) {
36+
function setScrollTop (position) {
3337
document.documentElement.scrollTop = document.body.scrollTop = position;
3438
}
3539

36-
function getOffsetTop(element) {
40+
function getOffsetTop (element) {
3741
const { top } = element.getBoundingClientRect();
3842
return top + getScrollTop();
3943
}

0 commit comments

Comments
 (0)