|
| 1 | +import { createSelector } from 'redux-bundler' |
| 2 | +import { readSetting, writeSetting } from './local-storage.js' |
| 3 | + |
| 4 | +const THEME_KEY = 'ipfs-theme' |
| 5 | +const THEMES = { |
| 6 | + LIGHT: 'light', |
| 7 | + DARK: 'dark', |
| 8 | + SYSTEM: 'system' |
| 9 | +} |
| 10 | + |
| 11 | +const getSystemTheme = () => { |
| 12 | + if (typeof window === 'undefined') return THEMES.LIGHT |
| 13 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? THEMES.DARK : THEMES.LIGHT |
| 14 | +} |
| 15 | + |
| 16 | +const getInitialTheme = () => { |
| 17 | + const saved = readSetting(THEME_KEY) |
| 18 | + if (saved && Object.values(THEMES).includes(saved)) { |
| 19 | + return saved |
| 20 | + } |
| 21 | + // Default to dark theme instead of system |
| 22 | + return THEMES.DARK |
| 23 | +} |
| 24 | + |
| 25 | +const applyTheme = (theme) => { |
| 26 | + const effectiveTheme = theme === THEMES.SYSTEM ? getSystemTheme() : theme |
| 27 | + document.documentElement.setAttribute('data-theme', effectiveTheme) |
| 28 | + document.documentElement.classList.remove('theme-light', 'theme-dark') |
| 29 | + document.documentElement.classList.add(`theme-${effectiveTheme}`) |
| 30 | +} |
| 31 | + |
| 32 | +const bundle = { |
| 33 | + name: 'theme', |
| 34 | + |
| 35 | + reducer: (state = getInitialTheme(), action) => { |
| 36 | + if (action.type === 'THEME_SET') { |
| 37 | + return action.payload |
| 38 | + } |
| 39 | + return state |
| 40 | + }, |
| 41 | + |
| 42 | + selectTheme: state => state.theme, |
| 43 | + |
| 44 | + selectEffectiveTheme: createSelector( |
| 45 | + 'selectTheme', |
| 46 | + (theme) => { |
| 47 | + return theme === THEMES.SYSTEM ? getSystemTheme() : theme |
| 48 | + } |
| 49 | + ), |
| 50 | + |
| 51 | + doSetTheme: (theme) => ({ dispatch }) => { |
| 52 | + if (!Object.values(THEMES).includes(theme)) { |
| 53 | + console.error(`Invalid theme: ${theme}`) |
| 54 | + return |
| 55 | + } |
| 56 | + writeSetting(THEME_KEY, theme) |
| 57 | + applyTheme(theme) |
| 58 | + dispatch({ type: 'THEME_SET', payload: theme }) |
| 59 | + }, |
| 60 | + |
| 61 | + doToggleTheme: () => ({ dispatch, store }) => { |
| 62 | + const currentTheme = store.selectTheme() |
| 63 | + const themes = [THEMES.LIGHT, THEMES.DARK, THEMES.SYSTEM] |
| 64 | + const currentIndex = themes.indexOf(currentTheme) |
| 65 | + const nextTheme = themes[(currentIndex + 1) % themes.length] |
| 66 | + dispatch({ actionCreator: 'doSetTheme', args: [nextTheme] }) |
| 67 | + }, |
| 68 | + |
| 69 | + init: (store) => { |
| 70 | + // Apply initial theme |
| 71 | + const theme = store.selectTheme() |
| 72 | + applyTheme(theme) |
| 73 | + |
| 74 | + // Listen for system theme changes |
| 75 | + if (typeof window !== 'undefined') { |
| 76 | + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') |
| 77 | + const handleChange = () => { |
| 78 | + const currentTheme = store.selectTheme() |
| 79 | + if (currentTheme === THEMES.SYSTEM) { |
| 80 | + applyTheme(THEMES.SYSTEM) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Modern browsers |
| 85 | + if (mediaQuery.addEventListener) { |
| 86 | + mediaQuery.addEventListener('change', handleChange) |
| 87 | + } else { |
| 88 | + // Fallback for older browsers |
| 89 | + mediaQuery.addListener(handleChange) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export default bundle |
| 96 | +export { THEMES } |
0 commit comments