-
Notifications
You must be signed in to change notification settings - Fork 39
Bindings for IntersectionObserver #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheSpyder
merged 4 commits into
TheSpyder:main
from
illusionalsagacity:intersection-observer
Sep 22, 2021
+143
−0
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
var Belt_Option = require("rescript/lib/js/belt_Option.js"); | ||
var Caml_option = require("rescript/lib/js/caml_option.js"); | ||
var TestHelpers = require("../testHelpers.js"); | ||
var Webapi__Dom__Document = require("../../src/Webapi/Dom/Webapi__Dom__Document.js"); | ||
|
||
var el = document.createElement("div"); | ||
|
||
var body = TestHelpers.unsafelyUnwrapOption(Belt_Option.flatMap(Webapi__Dom__Document.asHtmlDocument(document), (function (prim) { | ||
return Caml_option.nullable_to_opt(prim.body); | ||
}))); | ||
|
||
el.innerText = "Hello There"; | ||
|
||
el.setAttribute("style", "margin-top: 800px; margin-bottom: 800px"); | ||
|
||
el.appendChild(body); | ||
|
||
function handler(entries, observer) { | ||
entries.forEach(function (entry) { | ||
console.log(entry.time); | ||
console.log(entry.rootBounds); | ||
console.log(entry.boundingClientRect); | ||
console.log(entry.intersectionRect); | ||
console.log(entry.isIntersecting); | ||
console.log(entry.intersectionRatio); | ||
console.log(entry.target); | ||
|
||
}); | ||
observer.unobserve(el); | ||
|
||
} | ||
|
||
var observer = new IntersectionObserver(handler); | ||
|
||
observer.observe(el); | ||
|
||
observer.unobserve(el); | ||
|
||
observer.observe(el); | ||
|
||
observer.disconnect(); | ||
|
||
exports.el = el; | ||
exports.body = body; | ||
exports.handler = handler; | ||
exports.observer = observer; | ||
/* el Not a pure module */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Webapi/IntersectionObserver/Webapi__IntersectionObserver__IntersectionObserverEntry.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Spec: https://www.w3.org/TR/intersection-observer/#intersection-observer-entry | ||
*/ | ||
|
||
type t = Dom.intersectionObserverEntry | ||
|
||
/* Properties */ | ||
|
||
@get external time: t => float = "time"; | ||
@get external rootBounds: t => Dom.domRect = "rootBounds"; | ||
@get external boundingClientRect: t => Dom.domRect = "boundingClientRect"; | ||
@get external intersectionRect: t => Dom.domRect = "intersectionRect"; | ||
@get external isIntersecting: t => bool = "isIntersecting"; | ||
@get external intersectionRatio: t => float = "intersectionRatio"; | ||
@get external target: t => Dom.element = "target"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Spec: https://www.w3.org/TR/intersection-observer/ | ||
*/ | ||
module IntersectionObserverEntry = Webapi__IntersectionObserver__IntersectionObserverEntry | ||
|
||
type t = Dom.intersectionObserver | ||
|
||
type intersectionObserverInit = { | ||
root: option<Dom.element>, | ||
rootMargin: option<string>, | ||
threshold: option<array<float>>, // between 0 and 1. | ||
} | ||
|
||
@obj | ||
external makeInit: ( | ||
~root: Dom.element=?, | ||
~rootMargin: string=?, | ||
~threshold: array<float>=?, | ||
unit, | ||
) => intersectionObserverInit = "" | ||
|
||
@new | ||
external make: (@uncurry (array<IntersectionObserverEntry.t>, t) => unit) => t = | ||
"IntersectionObserver" | ||
|
||
@new | ||
external makeWithInit: ( | ||
@uncurry (array<IntersectionObserverEntry.t>, t) => unit, | ||
intersectionObserverInit, | ||
) => t = "IntersectionObserver" | ||
|
||
/* Properties */ | ||
|
||
@get @return(nullable) | ||
external root: t => option<Dom.element> = "root" | ||
@get external rootMargin: t => string = "rootMargin" | ||
@get external thresholds: t => array<float> = "thresholds" | ||
|
||
/* Methods */ | ||
|
||
@send external disconnect: t => unit = "disconnect" | ||
@send external observe: (t, Dom.element) => unit = "observe" | ||
@send external unobserve: (t, Dom.element) => unit = "unobserve" | ||
@send external takeRecords: t => array<IntersectionObserverEntry.t> = "takeRecords" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
let el = Webapi.Dom.document -> Webapi.Dom.Document.createElement("div") | ||
|
||
let body = | ||
Webapi.Dom.Document.asHtmlDocument(Webapi.Dom.document) | ||
->Belt.Option.flatMap(Webapi.Dom.HtmlDocument.body) | ||
->TestHelpers.unsafelyUnwrapOption | ||
|
||
Webapi.Dom.Element.setInnerText(el, "Hello There") | ||
Webapi.Dom.Element.setAttribute(el, "style", "margin-top: 800px; margin-bottom: 800px") | ||
Webapi.Dom.Element.appendChild(el, body) | ||
|
||
let handler = (entries, observer) => { | ||
Js.Array.forEach(entry => { | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.time(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.rootBounds(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.boundingClientRect(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.intersectionRect(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.isIntersecting(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.intersectionRatio(entry)) | ||
Js.log(Webapi.IntersectionObserver.IntersectionObserverEntry.target(entry)) | ||
}, entries) | ||
|
||
Webapi.IntersectionObserver.unobserve(observer, el) | ||
} | ||
|
||
let observer = Webapi.IntersectionObserver.make(handler) | ||
|
||
Webapi.IntersectionObserver.observe(observer, el) | ||
Webapi.IntersectionObserver.unobserve(observer, el) | ||
Webapi.IntersectionObserver.observe(observer, el) | ||
Webapi.IntersectionObserver.disconnect(observer) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.