From c08360233d892feb12d81e846849d1ed7af4e3b0 Mon Sep 17 00:00:00 2001 From: prototypa Date: Sun, 3 Dec 2023 12:17:47 -0500 Subject: [PATCH] Fix issue #290: ViewTransitions on Layout.astro disables the theme --- src/components/common/BasicScripts.astro | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/components/common/BasicScripts.astro b/src/components/common/BasicScripts.astro index b24b0eb..d9c5ad5 100644 --- a/src/components/common/BasicScripts.astro +++ b/src/components/common/BasicScripts.astro @@ -17,16 +17,19 @@ import { UI } from '~/utils/config'; } } - if ((defaultTheme && defaultTheme.endsWith(':only')) || (!localStorage.theme && defaultTheme !== 'system')) { - applyTheme(defaultTheme.replace(':only', '')); - } else if ( - localStorage.theme === 'dark' || - (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches) - ) { - applyTheme('dark'); - } else { - applyTheme('light'); - } + const initTheme = function () { + if ((defaultTheme && defaultTheme.endsWith(':only')) || (!localStorage.theme && defaultTheme !== 'system')) { + applyTheme(defaultTheme.replace(':only', '')); + } else if ( + localStorage.theme === 'dark' || + (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches) + ) { + applyTheme('dark'); + } else { + applyTheme('light'); + } + }; + initTheme(); function attachEvent(selector, event, fn) { const matches = typeof selector === 'string' ? document.querySelectorAll(selector) : selector; @@ -134,12 +137,13 @@ import { UI } from '~/utils/config'; document.getElementById('header')?.classList.remove('h-screen'); document.getElementById('header')?.classList.remove('expanded'); document.querySelector('#header nav')?.classList.add('hidden'); - } + }; window.onload = onLoad; window.onpageshow = onPageShow; document.addEventListener('astro:after-swap', () => { + initTheme(); onLoad(); onPageShow(); });