@mayank1513/nthul
Version:
Unleash the Power of React Server Components! Use multiple themes on your site with confidence, without losing any advantages of React Server Components.
2 lines (1 loc) • 1.87 kB
JavaScript
import{a as S,b as p}from"./chunk-JJ6PU5HF.mjs";import{useEffect as E}from"react";var f=E,$=t=>{f(()=>{let o=matchMedia("(prefers-color-scheme: dark)"),n=()=>{t(i=>({...i,s:o.matches?"dark":"light"}))};return n(),o.addEventListener("change",n),()=>{o.removeEventListener("change",n)}},[t])},u=t=>{let o=(t!=null?t:",system").split(",");return{t:o[0],c:o[1]}},y=0,v=(t,o,n)=>{f(()=>{if(o)return;y=Date.now();let i=n!=null?n:S;t(e=>({...e,...u(localStorage.getItem(i))}));let s=e=>{e.key===i&&t(c=>({...c,...u(e.newValue)}))};return addEventListener("storage",s),()=>{removeEventListener("storage",s)}},[o,t,n])},L=(t="none",o)=>{let n=document,i=n.createElement("style"),s=`transition: ${t.split(";")[0]} !important;`,e=o?`#${o},#${o} *,#${o} ~ *,#${o} ~ * *`:"*";return i.appendChild(n.createTextNode(`${e}{-webkit-${s}-moz-${s}-o-${s}-ms-${s}${s}}`)),n.head.appendChild(i),()=>{getComputedStyle(n.body),setTimeout(()=>{n.head.removeChild(i)},1)}},T=(t,o,n,i)=>{let s=["dark","light",`th-${o}`,n];i&&(s=s.map(e=>{var c;return(c=i[e])!=null?c:e})),t.forEach(e=>{e==null||e.classList.remove(s[0]),e==null||e.classList.remove(s[1]),e==null||e.classList.forEach(c=>{/(?:^|_)th-/.test(c)&&e.classList.remove(c)}),e==null||e.classList.add(s[2]),e==null||e.classList.add(s[3])})},k=(t,o,n,i)=>{let{t:s,c:e,s:c}=t,m=e==="system"?c:e,l=o!=null?o:S,h=!1,r=document,a=r.getElementById(l);h=!n&&(a==null?void 0:a.getAttribute("data-nth"))==="next";let d=o?[a]:[a,r.documentElement];T(d,s,m,i),h&&(r.cookie=`${l}=${s},${m}; max-age=31536000; SameSite=Strict;`)},w=({targetId:t,dontSync:o,themeTransition:n,styles:i})=>{if(t==="")throw new Error("id can not be an empty string");let[s,e]=p(t);return $(e),v(e,o,t),f(()=>{let c=L(n,t);if(k(s,t,o,i),!o&&y<Date.now()-300){let{t:m,c:l}=s,h=[m,l].join(","),r=t!=null?t:S;localStorage.setItem(r,h)}c()},[o,i,t,s,n]),null};export{w as a};