Skip to content

Commit d332339

Browse files
Ulises Laraoliviertassinari
Ulises Lara
authored andcommitted
[theme] Support breakpoints.between(a, b) with number (#19003)
1 parent 5533914 commit d332339

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

docs/src/pages/customization/breakpoints/breakpoints.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ const styles = theme => ({
171171

172172
#### Arguments
173173

174-
1. `start` (*String*): A breakpoint key (`xs`, `sm`, etc.).
175-
2. `end` (*String*): A breakpoint key (`xs`, `sm`, etc.).
174+
1. `start` (*String*): A breakpoint key (`xs`, `sm`, etc.) or a screen width number in pixels.
175+
2. `end` (*String*): A breakpoint key (`xs`, `sm`, etc.) or a screen width number in pixels.
176176

177177
#### Returns
178178

packages/material-ui/src/styles/createBreakpoints.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ export default function createBreakpoints(breakpoints) {
3838
}
3939

4040
function between(start, end) {
41-
const endIndex = keys.indexOf(end) + 1;
41+
const endIndex = keys.indexOf(end);
4242

43-
if (endIndex === keys.length) {
43+
if (endIndex === keys.length - 1) {
4444
return up(start);
4545
}
4646

4747
return (
48-
`@media (min-width:${values[start]}${unit}) and ` +
49-
`(max-width:${values[keys[endIndex]] - step / 100}${unit})`
48+
`@media (min-width:${
49+
typeof values[start] === 'number' ? values[start] : start
50+
}${unit}) and ` +
51+
`(max-width:${(endIndex !== -1 && typeof values[keys[endIndex + 1]] === 'number'
52+
? values[keys[endIndex + 1]]
53+
: end) -
54+
step / 100}${unit})`
5055
);
5156
}
5257

packages/material-ui/src/styles/createBreakpoints.test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('createBreakpoints', () => {
2323
assert.strictEqual(breakpoints.down('md'), '@media (max-width:1279.95px)');
2424
});
2525

26-
it('should use the specified key if it is not a recognized breakpoint', () => {
26+
it('should accept a number', () => {
2727
assert.strictEqual(breakpoints.down(600), '@media (max-width:599.95px)');
2828
});
2929

@@ -40,6 +40,13 @@ describe('createBreakpoints', () => {
4040
);
4141
});
4242

43+
it('should accept numbers', () => {
44+
assert.strictEqual(
45+
breakpoints.between(600, 800),
46+
'@media (min-width:600px) and (max-width:799.95px)',
47+
);
48+
});
49+
4350
it('on xl should call up', () => {
4451
assert.strictEqual(breakpoints.between('lg', 'xl'), '@media (min-width:1280px)');
4552
});

0 commit comments

Comments
 (0)