-
Notifications
You must be signed in to change notification settings - Fork 195
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
allow passing a children function that takes state and chooses what to render from it #76
Conversation
Nice work! We've been discussing this over at #74 |
visibility-sensor.js
Outdated
} | ||
|
||
return state; | ||
}, | ||
|
||
render: function () { | ||
if (this.props.children instanceof Function) return this.props.children(this.state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would destructure this.state
here to return explicit values. Prevents leaking of internal state details to the render callback in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neeharv agreed.
@jedwards1211 what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated it
@@ -66,6 +77,7 @@ Props | |||
- `resizeThrottle`: (default: `-1`) by specifying a value > -1, you are enabling throttle instead of the delay to trigger checks on resize event. Throttle supercedes delay. | |||
- `containment`: (optional) element to use as a viewport when checking visibility. Default behaviour is to use the browser window as viewport. | |||
- `delayedCall`: (default `false`) if is set to true, wont execute on page load ( prevents react apps triggering elements as visible before styles are loaded ) | |||
- `children`: can be a React element or a function. If you provide a function, it will be called with 1 argument `{isVisible: ?boolean, visibilityRect: Object}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use case for passing visibilityRect
to the render callback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's useful for determining partial visibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neeharv why not? Libs like react-virtualized
only render subcomponents that are within their own visibility rectangle, so I figured having visibilityRect
in the render callback could be useful.
Thanks @jedwards1211 I really like the proposal here - thanks for your patience while I found some time to review it :) Looks great and I like the simplicity it introduces. @neeharv anything else you think we ought to do here before merging? |
package.json
Outdated
@@ -36,8 +36,8 @@ | |||
"karma-mocha": "^0.1.9", | |||
"karma-phantomjs-launcher": "^0.1.4", | |||
"mocha": "^1.21.4", | |||
"react": "^0.14.0 || ^15.0.0", | |||
"react-dom": "^0.14.0 || ^15.0.0", | |||
"react": "^15.5.4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to update deps in this PR? Unsure of knock on effects. Maybe best left to keep the scope of this to just the code change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made that change because I don't think it's really appropriate to ||
multiple versions together in devDependencies
. I only do that in peerDependencies
. But it's true, we could leave this change out of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, reverted
Hey guys, the build errored saying that it couldn't find |
@jedwards1211 I think it's due to the deprecation warning shown here: #80 Please try rebasing and then we should be good to merge. |
@joshwnj oh. You might want to try using |
74eb48e
to
9b8c306
Compare
Okay, this should be ready! |
@jedwards1211 good call. There's a rewrite in motion, so I think we'll hold off adding greenkeeper until then. |
Published to npm v3.10.0. Thanks for your work on this @jedwards1211 ! And @neeharv for the reviews! |
Sweet, wow, that was fast! |
This is the number one feature I've wanted in
react-visibility-sensor
for a long time. It's a pattern I first saw inreact-motion
that I've really come to like. I also got the creator ofreact-measure
to add this pattern to his package. Here's what it looks like:This is super convenient if all you need to do is change what you render inside the component based upon whether it's visible, because you don't have to go to the trouble of storing the visibility state anywhere in your own code.
This PR doesn't cause any breaking changes; you can still use a React element for the children, and a children function can be used together with
onChange
or without it -- I madeonChange
optional.