Skip to content

Commit 17dce9f

Browse files
committed
Implement theme initialisation
Runs before body has rendered to prevent flashes of unstyled content
1 parent 0d97ed4 commit 17dce9f

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

index.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,22 @@
4242
name="twitter:description"
4343
content="Track, Discover and Share Anime and Manga"
4444
/>
45+
46+
<!-- sets theme before body loads so that we don't get a flash of unstyled content.
47+
Cannot be of type module, as that will run it after <body> has initialised -->
48+
<script src="/src/theme-init.ts"></script>
49+
<link
50+
rel="stylesheet"
51+
type="text/css"
52+
href="/src/styles/index.css"
53+
media="screen"
54+
/>
4555
</head>
4656

4757
<body>
48-
<div id="app"><!--ssr-outlet--></div>
58+
<div id="app">
59+
<!--ssr-outlet-->
60+
</div>
4961
<script type="module" src="/src/entry-client.tsx"></script>
5062
</body>
5163
</html>

src/styles/themes/dark.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* stylelint-disable scale-unlimited/declaration-strict-value */
22

3-
.theme-dark {
3+
:root[data-theme='dark'] {
44
/***** Color Palette *****/
55
--red: #da5e51;
66
--light-red: #fc755c;

src/styles/themes/light.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* stylelint-disable scale-unlimited/declaration-strict-value */
22

3-
.theme-light {
3+
:root {
44
/***** Color Palette *****/
55
--red: #da5e51;
66
--light-red: #fc755c;

src/styles/themes/oled.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* stylelint-disable scale-unlimited/declaration-strict-value */
22

3-
.theme-oled {
3+
:root[data-theme='oled'] {
44
/***** Color Palette *****/
55
--red: #da5e51;
66
--light-red: #fc755c;

src/theme-init.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is imported directly in index.html to eliminate flash of unstyled content
2+
// Do NOT add an export statement to this, or it will break
3+
const themeKey = 'theme';
4+
5+
const getPreferredTheme = (): string => {
6+
const storedTheme = localStorage.getItem(themeKey);
7+
8+
return storedTheme ??
9+
window.matchMedia('(prefers-color-scheme: dark)').matches
10+
? 'dark'
11+
: 'light';
12+
};
13+
14+
const setTheme = () => {
15+
const body = document.querySelector(':root');
16+
body?.setAttribute(`data-${themeKey}`, getPreferredTheme());
17+
};
18+
19+
setTheme();

tsconfig.head.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"isolatedModules": false
5+
},
6+
"include": ["src/theme-init.ts"]
7+
}

tsconfig.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
"isolatedModules": true,
2121
"allowJs": false
2222
},
23-
"include": ["./src"]
23+
"include": ["./src"],
24+
"references": [
25+
{ "path": "./tsconfig.node.json" },
26+
{ "path": "./tsconfig.head.json" }
27+
]
2428
}

0 commit comments

Comments
 (0)