Comments (4)
I also experience the flickering issue with scroll position restoration - I think it is due to the restoration logic being wrapped in a settimeout (this commit).
When starting from a non-zero vertical scroll position and navigating forward I often see one frame of the new page at the current scroll position before it sets the scroll position to 0.
On backwards navigation, I often see one frame at the current scroll position before the restoration logic kicks in and restores the previous scroll position.
I think the settimeout was added to allow Angular to figure out the size of page before starting the scroll position logic. I understand why this is necessary when going backwards to a non-zero scroll position (full DOM height needs to be set before scrolling) but on a forward navigation going to a 0 scroll position I would have thought the setttimeout was unnecessary.
Also it seems other SPAs such as Github and Youtube are able to do the scroll position restoration synchronously and have no flickering issues even on backwards navigation.
from angular.
The workaround I did for the UX is, make the body opacity zero while the angular is calculating the scroll position(very very short time).
Caution: It doesn't solve the root cause of the problem, it's just deceiving the user's eye. And not good for when using auxiliary routes.
this.router.events
.subscribe((event: any) => {
if (event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
document.body.style.opacity = '0';
setTimeout(()=>{
document.body.style.opacity = '';
},1)
}
})
from angular.
import { ViewportScroller, Location } from '@angular/common';
import { Injectable } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RouterScrollService {
// Scroll positions
private scrollPositions = new Map();
//
// Constructor
constructor(
private router: Router,
private viewportScroller: ViewportScroller,
private location: Location
) {
// Subscribe to router events
this.router.events
.pipe(filter((event) => event instanceof NavigationStart))
.subscribe((event: any) => {
if (event.navigationTrigger === 'imperative') {
// Get current scroll position
const currentScrollPosition = this.viewportScroller.getScrollPosition();
// Get current url
const currentUrl = this.location.path();
// Scroll to the top
this.viewportScroller.scrollToPosition([0, 0]);
// Get current navigation object
const navObject = this.router.getCurrentNavigation();
// If the navigation is done with (NavigationExtras) replaceUrl: true
if (navObject?.extras.replaceUrl) {
this.scrollPositions.set(event.url, currentScrollPosition);
this.scrollPositions.delete(currentUrl);
}
// If the navigation is normal
else {
// Save the url and scroll position
this.scrollPositions.set(currentUrl, currentScrollPosition);
}
} else if (event.navigationTrigger === 'popstate') {
// If scroll position is saved
if (this.scrollPositions.has(event.url)) {
// Scroll to the saved scroll position
this.viewportScroller.scrollToPosition(this.scrollPositions.get(event.url));
} else {
// Scroll to the top
this.viewportScroller.scrollToPosition([0, 0]);
}
}
});
}
}
I've implemented a temporary solution that involves scrolling to the top during imperative route transitions and restoring the saved scroll position during popstate back transitions.
This is very temporary solution.
Flickering introduces by scrollPositionRestoration: 'enabled' is significantly impacting user experience. It's imperative that we address this issue as soon as possible to ensure optimal user satisfaction
from angular.
Is there any progress about this issue? I appreciate you working on it.
from angular.
Related Issues (20)
- dispatchEvent won't call FormGroup.valueChanges in test HOT 4
- Add signals reflecting state of the AbstractControl HOT 3
- Add WritableComputed to allow computed with write operations HOT 5
- Error Message for missing [ngModelOptions]="{standalone: true}" entry dont show line in code where it is missing
- signal(Observable<T>) to signal<T>() operator HOT 5
- I smell a rat, stop ruining angular HOT 4
- Hydration doesn't work for ng-templates that are defined inside of another component
- The system cannot find the file specified. HOT 1
- easier access to untracked function for more readability HOT 1
- Angular.dev: Missing Overview Page for Components HOT 2
- ViewRef broken "Change detection usage" link doesn't work
- Async Pipe for map of Observables HOT 2
- Clarification for member sequence
- Allow search by query param | angular.dev
- Confusing error when preserveSymlinks is missing. HOT 1
- Allow style URL to use absolute paths HOT 1
- Hello world tutorial is broken in several ways HOT 2
- devtools signal lookup incorrectly displays signal with empty string HOT 1
- devtools injector tree tab "hide injectors with no providers" filtering incorrectly HOT 1
- Custom Validator Directive won't fire when signal changes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular.