UNPKG

@yamada-ui/react

Version:

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

37 lines (33 loc) 2.66 kB
"use client"; import { COLOR_MODE_STORAGE_KEY, THEME_SCHEME_STORAGE_KEY } from "../constant.js"; import { jsx } from "react/jsx-runtime"; //#region src/core/system/storage-script.tsx function getStorageScript(type, defaultKey) { return function({ key = defaultKey, type: storage = "localStorage", defaultValue } = {}) { if (type === "colorMode") if (storage === "localStorage") 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{}})();`; else 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{}})();`; else if (storage === "localStorage") 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{}})();`; else 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{}})();`; }; } function ColorModeScript({ nonce,...rest }) { return /* @__PURE__ */ jsx("script", { dangerouslySetInnerHTML: { __html: getStorageScript("colorMode", COLOR_MODE_STORAGE_KEY)({ defaultValue: "light", ...rest }) }, nonce }); } function ThemeSchemeScript({ nonce,...rest }) { return /* @__PURE__ */ jsx("script", { dangerouslySetInnerHTML: { __html: getStorageScript("themeScheme", THEME_SCHEME_STORAGE_KEY)({ defaultValue: "base", ...rest }) }, nonce }); } //#endregion export { ColorModeScript, ThemeSchemeScript, getStorageScript }; //# sourceMappingURL=storage-script.js.map