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

[system] Remove theme/styling allocations #43372

Merged
merged 20 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions packages/mui-material/src/Accordion/Accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import clsx from 'clsx';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import Collapse from '../Collapse';
import Paper from '../Paper';
Expand Down Expand Up @@ -46,7 +47,7 @@ const AccordionRoot = styled(Paper, {
];
},
})(
({ theme }) => {
memoTheme(({ theme }) => {
const transition = {
duration: theme.transitions.duration.shortest,
Comment on lines +50 to 52
Copy link
Contributor Author

@romgrk romgrk Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem with prettier, it's going to forcefully increase the indentation level everywhere inside memoTheme(({ theme }) => /* here */), which is one of the reasons I dislike prettier. Anyway, I've left the PR without prettifying it to ease the review, but I'll run it as a last step before merging.

Comment on lines 47 to 52
Copy link
Member

@oliviertassinari oliviertassinari Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure about the DX of this? As a developer looking at the source:https://github.com/romgrk/material-ui/blob/8921455731c32f13dd9d61fa49002feb21b673fa/packages/mui-material/src/Accordion/Accordion.js#L36, the first thing I ask myself: why is the source verbose like this? Couldn't this be a flag?

 const AccordionRoot = styled(Paper, {
   name: 'MuiAccordion',
   slot: 'Root',
+  onlyTheme: true,
   overridesResolver: (props, styles) => {
     const { ownerState } = props;

     return [
       { [`& .${accordionClasses.region}`]: styles.region },
       styles.root,
       !ownerState.square && styles.rounded,
       !ownerState.disableGutters && styles.gutters,
     ];
   },
 })(
  ({ theme }) => {

If we had a transpilation step, it could automatically detect those and add the flag 😄, this way, nobody will forget about it, or use it wrong.

Copy link
Contributor Author

@romgrk romgrk Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could have been a flag, I didn't think about it, but I didn't think about it too much because it can be refactored anytime and I want to iterate on this further.

Emotion is serializing+hashing those style objects on every render of every component, even if we return the same styling object. For internal components we can know, either with the flag on the styled option or a flag on the memoTheme return value (e.g. make it return a function with a .memoized = true flag), that all the styling only needs to react to theme changes. This means we could use emotion's serializeStyles when we memoize the style value, and return the serialized object directly, which emotion will handle here and skip the createStringFromObject on every render/component.

The only thing I need to figure out is variants, we probably need to transfer them from our style value to emotion's serialized style object.

One thing though, the flag would need to affect typings, I hope it's not too complex to setup.

Copy link
Contributor Author

@romgrk romgrk Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember why I picked a function though, initially I also had memoProps that would memo the value based on which props were accessed (in addition to memoTheme), so it was possible to have for the same component style functions that accessed the props and some that didn't (like the Grid).

I didn't end up including memoProps because the implementation was a bit complex (used a proxy to cache which props values were accessed, like SignalJS props work), and most components didn't access the props so the performance gains would have been lower, though I didn't benchmark the difference. That could improve Grid though.

Copy link
Member

@oliviertassinari oliviertassinari Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we could use emotion's serializeStyles when we memoize the style value, and return the serialized object directly, which emotion will handle here and skip the createStringFromObject on every render/component.

Oh, nice, like it used to be in Material UI v4 with JSS. Styles created once per component type.

The only thing I need to figure out is variants, we probably need to transfer them from our style value to emotion's serialized style object.

Maybe we can create custom class name for each variant and apply them conditionally, like Pigment CSS is doing or like we were doing in Material UI v4. The difference now is that we have CSS variables, we have fewer variants to create, e.g. only one for color and not one for color primary, color secondary, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up here: #43412

};
Expand Down Expand Up @@ -91,8 +92,8 @@ const AccordionRoot = styled(Paper, {
backgroundColor: (theme.vars || theme).palette.action.disabledBackground,
},
};
},
({ theme }) => ({
}),
memoTheme(({ theme }) => ({
variants: [
{
props: (props) => !props.square,
Expand Down Expand Up @@ -122,7 +123,7 @@ const AccordionRoot = styled(Paper, {
},
},
],
}),
})),
);

const AccordionHeading = styled('h3', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import { getAccordionDetailsUtilityClass } from './accordionDetailsClasses';

Expand All @@ -21,9 +22,11 @@ const AccordionDetailsRoot = styled('div', {
name: 'MuiAccordionDetails',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
padding: theme.spacing(1, 2, 2),
}));
})(
memoTheme(({ theme }) => ({
padding: theme.spacing(1, 2, 2),
})),
);

const AccordionDetails = React.forwardRef(function AccordionDetails(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiAccordionDetails' });
Expand Down
115 changes: 61 additions & 54 deletions packages/mui-material/src/AccordionSummary/AccordionSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import ButtonBase from '../ButtonBase';
import AccordionContext from '../Accordion/AccordionContext';
Expand All @@ -28,76 +29,82 @@ const AccordionSummaryRoot = styled(ButtonBase, {
name: 'MuiAccordionSummary',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => {
const transition = {
duration: theme.transitions.duration.shortest,
};
})(
memoTheme(({ theme }) => {
const transition = {
duration: theme.transitions.duration.shortest,
};

return {
display: 'flex',
minHeight: 48,
padding: theme.spacing(0, 2),
transition: theme.transitions.create(['min-height', 'background-color'], transition),
[`&.${accordionSummaryClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette.action.focus,
},
[`&.${accordionSummaryClasses.disabled}`]: {
opacity: (theme.vars || theme).palette.action.disabledOpacity,
},
[`&:hover:not(.${accordionSummaryClasses.disabled})`]: {
cursor: 'pointer',
},
variants: [
{
props: (props) => !props.disableGutters,
style: {
[`&.${accordionSummaryClasses.expanded}`]: {
minHeight: 64,
},
},
},
],
};
}),
);

return {
const AccordionSummaryContent = styled('div', {
name: 'MuiAccordionSummary',
slot: 'Content',
overridesResolver: (props, styles) => styles.content,
})(
memoTheme(({ theme }) => ({
display: 'flex',
minHeight: 48,
padding: theme.spacing(0, 2),
transition: theme.transitions.create(['min-height', 'background-color'], transition),
[`&.${accordionSummaryClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette.action.focus,
},
[`&.${accordionSummaryClasses.disabled}`]: {
opacity: (theme.vars || theme).palette.action.disabledOpacity,
},
[`&:hover:not(.${accordionSummaryClasses.disabled})`]: {
cursor: 'pointer',
},
flexGrow: 1,
margin: '12px 0',
variants: [
{
props: (props) => !props.disableGutters,
style: {
transition: theme.transitions.create(['margin'], {
duration: theme.transitions.duration.shortest,
}),
[`&.${accordionSummaryClasses.expanded}`]: {
minHeight: 64,
margin: '20px 0',
},
},
},
],
};
});

const AccordionSummaryContent = styled('div', {
name: 'MuiAccordionSummary',
slot: 'Content',
overridesResolver: (props, styles) => styles.content,
})(({ theme }) => ({
display: 'flex',
flexGrow: 1,
margin: '12px 0',
variants: [
{
props: (props) => !props.disableGutters,
style: {
transition: theme.transitions.create(['margin'], {
duration: theme.transitions.duration.shortest,
}),
[`&.${accordionSummaryClasses.expanded}`]: {
margin: '20px 0',
},
},
},
],
}));
})),
);

const AccordionSummaryExpandIconWrapper = styled('div', {
name: 'MuiAccordionSummary',
slot: 'ExpandIconWrapper',
overridesResolver: (props, styles) => styles.expandIconWrapper,
})(({ theme }) => ({
display: 'flex',
color: (theme.vars || theme).palette.action.active,
transform: 'rotate(0deg)',
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
[`&.${accordionSummaryClasses.expanded}`]: {
transform: 'rotate(180deg)',
},
}));
})(
memoTheme(({ theme }) => ({
display: 'flex',
color: (theme.vars || theme).palette.action.active,
transform: 'rotate(0deg)',
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
[`&.${accordionSummaryClasses.expanded}`]: {
transform: 'rotate(180deg)',
},
})),
);

