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

2 lines
4.3 KiB
JavaScript

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