8000 feat(common): Allow passing ScrollOptions to ViewportScroller by atscott · Pull Request #61002 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

feat(common): Allow passing ScrollOptions to ViewportScroller #61002

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

Closed
wants to merge 1 commit into from
Closed
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.
8000 Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions goldens/public-api/common/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,8 @@ export const VERSION: Version;
// @public
export abstract class ViewportScroller {
abstract getScrollPosition(): [number, number];
abstract scrollToAnchor(anchor: string): void;
abstract scrollToPosition(position: [number, number]): void;
abstract scrollToAnchor(anchor: string, options?: ScrollOptions): void;
abstract scrollToPosition(position: [number, number], options?: ScrollOptions): void;
abstract setHistoryScrollRestoration(scrollRestoration: 'auto' | 'manual'): void;
abstract setOffset(offset: [number, number] | (() => [number, number])): void;
// (undocumented)
Expand Down
20 changes: 12 additions & 8 deletions packages/common/src/viewport_scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ export abstract class ViewportScroller {
* Scrolls to a specified position.
* @param position A position in screen coordinates (a tuple with x and y values).
*/
abstract scrollToPosition(position: [number, number]): void;
abstract scrollToPosition(position: [number, number], options?: ScrollOptions): void;

/**
* Scrolls to an anchor element.
* @param anchor The ID of the anchor element.
*/
abstract scrollToAnchor(anchor: string): void;
abstract scrollToAnchor(anchor: string, options?: ScrollOptions): void;

/**
* Disables automatic scroll restoration provided by the browser.
Expand Down Expand Up @@ -97,8 +97,8 @@ export class BrowserViewportScroller implements ViewportScroller {
* Sets the scroll position.
* @param position The new position in screen coordinates.
*/
scrollToPosition(position: [number, number]): void {
this.window.scrollTo(position[0], position[1]);
scrollToPosition(position: [number, number], options?: ScrollOptions): void {
this.window.scrollTo({...options, left: position[0], top: position[1]});
}

/**
Expand All @@ -112,11 +112,11 @@ export class BrowserViewportScroller implements ViewportScroller {
* @see https://html.spec.whatwg.org/#the-indicated-part-of-the-document
* @see https://html.spec.whatwg.org/#scroll-to-fragid
*/
scrollToAnchor(target: string): void {
scrollToAnchor(target: string, options?: ScrollOptions): void {
const elSelected = findAnchorFromDocument(this.document, target);

if (elSelected) {
this.scrollToElement(elSelected);
this.scrollToElement(elSelected, options);
// After scrolling to the element, the spec dictates that we follow the focus steps for the
// target. Rather than following the robust steps, simply attempt focus.
//
Expand All @@ -140,12 +140,16 @@ export class BrowserViewportScroller implements ViewportScroller {
* The offset can be used when we know that there is a floating header and scrolling naively to an
* element (ex: `scrollIntoView`) leaves the element hidden behind the floating header.
*/
private scrollToElement(el: HTMLElement): void {
private scrollToElement(el: HTMLElement, options?: ScrollOptions): void {
const rect = el.getBoundingClientRect();
const left = rect.left + this.window.pageXOffset;
const top = rect.top + this.window.pageYOffset;
const offset = this.offset();
this.window.scrollTo(left - offset[0], top - offset[1]);
this.window.scrollTo({
...options,
left: left - offset[0],
top: top - offset[1],
});
}
}

Expand Down
15 changes: 14 additions & 1 deletion packages/common/test/viewport_scroller_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ describe('BrowserViewportScroller', () => {
expect(() => scroller.setHistoryScrollRestoration('manual')).not.toThrow();
});

it('should not allow overwriting position with options', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to have a test where scrollOption actually matter?

scroller.scrollToPosition([10, 10], {top: 0, left: 0} as any);
expect(windowSpy.scrollTo).toHaveBeenCalledWith({top: 10, left: 10});
});

it('should still allow scrolling if scrollRestoration is not writable', () => {
createNonWritableScrollRestoration();
scroller.scrollToPosition([10, 10]);
expect(windowSpy.scrollTo as jasmine.Spy).toHaveBeenCalledWith(10, 10);
expect(windowSpy.scrollTo).toHaveBeenCalledWith({top: 10, left: 10});
});
});

Expand Down Expand Up @@ -98,6 +103,14 @@ describe('BrowserViewportScroller', () => {
cleanup();
});

it('should not allow overwriting position with options', () => {
const {anchorNode, cleanup} = createTallElementWithShadowRoot();
anchorNode.name = anchor;
scroller.scrollToAnchor(anchor, {top: 0, left: 0} as any);
expect(scroller.getScrollPosition()[1]).not.toEqual(0);
cleanup();
});

function createTallElement() {
const tallItem = document.createElement('div');
tallItem.style.height = '3000px';
Expand Down
0