Registering the system them listener only once

This commit is contained in:
Jesús Espino 2021-03-21 08:51:22 +01:00
parent bc9be643a5
commit 0c39baae47
2 changed files with 13 additions and 9 deletions

View file

@ -4,11 +4,11 @@ import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
import {loadTheme} from './theme'
import {initThemes} from './theme'
import './styles/variables.scss'
import './styles/main.scss'
import './styles/labels.scss'
loadTheme()
initThemes()
ReactDOM.render(<App/>, document.getElementById('main-app'))

View file

@ -127,13 +127,6 @@ export function setTheme(theme: Theme | null): Theme {
}
export function loadTheme(): Theme {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)')
darkThemeMq.addListener(() => {
const themeStr = localStorage.getItem('theme')
if (!themeStr) {
setTheme(null)
}
})
const themeStr = localStorage.getItem('theme')
if (themeStr) {
try {
@ -146,3 +139,14 @@ export function loadTheme(): Theme {
return setTheme(null)
}
}
export function initThemes(): void {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)')
darkThemeMq.addEventListener('change', () => {
const themeStr = localStorage.getItem('theme')
if (!themeStr) {
setTheme(null)
}
})
loadTheme()
}