UNPKG

nextjs-themes

Version:

Unleash the Power of React Server Components! Use multiple themes on your site with confidence, without losing any advantages of React Server Components.

3 lines (2 loc) 3.44 kB
"use client"; "use strict";var L=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var z=(t,e)=>{for(var o in e)L(t,o,{get:e[o],enumerable:!0})},Q=(t,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of q(e))!$.call(t,s)&&s!==o&&L(t,s,{get:()=>e[s],enumerable:!(n=_(e,s))||n.enumerable});return t};var j=t=>Q(L({},"__esModule",{value:!0}),t);var ee={};z(ee,{ColorSwitch:()=>Z,ThemeSwitcher:()=>X,useTheme:()=>H});module.exports=j(ee);var I=(t,e,o,n,s)=>{window.m=matchMedia("(prefers-color-scheme: dark)");let c=["color-scheme","csp","theme","th"];window.u=(l,S)=>{var E;let x=(E=document.querySelector(S))!=null?E:document.documentElement,g=[];c.forEach((F,D)=>{g.push(F+"-"+l[D]),x.setAttribute("data-"+F,l[D])}),g[0]=l[0],o&&(g=g.map(F=>{var D;return(D=o[F])!=null?D:F})),x.className=g.join(" ")};let i=localStorage.getItem(t),h={...i?JSON.parse(i):e,s:m.matches?e.d:e.l},d,p,v,R;window.f=(l,S)=>{p=R=l,d=v=S},f(n,s),window.r=l=>{p=R!=null?R:l.f,d=v!=null?v:l.fc;let S=d!=null?d:l.c,x=S===e.c?l.s:S,g=x===""?l.t:x===e.d?l.d:l.l,E=p!=null?p:g;return[x||l.s,S,E,p!=null?p:l.t]},u(r(h),t)};var J=require("r18gs");var w="nth",a="light",y="dark",k="system";var M={t:"",d:y,l:a,c:k,s:a},C="undefined",P=t=>{let e=t!=null?t:"#"+w;return(0,J.useRGS)(e,()=>{let o=typeof m!==C&&localStorage.getItem(e);return o?{...JSON.parse(o),s:m.matches?y:a}:M})};var N,V,Y,U,B=(t="none")=>{let e=document.createElement("style");return e.textContent=`transition:${t.split(";")[0]}!important;`,document.head.appendChild(e),()=>{getComputedStyle(document.body),setTimeout(()=>document.head.removeChild(e),1)}},G=({forcedTheme:t,forcedColorScheme:e,targetSelector:o,themeTransition:n})=>{let s=o||"#"+w,[c,i]=P(o);if(typeof m!=C&&!U&&([N,V,Y,U]=[m,u,r,f],N.addEventListener("change",()=>i(h=>({...h,s:N.matches?y:a}))),addEventListener("storage",h=>{h.key===s&&i(d=>({...d,...JSON.parse(h.newValue||"{}")}))})),U){U(t,e);let h=B(n);V(Y(c),s),h();let{f:d,fc:p,...v}=c;localStorage.setItem(s,JSON.stringify(v))}return null};var T=require("react/jsx-runtime"),W=({targetSelector:t,nonce:e,styles:o,forcedTheme:n,forcedColorScheme:s})=>{let c=[t||"#"+w,M,o,n,s];return typeof window!=C&&I(...c),(0,T.jsx)("script",{dangerouslySetInnerHTML:{__html:`(${I})(${JSON.stringify(c).slice(1,-1)})`},nonce:e})},X=t=>(0,T.jsxs)(T.Fragment,{children:[(0,T.jsx)(W,{...t}),(0,T.jsx)(G,{...t})]});var A={s:"nth-s",system:"nth-system",dark:"nth-dark",a:"nth-a",light:"nth-light"};var O=[k,y,a],b,H=t=>{let[e,o]=P(t);!b&&typeof window!=C&&(b=window.r);let n=c=>i=>o(h=>({...h,[c]:i})),s={theme:e.t,darkTheme:e.d,lightTheme:e.l,colorSchemePref:e.c,systemColorScheme:e.s,resolvedColorScheme:e.c===k||e.c===""?e.s:e.c,resolvedTheme:e.t,setTheme:n("t"),setDarkTheme:n("d"),setLightTheme:n("l"),setThemeSet:({darkTheme:c,lightTheme:i})=>o(h=>({...h,d:c,l:i})),setColorSchemePref:n("c"),toggleColorScheme(c){let i=O.indexOf(e.c),h=O.length;(i===-1||c&&i===h-1)&&(i=0),n("c")(O[(i+1)%h])},setForcedColorScheme:n("fc"),setForcedTheme:n("f")};if(b){let c=b(e);s.resolvedColorScheme=c[0],s.resolvedTheme=c[1]}return s};var Z=({size:t=25,skipSystem:e,targetSelector:o,className:n,...s})=>{let{toggleColorScheme:c,colorSchemePref:i}=H(o);return(0,T.jsx)("button",{className:[A.s,A[i],n].join(" "),onClick:()=>c(e),style:{"--s":t+"px"},...s})};0&&(module.exports={ColorSwitch,ThemeSwitcher,useTheme});