Skip to content

Commit 5aa7cea

Browse files
Add IntersectionObserverAPI
1 parent 0d6538f commit 5aa7cea

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

src/IntersectionObserverAPI.js

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

src/IntersectionObserverAPI.res

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
@@warning("-30")
2+
3+
open DOMAPI
4+
5+
/**
6+
provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
7+
[See IntersectionObserver on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver)
8+
*/
9+
@editor.completeFrom(IntersectionObserver)
10+
type intersectionObserver = {
11+
/**
12+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/root)
13+
*/
14+
root: Null.t<unknown>,
15+
/**
16+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/rootMargin)
17+
*/
18+
rootMargin: string,
19+
/**
20+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/thresholds)
21+
*/
22+
thresholds: array<float>,
23+
}
24+
25+
/**
26+
This Intersection Observer API interface describes the intersection between the target element and its root container at a specific moment of transition.
27+
[See IntersectionObserverEntry on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry)
28+
*/
29+
type intersectionObserverEntry = {
30+
/**
31+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/time)
32+
*/
33+
time: float,
34+
/**
35+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/rootBounds)
36+
*/
37+
rootBounds: Null.t<domRectReadOnly>,
38+
/**
39+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/boundingClientRect)
40+
*/
41+
boundingClientRect: domRectReadOnly,
42+
/**
43+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/intersectionRect)
44+
*/
45+
intersectionRect: domRectReadOnly,
46+
/**
47+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/isIntersecting)
48+
*/
49+
isIntersecting: bool,
50+
/**
51+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/intersectionRatio)
52+
*/
53+
intersectionRatio: float,
54+
/**
55+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserverEntry/target)
56+
*/
57+
target: element,
58+
}
59+
60+
type intersectionObserverInit = {
61+
mutable root?: Null.t<unknown>,
62+
mutable rootMargin?: string,
63+
mutable threshold?: array<float>,
64+
}
65+
66+
type intersectionObserverCallback = (array<intersectionObserverEntry>, intersectionObserver) => unit

src/IntersectionObserverAPI/IntersectionObserver.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
open DOMAPI
2+
open IntersectionObserverAPI
3+
4+
/**
5+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver)
6+
*/
7+
@new
8+
external make: (
9+
~callback: intersectionObserverCallback,
10+
~options: intersectionObserverInit=?,
11+
) => intersectionObserver = "IntersectionObserver"
12+
13+
/**
14+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/observe)
15+
*/
16+
@send
17+
external observe: (intersectionObserver, element) => unit = "observe"
18+
19+
/**
20+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/unobserve)
21+
*/
22+
@send
23+
external unobserve: (intersectionObserver, element) => unit = "unobserve"
24+
25+
/**
26+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/disconnect)
27+
*/
28+
@send
29+
external disconnect: intersectionObserver => unit = "disconnect"
30+
31+
/**
32+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/takeRecords)
33+
*/
34+
@send
35+
external takeRecords: intersectionObserver => array<intersectionObserverEntry> = "takeRecords"

tests/IntersectionObserverAPI/IntersectionObserver__test.js

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let observer = IntersectionObserver.make(~callback=(entry, observer) => {
2+
Console.log2(entry, observer)
3+
})
4+
5+
let observer2 = IntersectionObserver.make(~callback=(entry, observer) => {
6+
Console.log2(entry, observer)
7+
}, ~options={rootMargin: "10px", threshold: [0.1]})
8+
9+
let root2 = observer2.root
10+
let rootMargin2 = observer2.rootMargin
11+
12+
let targetElement = Global.document->Document.querySelector("#targetElement")->Null.toOption
13+
switch targetElement {
14+
| Some(e) => {
15+
observer2->IntersectionObserver.observe(e)
16+
observer2->IntersectionObserver.unobserve(e)
17+
}
18+
| _ => Console.log("Target element not found.")
19+
}
20+
21+
let entries2 = observer2->IntersectionObserver.takeRecords
22+
Console.log(entries2->Array.length)
23+
24+
observer2->IntersectionObserver.disconnect

0 commit comments

Comments
 (0)