Skip to content

Commit

Permalink
feat(components): Add retainOnceInViewport prop to Viewport.WithPlace…
Browse files Browse the repository at this point in the history
…holder (#111)
  • Loading branch information
Blagoja Evkoski authored Jul 16, 2018
1 parent 1421a46 commit c88a826
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit c88a826

Please sign in to comment.