8000 fixup! feat(docs-infra): saves the scroll position before the change … · angular/angular@bf6bc41 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit bf6bc41

Browse files
committed
fixup! feat(docs-infra): saves the scroll position before the change of location
1 parent 09b3d1f commit bf6bc41

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

aio/src/app/app.component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ export class AppComponent implements OnInit {
248248

249249
@HostListener('click', ['$event.target', '$event.button', '$event.ctrlKey', '$event.metaKey', '$event.altKey'])
250250
onClick(eventTarget: HTMLElement, button: number, ctrlKey: boolean, metaKey: boolean, altKey: boolean): boolean {
251-
// We update the scroll position in the case there is no scroll event on the page before
252-
this.scrollService.updateScrollPositionInHistory();
253-
254251
// Hide the search results if we clicked outside both the "search box" and the "search results"
255252
if (!this.searchElements.some(element => element.nativeElement.contains(eventTarget))) {
256253
this.hideSearchResults();

aio/src/app/shared/scroll.service.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export class ScrollService {
6060
// the type is `hashchange` when the fragment identifier of the URL has changed. It allows us to go to position
6161
// just before a click on an anchor
6262
if (event.type === 'hashchange') {
63-
this.popStateFired = false;
6463
this.scrollToPosition();
6564
} else {
6665
// The popstate event is always triggered by doing a browser action such as a click on the back or forward button.
@@ -94,17 +93,23 @@ export class ScrollService {
9493
}
9594

9695
/**
97-
* When we load a document, we have to scroll to the correct position depending on whether this is a new location
98-
* , a back/forward in the history, or a refresh
96+
* When we load a document, we have to scroll to the correct position depending on whether this is a new location,
97+
* a back/forward in the history, or a refresh
9998
* @param delay before we scroll to the good position
10099
*/
101100
scrollAfterRender(delay: number) {
102-
// If we do rendering following a refresh, we use the scroll Position from the storage.
103-
if (this.getStoredScrollPosition()) {
104-
this.viewportScroller.scrollToPosition(this.getStoredScrollPosition() !);
101+
// If we do rendering following a refresh, we use the scroll position from the storage.
102+
const storedScrollPosition = this.getStoredScrollPosition();
103+
if (storedScrollPosition) {
104+
this.viewportScroller.scrollToPosition(storedScrollPosition);
105105
} else {
106-
if (!this.needToFixScrollPosition()) {
107-
// The document was reloaded following a link. If the location contains a hash, we have to wait for async
106+
if (this.needToFixScrollPosition()) {
107+
// The document was reloaded following a popState `event` (called by the forward/back button), so we manage
108+
// the scroll position
109+
this.scrollToPosition();
110+
} else {
111+
// The document was loaded either of the following cases: a direct navigation via typing the URL in the
112+
// address bar or a click on a link. If the location contains a hash, we have to wait for async
108113
// layout.
109114
if (this.isLocationWithHash()) {
110115
// Scroll 500ms after the new document has been inserted into the doc-viewer.
@@ -114,9 +119,6 @@ export class ScrollService {
114119
// If the location doesn't contain a hash, we scroll to the top of the page.
115120
this.scrollToTop();
116121
}
117-
} else {
118-
// The document was reloaded following a popState `event`, so we manage the scroll scrollPosition
119-
this.scrollToPosition();
120122
}
121123
}
122124
}

0 commit comments

Comments
 (0)
0