Skip to content

Commit

Permalink
feat: use css vars for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Aug 1, 2020
1 parent c65d5d6 commit 92b9174
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,44 @@ import { loadSources } from './load-sources'
import { Config } from './config'
import { isColor } from './utils'
import { enhanceWhitepaperConfig } from './enhance-whitepaper-config'
import { replaceAliasToVariable } from './replace-alias-to-variable'

const store = new Map()

StyleDictionaryApi.registerFormat({
name: 'css/whitepaper',
formatter: (dictionary) => {
formatter(dictionary, config) {
const defaultOptions = { selector: ':root', useAliasVariables: false }
const options = Object.assign(defaultOptions, (this as any).options)

const whitepaper = store.get('whitepaper')
const group = dictionary.allProperties.length ? dictionary.allProperties[0].group : 'unknown'
const selector = `.Theme_${group}_${whitepaper[group]}`
return `${selector} {\n${variablesWithPrefix(' --', dictionary.allProperties)}\n}\n`

const transformers = config.transforms.filter((transform) => transform.type === 'name')

const props = options.useAliasVariables
? replaceAliasToVariable(dictionary.allProperties, transformers[0].transformer)
: dictionary.allProperties

return `${selector} {\n${variablesWithPrefix(' --', props)}\n}\n`
},
})

// NOTE: Override default css/variables format.
StyleDictionaryApi.registerFormat({
name: 'css/variables',
formatter(dictionary, config) {
const defaultOptions = { selector: ':root', useAliasVariables: false }
const options = Object.assign(defaultOptions, (this as any).options)

const transformers = config.transforms.filter((transform) => transform.type === 'name')

const props = options.useAliasVariables
? replaceAliasToVariable(dictionary.allProperties, transformers[0].transformer)
: dictionary.allProperties

return `${options.selector} {\n${variablesWithPrefix(' --', props)}\n}\n`
},
})

Expand Down
18 changes: 18 additions & 0 deletions src/core/replace-alias-to-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import merge from 'deepmerge'
import { isAlias } from './utils'

/**
* Returns props with replaced alias with css-variables.
* @param props Props list
* @param transformer Name transformer
*/
export function replaceAliasToVariable<T extends Array<any>>(props: T, transformer: any): T {
const nextProps = merge([], props)
for (const prop of nextProps) {
if (isAlias(prop.original.value)) {
const variableName = transformer({ path: prop.original.value.replace(/value/, '') }, {})
prop.value = `var(--${variableName})`
}
}
return nextProps as T
}
4 changes: 4 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function isColor(value: string | number): boolean {
return /^(#|rgba?|hsla?|color|transparent)/.test(String(value))
}

export function isAlias(value: string | number): boolean {
return /^{.+}$/.test(String(value))
}

export function throttle<T extends []>(
callback: (..._: T) => void,
delay: number,
Expand Down

0 comments on commit 92b9174

Please sign in to comment.