UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 3.95 kB
{"version":3,"file":"storage-script.cjs","names":["COLOR_MODE_STORAGE_KEY","THEME_SCHEME_STORAGE_KEY"],"sources":["../../../../src/core/system/storage-script.tsx"],"sourcesContent":["\"use client\"\n\nimport type { ColorModeWithSystem, ThemeScheme } from \"./index.types\"\nimport type { Storage } from \"./storage-manager\"\nimport { COLOR_MODE_STORAGE_KEY, THEME_SCHEME_STORAGE_KEY } from \"../constant\"\n\nexport interface StorageScriptProps<Y extends string> {\n key?: string\n type?: Storage\n defaultValue?: Y\n nonce?: string\n}\n\nexport function getStorageScript<Y extends string>(\n type: \"colorMode\" | \"themeScheme\",\n defaultKey: string,\n) {\n return function ({\n key = defaultKey,\n type: storage = \"localStorage\",\n defaultValue,\n }: Omit<StorageScriptProps<Y>, \"nonce\"> = {}) {\n if (type === \"colorMode\") {\n if (storage === \"localStorage\") {\n return `!(function(){try{var a=function(c){var v=\"(prefers-color-scheme: dark)\",h=window.matchMedia(v).matches?\"dark\":\"light\",r=c===\"system\"?h:c,o=document.documentElement,s=document.body,l=\"light\",d=\"dark\",i=r===\"dark\";return s.classList.add(i?d:l),s.classList.remove(i?l:d),o.style.colorScheme=r,o.dataset.mode=r,r},m=\"${defaultValue}\",e=\"${key}\",t=localStorage.getItem(e);t?a(t):(localStorage.setItem(e,m),localStorage.setItem(\\`default-\\${e}\\`,a(m)))}catch{}})();`\n } else {\n return `!(function(){try{var a=function(o){var l=\"(prefers-color-scheme: dark)\",v=window.matchMedia(l).matches?\"dark\":\"light\",e=o===\"system\"?v:o,d=document.documentElement,m=document.body,i=\"light\",n=\"dark\",s=e===\"dark\";return m.classList.add(s?n:i),m.classList.remove(s?i:n),d.style.colorScheme=e,d.dataset.mode=e,e},h=\"${defaultValue}\",r=\"${key}\",t=document.cookie.match(new RegExp(\"(^| )\".concat(r,\"=([^;]+)\"))),c=t?t[2]:null;c?a(c):(document.cookie=\"\".concat(r,\"=\").concat(h,\"; max-age=31536000; path=/\"),document.cookie=\"\".concat(\\`default-\\${r}\\`,\"=\").concat(a(h),\"; max-age=31536000; path=/\"))}catch{}})();`\n }\n } else {\n if (storage === \"localStorage\") {\n return `!(function(){try{var a=function(c){var o=document.documentElement;return o.dataset.theme=c,c},m='${defaultValue}',e='${key}',t=localStorage.getItem(e);t?a(t):localStorage.setItem(e, a(m))}catch{}})();`\n } else {\n return `!(function(){try{var a=function(c){var o=document.documentElement;return o.dataset.theme=c,c},h='${defaultValue}',r='${key}',t=document.cookie.match(new RegExp('(^| )'.concat(r,'=([^;]+)'))),c=t?t[2]:null;c?a(c):(document.cookie=''.concat(r,'=').concat(a(h),'; max-age=31536000; path=/'))}catch{}})();`\n }\n }\n }\n}\n\nexport function ColorModeScript({\n nonce,\n ...rest\n}: StorageScriptProps<ColorModeWithSystem>) {\n const html = getStorageScript(\n \"colorMode\",\n COLOR_MODE_STORAGE_KEY,\n )({ defaultValue: \"light\", ...rest })\n\n return <script dangerouslySetInnerHTML={{ __html: html }} nonce={nonce} />\n}\n\nexport function ThemeSchemeScript({\n nonce,\n ...rest\n}: StorageScriptProps<ThemeScheme>) {\n const html = getStorageScript(\n \"themeScheme\",\n THEME_SCHEME_STORAGE_KEY,\n )({\n defaultValue: \"base\",\n ...rest,\n })\n\n return <script dangerouslySetInnerHTML={{ __html: html }} nonce={nonce} />\n}\n"],"mappings":";;;;;;;;;AAaA,SAAgB,iBACd,MACA,YACA;AACA,QAAO,SAAU,EACf,MAAM,YACN,MAAM,UAAU,gBAChB,iBACwC,EAAE,EAAE;AAC5C,MAAI,SAAS,YACX,KAAI,YAAY,eACd,QAAO,4TAA4T,aAAa,OAAO,IAAI;MAE3V,QAAO,4TAA4T,aAAa,OAAO,IAAI;WAGzV,YAAY,eACd,QAAO,oGAAoG,aAAa,OAAO,IAAI;MAEnI,QAAO,oGAAoG,aAAa,OAAO,IAAI;;;AAM3I,SAAgB,gBAAgB,EAC9B,MACA,GAAG,QACuC;AAM1C,QAAO,2CAAC;EAAO,yBAAyB,EAAE,QAL7B,iBACX,aACAA,wCACD,CAAC;GAAE,cAAc;GAAS,GAAG;GAAM,CAAC,EAEmB;EAAS;GAAS;;AAG5E,SAAgB,kBAAkB,EAChC,MACA,GAAG,QAC+B;AASlC,QAAO,2CAAC;EAAO,yBAAyB,EAAE,QAR7B,iBACX,eACAC,0CACD,CAAC;GACA,cAAc;GACd,GAAG;GACJ,CAAC,EAEsD;EAAS;GAAS"}