@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
41 lines (37 loc) • 2.94 kB
JavaScript
"use client";
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
const require_constant = require('../constant.cjs');
let react_jsx_runtime = require("react/jsx-runtime");
react_jsx_runtime = require_rolldown_runtime.__toESM(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__ */ (0, react_jsx_runtime.jsx)("script", {
dangerouslySetInnerHTML: { __html: getStorageScript("colorMode", require_constant.COLOR_MODE_STORAGE_KEY)({
defaultValue: "light",
...rest
}) },
nonce
});
}
function ThemeSchemeScript({ nonce,...rest }) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("script", {
dangerouslySetInnerHTML: { __html: getStorageScript("themeScheme", require_constant.THEME_SCHEME_STORAGE_KEY)({
defaultValue: "base",
...rest
}) },
nonce
});
}
//#endregion
exports.ColorModeScript = ColorModeScript;
exports.ThemeSchemeScript = ThemeSchemeScript;
exports.getStorageScript = getStorageScript;
//# sourceMappingURL=storage-script.cjs.map