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 13 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
4 changes: 2 additions & 2 deletions packages/mui-material/src/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const ButtonRoot = styled(ButtonBase, {
ownerState.fullWidth && styles.fullWidth,
];
},
})(({ theme }) => {
})(styled.fromTheme(theme => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure how PigmentCSS works, could this mess up its extraction?

Copy link
Member

Choose a reason for hiding this comment

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

I checked. Pigment CSS could work if the fromTheme is an independent function, not a member of the styled. Is it possible?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, found the fromTheme function below. This should work with Pigment CSS:

import { fromTheme } from '../some-path';

styled('div')(fromTheme(({ theme }) => ({})))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function doesn't need to be attached but I did it to make it easy for typescript types, but I think maybe TS can handle inferring theme's type just by being a styled(...)() argument.

As for the argument, I made it only (theme) => ... instead of ({ theme }) => ... because that ensures that the function only needs to memoize theme. If the whole props bundle is included, then there's more stuff to memoize and that is more expensive. I've already sketched out what needs to happen for functions that need access to props: https://github.com/mui/material-ui/pull/43372/files#diff-8d1ff500648fb816e094c97e276c5ae924a36d6ab51d2f13afb5e3974e00c490R259-R323

Copy link
Member

Choose a reason for hiding this comment

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

Agree, let's use fromTheme(({ theme }) => ({ … }). Can we update the types to only expect theme here and how TS errors if people try to access other props (that would be my only worry).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the code to fit in those constraints, and I've settled on memoTheme for the name.

const inheritContainedBackgroundColor =
theme.palette.mode === 'light' ? theme.palette.grey[300] : theme.palette.grey[800];

Expand Down Expand Up @@ -297,7 +297,7 @@ const ButtonRoot = styled(ButtonBase, {
},
],
};
});
}));

const ButtonStartIcon = styled('span', {
name: 'MuiButton',
Expand Down
5 changes: 3 additions & 2 deletions packages/mui-material/src/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const CheckboxRoot = styled(SwitchBase, {
ownerState.color !== 'default' && styles[`color${capitalize(ownerState.color)}`],
];
},
})(({ theme }) => ({

})(styled.fromTheme(theme => ({
color: (theme.vars || theme).palette.text.secondary,
variants: [
{
Expand Down Expand Up @@ -101,7 +102,7 @@ const CheckboxRoot = styled(SwitchBase, {
},
},
],
}));
})));

const defaultCheckedIcon = <CheckBoxIcon />;
const defaultIcon = <CheckBoxOutlineBlankIcon />;
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/Paper/Paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const PaperRoot = styled('div', {
ownerState.variant === 'elevation' && styles[`elevation${ownerState.elevation}`],
];
},
})(({ theme }) => ({
})(styled.fromTheme((theme) => ({
backgroundColor: (theme.vars || theme).palette.background.paper,
color: (theme.vars || theme).palette.text.primary,
transition: theme.transitions.create('box-shadow'),
Expand Down Expand Up @@ -68,7 +68,7 @@ const PaperRoot = styled('div', {
},
},
],
}));
})));

const Paper = React.forwardRef(function Paper(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiPaper' });
Expand Down
12 changes: 12 additions & 0 deletions packages/mui-styled-engine/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ export interface CreateStyledComponent<
Interpolation<ComponentProps & SpecificComponentProps & AdditionalProps & { theme: T }>
>
): StyledComponent<ComponentProps & AdditionalProps, SpecificComponentProps, JSXProps>;

/**
* Create a styling function memoized on theme.
*/
fromTheme(style: (theme: T) => object): (theme: T) => object;

/**
* Create a styling function memoized on props.
*/
fromProps(
style: (theme: FunctionInterpolation<ComponentProps & SpecificComponentProps & { theme: T }>) => object
): (theme: T) => object;
}

export interface CreateMUIStyled<
Expand Down
68 changes: 68 additions & 0 deletions packages/mui-system/src/createStyled/createStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,77 @@ export default function createStyled(input = {}) {
return muiStyledResolver;
};

styled.fromTheme = fromTheme;
styled.fromProps = fromProps;

return styled;
}

/**
* Memoize style function on props.theme
*/
function fromTheme(styleFn) {
let lastValue;
let lastTheme;

return (props) => {
const theme = props.theme;
let value = lastValue;
if (value === undefined || theme !== lastTheme) {
value = styleFn(theme);

lastValue = value;
lastTheme = theme;
}
return value;
};
}

/**
* Memoize style function on props
*/
function fromProps(styleFn) {
let lastValue;
let lastAccesses = new Map();

function didChange(newProps) {
for (const entry in lastAccesses.entries()) {
if (newProps[entry[0]] !== entry[1]) {
return true;
}
}
return false;
}

function createProxy(props) {
const proxy = {}
for (const key in props) {
if (Object.prototype.hasOwnProperty.call(props, key)) {
Object.defineProperty(proxy, key, {
get: () => {
const value = props[key];
lastAccesses.set(key, value)
return value;
}
})
}
}
return proxy;
}

return (props) => {
let value = lastValue;
if (value === undefined || didChange(props)) {
lastAccesses.clear()

value = styleFn(createProxy(props));

lastValue = value;
}
return value;
};
}

function isObjectEmpty(object) {
// eslint-disable-next-line
for (const _ in object) {
Expand Down
Loading