about summary refs log tree commit diff
path: root/bskyembed/src/color-mode.ts
blob: 2b392c6178ea2890823b178a35489b137a26dbe6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export function applyTheme(theme: 'light' | 'dark') {
  document.documentElement.classList.remove('light', 'dark')
  document.documentElement.classList.add(theme)
}

export function initColorMode() {
  applyTheme(
    window.matchMedia('(prefers-color-scheme: dark)').matches
      ? 'dark'
      : 'light',
  )
  window
    .matchMedia('(prefers-color-scheme: dark)')
    .addEventListener('change', mql => {
      applyTheme(mql.matches ? 'dark' : 'light')
    })
}