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

allow passing a children function that takes state and chooses what to render from it #76

Merged
merged 4 commits into from
Jun 7, 2017

Conversation

jedwards1211
Copy link
Contributor

@jedwards1211 jedwards1211 commented Apr 20, 2017

This is the number one feature I've wanted in react-visibility-sensor for a long time. It's a pattern I first saw in react-motion that I've really come to like. I also got the creator of react-measure to add this pattern to his package. Here's what it looks like:

  <VisibilitySensor>
    {({isVisible}) =>
      <div>I am {isVisible ? 'visible' : 'invisible'}</div>
    }
  </VisibilitySensor>

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 made onChange optional.

@neeharv
Copy link
Collaborator

neeharv commented Apr 20, 2017

Nice work! We've been discussing this over at #74

}

return state;
},

render: function () {
if (this.props.children instanceof Function) return this.props.children(this.state);
Copy link
Collaborator

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.

Copy link
Owner

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@neeharv @joshwnj yeah, makes sense

Copy link
Contributor Author

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}`
Copy link
Collaborator

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?

Copy link
Owner

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

Copy link
Contributor Author

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.

@joshwnj
Copy link
Owner

joshwnj commented Jun 6, 2017

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",
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, reverted

@jedwards1211
Copy link
Contributor Author

Hey guys, the build errored saying that it couldn't find uglifyjs. I'm confused because I don't see how that could have changed. Do you know why that would be?

@joshwnj
Copy link
Owner

joshwnj commented Jun 7, 2017

@jedwards1211 I think it's due to the deprecation warning shown here: #80

Please try rebasing and then we should be good to merge.

@jedwards1211
Copy link
Contributor Author

@joshwnj oh. You might want to try using yarn for the CI build so you can have a lockfile; then you would only see build failures like this when you explicitly choose to upgrade a dependency (or have something like Greenkeeper automatically PR dep uprades)

@jedwards1211
Copy link
Contributor Author

Okay, this should be ready!

@joshwnj
Copy link
Owner

joshwnj commented Jun 7, 2017

something like Greenkeeper automatically PR dep uprades

@jedwards1211 good call. There's a rewrite in motion, so I think we'll hold off adding greenkeeper until then.

@joshwnj joshwnj merged commit 9c11db6 into joshwnj:master Jun 7, 2017
@joshwnj
Copy link
Owner

joshwnj commented Jun 7, 2017

Published to npm v3.10.0.

Thanks for your work on this @jedwards1211 ! And @neeharv for the reviews!

@jedwards1211
Copy link
Contributor Author

Sweet, wow, that was fast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants