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

[Grid] Fix support for CSS variables #42574

Merged
merged 3 commits into from
Jun 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions packages/mui-material/src/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ import useTheme from '../styles/useTheme';
import GridContext from './GridContext';
import gridClasses, { getGridUtilityClass } from './gridClasses';

function getOffset(val) {
const parse = parseFloat(val);
if (Number.isNaN(parse)) {
return val;
}
return `${parse}${String(val).replace(String(parse), '') || 'px'}`;
}
Comment on lines -28 to -34
Copy link
Member Author

@oliviertassinari oliviertassinari Jun 8, 2024

Choose a reason for hiding this comment

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

I believe we can drop this since we have this new constraint: https://mui.com/material-ui/migration/v5-style-changes/#%E2%9C%85-remove-px-suffix.

It seems impossible to have values without units:

return typeof output === 'number' ? `${output}px` : output;
, so calc will always work.


export function generateGrid({ theme, ownerState }) {
let size;

Expand Down Expand Up @@ -81,7 +73,7 @@ export function generateGrid({ theme, ownerState }) {
if (ownerState.container && ownerState.item && ownerState.columnSpacing !== 0) {
const themeSpacing = theme.spacing(ownerState.columnSpacing);
if (themeSpacing !== '0px') {
const fullWidth = `calc(${width} + ${getOffset(themeSpacing)})`;
const fullWidth = `calc(${width} + ${themeSpacing})`;
more = {
flexBasis: fullWidth,
maxWidth: fullWidth,
Expand Down Expand Up @@ -178,9 +170,9 @@ export function generateRowGap({ theme, ownerState }) {

if (themeSpacing !== '0px') {
return {
marginTop: `-${getOffset(themeSpacing)}`,
marginTop: theme.spacing(-propValue),
[`& > .${gridClasses.item}`]: {
paddingTop: getOffset(themeSpacing),
paddingTop: themeSpacing,
},
};
}
Expand Down Expand Up @@ -222,11 +214,12 @@ export function generateColumnGap({ theme, ownerState }) {
styles = handleBreakpoints({ theme }, columnSpacingValues, (propValue, breakpoint) => {
const themeSpacing = theme.spacing(propValue);
if (themeSpacing !== '0px') {
const negativeValue = theme.spacing(-propValue);
return {
width: `calc(100% + ${getOffset(themeSpacing)})`,
marginLeft: `-${getOffset(themeSpacing)}`,
width: `calc(100% + ${themeSpacing})`,
marginLeft: negativeValue,
[`& > .${gridClasses.item}`]: {
paddingLeft: getOffset(themeSpacing),
paddingLeft: themeSpacing,
},
};
}
Expand Down
30 changes: 30 additions & 0 deletions packages/mui-system/src/spacing/spacing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ describe('system spacing', () => {
expect(output).to.deep.equal({ padding: '-2em' });
});

it('should support CSS variables single value', () => {
Copy link
Member Author

@oliviertassinari oliviertassinari Jun 8, 2024

Choose a reason for hiding this comment

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

I feel these tests were missing a bit from #40224. There are present, but higher up in the abstraction chain. Not that important though;

const output = spacing({
theme: {
vars: {
spacing: 'var(--mui-spacing)',
},
},
p: 1,
});
expect(output).to.deep.equal({ padding: 'calc(1 * var(--mui-spacing))' });
});

it('should support CSS variables array', () => {
const output = spacing({
theme: {
vars: {
spacing: [
'var(--mui-spacing-0)',
'var(--mui-spacing-1)',
'var(--mui-spacing-2)',
'var(--mui-spacing-3)',
'var(--mui-spacing-4)',
],
},
},
p: 2,
});
expect(output).to.deep.equal({ padding: 'var(--mui-spacing-2)' });
});

it('should support breakpoints', () => {
const output1 = spacing({
p: [1, 2],
Expand Down