fix(ux): defer old stylesheet removal until after page content is gone
Previous fix removed the old CSS when new CSS loaded, but that happened BEFORE the out-animation completed - causing a flash of unstyled content. Now the old stylesheet stays until replaceChildren removes the old DOM.
This commit is contained in:
+16
-15
@@ -70,9 +70,11 @@ function updateThemeColorForRoute(route) {
|
|||||||
let activePageStyle = null;
|
let activePageStyle = null;
|
||||||
|
|
||||||
function loadPageStyle(moduleName) {
|
function loadPageStyle(moduleName) {
|
||||||
if (!moduleName) return Promise.resolve();
|
if (!moduleName) return { ready: Promise.resolve(), cleanup: () => {} };
|
||||||
const href = `/styles/${moduleName}.css`;
|
const href = `/styles/${moduleName}.css`;
|
||||||
if (activePageStyle?.getAttribute('href') === href) return Promise.resolve();
|
if (activePageStyle?.getAttribute('href') === href) {
|
||||||
|
return { ready: Promise.resolve(), cleanup: () => {} };
|
||||||
|
}
|
||||||
|
|
||||||
const link = document.createElement('link');
|
const link = document.createElement('link');
|
||||||
link.rel = 'stylesheet';
|
link.rel = 'stylesheet';
|
||||||
@@ -80,20 +82,18 @@ function loadPageStyle(moduleName) {
|
|||||||
|
|
||||||
const oldLink = activePageStyle;
|
const oldLink = activePageStyle;
|
||||||
|
|
||||||
const loaded = new Promise((resolve) => {
|
const ready = new Promise((resolve) => {
|
||||||
link.onload = () => {
|
link.onload = resolve;
|
||||||
if (oldLink) oldLink.remove();
|
link.onerror = resolve;
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
link.onerror = () => {
|
|
||||||
if (oldLink) oldLink.remove();
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
document.head.appendChild(link);
|
document.head.appendChild(link);
|
||||||
activePageStyle = link;
|
activePageStyle = link;
|
||||||
return loaded;
|
|
||||||
|
return {
|
||||||
|
ready,
|
||||||
|
cleanup: () => { if (oldLink) oldLink.remove(); },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
@@ -202,9 +202,10 @@ async function renderPage(route, previousPath = null) {
|
|||||||
if (loading) loading.hidden = true;
|
if (loading) loading.hidden = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const style = loadPageStyle(route.module);
|
||||||
const [module] = await Promise.all([
|
const [module] = await Promise.all([
|
||||||
importPage(route.page),
|
importPage(route.page),
|
||||||
loadPageStyle(route.module),
|
style.ready,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (typeof module.render !== 'function') {
|
if (typeof module.render !== 'function') {
|
||||||
@@ -232,12 +233,12 @@ async function renderPage(route, previousPath = null) {
|
|||||||
await new Promise(r => setTimeout(r, 120));
|
await new Promise(r => setTimeout(r, 120));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seiten-Wrapper unsichtbar in den DOM einfügen, damit
|
// Alter Inhalt ist jetzt weg - altes Stylesheet kann entfernt werden
|
||||||
// document.getElementById() in render() die richtigen Elemente findet.
|
|
||||||
const pageWrapper = document.createElement('div');
|
const pageWrapper = document.createElement('div');
|
||||||
pageWrapper.className = 'page-transition';
|
pageWrapper.className = 'page-transition';
|
||||||
pageWrapper.style.opacity = '0';
|
pageWrapper.style.opacity = '0';
|
||||||
content.replaceChildren(pageWrapper);
|
content.replaceChildren(pageWrapper);
|
||||||
|
style.cleanup();
|
||||||
|
|
||||||
await module.render(pageWrapper, { user: currentUser });
|
await module.render(pageWrapper, { user: currentUser });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user