Skip to content

Commit 51f692a

Browse files
committed
feat(rescript-intersection-observer): add new observer with option
1 parent 798ef40 commit 51f692a

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

examples/dev/dev__intersection.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/dev/dev__intersection.res

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ let observer = Intersection.Observer.new(entries => {
2323
}
2424
})
2525

26+
let observerWithOptions = Intersection.Observer.newWithOption(entries => {
27+
switch entries {
28+
| [head] => {
29+
head->Intersection.ObserverEntry.intersectionRatio->Js.log
30+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.x->Js.log
31+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.width->Js.log
32+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.height->Js.log
33+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.top->Js.log
34+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.right->Js.log
35+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.bottom->Js.log
36+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.left->Js.log
37+
head->Intersection.ObserverEntry.boundingClientRect->DOMRect.toJson->Js.log
38+
head->Intersection.ObserverEntry.time->Js.log
39+
head->Intersection.ObserverEntry.isVisible->Js.log
40+
head->Intersection.ObserverEntry.target->Js.log
41+
let rect = DOMRect.rect(~x=1.0, ~y=2.0, ())
42+
rect->DOMRect.fromRect->Js.log
43+
}
44+
| _ => ()
45+
}
46+
}, Intersection.ObserverInit.new(~rootMargin="0px", ()))
47+
48+
observerWithOptions->Intersection.Observer.observe(htmlDom())
49+
50+
observerWithOptions->Intersection.Observer.unobserve(htmlDom())
51+
2652
observer->Intersection.Observer.observe(htmlDom())
2753

2854
observer->Intersection.Observer.unobserve(htmlDom())

rescript-intersection-observer/src/Intersection.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rescript-intersection-observer/src/Intersection.res

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,27 @@ module ObserverEntry = {
2020
external time: t => float = "time"
2121
}
2222

23+
module ObserverInit = {
24+
type t
25+
26+
@obj
27+
external new: (
28+
~root: Dom.element=?,
29+
~rootMargin: string=?,
30+
~thresholds: array<float>=?,
31+
unit,
32+
) => t = ""
33+
}
34+
2335
module Observer = {
2436
type t = Dom.intersectionObserver
2537

2638
@new
2739
external new: (array<ObserverEntry.t> => unit) => t = "IntersectionObserver"
40+
41+
@new
42+
external newWithOption: (array<ObserverEntry.t> => unit, ObserverInit.t) => t =
43+
"IntersectionObserver"
2844
// properties
2945

3046
// TODO: intersectionObserver.root return Dom.element or Dom.document

0 commit comments

Comments
 (0)