Add better scroll position management

This commit is contained in:
Caleb Braaten 2026-01-09 00:00:06 -08:00
parent 960f7ff4d0
commit cc21c06641

View File

@ -1,7 +1,7 @@
// Client-side script that runs on page load
// Example: TypeScript with type annotations
async function loadContent(url: string) {
async function loadContent(url: string, shouldScrollToTop: boolean = true) {
const response = await fetch(url, {
headers: {
'shell-loaded': 'true'
@ -13,6 +13,10 @@ async function loadContent(url: string) {
mainElement.outerHTML = html;
// Re-attach handlers to new links after content swap
attachLinkHandlers();
// Smooth scroll to top after navigation if needed
if (shouldScrollToTop) {
window.scrollTo({ top: 0, behavior: 'smooth' });
}
}
}
@ -27,14 +31,14 @@ function attachLinkHandlers() {
e.stopPropagation();
e.stopImmediatePropagation();
window.history.pushState({}, '', link.href);
await loadContent(link.href);
await loadContent(link.href, true);
}
});
}
// Listen for back/forward button clicks
window.addEventListener('popstate', async (event) => {
await loadContent(window.location.href);
await loadContent(window.location.href, false);
});
document.addEventListener('DOMContentLoaded', () => {