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
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -81,9 +81,33 @@ 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';

const handleStateChange = (status) => {
if (status.status === Sticky.STATUS_FIXED) {
console.log('the component is sticky');
}
}

<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

Expand Down
4 changes: 3 additions & 1 deletion src/Sticky.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
);
Expand Down