scala-js/scala-js-dom

scrollIntoView missing newer options

nafg opened this issue · 5 comments

nafg commented

def scrollIntoView(top: Boolean = js.native): Unit = js.native

From lib.dom.d.ts:

scrollIntoView(arg?: boolean | ScrollIntoViewOptions): void;
interface ScrollIntoViewOptions extends ScrollOptions {
    block?: ScrollLogicalPosition;
    inline?: ScrollLogicalPosition;
}
interface ScrollOptions {
    behavior?: ScrollBehavior;
}
type ScrollBehavior = "auto" | "smooth";
type ScrollLogicalPosition = "center" | "end" | "nearest" | "start";

In particular, it would be great not to have to write

div.asInstanceOf[js.Dynamic].scrollIntoView(js.Dynamic.literal(block = "nearest")).asInstanceOf[Unit]

More broadly how is this repo kept in sync with browser APIs? Can ScalablyTyped help somehow?

sjrd commented

More broadly how is this repo kept in sync with browser APIs?

By PRs from users who notice that something's not up to date.

ScalablyTyped focuses on applications by design. It's not designed for libraries that everyone depends on, such as the DOM.

I'll just mention in passing that the Typescript DOM definitions are 1) up to date for every TS release, 2) very detailed, 3) tagged by which standard (es5, esnext, etc) things were defined in. ST takes all this and massages it into Scala.js through a series of rewrites. If there is interest nothing in theory stops us from writing a custom conversion script which would "sync" all/some of it here.

In the mean time it's easy to copy/paste and adapt code as necessary @nafg :)

@nafg would you mind making a PR? would be much appreciated!! :)

  1. up to date for every TS release, 2) very detailed, 3) tagged by which standard (es5, esnext, etc) things were defined in

Sounds very appealing ... maybe one day :)

nafg commented

Sorry I never made a PR. For now here is the code I use in my project. Maybe it will be easy for a maintainer to adapt it into the codebase. Otherwise maybe I can do it in the next few days (but I may need a reminder).

object scrollIntoView {
  trait Options extends js.Object {
    val block   : js.UndefOr[String] = js.undefined
    val inline  : js.UndefOr[String] = js.undefined
    val behavior: js.UndefOr[String] = js.undefined
  }

  def blockNearest = new Options {
    override val block = "nearest"
  }

  def apply(elem: dom.Element)(options: Options): Unit =
    elem.asInstanceOf[js.Dynamic].scrollIntoView(options).asInstanceOf[Unit]
}

The obvious change that needs to be made is replacing the object apply method and its implementation with changing the signature of the existing method. I guess blockNearest is an arbitrary convenience object for my own use so it should be discarded. That leaves the Options trait.

I'm just going through utility code in my application codebase. I haven't looked up the DOM docs so it might be missing other things.