Skip to content

Commit 66f919b

Browse files
committed
_app.tsx: set initial light or dark mode based on browser instead of defaulting to light mode
1 parent ca3b8dd commit 66f919b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

.github/workflows/nextjs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
- name: Setup Node
5757
uses: actions/setup-node@v4
5858
with:
59-
node-version: "23"
59+
node-version: "22"
6060
cache: ${{ steps.detect-package-manager.outputs.manager }}
6161
- name: Setup Pages
6262
uses: actions/configure-pages@v5

pages/_app.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ let darkSwitch: HTMLInputElement;
2525
*/
2626
function initTheme() {
2727
if(darkSwitch){
28+
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
2829
const darkThemeSelected =
29-
localStorage.getItem('darkSwitch') !== null &&
30-
localStorage.getItem('darkSwitch') === 'dark';
30+
(localStorage.getItem("darkSwitch") !== null &&
31+
localStorage.getItem("darkSwitch") === "dark") ||
32+
(localStorage.getItem("darkSwitch") === null && darkThemeMq.matches);
3133
darkSwitch['checked'] = darkThemeSelected;
3234
darkThemeSelected ? document.body.setAttribute('data-theme', 'dark') :
3335
document.body.removeAttribute('data-theme');
@@ -47,7 +49,7 @@ function resetTheme() {
4749
localStorage.setItem('darkSwitch', 'dark');
4850
} else {
4951
document.body.removeAttribute('data-theme');
50-
localStorage.removeItem('darkSwitch');
52+
localStorage.setItem("darkSwitch", "light");
5153
}
5254
}
5355

0 commit comments

Comments
 (0)