urbit/pkg/interface/webterm/lib/useDark.ts
tomholford 438e6d4df9 ui: style tabs
also rename join --> useDark; clean up extraneous logging statements
2022-03-03 17:10:54 -06:00

23 lines
629 B
TypeScript

import { useEffect, useState } from 'react';
import useTermState from '../state';
export function useDark() {
const [osDark, setOsDark] = useState(false);
useEffect(() => {
const themeWatcher = window.matchMedia('(prefers-color-scheme: dark)');
const update = (e: MediaQueryListEvent) => {
setOsDark(e.matches);
};
setOsDark(themeWatcher.matches);
themeWatcher.addEventListener('change', update);
return () => {
themeWatcher.removeEventListener('change', update);
}
}, []);
const theme = useTermState(s => s.theme);
return theme === 'dark' || (osDark && theme === 'auto');
}