UNPKG

5.89 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import { globalCss } from "../zero-styled/index.js";
6import { useDefaultProps } from "../DefaultPropsProvider/index.js";
7
8// to determine if the global styles are static or dynamic
9import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
10const isDynamicSupport = typeof globalCss({}) === 'function';
11export const html = (theme, enableColorScheme) => ({
12 WebkitFontSmoothing: 'antialiased',
13 // Antialiasing.
14 MozOsxFontSmoothing: 'grayscale',
15 // Antialiasing.
16 // Change from `box-sizing: content-box` so that `width`
17 // is not affected by `padding` or `border`.
18 boxSizing: 'border-box',
19 // Fix font resize problem in iOS
20 WebkitTextSizeAdjust: '100%',
21 // When used under CssVarsProvider, colorScheme should not be applied dynamically because it will generate the stylesheet twice for server-rendered applications.
22 ...(enableColorScheme && !theme.vars && {
23 colorScheme: theme.palette.mode
24 })
25});
26export const body = theme => ({
27 color: (theme.vars || theme).palette.text.primary,
28 ...theme.typography.body1,
29 backgroundColor: (theme.vars || theme).palette.background.default,
30 '@media print': {
31 // Save printer ink.
32 backgroundColor: (theme.vars || theme).palette.common.white
33 }
34});
35export const styles = (theme, enableColorScheme = false) => {
36 const colorSchemeStyles = {};
37 if (enableColorScheme && theme.colorSchemes && typeof theme.getColorSchemeSelector === 'function') {
38 Object.entries(theme.colorSchemes).forEach(([key, scheme]) => {
39 const selector = theme.getColorSchemeSelector(key);
40 if (selector.startsWith('@')) {
41 // for @media (prefers-color-scheme), we need to target :root
42 colorSchemeStyles[selector] = {
43 ':root': {
44 colorScheme: scheme.palette?.mode
45 }
46 };
47 } else {
48 // else, it's likely that the selector already target an element with a class or data attribute
49 colorSchemeStyles[selector.replace(/\s*&/, '')] = {
50 colorScheme: scheme.palette?.mode
51 };
52 }
53 });
54 }
55 let defaultStyles = {
56 html: html(theme, enableColorScheme),
57 '*, *::before, *::after': {
58 boxSizing: 'inherit'
59 },
60 'strong, b': {
61 fontWeight: theme.typography.fontWeightBold
62 },
63 body: {
64 margin: 0,
65 // Remove the margin in all browsers.
66 ...body(theme),
67 // Add support for document.body.requestFullScreen().
68 // Other elements, if background transparent, are not supported.
69 '&::backdrop': {
70 backgroundColor: (theme.vars || theme).palette.background.default
71 }
72 },
73 ...colorSchemeStyles
74 };
75 const themeOverrides = theme.components?.MuiCssBaseline?.styleOverrides;
76 if (themeOverrides) {
77 defaultStyles = [defaultStyles, themeOverrides];
78 }
79 return defaultStyles;
80};
81
82// `ecs` stands for enableColorScheme. This is internal logic to make it work with Pigment CSS, so shorter is better.
83const SELECTOR = 'mui-ecs';
84const staticStyles = theme => {
85 const result = styles(theme, false);
86 const baseStyles = Array.isArray(result) ? result[0] : result;
87 if (!theme.vars && baseStyles) {
88 baseStyles.html[`:root:has(${SELECTOR})`] = {
89 colorScheme: theme.palette.mode
90 };
91 }
92 if (theme.colorSchemes) {
93 Object.entries(theme.colorSchemes).forEach(([key, scheme]) => {
94 const selector = theme.getColorSchemeSelector(key);
95 if (selector.startsWith('@')) {
96 // for @media (prefers-color-scheme), we need to target :root
97 baseStyles[selector] = {
98 [`:root:not(:has(.${SELECTOR}))`]: {
99 colorScheme: scheme.palette?.mode
100 }
101 };
102 } else {
103 // else, it's likely that the selector already target an element with a class or data attribute
104 baseStyles[selector.replace(/\s*&/, '')] = {
105 [`&:not(:has(.${SELECTOR}))`]: {
106 colorScheme: scheme.palette?.mode
107 }
108 };
109 }
110 });
111 }
112 return result;
113};
114const GlobalStyles = globalCss(isDynamicSupport ? ({
115 theme,
116 enableColorScheme
117}) => styles(theme, enableColorScheme) : ({
118 theme
119}) => staticStyles(theme));
120
121/**
122 * Kickstart an elegant, consistent, and simple baseline to build upon.
123 */
124function CssBaseline(inProps) {
125 const props = useDefaultProps({
126 props: inProps,
127 name: 'MuiCssBaseline'
128 });
129 const {
130 children,
131 enableColorScheme = false
132 } = props;
133 return /*#__PURE__*/_jsxs(React.Fragment, {
134 children: [isDynamicSupport && /*#__PURE__*/_jsx(GlobalStyles, {
135 enableColorScheme: enableColorScheme
136 }), !isDynamicSupport && !enableColorScheme && /*#__PURE__*/_jsx("span", {
137 className: SELECTOR,
138 style: {
139 display: 'none'
140 }
141 }), children]
142 });
143}
144process.env.NODE_ENV !== "production" ? CssBaseline.propTypes /* remove-proptypes */ = {
145 // ┌────────────────────────────── Warning ──────────────────────────────┐
146 // │ These PropTypes are generated from the TypeScript type definitions. │
147 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
148 // └─────────────────────────────────────────────────────────────────────┘
149 /**
150 * You can wrap a node.
151 */
152 children: PropTypes.node,
153 /**
154 * Enable `color-scheme` CSS property to use `theme.palette.mode`.
155 * For more details, check out https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme
156 * For browser support, check out https://caniuse.com/?search=color-scheme
157 * @default false
158 */
159 enableColorScheme: PropTypes.bool
160} : void 0;
161export default CssBaseline;
\No newline at end of file