Skip to content

Commit

Permalink
[theme] Support breakpoints.between(a, b) with number (#19003)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulises Lara authored and oliviertassinari committed Dec 31, 2019
1 parent 5533914 commit d332339
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/src/pages/customization/breakpoints/breakpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ const styles = theme => ({

#### Arguments

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

#### Returns

Expand Down
13 changes: 9 additions & 4 deletions packages/material-ui/src/styles/createBreakpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ export default function createBreakpoints(breakpoints) {
}

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

if (endIndex === keys.length) {
if (endIndex === keys.length - 1) {
return up(start);
}

return (
`@media (min-width:${values[start]}${unit}) and ` +
`(max-width:${values[keys[endIndex]] - step / 100}${unit})`
`@media (min-width:${
typeof values[start] === 'number' ? values[start] : start
}${unit}) and ` +
`(max-width:${(endIndex !== -1 && typeof values[keys[endIndex + 1]] === 'number'
? values[keys[endIndex + 1]]
: end) -
step / 100}${unit})`
);
}

Expand Down
9 changes: 8 additions & 1 deletion packages/material-ui/src/styles/createBreakpoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('createBreakpoints', () => {
assert.strictEqual(breakpoints.down('md'), '@media (max-width:1279.95px)');
});

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

Expand All @@ -40,6 +40,13 @@ describe('createBreakpoints', () => {
);
});

it('should accept numbers', () => {
assert.strictEqual(
breakpoints.between(600, 800),
'@media (min-width:600px) and (max-width:799.95px)',
);
});

it('on xl should call up', () => {
assert.strictEqual(breakpoints.between('lg', 'xl'), '@media (min-width:1280px)');
});
Expand Down

0 comments on commit d332339

Please sign in to comment.