Reduce navigation flash with instant theme background and view transitions.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+20
-1
@@ -41,10 +41,29 @@
|
||||
backdrop.addEventListener('click', closeNav);
|
||||
}
|
||||
|
||||
nav.querySelectorAll('a').forEach(function (link) {
|
||||
function prefetchNav(href) {
|
||||
if (!href || href.indexOf(window.location.origin) !== 0) return;
|
||||
if (href === window.location.href) return;
|
||||
var links = document.head.querySelectorAll('link[rel="prefetch"]');
|
||||
for (var i = 0; i < links.length; i++) {
|
||||
if (links[i].href === href) return;
|
||||
}
|
||||
var link = document.createElement('link');
|
||||
link.rel = 'prefetch';
|
||||
link.href = href;
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
|
||||
nav.querySelectorAll('a[href]').forEach(function (link) {
|
||||
link.addEventListener('click', function () {
|
||||
if (isMobileNav()) closeNav();
|
||||
});
|
||||
link.addEventListener('mouseenter', function () {
|
||||
prefetchNav(link.href);
|
||||
});
|
||||
link.addEventListener('touchstart', function () {
|
||||
prefetchNav(link.href);
|
||||
}, { passive: true });
|
||||
});
|
||||
|
||||
window.addEventListener('resize', function () {
|
||||
|
||||
@@ -19,10 +19,14 @@
|
||||
theme = 'dark';
|
||||
}
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
document.documentElement.style.colorScheme = theme;
|
||||
document.documentElement.style.background = theme === 'light' ? '#e8eef8' : '#050508';
|
||||
try {
|
||||
localStorage.setItem(KEY, theme);
|
||||
} catch (e) { /* ignore */ }
|
||||
updateButtons(theme);
|
||||
var meta = document.getElementById('meta-theme-color');
|
||||
if (meta) meta.setAttribute('content', theme === 'light' ? '#e8eef8' : '#050508');
|
||||
}
|
||||
|
||||
var saved = null;
|
||||
|
||||
Reference in New Issue
Block a user