Skip to content

Commit feb79b2

Browse files
author
rob
committed
feat: adds bindings for IntersectionObserver
1 parent 558c7d5 commit feb79b2

5 files changed

+140
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
var Belt_Option = require("rescript/lib/js/belt_Option.js");
4+
var Caml_option = require("rescript/lib/js/caml_option.js");
5+
var TestHelpers = require("../testHelpers.js");
6+
var Webapi__Dom__Document = require("../../src/Webapi/Dom/Webapi__Dom__Document.js");
7+
8+
var el = document.createElement("div");
9+
10+
var body = TestHelpers.unsafelyUnwrapOption(Belt_Option.flatMap(Webapi__Dom__Document.asHtmlDocument(document), (function (prim) {
11+
return Caml_option.nullable_to_opt(prim.body);
12+
})));
13+
14+
el.innerText = "Hello There";
15+
16+
el.setAttribute("style", "margin-top: 800px; margin-bottom: 800px");
17+
18+
el.appendChild(body);
19+
20+
function handler(entries, observer) {
21+
entries.forEach(function (entry) {
22+
console.log(entry.time);
23+
console.log(entry.rootBounds);
24+
console.log(entry.boundingClientRect);
25+
console.log(entry.intersectionRect);
26+
console.log(entry.isIntersecting);
27+
console.log(entry.intersectionRatio);
28+
console.log(entry.target);
29+
30+
});
31+
observer.unobserve(el);
32+
33+
}
34+
35+
var observer = new IntersectionObserver(handler);
36+
37+
observer.observe(el);
38+
39+
observer.unobserve(el);
40+
41+
observer.observe(el);
42+
43+
observer.disconnect();
44+
45+
exports.el = el;
46+
exports.body = body;
47+
exports.handler = handler;
48+
exports.observer = observer;
49+
/* el Not a pure module */

Diff for: src/Webapi.res

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Performance = Webapi__Performance
2626
/** @since 0.19.0 */
2727
module ReadableStream = Webapi__ReadableStream
2828

29+
module IntersectionObserver = Webapi__IntersectionObserver
2930
module ResizeObserver = Webapi__ResizeObserver
3031
module Url = Webapi__Url
3132

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Spec: https://www.w3.org/TR/intersection-observer/#intersection-observer-entry
3+
*/
4+
5+
type t = Dom.intersectionObserverEntry
6+
7+
/* Properties */
8+
9+
@get external time: t => float = "time";
10+
@get external rootBounds: t => Dom.domRect = "rootBounds";
11+
@get external boundingClientRect: t => Dom.domRect = "boundingClientRect";
12+
@get external intersectionRect: t => Dom.domRect = "intersectionRect";
13+
@get external isIntersecting: t => bool = "isIntersecting";
14+
@get external intersectionRatio: t => float = "intersectionRatio";
15+
@get external target: t => Dom.element = "target";

Diff for: src/Webapi/Webapi__IntersectionObserver.res

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Spec: https://www.w3.org/TR/intersection-observer/
3+
*/
4+
module IntersectionObserverEntry = Webapi__IntersectionObserver__IntersectionObserverEntry
5+
6+
type t = Dom.intersectionObserver
7+
8+
type intersectionObserverInit = {
9+
root: option<Dom.element>,
10+
rootMargin: option<string>,
11+
threshold: option<array<float>>, // between 0 and 1.
12+
}
13+
14+
@obj
15+
external makeInit: (
16+
~root: Dom.element=?,
17+
~rootMargin: string=?,
18+
~threshold: array<float>=?,
19+
unit,
20+
) => intersectionObserverInit = ""
21+
22+
@new
23+
external make: (@uncurry (array<IntersectionObserverEntry.t>, t) => unit) => t =
24+
"IntersectionObserver"
25+
26+
@new
27+
external makeWithInit: (
28+
@uncurry (array<IntersectionObserverEntry.t>, t) => unit,
29+
intersectionObserverInit,
30+
) => t = "IntersectionObserver"
31+
32+
/* Properties */
33+
34+
@get @return(nullable)
35+
external root: t => option<Dom.element> = "root"
36+
@get external rootMargin: t => string = "rootMargin"
37+
@get external thresholds: t => array<float> = "thresholds"
38+
39+
/* Methods */
40+
41+
@send external disconnect: t => unit = "disconnect"
42+
@send external observe: (t, Dom.element) => unit = "observe"
43+
@send external unobserve: (t, Dom.element) => unit = "unobserve"
44+
@send external takeRecords: t => array<IntersectionObserverEntry.t> = "takeRecords"

Diff for: tests/Webapi/Webapi__IntersectionObserver__test.res

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
let el = Webapi.Dom.document -> Webapi.Dom.Document.createElement("div")
2+
3+
let body =
4+
Webapi.Dom.Document.asHtmlDocument(Webapi.Dom.document)
5+
->Belt.Option.flatMap(Webapi.Dom.HtmlDocument.body)
6+
->TestHelpers.unsafelyUnwrapOption
7+
8+
Webapi.Dom.Element.setInnerText(el, "Hello There")
9+
Webapi.Dom.Element.setAttribute(el, "style", "margin-top: 800px; margin-bottom: 800px")
10+
Webapi.Dom.Element.appendChild(el, body)
11+
12+
let handler = (entries, observer) => {
13+
Js.Array.forEach(entry => {
14+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.time(entry))
15+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.rootBounds(entry))
16+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.boundingClientRect(entry))
17+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.intersectionRect(entry))
18+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.isIntersecting(entry))
19+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.intersectionRatio(entry))
20+
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.target(entry))
21+
}, entries)
22+
23+
Webapi.IntersectionObserver.unobserve(observer, el)
24+
}
25+
26+
let observer = Webapi.IntersectionObserver.make(handler)
27+
28+
Webapi.IntersectionObserver.observe(observer, el)
29+
Webapi.IntersectionObserver.unobserve(observer, el)
30+
Webapi.IntersectionObserver.observe(observer, el)
31+
Webapi.IntersectionObserver.disconnect(observer)

0 commit comments

Comments
 (0)