2 lines
4.6 KiB
JavaScript
2 lines
4.6 KiB
JavaScript
function e(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,t}var t=/*#__PURE__*/e(require("react"));const n=["light","dark"],r="(prefers-color-scheme: dark)",o="undefined"==typeof window,a=/*#__PURE__*/t.createContext(void 0),c={setTheme:e=>{},themes:[]},s=({forcedTheme:e,disableTransitionOnChange:o=!1,enableSystem:c=!0,enableColorScheme:s=!0,storageKey:d="theme",themes:h=["light","dark"],defaultTheme:f=(c?"system":"light"),attribute:v="data-theme",value:y,themeColor:g,children:b,nonce:$})=>{const[p,S]=t.useState(()=>e?"forced":i(d,f)),[w,C]=t.useState(()=>i(d)),E=y?Object.values(y):h,T=t.useRef(),k=t.useRef(),x=t.useRef(),O=t.useCallback(e=>{let t=e;if(!t)return;"system"===e&&c&&(t=m());const r=y?y[t]:t,a=o?u():null,l=document.documentElement;if("class"===v?(l.classList.remove(...E),r&&l.classList.add(r)):r?l.setAttribute(v,r):l.removeAttribute(v),g){let e=!1;const n=(i="string"==typeof g?g:g[t]).startsWith("var(--")?(k.current||(k.current=getComputedStyle(document.documentElement)),k.current.getPropertyValue(i.slice(4,-1))):i,r=(()=>{var t;if(null==(t=T.current)?void 0:t.isConnected)return T.current;const n=document.head.querySelector('meta[name="theme-color"]');if(n)return n;const r=document.createElement("meta");return r.setAttribute("name","theme-color"),e=!0,r})();T.current=r,n&&(r.removeAttribute("value"),r.setAttribute("content",n),e&&document.head.appendChild(r))}var i;if(s){const e=n.includes(f)?f:null,r=n.includes(t)?t:e;l.style.colorScheme=r}null==a||a()},[]),L=t.useCallback(t=>{if(S(t),!e)try{localStorage.setItem(d,t)}catch(e){}},[e]),j=t.useCallback(t=>{const n=m(t);C(n),"system"===p&&c&&!e&&O("system")},[p,e]);return t.useEffect(()=>{const e=window.matchMedia(r);return e.addListener(j),j(e),()=>e.removeListener(j)},[j]),t.useEffect(()=>{const t=t=>{t.key===d&&(e&&t.newValue?x.current=t.newValue:L(t.newValue||f))};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)},[L]),t.useEffect(()=>{if(e&&"forced"!==p&&L("forced"),!e&&x.current)return L(x.current),O(x.current),void(x.current=void 0);O(null!=e?e:p)},[e,p]),/*#__PURE__*/t.createElement(a.Provider,{value:{theme:p,setTheme:L,resolvedTheme:(null!=e?e:"system"===p)?w:p,themes:c?[...h,"system"]:h,systemTheme:c?w:void 0}},/*#__PURE__*/t.createElement(l,{forcedTheme:e,disableTransitionOnChange:o,enableSystem:c,enableColorScheme:s,storageKey:d,themes:h,defaultTheme:f,attribute:v,value:y,children:b,attrs:E,nonce:$}),b)},l=/*#__PURE__*/t.memo(({forcedTheme:e,storageKey:o,attribute:a,enableSystem:c,enableColorScheme:s,defaultTheme:l,value:i,attrs:u,nonce:m})=>{const d="system"===l,h="class"===a?`var d=document.documentElement,c=d.classList;c.remove(${u.map(e=>`'${e}'`).join(",")});`:`var d=document.documentElement,n='${a}',s='setAttribute';`,f=s?n.includes(l)&&l?`if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'${l}'`:"if(e==='light'||e==='dark')d.style.colorScheme=e":"",v=(e,t=!1,r=!0)=>{const o=i?i[e]:e,c=t?e+"|| ''":`'${o}'`;let l="";return s&&r&&!t&&n.includes(e)&&(l+=`d.style.colorScheme = '${e}';`),"class"===a?l+=t||o?`c.add(${c})`:"null":o&&(l+=`d[s](n,${c})`),l},y=e?`!function(){${h}${v(e)}}()`:c?`!function(){try{${h}var e=localStorage.getItem('${o}');if('system'===e||(!e&&${d})){var t='${r}',m=window.matchMedia(t);if(m.media!==t||m.matches){${v("dark")}}else{${v("light")}}}else if(e){${i?`var x=${JSON.stringify(i)};`:""}${v(i?"x[e]":"e",!0)}}${d?"":"else{"+v(l,!1,!1)+"}"}${f}}catch(e){}}()`:`!function(){try{${h}var e=localStorage.getItem('${o}');if(e){${i?`var x=${JSON.stringify(i)};`:""}${v(i?"x[e]":"e",!0)}}else{${v(l,!1,!1)};}${f}}catch(t){}}();`;/*#__PURE__*/return t.createElement("script",{nonce:m,dangerouslySetInnerHTML:{__html:y}})},()=>!0),i=(e,t)=>{if(o)return;let n;try{n=localStorage.getItem(e)||void 0}catch(e){}return n||t},u=()=>{const e=document.createElement("style");return e.appendChild(document.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}")),document.head.appendChild(e),()=>{window.getComputedStyle(document.body),setTimeout(()=>{document.head.removeChild(e)},1)}},m=e=>(e||(e=window.matchMedia(r)),e.matches?"dark":"light");exports.ThemeProvider=e=>t.useContext(a)?/*#__PURE__*/t.createElement(t.Fragment,null,e.children):/*#__PURE__*/t.createElement(s,e),exports.useTheme=()=>{var e;return null!==(e=t.useContext(a))&&void 0!==e?e:c};
|