-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtoggle.mjs
More file actions
1 lines (1 loc) · 1.8 KB
/
toggle.mjs
File metadata and controls
1 lines (1 loc) · 1.8 KB
1
var x="theme";function i(t){return t||x}function u(t){return(t||"").split(",").map(e=>e.trim()).filter(Boolean)}function d(t,e){if(!t.length)return null;let n=t.indexOf(e||"");return n===-1?t[0]:t[(n+1)%t.length]}function h(t,e){let n=u(t);return e?n.length>1?n.includes(e):t===e:t===""}var M=i("");function a(t){return i(t&&t.getAttribute("data-key"))}function v(t,e){t?(document.documentElement.setAttribute("data-theme",t),localStorage.setItem(e,t)):(document.documentElement.removeAttribute("data-theme"),localStorage.removeItem(e))}function g(t){return localStorage.getItem(t)||null}function L(t,e){return a(t)===e}function b(t){return[...document.querySelectorAll("[data-set-theme]")].filter(e=>L(e,t))}function y(t,e){let n=t.getAttribute("data-act-class");n&&(e?t.classList.add(n):t.classList.remove(n))}function m(t,e){b(e).forEach(o=>{if(o instanceof HTMLSelectElement){let c=t||"";o.querySelector(`[value="${c}"]`)&&(o.value=c);return}if(o instanceof HTMLInputElement&&o.type==="checkbox"){o.checked=!!t&&o.value===t;return}let s=o.getAttribute("data-set-theme")||"",r=h(s,t);y(o,r)})}function p(t,e){v(t,e),m(t,e)}var E=new WeakSet;function A(){let t=[...document.querySelectorAll("[data-toggle-theme]")];if(!t.length)return;[...new Set(t.map(n=>a(n)))].forEach(n=>{let o=t.filter(r=>a(r)===n),s=g(n);s&&(document.documentElement.setAttribute("data-theme",s),o.forEach(r=>{let c=r.getAttribute("data-act-class");c&&r.classList.add(c)})),m(s,n),o.forEach(r=>{E.has(r)||(E.add(r),r.addEventListener("click",function(){let c=u(this.getAttribute("data-toggle-theme")),l=document.documentElement.getAttribute("data-theme"),T=d(c,l);p(T,n);let f=this.getAttribute("data-act-class");f&&o.forEach(S=>S.classList.toggle(f))}))})})}function H(t=!0){t?document.addEventListener("DOMContentLoaded",A):A()}export{H as themeChange,A as themeToggle};