UNPKG

@mui/styled-engine-sc

Version:

styled() API wrapper package for styled-components.

106 lines (87 loc) 4.51 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. */ /* eslint-disable @typescript-eslint/naming-convention */ import { styled as scStyled } from 'styled-components'; // 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 = scStyled(tag).withConfig({ displayName: options.label, shouldForwardProp: options.shouldForwardProp }); } else { stylesFactory = scStyled(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; } export default styled; /** * For internal usage in `@mui/system` package */ export 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 export function internal_processStyles(tag, processor) { return internal_mutateStyles(tag, processor); } export function internal_serializeStyles(styles) { return styles; } export { ThemeContext, keyframes, css } from 'styled-components'; export { default as StyledEngineProvider } from "./StyledEngineProvider/index.mjs"; export { default as GlobalStyles } from "./GlobalStyles/index.mjs"; // 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