Skip to content

Commit

Permalink
fix: save current theme to handle system theme change (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue authored Jan 25, 2025
1 parent 597681f commit a9a90d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
5 changes: 2 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
cd $(dirname $0)
export PATH=$PATH:$PWD/node_modules/.bin

esbuild --minify --bundle --outdir=temp lib/theme-script.ts
echo "" > lib/theme-script.astro
echo "{/* Auto-generated by build.sh */}" > lib/theme-script.astro
echo "<script is:inline>" >> lib/theme-script.astro
cat temp/theme-script.js >> lib/theme-script.astro
esbuild --minify --bundle lib/theme-script.ts >> lib/theme-script.astro
echo "</script>" >> lib/theme-script.astro

astro build
4 changes: 2 additions & 2 deletions lib/theme-script.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

{/* Auto-generated by build.sh */}
<script is:inline>
"use strict";(()=>{(()=>{let t="theme-toggle";function n(){return window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}function a(){let e=localStorage.getItem(t);return e==="dark"||e==="light"?e:null}function r(){return a()||n()}function c(e){e===n()?localStorage.removeItem(t):localStorage.setItem(t,e)}function l(e){let o=document.documentElement;o.classList.toggle("dark",e==="dark"),o.dataset.theme=e,o.style.colorScheme=e}function m(e){c(e),l(e)}function s(){m(r())}s(),document.addEventListener("astro:after-swap",s),window.astroThemeToggle={setTheme:m,getTheme:r}})();})();
"use strict";(()=>{(()=>{let t="theme-toggle",n;function r(){return window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}function l(){let e=localStorage.getItem(t);return e==="dark"||e==="light"?e:null}function m(){return n||l()||r()}function s(e){e===r()?localStorage.removeItem(t):localStorage.setItem(t,e)}function h(e){let o=document.documentElement;o.classList.toggle("dark",e==="dark"),o.dataset.theme=e,o.style.colorScheme=e}function a(e){n=e,s(e),h(e)}function c(){a(m())}c(),document.addEventListener("astro:after-swap",c),window.astroThemeToggle={setTheme:a,getTheme:m}})();})();
</script>
11 changes: 7 additions & 4 deletions lib/theme-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

const storageKey = 'theme-toggle'

let currentTheme: Theme | undefined

function getSystemTheme(): Theme {
return window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'
}

function getUserTheme(): Theme | null {
function getStoredTheme(): Theme | null {
const theme = localStorage.getItem(storageKey)
return theme === 'dark' || theme === 'light' ? theme : null
}

function getTheme(): Theme {
return getUserTheme() || getSystemTheme()
return currentTheme || getStoredTheme() || getSystemTheme()
}

function setUserTheme(theme: Theme) {
function setStoredTheme(theme: Theme) {
if (theme === getSystemTheme()) {
localStorage.removeItem(storageKey)
} else {
Expand All @@ -34,7 +36,8 @@
}

function setTheme(theme: Theme) {
setUserTheme(theme)
currentTheme = theme
setStoredTheme(theme)
setStyle(theme)
}

Expand Down

0 comments on commit a9a90d4

Please sign in to comment.