Attempt to fix #514

This commit is contained in:
Lea Verou
2025-01-14 11:27:17 -05:00
parent e7979991e3
commit 0ac7916a1b
3 changed files with 10 additions and 8 deletions

View File

@@ -83,8 +83,6 @@
</details>
</nav>
{% block header %}
{% include 'breadcrumbs.njk' %}
<h1 class="title">{{ title }}</h1>

View File

@@ -30,18 +30,18 @@
<script type="module" src="/assets/scripts/theme-picker.js"></script>
{# Preset Theme #}
<link id="theme-stylesheet" rel="stylesheet" id="theme-stylesheet" href="/dist/styles/themes/{{ forceTheme or 'default' }}.css" render="blocking" fetchpriority="high" />
<link id="theme-stylesheet" rel="stylesheet" href="/dist/styles/themes/{{ forceTheme or 'default' }}.css" render="blocking" fetchpriority="high" />
{% if not forceTheme %}
<script>
if (localStorage.presetTheme) {
let preset = localStorage.presetTheme;
let script = document.currentScript;
let link = script.previousElementSibling;
let link = document.getElementById('theme-stylesheet');
let newLink = link.cloneNode();
newLink.href = link.href.replace("/default.css", `/${preset}.css`);
newLink.classList.add('wa-preset-theme');
newLink.addEventListener('load', () => {
link.remove();
});
}, { once: true });
link.after(newLink);
}
</script>

View File

@@ -5,7 +5,7 @@ const presetTheme = new ThemeAspect({
key: 'presetTheme',
picker: 'wa-select.preset-theme-selector',
applyChange() {
applyChange(options = {}) {
const oldStylesheets = [...document.querySelectorAll('#theme-stylesheet')];
const oldStylesheet = oldStylesheets.pop();
@@ -38,7 +38,7 @@ const presetTheme = new ThemeAspect({
oldStylesheet.remove();
},
{ behavior: 'smooth' },
{ behavior: 'smooth', ...options },
);
},
{ once: true },
@@ -61,4 +61,8 @@ function updateSelectionBeforeTurboLoad(e) {
document.addEventListener(eventName, updateSelectionBeforeTurboLoad);
});
window.addEventListener('turbo:render', e => {
presetTheme.applyChange({ behavior: 'instant' });
});
window.presetTheme = presetTheme;