Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] v5 emotion-less css & styled prototype #44269

Open
wants to merge 3 commits into
base: v5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/mui-system/src/createStyled.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export default function createStyled<Theme extends object = DefaultTheme>(option
slotShouldForwardProp?: (prop: PropertyKey) => boolean;
styleFunctionSx?: typeof styleFunctionSx;
}): CreateMUIStyled<Theme>;

export function applyStyled(props: any, componentName: string, overridesResolver: Function): string;
61 changes: 44 additions & 17 deletions packages/mui-system/src/createStyled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable no-underscore-dangle */
import styledEngineStyled, { internal_processStyles as processStyles } from '@mui/styled-engine';
import styledEngineStyled, {
css,
internal_processStyles as processStyles,
} from '@mui/styled-engine';
import { isPlainObject } from '@mui/utils/deepmerge';
import capitalize from '@mui/utils/capitalize';
import getDisplayName from '@mui/utils/getDisplayName';
Expand Down Expand Up @@ -181,28 +184,14 @@ export default function createStyled(input = {}) {
if (componentName && overridesResolver) {
expressionsWithDefaultTheme.push((props) => {
const theme = resolveTheme({ ...props, defaultTheme, themeId });
if (
!theme.components ||
!theme.components[componentName] ||
!theme.components[componentName].styleOverrides
) {
return null;
}
const styleOverrides = theme.components[componentName].styleOverrides;
const resolvedStyleOverrides = {};
// TODO: v7 remove iteration and use `resolveStyleArg(styleOverrides[slot])` directly
Object.entries(styleOverrides).forEach(([slotKey, slotStyle]) => {
resolvedStyleOverrides[slotKey] = processStyleArg(slotStyle, { ...props, theme });
});
return overridesResolver(props, resolvedStyleOverrides);
return applyThemeOverrides(theme, props, componentName, overridesResolver);
});
}

if (componentName && !skipVariantsResolver) {
expressionsWithDefaultTheme.push((props) => {
const theme = resolveTheme({ ...props, defaultTheme, themeId });
const themeVariants = theme?.components?.[componentName]?.variants;
return processStyleArg({ variants: themeVariants }, { ...props, theme });
return applyThemeVariants(theme, props, componentName);
});
}

Expand Down Expand Up @@ -245,3 +234,41 @@ export default function createStyled(input = {}) {
return muiStyledResolver;
};
}

export function applyStyled(props, componentName, overridesResolver) {
romgrk marked this conversation as resolved.
Show resolved Hide resolved
const styles = applyThemeOverrides(props, componentName, overridesResolver);

// NOTE: Later on, we might want to apply other MUI features:
romgrk marked this conversation as resolved.
Show resolved Hide resolved
// const styles = [
// applyThemeOverrides(props, componentName, overridesResolver),
// applyThemeVariants(props, componentName),
// applySystemSx(props, componentName),
// ]

return css(styles);
}

function applyThemeOverrides(theme, props, componentName, overridesResolver) {
if (
!theme.components ||
!theme.components[componentName] ||
!theme.components[componentName].styleOverrides
) {
return null;
}
const styleOverrides = theme.components[componentName].styleOverrides;
const resolvedStyleOverrides = {};
// TODO: v7 remove iteration and use `resolveStyleArg(styleOverrides[slot])` directly
Object.entries(styleOverrides).forEach(([slotKey, slotStyle]) => {
resolvedStyleOverrides[slotKey] = processStyleArg(slotStyle, { ...props, theme });
});
return overridesResolver(props, resolvedStyleOverrides);
}

function applyThemeVariants(theme, props, componentName) {
const themeVariants = theme?.components?.[componentName]?.variants;
if (!themeVariants) {
return null;
}
return processStyleArg({ variants: themeVariants }, { ...props, theme });
}
Loading