-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-modes.js
1 lines (1 loc) · 1.29 KB
/
color-modes.js
1
(()=>{"use strict";const e=localStorage.getItem("theme"),t=()=>e||(window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),a=function(e){"auto"===e&&window.matchMedia("(prefers-color-scheme: dark)").matches?document.documentElement.setAttribute("data-bs-theme","dark"):document.documentElement.setAttribute("data-bs-theme",e)};a(t());const r=(e,t=!1)=>{const a=document.querySelector("#bd-theme");if(!a)return;const r=document.querySelector("#bd-theme-text"),c=document.querySelector(".theme-icon-active use"),s=document.querySelector(`[data-bs-theme-value="${e}"]`),o=s.querySelector("svg use").getAttribute("href");document.querySelectorAll("[data-bs-theme-value]").forEach(e=>{e.classList.remove("active"),e.setAttribute("aria-pressed","false")}),s.classList.add("active"),s.setAttribute("aria-pressed","true"),c.setAttribute("href",o);const d=`${r.textContent} (${s.dataset.bsThemeValue})`;a.setAttribute("aria-label",d),t&&a.focus()};window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",()=>{"light"===e&&"dark"===e||a(t())}),window.addEventListener("DOMContentLoaded",()=>{r(t()),document.querySelectorAll("[data-bs-theme-value]").forEach(e=>{e.addEventListener("click",()=>{const t=e.getAttribute("data-bs-theme-value");localStorage.setItem("theme",t),a(t),r(t,!0)})})})})();