UNPKG

@mui/system

Version:

MUI System is a set of CSS utilities to help you build custom designs more efficiently. It makes it possible to rapidly lay out custom designs.

87 lines (85 loc) 3.21 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_MODE_STORAGE_KEY = exports.DEFAULT_COLOR_SCHEME_STORAGE_KEY = exports.DEFAULT_ATTRIBUTE = void 0; exports.default = InitColorSchemeScript; var React = _interopRequireWildcard(require("react")); var _jsxRuntime = require("react/jsx-runtime"); /** * Split this component for RSC import */ const DEFAULT_MODE_STORAGE_KEY = exports.DEFAULT_MODE_STORAGE_KEY = 'mode'; const DEFAULT_COLOR_SCHEME_STORAGE_KEY = exports.DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme'; const DEFAULT_ATTRIBUTE = exports.DEFAULT_ATTRIBUTE = 'data-color-scheme'; function InitColorSchemeScript(options) { const { defaultMode = 'system', defaultLightColorScheme = 'light', defaultDarkColorScheme = 'dark', modeStorageKey = DEFAULT_MODE_STORAGE_KEY, colorSchemeStorageKey = DEFAULT_COLOR_SCHEME_STORAGE_KEY, attribute: initialAttribute = DEFAULT_ATTRIBUTE, colorSchemeNode = 'document.documentElement', nonce } = options || {}; let setter = ''; let attribute = initialAttribute; if (initialAttribute === 'class') { attribute = '.%s'; } if (initialAttribute === 'data') { attribute = '[data-%s]'; } if (attribute.startsWith('.')) { const selector = attribute.substring(1); setter += `${colorSchemeNode}.classList.remove('${selector}'.replace('%s', light), '${selector}'.replace('%s', dark)); ${colorSchemeNode}.classList.add('${selector}'.replace('%s', colorScheme));`; } const matches = attribute.match(/\[([^\]]+)\]/); // case [data-color-scheme=%s] or [data-color-scheme] if (matches) { const [attr, value] = matches[1].split('='); if (!value) { setter += `${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', light)); ${colorSchemeNode}.removeAttribute('${attr}'.replace('%s', dark));`; } setter += ` ${colorSchemeNode}.setAttribute('${attr}'.replace('%s', colorScheme), ${value ? `${value}.replace('%s', colorScheme)` : '""'});`; } else { setter += `${colorSchemeNode}.setAttribute('${attribute}', colorScheme);`; } return /*#__PURE__*/(0, _jsxRuntime.jsx)("script", { suppressHydrationWarning: true, nonce: typeof window === 'undefined' ? nonce : '' // eslint-disable-next-line react/no-danger , dangerouslySetInnerHTML: { __html: `(function() { try { let colorScheme = ''; const mode = localStorage.getItem('${modeStorageKey}') || '${defaultMode}'; const dark = localStorage.getItem('${colorSchemeStorageKey}-dark') || '${defaultDarkColorScheme}'; const light = localStorage.getItem('${colorSchemeStorageKey}-light') || '${defaultLightColorScheme}'; if (mode === 'system') { // handle system mode const mql = window.matchMedia('(prefers-color-scheme: dark)'); if (mql.matches) { colorScheme = dark } else { colorScheme = light } } if (mode === 'light') { colorScheme = light; } if (mode === 'dark') { colorScheme = dark; } if (colorScheme) { ${setter} } } catch(e){}})();` } }, "mui-color-scheme-init"); }