Skip to content

feat: add simple dark mode #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html data-bs-theme="" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
Expand All @@ -24,12 +24,12 @@
<meta name="twitter:title" content="Rust Quiz" />
<meta name="twitter:description" content="What is the output of this Rust program?" />

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link id="hl-css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
<link rel="stylesheet" href="/rust-quiz/quiz.css">
</head>

<body class="bg-light">
<body>
<div class="page">
<div class="container">
<div class="pt-5">
Expand Down Expand Up @@ -110,13 +110,15 @@ <h2>Rust Quiz<span id="question-number" class="ms-4 notbold d-none">#<span id="c

<div id="reset" class="reset d-none">
<a href="javascript:reset();" class="reset-link"></a>
<a class="toggle-theme-link" data-bs-theme-value="light"></a>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/languages/rust.min.js"></script>
<script src="/rust-quiz/questions.js"></script>
<script src="/rust-quiz/quiz.js"></script>
<script src="/rust-quiz/theme-toggler.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions docs/quiz.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ p code {
color: crimson;
}

pre code {
font-size: .875em;
color: var(--bs-code-color);
word-wrap: break-word;
}

#form label {
display: inline !important;
}
Expand All @@ -95,3 +101,33 @@ blockquote {
padding-left: 30px;
font-style: italic;
}

.toggle-theme {
display: table-row;
vertical-align: bottom;
text-align: right;
height: 0;
line-height: 0;
}

.toggle-theme-link {
display: inline-block;
height: 0;
position: relative;
right: 70px;
bottom: 10px;
}

.toggle-theme-link:after {
content: "[Toggle Theme]";
}

[data-bs-theme=dark] p code {
--bs-code-color: #dd8888;
color: var(--bs-code-color);
}

[data-bs-theme=light] p code {
--bs-code-color: crimson;
color: var(--bs-code-color);
}
67 changes: 67 additions & 0 deletions docs/theme-toggler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(() => {
"use strict";
const hljsStyleCdnDefault =
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css";
const hljsStyleCdnDark =
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/base16/synth-midnight-terminal-dark.min.css";

const hlCssLinkTag = document.querySelector("#hl-css");
const storedTheme = localStorage.getItem("theme");
const cdnMap = {
light: hljsStyleCdnDefault,
dark: hljsStyleCdnDark,
};
const getPreferredTheme = () => {
if (storedTheme) {
return storedTheme;
}
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};
const setTheme = function (theme) {
if (
theme === "auto" &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
document.documentElement.setAttribute("data-bs-theme", "dark");
} else {
document.documentElement.setAttribute("data-bs-theme", theme);
}
};
const getToggleToTheme = (currentTheme) =>
currentTheme === "dark" ? "light" : "dark";
const initTheme = () => {
const currentTheme = getPreferredTheme();
setTheme(currentTheme);
const toggleTargetTheme = getToggleToTheme(currentTheme);
hlCssLinkTag.setAttribute("href", cdnMap?.[currentTheme]);
document.querySelectorAll("[data-bs-theme-value]").forEach((toggle) => {
const toggleTheme = () => {
document
.querySelectorAll("[data-bs-theme-value]")
.forEach((_toggle) => {
const targetTheme = getToggleToTheme(
document.documentElement.getAttribute("data-bs-theme"),
);
hlCssLinkTag.setAttribute("href", cdnMap?.[targetTheme]);
setTheme(targetTheme);
localStorage.setItem("theme", targetTheme);
});
};
console.log("", toggleTargetTheme);
toggle.setAttribute("data-bs-theme-value", toggleTargetTheme);
toggle.addEventListener("click", toggleTheme);
});
};
initTheme();
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
if (storedTheme !== "light" || storedTheme !== "dark") {
setTheme(getPreferredTheme());
}
});

window.rustQuizSetTheme = setTheme;
})();