Skip to content

Commit 4ef8760

Browse files
Add threshold Intersection observer options as prop
1 parent e9f2b33 commit 4ef8760

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Toggle prop saves GPU and battery by stopping the video when no longer in view!
7272
| `repeatOnInView` | boolean | Repeats callbacks for onInView/onNotInView, rather than firing just the first time. `default: false` |
7373
| `intoViewMargin` | string - css margin | Margin added to viewport for area to consider “in view”, can be negative. Use f.e. with positive value for lazy loading content just before in view, or with negative to start fading in element just after in view. Must be `px` or `%`. Default: ‘-20%’. |
7474
| `useInviewMonitor` | func | Convenient function that can be used to dynamically disable the monitor, for example for mobile devices. |
75+
| `threshold` | number | Number which indicate at what percentage of the target's visibility the observer's callback should be executed. |
7576

7677
## Contributing
7778

src/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class InviewMonitor extends Component {
1919
Perhaps use a polyfill like: https://cdn.polyfill.io/v2/polyfill.js?features=IntersectionObserver ?`)
2020
return
2121
}
22-
const { useInviewMonitor, intoViewMargin } = this.props
22+
const { useInviewMonitor, intoViewMargin, threshold } = this.props
2323
if (
2424
!this._element ||
2525
!useInviewMonitor ||
@@ -28,7 +28,8 @@ Perhaps use a polyfill like: https://cdn.polyfill.io/v2/polyfill.js?features=Int
2828
return
2929
}
3030
const options = {
31-
rootMargin: intoViewMargin
31+
rootMargin: intoViewMargin,
32+
threshold
3233
}
3334
// any performance benefits from trying to re-use the observer?
3435
// possible enhancement to add later on.
@@ -195,13 +196,15 @@ InviewMonitor.propTypes = {
195196
// because easier to toggle this prop, then toggle not using the component at all.
196197
useInviewMonitor: PropTypes.func,
197198

198-
intoViewMargin: PropTypes.string
199+
intoViewMargin: PropTypes.string,
200+
threshold: PropTypes.number
199201
}
200202
InviewMonitor.defaultProps = {
201203
classNameNotInView: '',
202204
childPropsNotInView: {},
203205
useInviewMonitor: () => true,
204-
intoViewMargin: '-20%'
206+
intoViewMargin: '-20%',
207+
threshold: 0
205208
}
206209

207210
export default InviewMonitor

0 commit comments

Comments
 (0)