Skip to content

Commit f6d2a7d

Browse files
authored
Merge pull request #21 from rescript-lang/scrollIntoView
Improve scrollIntoView
2 parents 0aaab65 + 16df030 commit f6d2a7d

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/DOMAPI/Document.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Returns the first element that is a descendant of node that matches selectors.
8484
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
8585
*/
8686
@send
87-
external querySelector: (document, string) => element = "querySelector"
87+
external querySelector: (document, string) => Null.t<element> = "querySelector"
8888

8989
/**
9090
Returns all element descendants of node that match selectors.

src/DOMAPI/DocumentFragment.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Returns the first element that is a descendant of node that matches selectors.
6666
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/DocumentFragment/querySelector)
6767
*/
6868
@send
69-
external querySelector: (T.t, string) => element = "querySelector"
69+
external querySelector: (T.t, string) => Null.t<element> = "querySelector"
7070

7171
/**
7272
Returns all element descendants of node that match selectors.

src/DOMAPI/Element.res

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Returns the first element that is a descendant of node that matches selectors.
267267
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
268268
*/
269269
@send
270-
external querySelector: (T.t, string) => element = "querySelector"
270+
external querySelector: (T.t, string) => Null.t<element> = "querySelector"
271271

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

390390
/**
391+
`scrollIntoView()`
392+
393+
Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.
394+
395+
```res
396+
element->Element.scrollIntoView()
397+
```
398+
399+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
400+
*/
401+
@send
402+
external scrollIntoView: T.t => unit = "scrollIntoView"
403+
404+
/**
405+
`scrollIntoView(true)`
406+
407+
Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.
408+
409+
```res
410+
element->Element.scrollIntoView_alignToTop()
411+
```
412+
391413
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
392414
*/
393415
@send
394-
external scrollIntoView: (T.t, ~arg: bool=?) => unit = "scrollIntoView"
416+
external scrollIntoView_alignToTop: (T.t, @as(json`true`) _) => unit = "scrollIntoView"
395417

396418
/**
419+
`scrollIntoView({ behavior: "smooth" })`
420+
421+
Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.
422+
423+
```res
424+
element->Element.scrollIntoView_withOptions({ behavior: DOMAPI.Smooth })
425+
```
426+
397427
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView)
398428
*/
399429
@send
400-
external scrollIntoView2: (T.t, ~arg: scrollIntoViewOptions=?) => unit = "scrollIntoView"
430+
external scrollIntoView_withOptions: (T.t, scrollIntoViewOptions) => unit = "scrollIntoView"
401431

402432
/**
403433
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Element/scrollTo)

src/Global.res

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ The event listener is appended to target's event listener list and is not append
535535
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
536536
*/
537537
external addEventListener: (
538-
~type_: string,
538+
~type_: eventType,
539539
~callback: eventListener<'event>,
540540
~options: addEventListenerOptions=?,
541541
) => unit = "addEventListener"
@@ -556,10 +556,10 @@ If an AbortSignal is passed for options's signal, then the event listener will b
556556
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
557557
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
558558
*/
559-
external addEventListener2: (
560-
~type_: string,
559+
external addEventListener_useCapture: (
560+
~type_: eventType,
561561
~callback: eventListener<'event>,
562-
~options: bool=?,
562+
@as(json`true`) _,
563563
) => unit = "addEventListener"
564564

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

585585
/**

tests/DOMAPI/HTMLElement__test.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/DOMAPI/HTMLElement__test.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
open WebAPI
2+
open WebAPI.Global
3+
4+
document
5+
->Document.querySelector("form")
6+
->Null.toOption
7+
->Option.forEach(form => {
8+
form->Element.scrollIntoView_withOptions({behavior: DOMAPI.Smooth})
9+
})

0 commit comments

Comments
 (0)