|
| 1 | +/*! |
| 2 | + * Color mode toggler for Bootstrap's docs (https://getbootstrap.com/) |
| 3 | + * Copyright 2011-2025 The Bootstrap Authors |
| 4 | + * Licensed under the Creative Commons Attribution 3.0 Unported License. |
| 5 | + */ |
| 6 | + |
| 7 | +(() => { |
| 8 | + "use strict"; |
| 9 | + |
| 10 | + const getStoredTheme = () => localStorage.getItem("theme"); |
| 11 | + const setStoredTheme = (theme) => localStorage.setItem("theme", theme); |
| 12 | + |
| 13 | + const getPreferredTheme = () => { |
| 14 | + const storedTheme = getStoredTheme(); |
| 15 | + if (storedTheme) { |
| 16 | + return storedTheme; |
| 17 | + } |
| 18 | + |
| 19 | + return window.matchMedia("(prefers-color-scheme: dark)").matches |
| 20 | + ? "dark" |
| 21 | + : "light"; |
| 22 | + }; |
| 23 | + |
| 24 | + const setTheme = (theme) => { |
| 25 | + let iconElem = document |
| 26 | + .getElementById("themeDropdown") |
| 27 | + .getElementsByTagName("i")[0]; |
| 28 | + |
| 29 | + let icons = ["bi-sun-fill", "bi-moon-stars-fill"]; |
| 30 | + if (theme === "auto") { |
| 31 | + let newIcon = window.matchMedia("(prefers-color-scheme: dark)").matches |
| 32 | + ? "bi-moon-stars-fill" |
| 33 | + : "bi-sun-fill"; |
| 34 | + iconElem.classList.remove(...icons); |
| 35 | + iconElem.classList.add(newIcon); |
| 36 | + |
| 37 | + document.documentElement.setAttribute( |
| 38 | + "data-bs-theme", |
| 39 | + window.matchMedia("(prefers-color-scheme: dark)").matches |
| 40 | + ? "dark" |
| 41 | + : "light" |
| 42 | + ); |
| 43 | + } else { |
| 44 | + let newIcon = theme == "dark" ? "bi-moon-stars-fill" : "bi-sun-fill"; |
| 45 | + iconElem.classList.remove(...icons); |
| 46 | + iconElem.classList.add(newIcon); |
| 47 | + |
| 48 | + document.documentElement.setAttribute("data-bs-theme", theme); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + setTheme(getPreferredTheme()); |
| 53 | + |
| 54 | + const showActiveTheme = (theme, focus = false) => { |
| 55 | + const themeSwitcher = document.querySelector("#bd-theme"); |
| 56 | + |
| 57 | + if (!themeSwitcher) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const themeSwitcherText = document.querySelector("#bd-theme-text"); |
| 62 | + const activeThemeIcon = document.querySelector(".theme-icon-active use"); |
| 63 | + const btnToActive = document.querySelector( |
| 64 | + `[data-bs-theme-value="${theme}"]` |
| 65 | + ); |
| 66 | + const svgOfActiveBtn = btnToActive |
| 67 | + .querySelector("svg use") |
| 68 | + .getAttribute("href"); |
| 69 | + |
| 70 | + document.querySelectorAll("[data-bs-theme-value]").forEach((element) => { |
| 71 | + element.classList.remove("active"); |
| 72 | + element.setAttribute("aria-pressed", "false"); |
| 73 | + }); |
| 74 | + |
| 75 | + btnToActive.classList.add("active"); |
| 76 | + btnToActive.setAttribute("aria-pressed", "true"); |
| 77 | + activeThemeIcon.setAttribute("href", svgOfActiveBtn); |
| 78 | + const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`; |
| 79 | + themeSwitcher.setAttribute("aria-label", themeSwitcherLabel); |
| 80 | + |
| 81 | + if (focus) { |
| 82 | + themeSwitcher.focus(); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + window |
| 87 | + .matchMedia("(prefers-color-scheme: dark)") |
| 88 | + .addEventListener("change", () => { |
| 89 | + const storedTheme = getStoredTheme(); |
| 90 | + if (storedTheme !== "light" && storedTheme !== "dark") { |
| 91 | + setTheme(getPreferredTheme()); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + window.addEventListener("DOMContentLoaded", () => { |
| 96 | + showActiveTheme(getPreferredTheme()); |
| 97 | + |
| 98 | + document.querySelectorAll("[data-bs-theme-value]").forEach((toggle) => { |
| 99 | + toggle.addEventListener("click", () => { |
| 100 | + const theme = toggle.getAttribute("data-bs-theme-value"); |
| 101 | + setStoredTheme(theme); |
| 102 | + setTheme(theme); |
| 103 | + showActiveTheme(theme, true); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | +})(); |
0 commit comments