Skip to content

Improve scrollIntoView #21

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 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/DOMAPI/Document.res
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Returns the first element that is a descendant of node that matches selectors.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
*/
@send
external querySelector: (document, string) => element = "querySelector"
external querySelector: (document, string) => Null.t<element> = "querySelector"

/**
Returns all element descendants of node that match selectors.
Expand Down
2 changes: 1 addition & 1 deletion src/DOMAPI/DocumentFragment.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Returns the first element that is a descendant of node that matches selectors.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelector)
*/
@send
external querySelector: (T.t, string) => element = "querySelector"
external querySelector: (T.t, string) => Null.t<element> = "querySelector"

/**
Returns all element descendants of node that match selectors.
Expand Down
36 changes: 33 additions & 3 deletions src/DOMAPI/Element.res
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Returns the first element that is a descendant of node that matches selectors.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
*/
@send
external querySelector: (T.t, string) => element = "querySelector"
external querySelector: (T.t, string) => Null.t<element> = "querySelector"

/**
Returns all element descendants of node that match selectors.
Expand Down Expand Up @@ -388,16 +388,46 @@ When supplied, options's navigationUI member indicates whether showing navigatio
external scrollBy2: (T.t, ~x: float, ~y: float) => unit = "scrollBy"

/**
`scrollIntoView()`

Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.

```res
element->Element.scrollIntoView()
```

[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
*/
@send
external scrollIntoView: T.t => unit = "scrollIntoView"

/**
`scrollIntoView(true)`

Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.

```res
element->Element.scrollIntoView_alignToTop()
```

[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
*/
@send
external scrollIntoView: (T.t, ~arg: bool=?) => unit = "scrollIntoView"
external scrollIntoView_alignToTop: (T.t, @as(json`true`) _) => unit = "scrollIntoView"

/**
`scrollIntoView({ behavior: "smooth" })`

Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.

```res
element->Element.scrollIntoView_withOptions({ behavior: DOMAPI.Smooth })
```

[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
*/
@send
external scrollIntoView2: (T.t, ~arg: scrollIntoViewOptions=?) => unit = "scrollIntoView"
external scrollIntoView_withOptions: (T.t, scrollIntoViewOptions) => unit = "scrollIntoView"

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollTo)
Expand Down
14 changes: 7 additions & 7 deletions src/Global.res
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ The event listener is appended to target's event listener list and is not append
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
external addEventListener: (
~type_: string,
~type_: eventType,
~callback: eventListener<'event>,
~options: addEventListenerOptions=?,
) => unit = "addEventListener"
Expand All @@ -556,10 +556,10 @@ If an AbortSignal is passed for options's signal, then the event listener will b
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
external addEventListener2: (
~type_: string,
external addEventListener_useCapture: (
~type_: eventType,
~callback: eventListener<'event>,
~options: bool=?,
@as(json`true`) _,
) => unit = "addEventListener"

/**
Expand All @@ -576,10 +576,10 @@ external removeEventListener: (
Removes the event listener in target's event listener list with the same type, callback, and options.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
external removeEventListener2: (
~type_: string,
external removeEventListener_useCapture: (
~type_: eventType,
~callback: eventListener<'event>,
~options: bool=?,
@as(json`true`) _,
) => unit = "removeEventListener"

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/DOMAPI/HTMLElement__test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/DOMAPI/HTMLElement__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
open WebAPI
open WebAPI.Global

document
->Document.querySelector("form")
->Null.toOption
->Option.forEach(form => {
form->Element.scrollIntoView_withOptions({behavior: DOMAPI.Smooth})
})
Loading