Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/AvraamMavridis/react-intersection-visible-hook

React Hook to track the visibility of a functional component
https://github.com/AvraamMavridis/react-intersection-visible-hook

hook intersectionobserver react visibility

Last synced: about 2 months ago
JSON representation

React Hook to track the visibility of a functional component

Lists

README

        

# react-intersection-visible-hook

React hook to track the visibility of a functional component based on IntersectionVisible Observer.

In the following example we changed the background color of the body depending on the visibility of the blue box.

*Demo and tests are coming*

```
import useVisibility from 'react-intersection-visible-hook'
```

### How to use it

```js
function App() {
const nodeRef = useRef(null);
const visibility = useVisibility(nodeRef);

return (


Hello



);
}
```

##### With options

```js
const options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}

function App() {
const nodeRef = useRef(null);
const visibility = useVisibility(nodeRef, options);

return (


Hello


{visibility.isIntersecting ? 'Component is visible' : 'Component is hidden' }



);
}
```

The `visibility` object contains

```
boundingClientRect
intersectionRatio
intersectionRect
isIntersecting
rootBounds
target
time
```

### Contribute

Any pull-request is more than welcome :boom: :smile:

### License

MIT