Skip to content

Commit 9045feb

Browse files
committed
feat: check prefers-color-scheme for theme on load
1 parent d82565f commit 9045feb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

playground/app.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { isValidUrl } from './utils/isValidUrl';
2424
import CompilerWorker from '../src/workers/compiler?worker';
2525
import FormatterWorker from '../src/workers/formatter?worker';
2626
import useZoom from '../src/hooks/useZoom';
27+
import { isDarkTheme } from './utils/isDarkTheme';
2728

2829
(window as any).MonacoEnvironment = {
2930
getWorker(_moduleId: unknown, label: string) {
@@ -96,7 +97,7 @@ export const App = (): JSX.Element => {
9697
'noEditableTabs',
9798
].map((key) => key in params);
9899

99-
const [dark, setDark] = createSignal(localStorage.getItem('dark') === 'true');
100+
const [dark, setDark] = createSignal(isDarkTheme());
100101

101102
createEffect(() => {
102103
const action = dark() ? 'add' : 'remove';

playground/utils/isDarkTheme.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const isDarkTheme = () => {
2+
if (typeof window !== 'undefined') {
3+
if (window.localStorage) {
4+
const isDarkTheme = window.localStorage.getItem('dark');
5+
if (typeof isDarkTheme === 'string') {
6+
return isDarkTheme === 'true';
7+
}
8+
}
9+
10+
const userMedia = window.matchMedia('(prefers-color-scheme: dark)');
11+
if (userMedia.matches) {
12+
return true;
13+
}
14+
}
15+
16+
// Default theme is light.
17+
return false;
18+
};

0 commit comments

Comments
 (0)