Skip to content

Commit 19b8862

Browse files
authored
Merge pull request #28 from WilliamHolmes/master
Add Option to always observe visibility
2 parents 8809788 + cc9ba1a commit 19b8862

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ It allow to pass config options as param (optional).
5858
{
5959
root: null,
6060
rootMargin: '0px',
61-
threshold: 1.0
61+
threshold: 1.0,
62+
always: false
6263
}
6364
```
6465

@@ -70,16 +71,28 @@ import { useVisibilityHook } from 'react-observer-api';
7071
export default () => {
7172
const { setElement, isVisible } = useVisibilityHook({
7273
threshold: 0.5,
73-
rootMargin: '100px'
74+
rootMargin: '100px',
75+
always: false
7476
});
7577
...
7678
}
7779
```
7880

81+
#### Always Observe
82+
83+
For some cases, you may want to continue to observe the dom node as it enters and exits the viewport. In this scenario, passing ```always: true``` in the config will enable this.
84+
85+
```jsx
86+
useVisibilityHook({ always: true });
87+
88+
<LazyLoad config={{ always: true }}>
89+
```
90+
7991
#### Force Visible
8092

8193
For some case, based on condition/logic may need to show the dom before it reaches to viewport. In that scenario, by calling ```forceVisible()``` will load the dom.
8294

95+
8396
```jsx
8497
import { useVisibilityHook } from 'react-observer-api';
8598

@@ -122,7 +135,7 @@ export default () => {
122135
#### Optional Props
123136
| prop | Type | Default | Description |
124137
| ------------- |-----| -----| ----------- |
125-
| options | object | { root: null, threshold: 0.25, rootMargin: '-10px' } | [Click for more usage about options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_concepts_and_usage)|
138+
| options | object | { root: null, threshold: 0.25, rootMargin: '-10px', always: false } | [Click for more usage about options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_concepts_and_usage)|
126139
| as | string | div | Wrapper element can be change by passing valid tag name. Ex: span / p / div |
127140
| style | object | {} | Custom CSS for wrapper element|
128141
| forceVisible | boolean | false | Passing true to render dom without waiting to reach the viewport|

src/useVisibilityHook.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { useState, useEffect } from 'react';
33
const defaultConfig = {
44
root: null,
55
threshold: 1.0,
6-
rootMargin: '0px'
6+
rootMargin: '0px',
7+
always: false
78
};
89

910
const useVisibilityHook = (options = {}, visible = false) => {
@@ -21,7 +22,9 @@ const useVisibilityHook = (options = {}, visible = false) => {
2122
};
2223

2324
const visibilityCallBack = ([entry]) => {
24-
if (entry.isIntersecting) {
25+
if (options.always) {
26+
setIsVisible(entry.isIntersecting);
27+
} else if (entry.isIntersecting) {
2528
setIsVisible(true);
2629
observer.disconnect();
2730
}
@@ -48,4 +51,4 @@ const useVisibilityHook = (options = {}, visible = false) => {
4851
return { setElement, isVisible, forceVisible, forceCheck };
4952
}
5053

51-
export default useVisibilityHook;
54+
export default useVisibilityHook;

0 commit comments

Comments
 (0)