Organize/.cdp/nt-beta/package/dist/index.umd.js

2 lines
4.7 KiB
JavaScript

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