-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to arrays of breakpoints with string values
Supports all valid CSS units. ```javascript const stringBreakpoints = ['576px', '768px', '992px', '1200px'] ```
- Loading branch information
1 parent
c54e8ec
commit 4c4f1c3
Showing
5 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import '../../lib/__test__/useBreakpoints.mock' | ||
import isValidUnit from '../isValidUnit' | ||
|
||
describe('Validates breakpoint strings with units', () => { | ||
it('Should throw an error if an invalid CSS unit is provided', () => { | ||
expect(() => isValidUnit('1000pxx')).toThrowError( | ||
`"pxx" is not a valid CSS unit.` | ||
) | ||
}) | ||
|
||
it('Should return true if a valid CSS is provided', () => { | ||
expect(() => isValidUnit('1000px')).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import '../../lib/__test__/useBreakpoints.mock' | ||
import makeMediaQueryList from '../makeMediaQueryList' | ||
|
||
describe('Generates media query listeners', () => { | ||
const providedBreakPointsAreNumbers = [ | ||
window.matchMedia('(min-width: 300px) and (max-width: 500px)'), | ||
window.matchMedia('(min-width: 500px) and (max-width: 800px)'), | ||
window.matchMedia('(min-width: 800px) and (max-width: 1200px)'), | ||
window.matchMedia('(min-width: 1200px) ') | ||
] | ||
|
||
const providedBreakPointsAreStrings = [ | ||
window.matchMedia('(min-width: 300em) and (max-width: 500px)'), | ||
window.matchMedia('(min-width: 500px) and (max-width: 800cm)'), | ||
window.matchMedia('(min-width: 800cm) and (max-width: 1200rem)'), | ||
window.matchMedia('(min-width: 1200rem) ') | ||
] | ||
|
||
it('Should create an array of mediaQueries with an array of type numbers', () => { | ||
const expected = JSON.stringify(makeMediaQueryList([300, 500, 800, 1200])) | ||
const received = JSON.stringify(providedBreakPointsAreNumbers) | ||
|
||
expect(expected).toEqual(received) | ||
}) | ||
|
||
it('Should create an array of mediaQueries with an array of type strings', () => { | ||
const expected = JSON.stringify( | ||
makeMediaQueryList(['300em', '500px', '800cm', '1200rem']) | ||
) | ||
const received = JSON.stringify(providedBreakPointsAreStrings) | ||
|
||
expect(expected).toEqual(received) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const units: string[] = [ | ||
'cm', | ||
'mm', | ||
'in', | ||
'px', | ||
'pt', | ||
'pc', | ||
'em', | ||
'ex', | ||
'ch', | ||
'rem', | ||
'vw', | ||
'vh', | ||
'vmin', | ||
'vmax', | ||
'%' | ||
] | ||
export default function isValidUnit(breakpoint: string) { | ||
const unit: string[] = breakpoint.match(/(\D+)/g) || [''] | ||
|
||
if (!units.includes(unit[0])) { | ||
throw new Error( | ||
`"${breakpoint.match(/[^0-9]+/g)}" is not a valid CSS unit.` | ||
) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters