File tree 4 files changed +55
-5
lines changed
4 files changed +55
-5
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ Add `bs-react-is-visible` to `bs-dependencies` in `bsconfig.json`
23
23
24
24
## Usage
25
25
26
+ ### Basic
27
+
26
28
``` reason
27
29
[@react.component]
28
30
let make = () => {
@@ -34,6 +36,19 @@ let make = () => {
34
36
};
35
37
```
36
38
39
+ ### With options
40
+
41
+ ``` reason
42
+ [@react.component]
43
+ let make = () => {
44
+ let (isVisible, ref) = ReactIsVisible.useIsVisible(~options={once: true}, ());
45
+
46
+ <h1 ref>
47
+ {(isVisible ? "I'm triggered as visible once!" : "I'm not visible") |> React.string}
48
+ </h1>;
49
+ };
50
+ ```
51
+
37
52
## Polyfill
38
53
39
54
[ Browser compatibility.] ( https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Browser_compatibility )
Original file line number Diff line number Diff line change @@ -64,7 +64,14 @@ let make = () => {
64
64
style= {Style . link(active === "lazyload" )}>
65
65
{"Lazy Load" |> React . string}
66
66
</a >
67
+ <a
68
+ href= "#triggeronce"
69
+ title= "TriggerOnce"
70
+ onClick= {_ => setActiveExample(_ => (<TriggerOnce />, "triggeronce" ))}
71
+ style= {Style . link(active === "triggeronce" )}>
72
+ {"Trigger Once" |> React . string}
73
+ </a >
67
74
</nav >
68
75
activeExample
69
76
</div >;
70
- };
77
+ };
Original file line number Diff line number Diff line change
1
+ module Style = {
2
+ let fixed = ReactDOMRe . Style . make(~position= "fixed" , ~right= "2rem" , () );
3
+ };
4
+
5
+ [@ react . component ]
6
+ let make = () => {
7
+ let (isVisible , ref ) =
8
+ ReactIsVisible . useIsVisible(~options= {once: true }, () );
9
+
10
+ <div style= Basic . Style . wrapper>
11
+ <h1 > {"Scroll down" |> React . string} </h1 >
12
+ <h2 style= Style . fixed>
13
+ {"Was triggered: " ++ string_of_bool(isVisible) ++ "" |> React . string}
14
+ </h2 >
15
+ <div ref style= {Basic . Style . box(isVisible)}>
16
+ {(isVisible ? "I'm visible!" : "I'm not visible!" ) |> React . string}
17
+ </div >
18
+ </div >;
19
+ };
Original file line number Diff line number Diff line change @@ -12,19 +12,28 @@ module VO = {
12
12
external getSubscribers : unit => ' subscriberList = "getSubscribers" ;
13
13
};
14
14
15
- let useIsVisible = () => {
15
+ type options = {once: bool };
16
+ let defaultOptions = {once: false };
17
+
18
+ let useIsVisible = (~options= defaultOptions, () ) => {
16
19
let (isVisible , setIsVisible ) = React . useState(() => false );
17
20
let nodeRef = React . useRef(Js . Nullable . null);
18
21
19
22
React . useEffect0(() => {
20
- let handleVisibilityChange = entry =>
23
+ let handleVisibilityChange = (el , entry) => {
21
24
setIsVisible(_prevIntersecting => entry## isIntersecting);
22
25
26
+ if (entry## isIntersecting && options. once) {
27
+ VO . unwatch(el);
28
+ };
29
+ };
30
+
23
31
let domElement =
24
32
switch (nodeRef |> React . Ref . current |> Js . Nullable . toOption) {
25
33
| Some (el ) =>
26
- VO . watch(el, handleVisibilityChange);
34
+ VO . watch(el, entry => handleVisibilityChange(el , entry) );
27
35
Some (el);
36
+
28
37
| None => None
29
38
};
30
39
@@ -35,4 +44,4 @@ let useIsVisible = () => {
35
44
});
36
45
37
46
(isVisible, ReactDOMRe . Ref . domRef(nodeRef));
38
- };
47
+ };
You can’t perform that action at this time.
0 commit comments