const AccordionSummary = React.forwardRef(function AccordionSummary(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiAccordionSummary' });
Expand Down
135 changes: 69 additions & 66 deletions packages/mui-material/src/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { darken, lighten } from '@mui/system/colorManipulator';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import useSlot from '../utils/useSlot';
import capitalize from '../utils/capitalize';
Expand Down Expand Up @@ -47,72 +48,74 @@ const AlertRoot = styled(Paper, {
styles[`${ownerState.variant}${capitalize(ownerState.color || ownerState.severity)}`],
];
},
})(({ theme }) => {
const getColor = theme.palette.mode === 'light' ? darken : lighten;
const getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken;
return {
...theme.typography.body2,
backgroundColor: 'transparent',
display: 'flex',
padding: '6px 16px',
variants: [
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.light)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'standard' },
style: {
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
backgroundColor: theme.vars
? theme.vars.palette.Alert[`${color}StandardBg`]
: getBackgroundColor(theme.palette[color].light, 0.9),
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color: theme.palette[color].main,
},
},
})),
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.light)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'outlined' },
style: {
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${(theme.vars || theme).palette[color].light}`,
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color: theme.palette[color].main,
},
},
})),
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.dark)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'filled' },
style: {
fontWeight: theme.typography.fontWeightMedium,
...(theme.vars
? {
color: theme.vars.palette.Alert[`${color}FilledColor`],
backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`],
}
: {
backgroundColor:
theme.palette.mode === 'dark'
? theme.palette[color].dark
: theme.palette[color].main,
color: theme.palette.getContrastText(theme.palette[color].main),
}),
},
})),
],
};
});
})(
memoTheme(({ theme }) => {
const getColor = theme.palette.mode === 'light' ? darken : lighten;
const getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken;
return {
...theme.typography.body2,
backgroundColor: 'transparent',
display: 'flex',
padding: '6px 16px',
variants: [
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.light)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'standard' },
style: {
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
backgroundColor: theme.vars
? theme.vars.palette.Alert[`${color}StandardBg`]
: getBackgroundColor(theme.palette[color].light, 0.9),
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color: theme.palette[color].main,
},
},
})),
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.light)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'outlined' },
style: {
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${(theme.vars || theme).palette[color].light}`,
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color: theme.palette[color].main,
},
},
})),
...Object.entries(theme.palette)
.filter(([, value]) => value && value.main && value.dark)
.map(([color]) => ({
props: { colorSeverity: color, variant: 'filled' },
style: {
fontWeight: theme.typography.fontWeightMedium,
...(theme.vars
? {
color: theme.vars.palette.Alert[`${color}FilledColor`],
backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`],
}
: {
backgroundColor:
theme.palette.mode === 'dark'
? theme.palette[color].dark
: theme.palette[color].main,
color: theme.palette.getContrastText(theme.palette[color].main),
}),
},
})),
],
};
}),
);

const AlertIcon = styled('div', {
name: 'MuiAlert',
Expand Down
15 changes: 9 additions & 6 deletions packages/mui-material/src/AlertTitle/AlertTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import Typography from '../Typography';
import { getAlertTitleUtilityClass } from './alertTitleClasses';
Expand All @@ -22,12 +23,14 @@ const AlertTitleRoot = styled(Typography, {
name: 'MuiAlertTitle',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => {
return {
fontWeight: theme.typography.fontWeightMedium,
marginTop: -2,
};
});
})(
memoTheme(({ theme }) => {
return {
fontWeight: theme.typography.fontWeightMedium,
marginTop: -2,
};
}),
);

const AlertTitle = React.forwardRef(function AlertTitle(inProps, ref) {
const props = useDefaultProps({
Expand Down
Loading
Loading