@@ -44,13 +44,13 @@ export abstract class ViewportScroller {
4444 * Scrolls to a specified position.
4545 * @param position A position in screen coordinates (a tuple with x and y values).
4646 */
47- abstract scrollToPosition(position: [number, number]): void;
47+ abstract scrollToPosition(position: [number, number], options?: ScrollOptions ): void;
4848
4949 /**
5050 * Scrolls to an anchor element.
5151 * @param anchor The ID of the anchor element.
5252 */
53- abstract scrollToAnchor(anchor: string): void;
53+ abstract scrollToAnchor(anchor: string, options?: ScrollOptions ): void;
5454
5555 /**
5656 * Disables automatic scroll restoration provided by the browser.
@@ -97,8 +97,8 @@ export class BrowserViewportScroller implements ViewportScroller {
9797 * Sets the scroll position.
9898 * @param position The new position in screen coordinates.
9999 */
100- scrollToPosition(position: [number, number]): void {
101- this.window.scrollTo(position[0], position[1]);
100+ scrollToPosition(position: [number, number], options?: ScrollOptions ): void {
101+ this.window.scrollTo({...options, left: position[0], top: position[1]} );
102102 }
103103
104104 /**
@@ -112,11 +112,11 @@ export class BrowserViewportScroller implements ViewportScroller {
112112 * @see https://html.spec.whatwg.org/#the-indicated-part-of-the-document
113113 * @see https://html.spec.whatwg.org/#scroll-to-fragid
114114 */
115- scrollToAnchor(target: string): void {
115+ scrollToAnchor(target: string, options?: ScrollOptions ): void {
116116 const elSelected = findAnchorFromDocument(this.document, target);
117117
118118 if (elSelected) {
119- this.scrollToElement(elSelected);
119+ this.scrollToElement(elSelected, options );
120120 // After scrolling to the element, the spec dictates that we follow the focus steps for the
121121 // target. Rather than following the robust steps, simply attempt focus.
122122 //
@@ -140,12 +140,16 @@ export class BrowserViewportScroller implements ViewportScroller {
140140 * The offset can be used when we know that there is a floating header and scrolling naively to an
141141 * element (ex: `scrollIntoView`) leaves the element hidden behind the floating header.
142142 */
143- private scrollToElement(el: HTMLElement): void {
143+ private scrollToElement(el: HTMLElement, options?: ScrollOptions ): void {
144144 const rect = el.getBoundingClientRect();
145145 const left = rect.left + this.window.pageXOffset;
146146 const top = rect.top + this.window.pageYOffset;
147147 const offset = this.offset();
148- this.window.scrollTo(left - offset[0], top - offset[1]);
148+ this.window.scrollTo({
149+ ...options,
150+ left: left - offset[0],
151+ top: top - offset[1],
152+ });
149153 }
150154}
151155
0 commit comments