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 value to css variable transformer #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ StyleDictionary.registerTransform({
- [dimension/remToPixel](#dimensionremtopixel)
- [dimension/pixelUnitless](#dimensionpixelunitless)
- [clamp/css](#clampcss)
- [value/cssVariable](#valuetocssvariable)
- Filters
- [isSource](#issource)
- [isColor](#iscolor)
Expand Down Expand Up @@ -981,6 +982,49 @@ const myStyleDictionary = StyleDictionary.extend({
}
```

### value/cssVariable

This `value` transformer replaces the value of any token, with a css variable of the same name as the token.
The idea is that if you want to generate e.g. a js object that references css variables, you use this variable

```js
const myStyleDictionary = StyleDictionary.extend({
"platforms": {
"js": {
"transforms": ['value/cssVariable'],
"files": [{
// ...
}],
}
}
});
```
##### Before transformation

```js
{
colors: {
danger: {
value: "#ff0000",
$type: "color"
}
}
}
```

##### After transformation

```js
{
size: {
small: {
value: "var(--colors-danger)",
$type: "color"
}
}
}
```

## 🚦 Filters

Filters are used to filter out unwanted tokens when [configuring output files](https://amzn.github.io/style-dictionary/#/config?id=file)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('index.js', () => {
expect(StyleDictionary.transform["shadow/css"]).toBeDefined();
expect(StyleDictionary.transform["font/css"]).toBeDefined();
expect(StyleDictionary.transform["fontFamily/css"]).toBeDefined();
expect(StyleDictionary.transform["variables/css"]).toBeDefined();
expect(StyleDictionary.transform["value/cssVariable"]).toBeDefined();
expect(StyleDictionary.transform["fontWeight/number"]).toBeDefined();
expect(StyleDictionary.transform["gradient/css"]).toBeDefined();
expect(StyleDictionary.transform["cubicBezier/css"]).toBeDefined();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('index.ts', () => {
expect(StyleDictionary.transform['shadow/css']).toBeDefined()
expect(StyleDictionary.transform['font/css']).toBeDefined()
expect(StyleDictionary.transform['fontFamily/css']).toBeDefined()
expect(StyleDictionary.transform['variables/css']).toBeDefined()
expect(StyleDictionary.transform['value/cssVariable']).toBeDefined()
expect(StyleDictionary.transform['fontWeight/number']).toBeDefined()
expect(StyleDictionary.transform['gradient/css']).toBeDefined()
expect(StyleDictionary.transform['cubicBezier/css']).toBeDefined()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import StyleDictionary from "style-dictionary";
import { variablesCss } from "../../src/transformer/variables-css";
import { valueToCssVariable } from "../../src/transformer/value-to-css-variable";

describe("Transformer: variablesCss", () => {
describe("Transformer: valueToCssVariable", () => {
const items = [
{
name: "red",
Expand All @@ -20,13 +20,13 @@ describe("Transformer: variablesCss", () => {

it("transforms names to CSS variable notation", () => {
expect(
items.map((item) => variablesCss.transformer(item, {}))
items.map((item) => valueToCssVariable.transformer(item, {}))
).toStrictEqual(["var(--red)", "var(--white)"]);
});

it("adds prefix to CSS variable notation", () => {
expect(
items.map((item) => variablesCss.transformer(item, options))
items.map((item) => valueToCssVariable.transformer(item, options))
).toStrictEqual([
"var(--PREFIX-red, #ff0000)",
"var(--PREFIX-white, #ffffff)",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { fontWeightToNumber } from './transformer/font-weight-to-number'
import { gradientCss } from './transformer/gradient-css'
import { namePathToDotNotation } from './transformer/name-path-to-dot-notation'
import { shadowCss } from './transformer/shadow-css'
import { variablesCss } from './transformer/variables-css'
import { valueToCssVariable } from './transformer/value-to-css-variable'

/**
* Parsers
Expand Down Expand Up @@ -161,8 +161,8 @@ OrigialStyleDictionary.registerTransform({
})

OrigialStyleDictionary.registerTransform({
name: 'variables/css',
...variablesCss
name: 'value/cssVariable',
...valueToCssVariable
})
/**
* Transform groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function formatCssVariable(name: string, value: string, options: CssOptions) {
* variablesCss
* @description convert the `name` to a css variable
*/
export const variablesCss: StyleDictionary.Transform = {
export const valueToCssVariable: StyleDictionary.Transform = {
type: `value`,
transitive: true,
transformer: (
Expand Down
Loading