Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to remove descriptions from css output #618

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-apes-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

add option to remove descriptions from css output
6 changes: 6 additions & 0 deletions src/PrimerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
cssCustomMedia,
jsonOneDimensional,
cssWrapMediaQuery,
cssVariables,
} from './formats'

/**
Expand Down Expand Up @@ -53,6 +54,11 @@ StyleDictionary.registerFormat({
formatter: cssWrapMediaQuery,
})

StyleDictionary.registerFormat({
name: 'css/variables',
formatter: cssVariables,
})

StyleDictionary.registerFormat({
name: 'scss/mixin-css-variables',
formatter: scssMixinCssVariables,
Expand Down
10 changes: 7 additions & 3 deletions src/formats/cssThemed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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

/**
Expand All @@ -11,18 +12,21 @@ const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
*/
export const cssThemed: StyleDictionary.Formatter = ({dictionary, options = {}, file}: FormatterArguments) => {
const {selector, selectorLight, selectorDark} = options
const {outputReferences} = options
const {outputReferences, descriptions} = options
const formatting: LineFormatting = {
commentStyle: descriptions ? 'long' : 'none',
}
// add file header
const output = [fileHeader({file})]
// add single theme css
output.push(`${selector || ':root'}${selectorLight ? `, ${selectorLight}` : ''}{
${formattedVariables({format: 'css', dictionary, outputReferences})}
${formattedVariables({format: 'css', dictionary, outputReferences, formatting})}
}`)
// add auto dark theme css
if (selectorDark) {
output.push(`@media (prefers-color-scheme: dark) {
${selectorDark} {
${formattedVariables({format: 'css', dictionary, outputReferences})}
${formattedVariables({format: 'css', dictionary, outputReferences, formatting})}
}
}`)
}
Expand Down
26 changes: 26 additions & 0 deletions src/formats/cssVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import StyleDictionary from 'style-dictionary'
import type {FormatterArguments} from 'style-dictionary/types/Format'
import type {LineFormatting} from 'style-dictionary/types/FormatHelpers'
const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers

/**
* @description Converts `StyleDictionary.dictionary.tokens` to css with prefers dark and light section
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
* @param arguments.options outputReferences `boolean`, selector `string`, selectorLight `string`, selectorDark `string`
* @returns formatted `string`
*/
export const cssVariables: StyleDictionary.Formatter = ({dictionary, options = {}, file}: FormatterArguments) => {
const selector = options.selector ? options.selector : `:root`
const {outputReferences, descriptions} = options
const formatting: LineFormatting = {
commentStyle: descriptions ? 'long' : 'none',
}
// add file header

return `${fileHeader({file})}${selector} {\n${formattedVariables({
format: 'css',
dictionary,
outputReferences,
formatting,
})}\n}\n`
}
8 changes: 6 additions & 2 deletions src/formats/cssWrapMediaQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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

/**
Expand All @@ -9,15 +10,18 @@ const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
* @returns formatted `string`
*/
export const cssWrapMediaQuery: StyleDictionary.Formatter = ({dictionary, options, file}: FormatterArguments) => {
const {outputReferences} = options
const {outputReferences, descriptions} = options
const formatting: LineFormatting = {
commentStyle: descriptions ? 'long' : 'none',
}
// get specific media query or use screen
const mediaQuery = options.mediaQuery?.[file.destination] || 'screen'
// add file header
const output = [fileHeader({file})]
// add meadia query
output.push(`@media ${mediaQuery} {\n:root {`)
// add tokens
output.push(formattedVariables({format: 'css', dictionary, outputReferences}))
output.push(formattedVariables({format: 'css', dictionary, outputReferences, formatting}))
// close media query
output.push(`}\n}`)
// return prettified
Expand Down
1 change: 1 addition & 0 deletions src/formats/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {cssThemed} from './cssThemed'
export {cssCustomMedia} from './cssCustomMedia'
export {cssWrapMediaQuery} from './cssWrapMediaQuery'
export {cssVariables} from './cssVariables'
export {javascriptCommonJs} from './javascriptCommonJs'
export {javascriptEsm} from './javascriptEsm'
export {jsonNestedPrefixed} from './jsonNestedPrefixed'
Expand Down
3 changes: 3 additions & 0 deletions src/platforms/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
options: {
showFileHeader: false,
outputReferences: false,
descriptions: false,
selector,
selectorLight,
selectorDark,
Expand All @@ -56,6 +57,7 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
filter: token => isSource(token) && options?.themed !== true,
options: {
showFileHeader: false,
descriptions: false,
...options?.options,
},
},
Expand All @@ -77,6 +79,7 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
'src/tokens/functional/size/size-fine.json',
]),
options: {
descriptions: false,
showFileHeader: false,
mediaQuery: {
'css/functional/size/size-fine.css': '(pointer: fine)',
Expand Down
3 changes: 2 additions & 1 deletion src/tokens/functional/size/border.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"boxShadow": {
"thin": {
"value": "inset 0 0 0 {borderWidth.thin}"
"value": "inset 0 0 0 {borderWidth.thin}",
"description": "Thin shadow for borders"
},
"thick": {
"value": "inset 0 0 0 {borderWidth.thick}"
Expand Down