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