Skip to content

Commit b44d5b1

Browse files
authored
Prevent dark mode flash on page refresh (#182)
1 parent b16ce07 commit b44d5b1

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

index.html

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
content="Yorkie Dashboard is an administrative tool that allows user to manage projects and documents."
3232
/>
3333

34+
<script>
35+
(function () {
36+
const theme = window.localStorage.getItem('theme') || 'system';
37+
const isDarkMode =
38+
theme === 'dark' || (theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches);
39+
if (isDarkMode) {
40+
window.document.documentElement.classList.add('darkmode');
41+
window.document.documentElement.style.colorScheme = 'dark';
42+
}
43+
})();
44+
</script>
3445
<!-- Start Single Page Apps for GitHub Pages -->
3546
<script type="text/javascript">
3647
// Single Page Apps for GitHub Pages

src/App.tsx

+27-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import React, { useEffect } from 'react';
18-
import './assets/styles/style.scss'
18+
import './assets/styles/style.scss';
1919
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
2020
import {
2121
PublicRoute,
@@ -37,12 +37,37 @@ import { DocumentDetail } from 'features/documents';
3737
import { selectPreferences } from 'features/users/usersSlice';
3838
import { TestPage, ButtonView, PopoverView, DropdownView, InputView, BreadcrumbView, ModalView } from 'test';
3939

40+
const applyTheme = (theme: 'light' | 'dark') => {
41+
if (theme === 'light') {
42+
window.document.documentElement.classList.remove('darkmode');
43+
window.document.documentElement.style.colorScheme = 'light';
44+
} else {
45+
window.document.documentElement.classList.add('darkmode');
46+
window.document.documentElement.style.colorScheme = 'dark';
47+
}
48+
};
49+
4050
function App() {
4151
const { theme } = useAppSelector(selectPreferences);
4252
useEffect(() => {
43-
document.body.classList.toggle('darkmode', theme.darkMode);
53+
applyTheme(theme.darkMode ? 'dark' : 'light');
4454
}, [theme.darkMode]);
4555

56+
useEffect(() => {
57+
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
58+
const handleSystemThemeChange = (e: MediaQueryListEvent) => {
59+
applyTheme(e.matches ? 'dark' : 'light');
60+
};
61+
62+
if (theme.useSystem) {
63+
applyTheme(mediaQueryList.matches ? 'dark' : 'light');
64+
mediaQueryList.addEventListener('change', handleSystemThemeChange);
65+
}
66+
return () => {
67+
mediaQueryList.removeEventListener('change', handleSystemThemeChange);
68+
};
69+
}, [theme.useSystem]);
70+
4671
return (
4772
<Router basename={import.meta.env.BASE_URL}>
4873
<Routes>

src/assets/styles/base/_common.scss

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ a {
129129
@extend %visuallyhidden;
130130
}
131131

132+
html {
133+
background-color: var(--gray-000);
134+
}
135+
132136
// color
133137
$palette: (
134138
'gray': (

0 commit comments

Comments
 (0)