Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless-org/mindspace. Thoughtless blogs about God and the world mindspace.trueberryless.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156<!-- This is a critical style block to prevent flashing on page load in dark mode. It is injected into the <head> of the document before the main stylesheets are loaded.--><style is:inline> :root { --bg: #ffffff; }
html.dark { --bg: #1c1c1c; }
html { background-color: var(--bg); color-scheme: light; }
html.dark { color-scheme: dark; }</style>
<script is:inline> // Global Theme Manager ;(function () { // Prevent duplicate initialization if (window.ThemeManager && window.ThemeManager.initialized) { return }
window.ThemeManager = { STORAGE_KEY: 'chiri-theme', initialized: false,
getSystemTheme() { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' },
getStoredTheme() { try { return localStorage.getItem(this.STORAGE_KEY) } catch { return null } },
setStoredTheme(theme) { try { if (theme === 'system') { localStorage.removeItem(this.STORAGE_KEY) } else { localStorage.setItem(this.STORAGE_KEY, theme) } } catch (e) { console.warn('Failed to store theme preference:', e) } },
getEffectiveTheme() { const stored = this.getStoredTheme() return stored || this.getSystemTheme() },
isUsingSystemTheme() { return this.getStoredTheme() === null },
applyTheme(theme) { document.documentElement.classList.remove('light', 'dark') document.documentElement.classList.add(theme)
// Dispatch event for other components document.dispatchEvent( new CustomEvent('themechange', { detail: { theme, isUserChoice: !this.isUsingSystemTheme(), isSystemTheme: this.isUsingSystemTheme() } }) ) },
toggle() { const currentTheme = this.getEffectiveTheme() // Simply toggle between light and dark const newTheme = currentTheme === 'dark' ? 'light' : 'dark'
this.setStoredTheme(newTheme) this.applyTheme(newTheme) },
init() { if (this.initialized) return
// Set initial theme (maintain current theme when refreshing page) this.applyTheme(this.getEffectiveTheme())
// Listen for system theme changes window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => { const newSystemTheme = e.matches ? 'dark' : 'light'
// Always follow system theme changes and update stored theme preference this.setStoredTheme(newSystemTheme) this.applyTheme(newSystemTheme) })
this.initialized = true } }
// Initialize theme manager window.ThemeManager.init()
// Apply theme when needed const applyTheme = () => { if (window.ThemeManager) { const theme = window.ThemeManager.getEffectiveTheme() window.ThemeManager.applyTheme(theme) } }
// Apply immediately on script load applyTheme()
// Listen for navigation events document.addEventListener('astro:before-preparation', applyTheme)
// Apply theme after DOM changes (both swap and page load) const applyThemeAfterNavigation = () => { if (window.ThemeManager) { const currentTheme = window.ThemeManager.getEffectiveTheme() const appliedTheme = document.documentElement.classList.contains('dark') ? 'dark' : 'light'
if (currentTheme !== appliedTheme) { // Use requestAnimationFrame to ensure theme is applied in the next frame, avoiding conflicts with view transition requestAnimationFrame(() => { window.ThemeManager.applyTheme(currentTheme) }) } } }
document.addEventListener('astro:after-swap', applyThemeAfterNavigation) document.addEventListener('astro:page-load', applyThemeAfterNavigation)
// Handle system theme changes window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { if (window.ThemeManager && window.ThemeManager.isUsingSystemTheme()) { setTimeout(applyTheme, 0) } }) })()</script>