(theme: 'system' | 'light' | 'dark')
| 8 | * @param theme - The desired theme ('system', 'light', or 'dark') |
| 9 | */ |
| 10 | export function syncThemeToNextThemes(theme: 'system' | 'light' | 'dark') { |
| 11 | if (typeof window === 'undefined') return |
| 12 | |
| 13 | const oldValue = localStorage.getItem('sim-theme') |
| 14 | localStorage.setItem('sim-theme', theme) |
| 15 | |
| 16 | window.dispatchEvent( |
| 17 | new StorageEvent('storage', { |
| 18 | key: 'sim-theme', |
| 19 | newValue: theme, |
| 20 | oldValue: oldValue, |
| 21 | storageArea: localStorage, |
| 22 | url: window.location.href, |
| 23 | }) |
| 24 | ) |
| 25 | |
| 26 | const root = document.documentElement |
| 27 | root.classList.remove('light', 'dark') |
| 28 | |
| 29 | if (theme === 'system') { |
| 30 | const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
| 31 | root.classList.add(systemTheme) |
| 32 | } else { |
| 33 | root.classList.add(theme) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Gets the current theme from next-themes localStorage |
no test coverage detected