11# React viewport detect
2- This is a React hook easy to use. This hook allow you to know if an element is present in the current viewport.
3- It returns you a boolean. The boolean will be set at true when the element appear on the viewport but only once.
2+ A React hook to detect if an HTML Element has been display in the viewport.
3+ This is easy to use. You just have to provide an react ref.
4+ By default it freeze the value when the element has been display.
5+ That is nice to triggered one time animation !
46
57[ ![ NPM Version] ( https://badgen.net/npm/v/react-viewport-detect )] ( https://www.npmjs.com/package/react-mui-snackbar )
6- ![ Installed size] ( https://badgen.net/packagephobia/install/react-viewport-detect )
8+ ![ Minified size] ( https://img.shields.io/bundlephobia/min/react-viewport-detect )
9+ [ ![ GPLv3 License] ( https://img.shields.io/badge/License-GPL%20v3-yellow.svg )] ( https://opensource.org/licenses/ )
710
8- ## Installation
9- ``` bash
10- npm i react-viewport-detect
11- ```
1211
1312## Usage
14- To use this package you need a React ref.
15- By default the value is 'False'. Once the ref is present in the VP the boolean will be set at true.
1613
17- ```` jsx
18- import useVisible from " react-viewport-detect" ;
19- import {ref } from " react" ;
14+ ### Basic Usage
15+ ``` jsx
16+ import {useViewportDetection } from " react-viewport-detect" ;
17+ import {useRef , useEffect } from " react" ;
2018
2119const App () = > {
2220
23- const ref = ref () ;
24- const isVisible = useVisible (ref) ;
21+ const ref = useRef () ;
22+ const isVisible = useViewportDetection (ref) ;
23+
24+ useEffect (() => {
25+ console .log (isVisible) ;
26+ },[isVisible]) ;
27+
28+ return (
29+ < div>
30+ < h1> React visible hook< / h1>
31+ < div ref= {ref} >
32+ < p>
33+ Do I am visible ?
34+ < / p>
35+ < / div>
36+ < / div>
37+ );
38+
39+ };
40+ ` ` `
41+
42+ ### Advanced
43+ ` ` ` jsx
44+ import {useViewportDetection } from " react-viewport-detect" ;
45+ import {useRef , useEffect } from " react" ;
46+
47+ const App () = > {
2548
49+ const ref = useRef () ;
50+ const isVisible = useViewportDetection (ref, {freeze: false , threshold: 1 }) ;
51+
52+ useEffect (() => {
53+ console .log (isVisible) ;
54+ },[isVisible]) ;
2655
2756 return (
2857 < div>
@@ -36,13 +65,27 @@ const App () => {
3665 );
3766
3867};
39- ` ` ` `
68+ ` ` `
69+
70+ ## Function parameters
71+ ### useViewportDetection
72+
73+ | Parameter | Type | Description | Default |
74+ |-----------------------------------------|-------|---------------------------------------------------------------------------------------------------|------------------------------------------------------------------|
75+ | ` ref` | React ref | The React ref to detect | none |
76+ | ` {rootMargin, root, threshold, freeze}` | Array | Intersection Observer options + freeze. Freeze is to keep the value when it has been display once | ` {rootMargin = 0px , root = null , threshold = 0 , freeze = true }` |
77+ [More about Intersection Observer options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)
4078
41- ## Licence
42- [GPL 3 ](https: // www.gnu.org/licenses/gpl-3.0.fr.html)
79+
80+ ## Next steps
81+
82+ - TypeScript Support
83+ - Example page
84+ - Improve the DX
85+ - Unit Testing
4386
4487## Contributing
45- Pull requests are welcome . For major changes, please open an issue first to discuss what you would like to change.
4688
47- ## Authors
48- - [Alexandre BAUDRY ](https: // github.com/Alexandrebdry)
89+ Contributions are always welcome!
90+ You can create a new pull request with changes or create a new issue and I will update this project
91+
0 commit comments