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] Fix duplicated styles in Box #33774

Merged
merged 2 commits into from Aug 8, 2022

Conversation

iamxukai
Copy link
Contributor

@iamxukai iamxukai commented Aug 3, 2022

fixes #33443

The Box should not forward the sx prop to the override component. We can overwrite the shouldForwardProp function of the @mui/styled-engine to solve the problem.

@mui-bot
Copy link

mui-bot commented Aug 3, 2022

Details of bundle changes

Generated by 🚫 dangerJS against 3d0d5d6

@mnajdova
Copy link
Member

mnajdova commented Aug 3, 2022

Actually, I believe we should just use the styled() from the @mui/system instead of @mui/styled-engine and that would solve the issue. This is what I mean:

index 60ef467b72..3365f69e58 100644
--- a/packages/mui-system/src/createBox.js
+++ b/packages/mui-system/src/createBox.js
@@ -1,6 +1,6 @@
 import * as React from 'react';
 import clsx from 'clsx';
-import styled from '@mui/styled-engine';
+import styled from './styled';
 import defaultStyleFunctionSx, { extendSxProp } from './styleFunctionSx';
 import useTheme from './useTheme';

We would need to maybe apply this to the other system components too. Lastly, let's add test for this :)

@iamxukai
Copy link
Contributor Author

iamxukai commented Aug 3, 2022

@mnajdova I agree with you. It would seem that If we use the styled() from the @mui/system instead of @mui/styled-engine, we should give the skipSx option with value true.

index 60ef467b72..aa00bbfc6f 100644
--- a/packages/mui-system/src/createBox.js
+++ b/packages/mui-system/src/createBox.js
@@ -1,6 +1,6 @@
 import * as React from 'react';
 import clsx from 'clsx';
-import styled from '@mui/styled-engine';
+import styled from './styled';
 import defaultStyleFunctionSx, { extendSxProp } from './styleFunctionSx';
 import useTheme from './useTheme';

@@ -11,7 +11,7 @@ export default function createBox(options = {}) {
     generateClassName,
     styleFunctionSx = defaultStyleFunctionSx,
   } = options;
-  const BoxRoot = styled('div')(styleFunctionSx);
+  const BoxRoot = styled('div', { skipSx: true })(styleFunctionSx);

   const Box = React.forwardRef(function Box(inProps, ref) {
     const theme = useTheme(defaultTheme);

@iamxukai
Copy link
Contributor Author

iamxukai commented Aug 3, 2022

Even though I apply this (including a true skipSx option) to Container, the component is still the case. The following styles seem to be duplicated:

const ContainerRoot = createStyledComponent(
({ theme, ownerState }: StyleFnProps<Theme>) =>
({
width: '100%',
marginLeft: 'auto',
boxSizing: 'border-box',
marginRight: 'auto',
display: 'block', // Fix IE11 layout when used with main.
...(!ownerState.disableGutters && {
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(2),
// @ts-ignore module augmentation fails if custom breakpoints are used
[theme.breakpoints.up('sm')]: {
paddingLeft: theme.spacing(3),
paddingRight: theme.spacing(3),
},
}),
} as Interpolation<StyleFnProps<Theme>>),
({ theme, ownerState }: StyleFnProps<Theme>) =>
ownerState.fixed &&
Object.keys(theme.breakpoints.values).reduce((acc, breakpointValueKey) => {
const breakpoint = breakpointValueKey;
const value = theme.breakpoints.values[breakpoint as Breakpoint];
if (value !== 0) {
// @ts-ignore
acc[theme.breakpoints.up(breakpoint)] = {
maxWidth: `${value}${theme.breakpoints.unit}`,
};
}
return acc;
}, {}),
({ theme, ownerState }: StyleFnProps<Theme>) => ({
// @ts-ignore module augmentation fails if custom breakpoints are used
...(ownerState.maxWidth === 'xs' && {
// @ts-ignore module augmentation fails if custom breakpoints are used
[theme.breakpoints.up('xs')]: {
// @ts-ignore module augmentation fails if custom breakpoints are used
maxWidth: Math.max(theme.breakpoints.values.xs, 444),
},
}),
...(ownerState.maxWidth &&
// @ts-ignore module augmentation fails if custom breakpoints are used
ownerState.maxWidth !== 'xs' && {
// @ts-ignore module augmentation fails if custom breakpoints are used
[theme.breakpoints.up(ownerState.maxWidth)]: {
// @ts-ignore module augmentation fails if custom breakpoints are used
maxWidth: `${theme.breakpoints.values[ownerState.maxWidth]}${theme.breakpoints.unit}`,
},
}),
}),
);

@iamxukai iamxukai marked this pull request as draft August 4, 2022 01:36
@mnajdova
Copy link
Member

mnajdova commented Aug 4, 2022

@iamxukai ignore what I've said, we want to keep the Box as light as we can, so just adding the shouldForwardProp logic sounds great. I am not sure we need the ownerState there tough. cc @siriwatknp for a final review.

@iamxukai iamxukai force-pushed the system/fix-duplicated-styles-in-Box branch from c0e3aa9 to 933aaf1 Compare August 4, 2022 12:22
@iamxukai iamxukai marked this pull request as ready for review August 4, 2022 12:29
@siriwatknp siriwatknp added bug 🐛 Something doesn't work package: system Specific to @mui/system labels Aug 4, 2022
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 Thanks for your first contribution! look forward to see more.

@siriwatknp
Copy link
Member

siriwatknp commented Aug 5, 2022

@iamxukai I pushed a commit to just remove ownerState from the shouldForwardProp and add a couple of tests. I think we're good!

will merge once the CIs are green.

@siriwatknp siriwatknp merged commit 764a6a8 into mui:master Aug 8, 2022
@Him-2C
Copy link

Him-2C commented Aug 9, 2022

I got many warnings when passing some props style with styled function
image

So what happen when I put many props attribute?

@siriwatknp
Copy link
Member

I got many warnings when passing some props style with styled function image

So what happen when I put many props attribute?

Can you open a new issue with a code sandbox reproduction?

@Him-2C
Copy link

Him-2C commented Aug 10, 2022

@siriwatknp sure try this and check console.log
https://codesandbox.io/s/gallant-lamport-sn25cz?file=/src/App.tsx

@siriwatknp
Copy link
Member

@siriwatknp sure try this and check console.log https://codesandbox.io/s/gallant-lamport-sn25cz?file=/src/App.tsx

There is no need to styled the Box, do this instead:

const BoxWrapper = styled('div')((props: BoxWrapperProps) => ({
  color: "#FFF",
  backgroundImage: props.watermarkUrl ? `url(${props.watermarkUrl})` : "none"
}));

@Him-2C
Copy link

Him-2C commented Aug 10, 2022

@siriwatknp Sorry that I mean example code, what about if I want to use Paper component or something else ?

daniel-rabe pushed a commit to daniel-rabe/material-ui that referenced this pull request Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[System] MUI duplicates sx props when it's used with component prop
5 participants