// types.ts
export type ThemeMode = 'light' | 'dark' | 'system' | 'sepia';
export interface ThemeColors {
primary: string;
secondary: string;
tertiary: string;
background: string;
surface: string;
text: {
primary: string;
secondary: string;
disabled: string;
};
error: string;
warning: string;
success: string;
info: string;
border: string;
divider: string;
}
export interface Theme {
name: string;
colors: ThemeColors;
spacing: {
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
};
borderRadius: {
sm: string;
md: string;
lg: string;
pill: string;
};
shadows: {
sm: string;
md: string;
lg: string;
};
}
// themes.ts
import { Theme, ThemeMode } from './types';
export const lightTheme: Theme = {
name: 'Light',
colors: {
primary: '#0066cc',
secondary: '#6c757d',
tertiary: '#8a3ffc',
background: '#f5f5f5',
surface: '#ffffff',
text: {
primary: '#212121',
secondary: '#757575',
disabled: '#9e9e9e',
},
error: '#d32f2f',
warning: '#f57c00',
success: '#388e3c',
info: '#0288d1',
border: '#e0e0e0',
divider: '#eeeeee',
},
spacing: {
xs: '4px',
sm: '8px',
md: '16px',
lg: '24px',
xl: '32px',
},
borderRadius: {
sm: '4px',
md: '8px',
lg: '12px',
pill: '9999px',
},
shadows: {
sm: '0 1px 3px rgba(0,0,0,0.12)',
md: '0 4px 6px rgba(0,0,0,0.1)',
lg: '0 10px 15px rgba(0,0,0,0.1)',
},
};
export const darkTheme: Theme = {
name: 'Dark',
colors: {
primary: '#4dabf5',
secondary: '#b0bec5',
tertiary: '#be95ff',
background: '#121212',
surface: '#1e1e1e',
text: {
primary: '#ffffff',
secondary: '#b0bec5',
disabled: '#6c757d',
},
error: '#f44336',
warning: '#ffb74d',
success: '#66bb6a',
info: '#29b6f6',
border: '#424242',
divider: '#333333',
},
spacing: lightTheme.spacing,
borderRadius: lightTheme.borderRadius,
shadows: {
sm: '0 1px 3px rgba(0,0,0,0.24)',
md: '0 4px 6px rgba(0,0,0,0.24)',
lg: '0 10px 15px rgba(0,0,0,0.24)',
},
};
export const sepiaTheme: Theme = {
name: 'Sepia',
colors: {
primary: '#8c6d3f',
secondary: '#6b5a3d',
tertiary: '#a8805a',
background: '#f8f1e3',
surface: '#fdf5e6',
text: {
primary: '#5f4b32',
secondary: '#7d684a',
disabled: '#a89c88',
},
error: '#b5464a',
warning: '#d9a566',
success: '#739a6d',
info: '#7b95a8',
border: '#d3c4a8',
divider: '#e9dcc9',
},
spacing: lightTheme.spacing,
borderRadius: lightTheme.borderRadius,
shadows: lightTheme.shadows,
};
export const THEME_MODES: Record<string, ThemeMode> = {
LIGHT: 'light',
DARK: 'dark',
SYSTEM: 'system',
SEPIA: 'sepia',
};
// themeUtils.ts
import { Theme, ThemeMode } from './types';
import { lightTheme, darkTheme, sepiaTheme, THEME_MODES } from './themes';
export const getThemeByMode = (mode: ThemeMode): Theme => {
switch (mode) {
case THEME_MODES.LIGHT:
return lightTheme;
case THEME_MODES.DARK:
return darkTheme;
case THEME_MODES.SEPIA:
return sepiaTheme;
case THEME_MODES.SYSTEM:
return prefersDarkMode() ? darkTheme : lightTheme;
default:
return lightTheme;
}
};
export const prefersDarkMode = (): boolean => {
if (typeof window === 'undefined') return false;
return window.matchMedia('(prefers-color-scheme: dark)').matches;
};
export const listenToSystemThemeChanges = (callback: (isDark: boolean) => void): () => void => {
if (typeof window === 'undefined') return () => {};
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const listener = (e: MediaQueryListEvent) => callback(e.matches);
mediaQuery.addEventListener('change', listener);
return () => mediaQuery.removeEventListener('change', listener);
};
Would you like me to explain or break down this code?