Skip to content

Commit 0e0733e

Browse files
authored
Merge pull request #27 from illusionalsagacity/intersection-observer
Bindings for IntersectionObserver
2 parents 72f023b + 259cc86 commit 0e0733e

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Done:
55
* Remove deprecated APIs
66
* `Window.getSelection` now returns an option, to better match the definition
77
* Add scrollToWithOptions to window
8+
* Added IntersectionObserver and IntersectionObserverEntry bindings
89

910
Todo:
1011
* Convert to rescript syntax
+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+
body.appendChild(el);
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

Diff for: src/Webapi/Webapi__IntersectionObserver.res

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Spec: https://www.w3.org/TR/intersection-observer/
3+
*/
4+
5+
module IntersectionObserverEntry = {
6+
/**
7+
* Spec: https://www.w3.org/TR/intersection-observer/#intersection-observer-entry
8+
*/
9+
10+
type t = Dom.intersectionObserverEntry
11+
12+
/* Properties */
13+
14+
@get external time: t => float = "time"
15+
@get external rootBounds: t => Dom.domRect = "rootBounds"
16+
@get external boundingClientRect: t => Dom.domRect = "boundingClientRect"
17+
@get external intersectionRect: t => Dom.domRect = "intersectionRect"
18+
@get external isIntersecting: t => bool = "isIntersecting"
19+
@get external intersectionRatio: t => float = "intersectionRatio"
20+
@get external target: t => Dom.element = "target"
21+
}
22+
23+
type t = Dom.intersectionObserver
24+
25+
type intersectionObserverInit = {
26+
root: option<Dom.element>,
27+
rootMargin: option<string>,
28+
threshold: option<array<float>>, // between 0 and 1.
29+
}
30+
31+
@obj
32+
external makeInit: (
33+
~root: Dom.element=?,
34+
~rootMargin: string=?,
35+
~threshold: array<float>=?,
36+
unit,
37+
) => intersectionObserverInit = ""
38+
39+
@new
40+
external make: (@uncurry (array<IntersectionObserverEntry.t>, t) => unit) => t =
41+
"IntersectionObserver"
42+
43+
@new
44+
external makeWithInit: (
45+
@uncurry (array<IntersectionObserverEntry.t>, t) => unit,
46+
intersectionObserverInit,
47+
) => t = "IntersectionObserver"
48+
49+
/* Properties */
50+
51+
@get @return(nullable)
52+
external root: t => option<Dom.element> = "root"
53+
@get external rootMargin: t => string = "rootMargin"
54+
@get external thresholds: t => array<float> = "thresholds"
55+
56+
/* Methods */
57+
58+
@send external disconnect: t => unit = "disconnect"
59+
@send external observe: (t, Dom.element) => unit = "observe"
60+
@send external unobserve: (t, Dom.element) => unit = "unobserve"
61+
@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(body, el)
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)