Skip to content

Commit

Permalink
Merge pull request #38 from yahoo/freeze
Browse files Browse the repository at this point in the history
Adding support for shouldFreeze hook
  • Loading branch information
src-code committed May 24, 2016
2 parents 9c5b693 + 23d53d3 commit 24082b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var Sticky = require('react-stickynode');
- `enableTransforms {Boolean}` - Enable the use of CSS3 transforms (true by default).
- `activeClass {String}` - Class name to be applied to the element when the sticky state is active (`active` by default).
- `onStateChange {Function}` - Callback for when the sticky state changes. See below.
- `shouldFreeze {Function}` - Callback to indicate when the sticky plugin should freeze position and ignore scroll/resize events. See below.

### Handling State Change

Expand All @@ -78,6 +79,10 @@ const handleStateChange = (status) => {
</Sticky>
```

### Freezing

You can provide a function in the `shouldFreeze` prop which will tell the component to temporarily stop updating during prop and state changes, as well as ignore scroll and resize events. This function should return a boolean indicating whether the component should currently be frozen.

## Install & Development

**Install**
Expand Down
21 changes: 19 additions & 2 deletions src/Sticky.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Sticky extends Component {
this.delta = 0;
this.stickyTop = 0;
this.stickyBottom = 0;
this.frozen = false;

this.bottomBoundaryTarget;
this.topTarget;
Expand Down Expand Up @@ -170,17 +171,31 @@ class Sticky extends Component {
}

handleResize (e, ae) {
if (this.props.shouldFreeze()) {
return;
}

winHeight = ae.resize.height;
this.updateInitialDimension();
this.update();
}

handleScrollStart (e, ae) {
this.frozen = this.props.shouldFreeze();

if (this.frozen) {
return;
}

scrollTop = ae.scroll.top;
this.updateInitialDimension();
}

handleScroll (e, ae) {
if (this.frozen) {
return;
}

scrollDelta = ae.scroll.delta;
scrollTop = ae.scroll.top;
this.update();
Expand Down Expand Up @@ -337,7 +352,7 @@ class Sticky extends Component {
}

shouldComponentUpdate (nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
return !this.props.shouldFreeze() && (!isEqual(this.props, nextProps) || !isEqual(this.state, nextState));
}

render () {
Expand Down Expand Up @@ -369,6 +384,7 @@ class Sticky extends Component {
Sticky.displayName = 'Sticky';

Sticky.defaultProps = {
shouldFreeze: function () { return false; },
enabled: true,
top: 0,
bottomBoundary: 0,
Expand Down Expand Up @@ -397,7 +413,8 @@ Sticky.propTypes = {
]),
enableTransforms: PropTypes.bool,
activeClass: PropTypes.string,
onStateChange: PropTypes.func
onStateChange: PropTypes.func,
shouldFreeze: PropTypes.func
};

Sticky.STATUS_ORIGINAL = STATUS_ORIGINAL;
Expand Down

0 comments on commit 24082b9

Please sign in to comment.