-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add css/mediaQuery formatter and replace use of cssWrapMediaQuery (#926)
- Loading branch information
1 parent
0697ada
commit 8357f04
Showing
7 changed files
with
185 additions
and
74 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 @@ | ||
--- | ||
'@primer/primitives': minor | ||
--- | ||
|
||
Add css/mediaQuery formatter and replace use of cssWrapMediaQuery |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type {TransformedToken} from 'style-dictionary' | ||
import StyleDictionary from 'style-dictionary' | ||
import type {FormatterArguments} from 'style-dictionary/types/Format' | ||
import {format} from 'prettier' | ||
import type {LineFormatting} from 'style-dictionary/types/FormatHelpers' | ||
const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers | ||
|
||
export const cssMediaQuery: StyleDictionary.Formatter = ({ | ||
dictionary: originalDictionary, | ||
options = { | ||
queries: [], | ||
}, | ||
file, | ||
platform, | ||
}: FormatterArguments): string => { | ||
// get options | ||
const {outputReferences, descriptions} = options | ||
// selector | ||
const selector = file.options?.selector || ':root' | ||
// query extension property | ||
const queryExtProp = file.options?.queryExtensionProperty || 'mediaQuery' | ||
// get queries from file options | ||
const queries = file.options?.queries || [ | ||
{ | ||
query: undefined, | ||
matcher: () => true, | ||
}, | ||
] | ||
// set formatting | ||
const formatting: LineFormatting = { | ||
commentStyle: descriptions ? 'long' : 'none', | ||
} | ||
// clone dictionary | ||
const dictionary = {...originalDictionary} | ||
// add prefix to tokens | ||
if (platform.prefix) { | ||
dictionary.allTokens = dictionary.allTokens.map( | ||
token => | ||
({ | ||
...token, | ||
name: `${platform.prefix}-${token.name}`, | ||
path: [platform.prefix, ...token.path], | ||
} as TransformedToken), | ||
) | ||
} | ||
// get queries from tokens | ||
for (const designToken of dictionary.allTokens) { | ||
const query = designToken.$extensions?.[queryExtProp] | ||
// early abort if query does not exist on token | ||
if (!query) continue | ||
// if query exists already from other token | ||
const currentQueryIndex = queries.findIndex((q: {query: string; matcher: () => boolean}) => q.query === query) | ||
|
||
// if query exists | ||
if (currentQueryIndex > -1) { | ||
queries[currentQueryIndex] = { | ||
...queries[currentQueryIndex], | ||
matcher: (token: TransformedToken) => | ||
queries[currentQueryIndex].matcher(token) || | ||
token.$extensions[queryExtProp] === queries[currentQueryIndex].query, | ||
} | ||
} | ||
// if query does not exist | ||
else { | ||
queries.push({ | ||
query, | ||
matcher: (token: TransformedToken) => token.$extensions?.[queryExtProp] === query, | ||
}) | ||
} | ||
} | ||
// add file header | ||
const output = [fileHeader({file})] | ||
// add single theme css | ||
for (const query of queries) { | ||
const {query: queryString, matcher} = query | ||
// filter tokens to only include the ones that pass the matcher | ||
const filteredDictionary = { | ||
...dictionary, | ||
allTokens: dictionary.allTokens.filter(matcher || (() => true)), | ||
} | ||
// early abort if no matches | ||
if (!filteredDictionary.allTokens.length) continue | ||
// add tokens into root | ||
const rootCss = `${selector} {${formattedVariables({ | ||
format: 'css', | ||
dictionary: filteredDictionary, | ||
outputReferences, | ||
formatting, | ||
})}}` | ||
// add css with or without query | ||
output.push(queryString ? `${queryString} { ${rootCss} }` : rootCss) | ||
} | ||
// return prettified | ||
return format(output.join('\n'), {parser: 'css', printWidth: 500}) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.