Skip to content

Commit

Permalink
add disableHistory pro
Browse files Browse the repository at this point in the history
  • Loading branch information
bySabi committed Nov 24, 2017
1 parent cf897b6 commit 04c3ccc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master (unreleased)

## 4.1.0

- Add `disableHistory` prop for enable/disable update browser history with scroll behaviours. Default is `false`

## 4.0.0

- Add support to changeable props. All props become responsive
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export default (props) => (
* @param1 Received click event
*/
afterAnimate: PropTypes.func

/**
* enable/disable update browser history with scroll behaviours
* Default to `false`
*/
disableHistory: PropTypes.bool
}
```
### Reactive `props`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-scrollchor",
"version": "4.0.0",
"version": "4.1.0",
"description": "A React component for scroll to #hash links with smooth animations",
"files": [
"lib"
Expand Down
29 changes: 16 additions & 13 deletions src/scrollchor.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import { animateScroll } from './utils';
import { animateScroll, updateHistory } from './utils';

export default class Scrollchor extends React.Component {
constructor(props) {
constructor (props) {
super(props);
this._setup(props);
this.simulateClick = this._handleClick;
Expand All @@ -17,8 +17,9 @@ export default class Scrollchor extends React.Component {
easing: PropTypes.func
}),
beforeAnimate: PropTypes.func,
afterAnimate: PropTypes.func
};
afterAnimate: PropTypes.func,
disableHistory: PropTypes.bool
}

_setup = props => {
this._to = (props.to && props.to.replace(/^#/, '')) || '';
Expand All @@ -30,23 +31,25 @@ export default class Scrollchor extends React.Component {
} =
props.animate || {};
this._animate = { offset, duration, easing };
this._beforeAnimate = props.beforeAnimate || function() {};
this._afterAnimate = props.afterAnimate || function() {};
};
this._beforeAnimate = props.beforeAnimate || function () {};
this._afterAnimate = props.afterAnimate || function () {};
this._disableHistory = props.disableHistory;
}

_handleClick = event => {
this._beforeAnimate(event);
event && event.preventDefault();
animateScroll(this._to, this._animate);
const id = animateScroll(this._to, this._animate);
this._disableHistory || updateHistory(id);
this._afterAnimate(event);
};
}

componentWillReceiveProps(props) {
componentWillReceiveProps (props) {
this._setup(props);
}

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

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

// Default easing function
// jQuery easing 'swing'
function easeOutQuad(x, t, b, c, d) {
function easeOutQuad (x, t, b, c, d) {
return -c * (t /= d) * (t - 2) + b;
}
16 changes: 10 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warning from 'fbjs/lib/warning';

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

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

function animateFn(elapsedTime = 0) {
function animateFn (elapsedTime = 0) {
const increment = 20;
const elapsed = elapsedTime + increment;
const position = easing(null, elapsed, start, change, duration);
setScrollTop(position);
elapsed < duration &&
setTimeout(function() {
setTimeout(function () {
animateFn(elapsed);
}, increment);
}

animateFn();
return id;
}

export function updateHistory (id) {
window.location.hash = id;
}

function getScrollTop() {
function getScrollTop () {
// like jQuery -> $('html, body').scrollTop
return document.documentElement.scrollTop || document.body.scrollTop;
}

function setScrollTop(position) {
function setScrollTop (position) {
document.documentElement.scrollTop = document.body.scrollTop = position;
}

function getOffsetTop(element) {
function getOffsetTop (element) {
const { top } = element.getBoundingClientRect();
return top + getScrollTop();
}

0 comments on commit 04c3ccc

Please sign in to comment.