Skip to content

Commit 569505c

Browse files
committed
Auto-detect preferred colorscheme
1 parent 6f26383 commit 569505c

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

util/gh-pages/script.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,23 @@ function setTheme(theme, store) {
343343
let enableNight = false;
344344
let enableAyu = false;
345345

346-
if (theme == "ayu") {
347-
enableAyu = true;
348-
} else if (theme == "coal" || theme == "navy") {
349-
enableNight = true;
350-
} else if (theme == "rust") {
351-
enableHighlight = true;
352-
} else {
353-
enableHighlight = true;
354-
// this makes sure that an unknown theme request gets set to a known one
355-
theme = "light";
346+
switch(theme) {
347+
case "ayu":
348+
enableAyu = true;
349+
break;
350+
case "coal":
351+
case "navy":
352+
enableNight = true;
353+
break;
354+
case "rust":
355+
enableHighlight = true;
356+
break;
357+
default:
358+
enableHighlight = true;
359+
theme = "light";
360+
break;
356361
}
362+
357363
document.getElementsByTagName("body")[0].className = theme;
358364

359365
document.getElementById("styleHighlight").disabled = !enableHighlight;
@@ -368,4 +374,10 @@ function setTheme(theme, store) {
368374
}
369375

370376
// loading the theme after the initial load
371-
setTheme(localStorage.getItem('clippy-lint-list-theme'), false);
377+
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
378+
const theme = localStorage.getItem('clippy-lint-list-theme');
379+
if (prefersDark.matches && !theme) {
380+
setTheme("coal", false);
381+
} else {
382+
setTheme(theme, false);
383+
}

0 commit comments

Comments
 (0)