-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve media query validation messaging (#684)
- Loading branch information
1 parent
64378b0
commit e531251
Showing
5 changed files
with
86 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vanilla-extract/css': patch | ||
--- | ||
|
||
Improve media query validation messaging |
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
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import outdent from 'outdent'; | ||
import { parse } from 'css-mediaquery'; | ||
import { toAST } from 'media-query-parser'; | ||
|
||
const mediaTypes = ['all', 'print', 'screen']; | ||
const createMediaQueryError = (mediaQuery: string, msg: string) => | ||
new Error( | ||
outdent` | ||
Invalid media query: "${mediaQuery}" | ||
export const validateMediaQuery = (mediaQuery: string) => { | ||
const { type, expressions } = parse(mediaQuery)?.[0]; | ||
${msg} | ||
const isAllQuery = mediaQuery === 'all'; | ||
const isValidType = mediaTypes.includes(type); | ||
Read more on MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries | ||
`, | ||
); | ||
|
||
// If the parser returns all for the type, we should have expressions | ||
// or the query should match 'all' otherwise it is invalid | ||
if (!isValidType || (!isAllQuery && type === 'all' && !expressions.length)) { | ||
throw new Error( | ||
outdent` | ||
Invalid media query: ${mediaQuery} | ||
export const validateMediaQuery = (mediaQuery: string) => { | ||
if (mediaQuery === '') { | ||
throw createMediaQueryError(mediaQuery, 'Query is empty'); | ||
} | ||
|
||
A media query can contain an optional media type and any number of media feature expressions. | ||
Read more on MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries | ||
`, | ||
); | ||
try { | ||
toAST(mediaQuery); | ||
} catch (e: any) { | ||
throw createMediaQueryError(mediaQuery, e.message); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.