Skip to content

Commit

Permalink
[system] Merge breakpoints in correct order (#23380)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova authored Nov 5, 2020
1 parent 5d08d0b commit e53a954
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/material-ui/src/Box/styleFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ const getThemeValue = (prop, value, theme) => {
return { [prop]: value };
};

function createEmptyBreakpointObject(breakpoints) {
const breakpointsInOrder = breakpoints.keys.reduce((acc, key) => {
const breakpointStyleKey = breakpoints.up(key);
acc[breakpointStyleKey] = {};
return acc;
}, {});
return breakpointsInOrder;
}

function removeUnusedBreakpoints(breakpointKeys, style) {
return breakpointKeys.reduce(
(acc, key) => {
const breakpointOutput = acc[key];
const isBreakpointUnused = Object.keys(breakpointOutput).length === 0;
if (isBreakpointUnused) {
delete acc[key];
}
return acc;
},
{ ...style },
);
}

const mergeBreakpointsInOrder = (breakpoints, ...styles) => {
const emptyBreakpoints = createEmptyBreakpointObject(breakpoints);
const mergedOutput = [emptyBreakpoints, ...styles].reduce(
(prev, next) => deepmerge(prev, next),
{},
);
return removeUnusedBreakpoints(Object.keys(emptyBreakpoints), mergedOutput);
};

export const styleFunctionSx = (styles, theme) => {
if (!styles) return null;

Expand Down Expand Up @@ -121,7 +153,7 @@ const styleFunction = (props) => {

const sxValue = styleFunctionSx(props.sx, props.theme);

return deepmerge(result, sxValue);
return mergeBreakpointsInOrder(props.theme.breakpoints, result, sxValue);
};

styleFunction.filterProps = filterProps;
Expand Down
45 changes: 45 additions & 0 deletions packages/material-ui/src/Box/styleFunction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ describe('styleFunction', () => {
},
});
});

it('writes breakpoints in correct order', () => {
const result = styleFunction({
theme,
sx: { m: { md: 1, lg: 2 }, p: { xs: 0, sm: 1, md: 2 } },
});

// Test the order
expect(Object.keys(result)).to.deep.equal([
'@media (min-width:0px)',
'@media (min-width:600px)',
'@media (min-width:960px)',
'@media (min-width:1280px)',
]);

expect(result).to.deep.equal({
'@media (min-width:0px)': { padding: '0px' },
'@media (min-width:600px)': { padding: '10px' },
'@media (min-width:960px)': { padding: '20px', margin: '10px' },
'@media (min-width:1280px)': { margin: '20px' },
});
});
});

describe('breakpoints', () => {
Expand Down Expand Up @@ -213,5 +235,28 @@ describe('styleFunction', () => {
'@media (min-width:960px)': { padding: '70px', margin: '30px' },
});
});

it('writes breakpoints in correct order', () => {
const result = styleFunction({
theme,
m: { md: 1, lg: 2 },
sx: { p: { xs: 0, sm: 1, md: 2 } },
});

// Test the order
expect(Object.keys(result)).to.deep.equal([
'@media (min-width:0px)',
'@media (min-width:600px)',
'@media (min-width:960px)',
'@media (min-width:1280px)',
]);

expect(result).to.deep.equal({
'@media (min-width:0px)': { padding: '0px' },
'@media (min-width:600px)': { padding: '10px' },
'@media (min-width:960px)': { padding: '20px', margin: '10px' },
'@media (min-width:1280px)': { margin: '20px' },
});
});
});
});

0 comments on commit e53a954

Please sign in to comment.