Skip to content

Commit 6d10a75

Browse files
authored
[Box] Add breakpoint value support to maxWidth prop (#26984)
1 parent b49aa0c commit 6d10a75

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

docs/src/pages/system/sizing/sizing.md

+29-12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
## Supported values
66

7-
The sizing style functions support different property input types:
7+
The sizing properties: `width`, `height`, `minHeight`, `maxHeight`, `minWidth`, and `maxWidth` are using the following custom transform function for the value:
8+
9+
```js
10+
function transform(value) {
11+
return value <= 1 ? `${value * 100}%` : value;
12+
}
13+
```
14+
15+
If the value is between [0, 1], it's converted to percent.
16+
Otherwise, it is directly set on the CSS property.
817

918
{{"demo": "pages/system/sizing/Values.js", "defaultCodeOpen": false}}
1019

1120
```jsx
12-
// Numbers in [0,1] are multiplied by 100 and converted to % values.
13-
<Box sx={{ width: 1/4 }}>
21+
<Box sx={{ width: 1/4 }}> // Equivalent to width: '25%'
1422
<Box sx={{ width: 300 }}> // Numbers are converted to pixel values.
1523
<Box sx={{ width: '75%' }}> // String values are used as raw CSS.
1624
<Box sx={{ width: 1 }}> // 100%
@@ -28,6 +36,15 @@ The sizing style functions support different property input types:
2836
<Box sx={{ width: 'auto' }}>
2937
```
3038

39+
### Max-width
40+
41+
The max-width property allows setting a constraint on your breakpoints.
42+
In this example, the value resolves to [`theme.breakpoints.values.md`](/customization/default-theme/?expand-path=$.breakpoints.values).
43+
44+
```jsx
45+
<Box sx={{ maxWidth: 'md' }}>
46+
```
47+
3148
## Height
3249

3350
{{"demo": "pages/system/sizing/Height.js", "defaultCodeOpen": false}}
@@ -45,12 +62,12 @@ The sizing style functions support different property input types:
4562
import { sizing } from '@material-ui/system';
4663
```
4764

48-
| Import name | Prop | CSS property | Theme key |
49-
| :---------- | :---------- | :----------- | :-------- |
50-
| `width` | `width` | `width` | none |
51-
| `maxWidth` | `maxWidth` | `max-width` | none |
52-
| `minWidth` | `minWidth` | `min-width` | none |
53-
| `height` | `height` | `height` | none |
54-
| `maxHeight` | `maxHeight` | `max-height` | none |
55-
| `minHeight` | `minHeight` | `min-height` | none |
56-
| `boxSizing` | `boxSizing` | `box-sizing` | none |
65+
| Import name | Prop | CSS property | Theme key |
66+
| :---------- | :---------- | :----------- | :------------------------------------------------------------------------------------------- |
67+
| `width` | `width` | `width` | none |
68+
| `maxWidth` | `maxWidth` | `max-width` | [`theme.breakpoints.values`](/customization/default-theme/?expand-path=$.breakpoints.values) |
69+
| `minWidth` | `minWidth` | `min-width` | none |
70+
| `height` | `height` | `height` | none |
71+
| `maxHeight` | `maxHeight` | `max-height` | none |
72+
| `minHeight` | `minHeight` | `min-height` | none |
73+
| `boxSizing` | `boxSizing` | `box-sizing` | none |

docs/src/pages/system/the-sx-prop/the-sx-prop.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The `borderRadius` properties multiples the value it receives by the `theme.shap
3838
// equivalent to borderRadius: theme => 2 * theme.shape.borderRadius
3939
```
4040

41-
_Head to the [borders page](/system/borders/) for more examples._
41+
_Head to the [borders page](/system/borders/) for more details._
4242

4343
### Display
4444

@@ -48,7 +48,7 @@ The `displayPrint` property allows you to specify CSS `display` value, that will
4848
<Box sx={{ displayPrint: 'none' }} /> // equivalent to '@media print': { display: 'none' }
4949
```
5050

51-
_Head to the [display page](/system/display/) for more examples._
51+
_Head to the [display page](/system/display/) for more details._
5252

5353
### Grid
5454

@@ -59,7 +59,7 @@ The grid CSS properties `gap`, `rowGap` and `columnGap` multiply the values they
5959
// equivalent to gap: theme => theme.spacing(2)
6060
```
6161

62-
_Head to the [grid page](/system/grid/) for more examples._
62+
_Head to the [grid page](/system/grid/) for more details._
6363

6464
### Palette
6565

@@ -77,7 +77,7 @@ The `backgroundColor` property is also available trough its alias `bgcolor`.
7777
// equivalent to backgroundColor: theme => theme.palette.primary.main
7878
```
7979

80-
_Head to the [palette page](/system/palette/) for more examples._
80+
_Head to the [palette page](/system/palette/) for more details._
8181

8282
### Positions
8383

@@ -88,7 +88,7 @@ The `zIndex` property maps its value to the `theme.zIndex` value.
8888
// equivalent to backgroundColor: theme => theme.zIndex.tooltip
8989
```
9090

91-
_Head to the [positions page](/system/positions/) for more examples._
91+
_Head to the [positions page](/system/positions/) for more details._
9292

9393
### Shadows
9494

@@ -99,7 +99,7 @@ The `boxShadow` property maps its value to the `theme.shadows` value.
9999
// equivalent to boxShadow: theme => theme.shadows[1]
100100
```
101101

102-
_Head to the [shadows page](/system/shadows/) for more examples._
102+
_Head to the [shadows page](/system/shadows/) for more details._
103103

104104
### Sizing
105105

@@ -111,14 +111,15 @@ function transform(value) {
111111
}
112112
```
113113

114-
Basically, if the value is between [0, 1] it is converted to percent, otherwise it is directly set on the CSS property.
114+
If the value is between [0, 1], it's converted to percent.
115+
Otherwise, it is directly set on the CSS property.
115116

116117
```jsx
117-
<Box sx={{ width: 0.5 }} /> // equivalent to width: '50%'
118+
<Box sx={{ width: 1/2 }} /> // equivalent to width: '50%'
118119
<Box sx={{ width: 20 }} /> // equivalent to width: '20px'
119120
```
120121

121-
_Head to the [sizing page](/system/sizing/) for more examples._
122+
_Head to the [sizing page](/system/sizing/) for more details._
122123

123124
### Spacing
124125

@@ -148,7 +149,7 @@ The following aliases are availabel for the spacing properties:
148149
| `px` | `padding-left`, `padding-right` |
149150
| `py` | `padding-top`, `padding-bottom` |
150151

151-
_Head to the [spacing page](/system/spacing/) for more examples._
152+
_Head to the [spacing page](/system/spacing/) for more details._
152153

153154
### Typography
154155

@@ -173,7 +174,7 @@ There is additional `typography` prop available, which sets all values defined i
173174
// equivalent to { ...theme.typography.body1 }
174175
```
175176

176-
_Head to the [typography page](/system/typography/) for more examples._
177+
_Head to the [typography page](/system/typography/) for more details._
177178

178179
## Responsive values
179180

packages/material-ui-system/src/sizing.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import style from './style';
22
import compose from './compose';
3+
import { handleBreakpoints } from './breakpoints';
34

45
function transform(value) {
56
return value <= 1 ? `${value * 100}%` : value;
@@ -10,10 +11,19 @@ export const width = style({
1011
transform,
1112
});
1213

13-
export const maxWidth = style({
14-
prop: 'maxWidth',
15-
transform,
16-
});
14+
export const maxWidth = (props) => {
15+
if (props.maxWidth) {
16+
const styleFromPropValue = (propValue) => {
17+
const breakpoint = props.theme.breakpoints.values[propValue];
18+
return {
19+
maxWidth: breakpoint || transform(propValue),
20+
};
21+
};
22+
return handleBreakpoints(props, props.maxWidth, styleFromPropValue);
23+
}
24+
return null;
25+
};
26+
maxWidth.filterProps = ['maxWidth'];
1727

1828
export const minWidth = style({
1929
prop: 'minWidth',

packages/material-ui-system/src/styleFunctionSx/styleFunctionSx.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('styleFunctionSx', () => {
6363
fontFamily: 'default',
6464
fontWeight: 'light',
6565
fontSize: 'fontSize',
66+
maxWidth: 'sm',
6667
displayPrint: 'block',
6768
border: [1, 2, 3, 4, 5],
6869
},
@@ -76,6 +77,7 @@ describe('styleFunctionSx', () => {
7677
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
7778
fontWeight: 300,
7879
fontSize: 14,
80+
maxWidth: 600,
7981
'@media print': {
8082
display: 'block',
8183
},

0 commit comments

Comments
 (0)