Skip to content

Commit 9d30881

Browse files
authored
Merge pull request #43 from stephanoskomnenos/intersection-observer-api
Add IntersectionObserverAPI
2 parents 0d6538f + 57769c3 commit 9d30881

12 files changed

+262
-0
lines changed

src/DOMAPI/Document.js

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

src/DOMAPI/Document.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,5 @@ external hasStorageAccess: document => promise<bool> = "hasStorageAccess"
458458
*/
459459
@send
460460
external requestStorageAccess: document => promise<unit> = "requestStorageAccess"
461+
462+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof Document`)

src/DOMAPI/Element.js

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

src/DOMAPI/Element.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,5 @@ Returns true if qualifiedName is now present, and false otherwise.
498498
include Impl({
499499
type t = element
500500
})
501+
502+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof Element`)

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

src/IntersectionObserverAPI/IntersectionObserverRoot.js

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
open DOMAPI
2+
open IntersectionObserverAPI
3+
4+
external fromDocument: document => root = "%identity"
5+
external fromElement: element => root = "%identity"
6+
external fromNull: root = "null"
7+
8+
type decoded =
9+
| Element(element)
10+
| Document(document)
11+
| Null
12+
13+
let decode = (t: root): decoded => {
14+
open Prelude
15+
if Element.isInstanceOf(t) {
16+
Element(t->unsafeConversation)
17+
} else if Document.isInstanceOf(t) {
18+
Document(t->unsafeConversation)
19+
} else {
20+
Null
21+
}
22+
}

0 commit comments

Comments
 (0)