Skip to content

Add Façade Types to Support ResizeObserver #694

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
merged 11 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding facade for ResizeObserver and required types
  • Loading branch information
chrisshafer committed Apr 13, 2022
commit 53832c8decf43a5ee9ed9e4b29eedb0895c7466c
55 changes: 55 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/DOMRectReadOnly.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
trait DOMRectReadOnly extends js.Object {

/** The x coordinate of the DOMRect's origin.
*
* MDN
*/
def x: Double = js.native

/** The y coordinate of the DOMRect's origin.
*
* MDN
*/
def y: Double = js.native

/** The width of the DOMRect.
*
* MDN
*/
def width: Double = js.native

/** The height of the DOMRect.
*
* MDN
*/
def height: Double = js.native

/** Returns the top coordinate value of the DOMRect (usually the same as y.)
*
* MDN
*/
def top: Double = js.native

/** Returns the right coordinate value of the DOMRect (usually the same as x + width).
*
* MDN
*/
def right: Double = js.native

/** Returns the bottom coordinate value of the DOMRect (usually the same as y + height)
*
* MDN
*/
def bottom: Double = js.native

/** Returns the left coordinate value of the DOMRect (usually the same as x)
*
* MDN
*/
def left: Double = js.native
}
33 changes: 33 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/ResizeObserver.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

/** The ResizeObserver constructor creates a new ResizeObserver object, which can be used to report changes to the
* content or border box of an Element or the bounding box of an SVGElement - MDN
*
* @param callback
* The function called whenever an observed resize occurs. - MDN
*/
@js.native
@JSGlobal
class ResizeObserver(callback: js.Function2[js.Array[ResizeObserverEntry], ResizeObserver, _]) extends js.Object {

/** Starts observing the specified Element or SVGElement.
*
* MDN
*/
def observe(target: Node, options: js.UndefOr[ResizeObserverOptions] = js.native): Unit = js.native

/** Unobserves all observed Element or SVGElement targets.
*
* MDN
*/
def disconnect(): Unit = js.native

/** Ends the observing of a specified Element or SVGElement.
*
* MDN
*/
def unobserve(target: Node): Unit = js.native
}
41 changes: 41 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/ResizeObserverEntry.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.scalajs.dom

import scala.scalajs.js

/** The ResizeObserverEntry interface represents the object passedto the ResizeObserver() constructor's callback
* function, which allows you to access the new dimensions of the Element or SVGElement being observed.
*
* MDN
*/
@js.native
trait ResizeObserverEntry extends js.Object {

/** A reference to the Element or SVGElement being observed
*
* MDN
*/
def target: Node = js.native

/** An array containing objects with the new border box size of the observed element. The array is necessary to
* support elements that have multiple fragments, which occur in multi-column scenarios.
*
* MDN
*/
def borderBoxSize: js.Array[ResizeObserverSize] = js.native

/** An array containing objects with the new content box size of the observed element. The array is necessary to
* support elements that have multiple fragments, which occur in multi-column scenarios.
*
* MDN
*/
def contentBoxSize: js.Array[ResizeObserverSize] = js.native

/** A [[DOMRectReadOnly]] object containing the new size of the observed element when the callback is run. Note that
* this is better supported than the above two properties, but it is left over from an earlier implementation of the
* Resize Observer API, is still included in the spec for web compat reasons, and may be deprecated in future
* versions.
*
* MDN
*/
def contentRect: DOMRectReadOnly = js.native
}
21 changes: 21 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/ResizeObserverOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
trait ResizeObserverOptions extends js.Object {
var box: js.UndefOr[String] = js.native
}

/** Factory for [[ResizeObserverOptions]] objects. */
object ResizeObserverOptions {

/** Creates a new [[ResizeObserverOptions]] object with the given values. */
def apply(
box: Option[String] = None
): ResizeObserverOptions = {
val res = js.Dynamic.asInstanceOf[ResizeObserverOptions]
box.foreach(res.box = _)
res
}
}
23 changes: 23 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/ResizeObserverSize.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
trait ResizeObserverSize extends js.Object {

/** The length of the observed element's border box in the block dimension. For boxes with a horizontal writing-mode,
* this is the vertical dimension, or height; if the writing-mode is vertical, this is the horizontal dimension, or
* width.
*
* MDN
*/
def blockSize: Double = js.native

/** The length of the observed element's border box in the inline dimension. For boxes with a horizontal writing-mode,
* this is the horizontal dimension, or width; if the writing-mode is vertical, this is the vertical dimension, or
* height.
*
* MDN
*/
def inlineSize: Double = js.native
}