Skip to content
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

Auto light/dark theme and button toggle to change theme #38

Merged
Merged
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
82 changes: 82 additions & 0 deletions web/themeSwitcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*/
(() => {
"use strict";
const getStoredTheme = () => localStorage.getItem("theme");
const setStoredTheme = (theme) => localStorage.setItem("theme", theme);

const getPreferredTheme = () => {
const storedTheme = getStoredTheme();
if (storedTheme) {
return storedTheme;
}

return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};

const setTheme = (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);
}
};

setTheme(getPreferredTheme());

const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector("#bd-theme");

if (!themeSwitcher) {
return;
}

const themeSwitcherText = document.querySelector("#bd-theme-text");
const btnToActive = document.querySelector(
`[data-bs-theme-value="${theme}"]`
);

document.querySelectorAll("[data-bs-theme-value]").forEach((element) => {
element.classList.remove("active");
element.setAttribute("aria-pressed", "false");
});

btnToActive.classList.add("active");
btnToActive.setAttribute("aria-pressed", "true");
themeSwitcher.textContent = `Theme (${btnToActive.dataset.bsThemeValue})`;

if (focus) {
themeSwitcher.focus();
}
};

window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
const storedTheme = getStoredTheme();
if (storedTheme !== "light" && storedTheme !== "dark") {
setTheme(getPreferredTheme());
}
});

window.addEventListener("DOMContentLoaded", () => {
showActiveTheme(getPreferredTheme());

document.querySelectorAll("[data-bs-theme-value]").forEach((toggle) => {
toggle.addEventListener("click", () => {
const theme = toggle.getAttribute("data-bs-theme-value");
setStoredTheme(theme);
setTheme(theme);
showActiveTheme(theme, true);
});
});
});
})();
61 changes: 45 additions & 16 deletions web/timeseries.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM"
crossorigin="anonymous"
/>
<script src="./themeSwitcher.js"></script>
<script
src="https://kit.fontawesome.com/f6cc126dcc.js"
crossorigin="anonymous"
Expand Down Expand Up @@ -249,29 +250,49 @@
>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/blaylockbk/Herbie">🏁 Herbie</a>
<a class="nav-link" href="https://github.com/blaylockbk/Herbie"
>🏁 Herbie</a
>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/blaylockbk/goes2go">🌎 GOES-2-go</a>
<a class="nav-link" href="https://github.com/blaylockbk/goes2go"
>🌎 GOES-2-go</a
>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/blaylockbk/SynopticPy">🌡️ SynopticPy</a>
<a
class="nav-link"
href="https://github.com/blaylockbk/SynopticPy"
>🌡️ SynopticPy</a
>
</li>
</ul>
</div>
<!-- Title -->
<a class="navbar-brand"
><h2 style="display: inline">Station Timeseries</h2></a
>
<!-- README button; trigger modal -->
<button
type="button"
class="btn btn-outline-info"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
README
</button>
<div class="d-flex gap-2">
<div class="dropdown">
<button class="btn btn-outline-light dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" id="bd-theme">
Theme
</button>
<ul class="dropdown-menu">
<li><button class="dropdown-item" data-bs-theme-value="auto">Auto</button></li>
<li><button class="dropdown-item" data-bs-theme-value="light">Light</li>
<li><button class="dropdown-item" data-bs-theme-value="dark">Dark</li>
</ul>
</div>
<!-- README button; trigger modal -->
<button
type="button"
class="btn btn-outline-info"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
README
</button>
</div>
</div>
</nav>

Expand Down Expand Up @@ -877,7 +898,9 @@ <h2 class="accordion-header">
value="english"
checked
/>
<label class="btn btn-outline-dark btn-sm" for="unitsRadio1"
<label
class="btn btn-outline-secondary btn-sm"
for="unitsRadio1"
>English Units</label
>
<input
Expand All @@ -888,7 +911,9 @@ <h2 class="accordion-header">
autocomplete="off"
value="metric"
/>
<label class="btn btn-outline-dark btn-sm" for="unitsRadio2"
<label
class="btn btn-outline-secondary btn-sm"
for="unitsRadio2"
>Metric Units</label
>
</div>
Expand All @@ -908,7 +933,9 @@ <h2 class="accordion-header">
value="local"
checked
/>
<label class="btn btn-outline-dark btn-sm" for="timezoneRadio1"
<label
class="btn btn-outline-secondary btn-sm"
for="timezoneRadio1"
>Local Time</label
>
<input
Expand All @@ -919,7 +946,9 @@ <h2 class="accordion-header">
autocomplete="off"
value="utc"
/>
<label class="btn btn-outline-dark btn-sm" for="timezoneRadio2"
<label
class="btn btn-outline-secondary btn-sm"
for="timezoneRadio2"
>UTC Time</label
>
</div>
Expand Down