UNPKG

@mui/styled-engine-sc

Version:

styled() API wrapper package for styled-components.

144 lines (125 loc) 5.54 kB
/** * @mui/styled-engine-sc v9.1.1 * * @license MIT * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "GlobalStyles", { enumerable: true, get: function () { return _GlobalStyles.default; } }); Object.defineProperty(exports, "StyledEngineProvider", { enumerable: true, get: function () { return _StyledEngineProvider.default; } }); Object.defineProperty(exports, "ThemeContext", { enumerable: true, get: function () { return _styledComponents.ThemeContext; } }); Object.defineProperty(exports, "css", { enumerable: true, get: function () { return _styledComponents.css; } }); exports.default = void 0; exports.internal_mutateStyles = internal_mutateStyles; exports.internal_processStyles = internal_processStyles; exports.internal_serializeStyles = internal_serializeStyles; Object.defineProperty(exports, "keyframes", { enumerable: true, get: function () { return _styledComponents.keyframes; } }); var _styledComponents = require("styled-components"); var _StyledEngineProvider = _interopRequireDefault(require("./StyledEngineProvider")); var _GlobalStyles = _interopRequireDefault(require("./GlobalStyles")); /* eslint-disable @typescript-eslint/naming-convention */ // Re-export the full `styled-components` *type* surface, matching the // hand-written `.d.ts` that this conversion replaces. Type-only: emits // nothing at runtime. Local declarations below intentionally shadow some // styled-components names (`Keyframes`, `Interpolation`, `StyledComponent`, // `CSSObject`, …) — local declarations win over re-exports. // Helper type operators // Pick that distributes over union types // Any prop that has a default prop becomes optional, but its type is unchanged // Undeclared default props are augmented into the resulting allowable attributes // If declared props have indexed properties, ignore default props entirely as keyof gets widened // Wrap in an outer-level conditional type to allow distribution over props that are unions function styled(tag, options) { let stylesFactory; if (options) { stylesFactory = (0, _styledComponents.styled)(tag).withConfig({ displayName: options.label, shouldForwardProp: options.shouldForwardProp }); } else { stylesFactory = (0, _styledComponents.styled)(tag); } if (process.env.NODE_ENV !== 'production') { const fn = (...styles) => { const component = typeof tag === 'string' ? `"${tag}"` : 'component'; if (styles.length === 0) { console.error([`MUI: Seems like you called \`styled(${component})()\` without a \`style\` argument.`, 'You must provide a `styles` argument: `styled("div")(styleYouForgotToPass)`.'].join('\n')); } else if (styles.some(style => style === undefined)) { console.error(`MUI: the styled(${component})(...args) API requires all its args to be defined.`); } return stylesFactory(...styles); }; fn.withConfig = stylesFactory.withConfig; return fn; } return stylesFactory; } var _default = exports.default = styled; /** * For internal usage in `@mui/system` package */ function internal_mutateStyles(tag, processor) { // Styled-components attaches an instance to `componentStyle`. // https://github.com/styled-components/styled-components/blob/da8151762dcf72735ffba358173d4c097f6d5888/packages/styled-components/src/models/StyledComponent.ts#L257 // // The instance contains `rules` (the styles) // https://github.com/styled-components/styled-components/blob/da8151762dcf72735ffba358173d4c097f6d5888/packages/styled-components/src/models/ComponentStyle.ts#L23 if (tag.componentStyle) { tag.componentStyle.rules = processor(tag.componentStyle.rules); } } // Not needed anymore, but fixes https://github.com/mui/material-ui/issues/44112 // TODO: Remove it in v7 function internal_processStyles(tag, processor) { return internal_mutateStyles(tag, processor); } function internal_serializeStyles(styles) { return styles; } // These are the same as the ones in @mui/styled-engine // CSS.PropertiesFallback are necessary so that we support spreading of the mixins. For example: // '@font-face'?: Fontface | Fontface[] // Omit variants as a key, because we have a special handling for it // cannot be made a self-referential interface, breaks WithPropNested // see https://github.com/microsoft/TypeScript/issues/34796 // adapter for compatibility with @mui/styled-engine // abuse Pick to strip the call signature from ForwardRefExoticComponent // any doesn't count as assignable to never in the extends clause, and we default A to never // remove the call signature from StyledComponent so Interpolation can still infer InterpolationFunction // These are typings coming from styled-components // They are adjusted to accept the extended options coming from mui // Same as in styled-components, but copied here so that it would use the Interpolation & CSS typings from above // same as ThemedStyledFunction in styled-components, but without attrs, and withConfig // Config to be used with withConfig /** Same as StyledConfig but shouldForwardProp must be a type guard */ // same as ThemedBaseStyledInterface in styled-components, but with added options & common props for MUI components