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 maxWidth incorrectly resolving breakpoints with non-pixel units #38633

Merged
merged 4 commits into from
Sep 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/mui-system/src/Box/Box.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, describeConformance } from 'test/utils';
import { Box } from '@mui/system';
import { Box, createTheme, ThemeProvider } from '@mui/system';

describe('<Box />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -279,4 +279,34 @@ describe('<Box />', () => {

expect(getByTestId('regular-box')).to.have.class('MuiBox-root');
});

describe('prop: maxWidth', () => {
it('should resolve breakpoints with custom units', function test() {
const isJSDOM = /jsdom/.test(window.navigator.userAgent);

if (isJSDOM) {
this.skip();
}

const theme = createTheme({
breakpoints: {
unit: 'rem',
values: {
xs: 10,
},
},
});

const { container } = render(
<ThemeProvider theme={theme}>
<Box maxWidth="xs" />,
</ThemeProvider>,
);

expect(container.firstChild).toHaveComputedStyle({
// 10rem x 16px = 160px
maxWidth: '160px',
});
});
});
});
15 changes: 14 additions & 1 deletion packages/mui-system/src/sizing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ export const maxWidth = (props) => {
const styleFromPropValue = (propValue) => {
const breakpoint =
props.theme?.breakpoints?.values?.[propValue] || breakpointsValues[propValue];

if (!breakpoint) {
return {
maxWidth: sizingTransform(propValue),
};
}

if (props.theme?.breakpoints?.unit !== 'px') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Do we need to check if it's not px? Why not just append the unit?

Copy link
Member Author

Choose a reason for hiding this comment

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

@brijeshb42 I just didn't want to change the existing behavior for pixels - i.e. returning maxWidth: 100 instead of maxWidth: '100px' 😬

return {
maxWidth: `${breakpoint}${props.theme.breakpoints.unit}`,
};
}

return {
maxWidth: breakpoint || sizingTransform(propValue),
maxWidth: breakpoint,
};
};
return handleBreakpoints(props, props.maxWidth, styleFromPropValue);
Expand Down
23 changes: 23 additions & 0 deletions packages/mui-system/src/sizing.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { createTheme } from '@mui/system';
import sizing from './sizing';

describe('sizing', () => {
Expand All @@ -19,4 +20,26 @@ describe('sizing', () => {
maxWidth: 0,
});
});

describe('maxWidth', () => {
it('should work with custom units', () => {
const theme = createTheme({
breakpoints: {
unit: 'rem',
values: {
xs: 10,
},
},
});

const output = sizing({
maxWidth: 'xs',
theme,
});

expect(output).to.deep.equal({
maxWidth: '10rem',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ describe('styleFunctionSx', () => {
breakpoints: {
keys: ['xs', 'sm', 'md', 'lg', 'xl'],
values: breakpointsValues,
unit: 'px',
up: (key) => {
return `@media (min-width:${breakpointsValues[key]}px)`;
},
},
unit: 'px',
palette: {
primary: {
main: 'rgb(0, 0, 255)',
Expand Down