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

feat(components): Add retainOnceInViewport prop to Viewport.WithPlaceholder #111

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion packages/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ render() {

### With Placeholder

A higher-order component that can be used to display a placeholder while the component is not in the viewport.
A higher-order component that can be used to display a placeholder while the wrapped component is not in the viewport.
This can improve user experience since it can serve as a mechanism for lazy loading.

#### Usage
Expand All @@ -84,6 +84,7 @@ render() {
// placeholder={Placeholder} // passing down a placeholder at render time
source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }}
preTriggerRatio={0.5}
retainOnceInViewport={true}
style={{ width: 50, height: 50 }} />
)
}
Expand All @@ -94,3 +95,4 @@ render() {
| Prop | Description | Default |
|---|---|---|
|**`placeholder`**| Useful for passing down a placeholder at render time. | `null` |
|**`retainOnceInViewport`**| Whether to keep the wrapped component displayed once it enters the viewport. | `false` |
12 changes: 12 additions & 0 deletions packages/components/src/viewport/withPlaceholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ export default (WrappedComponent, PlaceholderComponent) => {
super(props, context)
}

componentDidUpdate() {
if (this.props.inViewport && !this._hasEnteredViewport) {
this._hasEnteredViewport = true
}
}

render() {
if (this.props.inViewport) {
return <WrappedComponent {...this.props} />
}

if (this.props.retainOnceInViewport && this._hasEnteredViewport) {
return <WrappedComponent {...this.props} />
}

return this.props.placeholder ? (
<this.props.placeholder />
) : PlaceholderComponent ? (
Expand All @@ -23,6 +34,7 @@ export default (WrappedComponent, PlaceholderComponent) => {
static propTypes = {
inViewport: PropTypes.bool.isRequired,
placeholder: PropTypes.func,
retainOnceInViewport: PropTypes.bool,
}

static displayName = `WithPlaceholder(${WrappedComponent.displayName ||
Expand Down