Skip to content
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

Refactor scrollBehavior in router.ts to handle hash navigation and savedPosition (#581) #582

Merged
merged 1 commit into from
Apr 8, 2024
Merged
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.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions resources/src/plugins/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import routes from '@/router/routes';

export const Router = createRouter({
scrollBehavior(to, from, savedPosition) {
// Returning the savedPosition will result in a direct jump to the saved position
if (to.hash) {
// Check for a hash in the incoming route
return new Promise((resolve) => {
// Wait until the next tick to ensure the page has rendered
setTimeout(() => {
const element = document.querySelector(to.hash);
if (element) {
Expand All @@ -18,13 +15,11 @@ export const Router = createRouter({
behavior: 'smooth',
});
} else {
// Fallback to top of the page if the element is not found
resolve({ top: 0 });
}
}, 250); // Adjust delay as needed
}, 250);
});
}
console.log('savedPosition', savedPosition);
if (savedPosition) {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down