All files / src/desktop Theme.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23    86x       80x 1x 1x 1x   79x 79x 80x 15x 79x       11x 11x    
export type Theme = 'system' | 'light' | 'dark';
 
export const onMac = () => /Macintosh|Mac OS X/.test(navigator.userAgent);
 
/** macOS windows always use the main window's dark solid look, so the editor must match it. */
export function applyTheme(): Theme {
  if (onMac()) {
    document.documentElement.dataset.theme = 'dark';
    document.documentElement.dataset.appearance = 'mac';
    return 'dark';
  }
  const saved = localStorage.getItem('theme');
  const theme = saved === 'light' || saved === 'dark' ? saved : 'system';
  if (theme === 'system') delete document.documentElement.dataset.theme;
  else document.documentElement.dataset.theme = theme;
  return theme;
}
 
export function changeTheme(theme: Theme) {
  localStorage.setItem('theme', theme);
  applyTheme();
}