Skip to content

Commit 57769c3

Browse files
Use variant for IntersectionObserver root
1 parent 5aa7cea commit 57769c3

File tree

9 files changed

+94
-6
lines changed

9 files changed

+94
-6
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.res

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
open DOMAPI
44

5+
@editor.completeFrom(IntersectionObserverRoot)
6+
type root
7+
58
/**
69
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.
710
[See IntersectionObserver on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver)
@@ -11,7 +14,7 @@ type intersectionObserver = {
1114
/**
1215
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/root)
1316
*/
14-
root: Null.t<unknown>,
17+
root: root,
1518
/**
1619
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/rootMargin)
1720
*/
@@ -58,7 +61,7 @@ type intersectionObserverEntry = {
5861
}
5962

6063
type intersectionObserverInit = {
61-
mutable root?: Null.t<unknown>,
64+
mutable root?: root,
6265
mutable rootMargin?: string,
6366
mutable threshold?: array<float>,
6467
}

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+
}

tests/IntersectionObserverAPI/IntersectionObserver__test.js

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

tests/IntersectionObserverAPI/IntersectionObserver__test.res

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@ let observer = IntersectionObserver.make(~callback=(entry, observer) => {
22
Console.log2(entry, observer)
33
})
44

5+
let root = Global.document->Document.querySelector("#root")->Null.getUnsafe
6+
57
let observer2 = IntersectionObserver.make(~callback=(entry, observer) => {
68
Console.log2(entry, observer)
7-
}, ~options={rootMargin: "10px", threshold: [0.1]})
9+
}, ~options={
10+
root: root->IntersectionObserverRoot.fromElement,
11+
rootMargin: "10px",
12+
threshold: [0.1],
13+
})
814

9-
let root2 = observer2.root
15+
switch observer2.root->IntersectionObserverRoot.decode {
16+
| Element(_) => Console.log("Element")
17+
| Document(_) => Console.log("Document")
18+
| Null => Console.log("Null")
19+
}
1020
let rootMargin2 = observer2.rootMargin
1121

1222
let targetElement = Global.document->Document.querySelector("#targetElement")->Null.toOption

0 commit comments

Comments
 (0)