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

[Stack, system] Apply correct responsive styles if any custom breakpoints are provided #32913

Merged
5 changes: 4 additions & 1 deletion packages/mui-material/src/Stack/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export const style = ({ ownerState, theme }) => {
const transformer = createUnarySpacing(theme);

const base = Object.keys(theme.breakpoints.values).reduce((acc, breakpoint) => {
if (ownerState.spacing[breakpoint] != null || ownerState.direction[breakpoint] != null) {
if (
(typeof ownerState.spacing === 'object' && ownerState.spacing[breakpoint] != null) ||
(typeof ownerState.direction === 'object' && ownerState.direction[breakpoint] != null)
) {
acc[breakpoint] = true;
}
return acc;
Expand Down
68 changes: 68 additions & 0 deletions packages/mui-material/src/Stack/Stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,73 @@ describe('<Stack />', () => {
flexDirection: 'column',
});
});

it('should generate correct styles if custom breakpoints are provided in theme', () => {
expect(
style({
ownerState: {
direction: 'column',
spacing: 4,
},
theme: {
...defaultTheme,
breakpoints: {
values: {
smallest: 0,
small: 375,
mobile: 600,
tablet: 992,
desktop: 1200,
},
},
},
}),
).to.deep.equal({
'& > :not(style) + :not(style)': {
margin: 0,
marginTop: '32px',
},
display: 'flex',
flexDirection: 'column',
});
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
});

it('should generate correct responsive styles if custom responsive spacing values are provided', () => {
const breakpointsValues = {
smallest: 0,
small: 375,
mobile: 600,
tablet: 992,
desktop: 1200,
};

const customTheme = {
...defaultTheme,
breakpoints: {
values: breakpointsValues,
keys: ['smallest', 'small', 'mobile', 'tablet', 'desktop'],
up: (key) => `@media (min-width:${breakpointsValues[key]}px)`,
},
};

expect(
style({
ownerState: {
direction: 'column',
spacing: { small: 4 },
},
theme: customTheme,
}),
).to.deep.equal({
[`@media (min-width:${customTheme.breakpoints.values.small}px)`]: {
'& > :not(style) + :not(style)': {
margin: 0,
marginTop: '32px',
},
},
display: 'flex',
flexDirection: 'column',
});
});
});
});
6 changes: 4 additions & 2 deletions packages/mui-system/src/breakpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ export function resolveBreakpointValues({
acc[breakpoint] =
breakpointValues[i] != null ? breakpointValues[i] : breakpointValues[previous];
previous = i;
} else {
} else if (typeof breakpointValues === 'object') {
acc[breakpoint] =
breakpointValues[breakpoint] != null
? breakpointValues[breakpoint]
: breakpointValues[previous] || breakpointValues;
: breakpointValues[previous];
previous = breakpoint;
} else {
acc[breakpoint] = breakpointValues;
}
return acc;
}, {});
Expand Down
26 changes: 26 additions & 0 deletions packages/mui-system/src/breakpoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,32 @@ describe('breakpoints', () => {
expect(values).to.equal(3);
});

it('return prop as it is for prop of fixed string value', () => {
const directionValue = 'columns';
const values = resolveBreakpointValues({
values: directionValue,
});
expect(values).to.equal('columns');
});

it('given custom base, resolve breakpoint values for prop of string type', () => {
const directionValue = 'columns';
const values = resolveBreakpointValues({
values: directionValue,
base: { small: true },
});
expect(values).to.deep.equal({ small: directionValue });
});

it('given custom base, resolve breakpoint values for prop of number type', () => {
const spacingValue = 3;
const values = resolveBreakpointValues({
values: spacingValue,
base: { small: true },
});
expect(values).to.deep.equal({ small: spacingValue });
});

it('given custom base, resolve breakpoint values for prop of array type', () => {
const columns = [1, 2, 3];
const customBase = { extraSmall: true, small: true, medium: true, large: true };
Expand Down