diff options
| author | Alejandro Soto <alejandro@34project.org> | 2025-09-13 14:55:15 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2025-09-13 14:55:15 -0600 |
| commit | a2ea06d513a5802964f8f0ef5795cec7e548ed7b (patch) | |
| tree | 8afb58e3749d19bc46cffc6473f3059d647c515b /static/js/initializeTheme.js | |
Squashed 'themes/tabi-lean/' content from commit 95c8796
git-subtree-dir: themes/tabi-lean
git-subtree-split: 95c879696445ede40daa7a30a88dae5dd74d5c0c
Diffstat (limited to 'static/js/initializeTheme.js')
| -rw-r--r-- | static/js/initializeTheme.js | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/static/js/initializeTheme.js b/static/js/initializeTheme.js new file mode 100644 index 0000000..95e754c --- /dev/null +++ b/static/js/initializeTheme.js @@ -0,0 +1,25 @@ +(function () { + // Get the default theme from the HTML data-theme attribute. + const defaultTheme = document.documentElement.getAttribute('data-theme'); + + // Set the data-default-theme attribute only if defaultTheme is not null. + if (defaultTheme) { + document.documentElement.setAttribute('data-default-theme', defaultTheme); + } + + // Attempt to retrieve the current theme from the browser's local storage. + const storedTheme = localStorage.getItem('theme'); + + if (storedTheme) { + document.documentElement.setAttribute('data-theme', storedTheme); + } else if (defaultTheme) { + document.documentElement.setAttribute('data-theme', defaultTheme); + } else { + // If no theme is found in local storage and no default theme is set, use user's system preference. + const isSystemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; + document.documentElement.setAttribute( + 'data-theme', + isSystemDark ? 'dark' : 'light' + ); + } +})(); |
