Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce children function support #120

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ import Sticky from 'react-stickynode';

### Handling State Change

You can be notified when the state of the sticky component changes by passing a callback to the `onStateChange` prop. The callback will receive an object in the format `{status: CURRENT_STATUS}`, with `CURRENT_STATUS` being an integer representing the status:
You can be notified when the state of the sticky component changes by passing a callback to the `onStateChange` prop. The callback will receive an object in the format `{status: CURRENT_STATUS}`, with `CURRENT_STATUS` being an integer representing the status:

- 0 (STATUS_ORIGINAL) - The default status, located at the original position.
- 1 (STATUS_RELEASED) - The released status, located at somewhere on document, but not default one.
@@ -81,9 +81,27 @@ const handleStateChange = (status) => {
</Sticky>
```

### Freezing
Also `Sticky` supports children functions:

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.
```js
import Sticky from 'react-stickynode';

<Sticky>
{status => {
if (status.status === Sticky.STATUS_FIXED) {
return 'the component is sticky';
}
if (status.status === Sticky.STATUS_ORIGINAL) {
return 'the component in the original position';
}
return 'the component is released'
}}
</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

4 changes: 3 additions & 1 deletion src/Sticky.jsx
Original file line number Diff line number Diff line change
@@ -380,10 +380,12 @@ class Sticky extends Component {
[this.props.releasedClass]: this.state.status === STATUS_RELEASED
})

var children = this.props.children;

return (
<div ref={(outer) => { this.outerElement = outer; }} className={outerClasses} style={outerStyle}>
<div ref={(inner) => { this.innerElement = inner; }} className='sticky-inner-wrapper' style={innerStyle}>
{this.props.children}
{typeof children === 'function' ? children({ status: this.state.status }) : children}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass the state into the children instead of the status only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure other things from the component state should be available to children, also onStateChange passes status only too https://github.com/yahoo/react-stickynode/blob/master/src/Sticky.jsx#L290-L293

</div>
</div>
);
16 changes: 16 additions & 0 deletions tests/unit/Sticky-test.js
Original file line number Diff line number Diff line change
@@ -171,6 +171,22 @@ describe('Sticky', function () {
sinon.assert.calledWith(callback.secondCall, {status: Sticky.STATUS_ORIGINAL});
});

it('should call the children function on state change', function () {
var childrenStub = sinon.stub().returns(null);
jsx.renderComponent(Sticky, { children: childrenStub });

sinon.assert.notCalled(childrenStub);

// Scroll down to 10px, and status should change to FIXED
window.scrollTo(0, 10);
sinon.assert.calledWith(childrenStub, {status: Sticky.STATUS_FIXED});

// Scroll up to 0px, and Sticky should reset
window.scrollTo(0, 0);
sinon.assert.calledTwice(childrenStub);
sinon.assert.calledWith(childrenStub.secondCall, {status: Sticky.STATUS_ORIGINAL});
});

it('should work as expected (long Sticky)', function () {
STICKY_HEIGHT = 1200;
sticky = jsx.renderComponent(Sticky);