Skip to content

Commit

Permalink
Add Tokens Studio support, deprecate Figma sync
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jun 5, 2023
1 parent 9f967b5 commit 52f898b
Show file tree
Hide file tree
Showing 41 changed files with 1,117 additions and 1,308 deletions.
6 changes: 6 additions & 0 deletions .changeset/chatty-queens-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@cobalt-ui/core': minor
'@cobalt-ui/cli': minor
---

Deprecate Figma sync CLI and core functionality (in favor of Tokens Studio support)
6 changes: 6 additions & 0 deletions .changeset/clever-rats-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@cobalt-ui/core': minor
'@cobalt-ui/cli': minor
---

Add Tokens Studio support
12 changes: 6 additions & 6 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"dev:serve": "astro dev"
},
"devDependencies": {
"@cobalt-ui/cli": "^1.0.0",
"@cobalt-ui/core": "^1.0.0",
"@cobalt-ui/plugin-css": "^1.2.0",
"@cobalt-ui/plugin-sass": "^1.2.0",
"astro": "^2.5.4",
"@cobalt-ui/cli": "workspace:^",
"@cobalt-ui/core": "workspace:^",
"@cobalt-ui/plugin-css": "workspace:^",
"@cobalt-ui/plugin-sass": "workspace:^",
"astro": "^2.5.7",
"npm-run-all": "^4.1.5",
"sass": "^1.62.1",
"shiki": "^0.14.2",
"vite": "^4.3.8"
"vite": "^4.3.9"
}
}
Binary file added docs/public/images/tokens-studio-for-figma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/src/docs-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
{"title": "Guides", "pathname": "/docs/guides"},
{"title": "Best Practices", "pathname": "/docs/guides/best-practices"},
{"title": "Modes", "pathname": "/docs/guides/modes"},
{"title": "Figma", "pathname": "/docs/guides/figma"},
{"title": "Tokens Studio", "pathname": "/docs/guides/tokens-studio"},
{"title": "Create Plugins", "pathname": "/docs/guides/plugins"},
{"title": "Plugins", "pathname": "/docs/plugins"},
{"title": "CSS", "pathname": "/docs/plugins/css"},
{"title": "Sass", "pathname": "/docs/plugins/sass"},
{"title": "JS/JSON/TS", "pathname": "/docs/plugins/js"},
{"title": "Reference", "pathname": "/docs/reference"},
{"title": "Config", "pathname": "/docs/reference/config"},
{"title": "Config API", "pathname": "/docs/reference/config"},
{"title": "About", "pathname": "/docs/reference/about"}
]
}
2 changes: 2 additions & 0 deletions docs/src/pages/docs/getting-started/generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import pluginJS from '@cobalt-ui/plugin-js';

/** @type import('@cobalt-ui/core').Config */
export default {
tokens: './tokens.json',
outDir: './tokens/',
plugins: [pluginCSS(), pluginJS()],
};
```
Expand Down
6 changes: 4 additions & 2 deletions docs/src/pages/docs/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The W3C Community Group’s approach differs in being the [largest collaborative

## Requirements

- [Node.js](https://nodejs.org) v16 or higher (newer is always recommended).
- [Node.js](https://nodejs.org) v18 or higher (newer is always recommended).

## Install

Expand All @@ -47,6 +47,8 @@ import pluginCSS from '@cobalt-ui/plugin-css';

/** @type import('@cobalt-ui/core').Config */
export default {
tokens: './tokens.json',
outDir: './tokens/',
plugins: [pluginCSS()],
};
```
Expand All @@ -62,7 +64,7 @@ This will generate a **tokens/tokens.css** file in your project for you to use.
## Next steps

- [Learn about tokens](/docs/tokens)
- [Sync with Figma](/docs/guides/figma)
- [Sync with Figma using Tokens Studio](/docs/guides/tokens-studio)
- [Learn how to configure Cobalt](/docs/reference/config)
- [Add plugins](/docs/plugins)

Expand Down
216 changes: 2 additions & 214 deletions docs/src/pages/docs/guides/figma.md
Original file line number Diff line number Diff line change
@@ -1,220 +1,8 @@
---
title: Figma
title: Syncing with Figma
layout: ../../../layouts/docs.astro
---

# Figma

## Setup

1. Start a new project with `npx co init`
2. Generate a [new Figma API Key][figma-api-key].
3. Save this key as `FIGMA_API_KEY` in a [`.env` file][dotenv].

```
FIGMA_API_KEY=285541-dd09c1b4-c1d3-41a2-802b-f3866f0dadc1
```

…or as an [environment variable in your system][env-system]

4. In your Figma Doc, click **Share**, then **Copy link**.
5. In `tokens.config.mjs`, under `figma`, paste your share link, and specify component names and properties within each link ([instructions](#mapping)):

```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
/** docs to sync */
docs: [
{
url: 'https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2', // “Share” > Copy link
tokens: [
{style: 'Blue', token: 'color.blue', type: 'color'},
{component: 'Icon.Alert', token: 'icon.alert', type: 'file'},
],
},
],
},
};
```

6. Run `npx co sync` to update `tokens.json` with the new values

## Mapping

Give Cobalt a list of every Figma file you want to sync, along with components
and styles, and Cobalt will do the rest! After the initial setup, you’ll only
have to edit mappings when adding or removing components.

| Property | Description |
| :--------------------- | :----------------------------------------------------------------------------- |
| `style` \| `component` | Specify the name of a Figma style or component (must be one or the other) |
| `type` | The [type][types] of token. Only a few types are supported (documented below). |
| `token` | Where you’d like the token to live in `tokens.json`. |

### Colors / Gradients

![](/images/figma-colors.png)

| Type | Effect |
| :--------- | :-------------------------------------------------------- |
| `color` | Extract any **solid fills** from a style or component. |
| `gradient` | Extract any **gradient fills** from a style or component. |

Use `type: "color"` or `type: "gradient"` to extract fills from a Figma style or component.

<!-- prettier-ignore -->
```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
docs: [
{
url: "https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2", // “Share” > Copy link
tokens: [
{style: "Black", token: "color.black", type: "color"},
{style: "Dark Gray", token: "color.dark-gray", type: "color"},
{style: "Blue", token: "color.blue", type: "color"},
{style: "Red", token: "color.red", type: "color"},
{style: "Green", token: "color.green", type: "color"},
{style: "Purple", token: "color.purple", type: "color"},
{style: "Red Gradient", token: "gradient.red", type: "gradient"},
],
},
],
},
};
```

### Files / Icons

![](/images/figma-icons.png)

By using `type: "link"` along with a `filename` path you can save a component locally or to a Git repo. This is great for icons.

<!-- prettier-ignore -->
```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
docs: [
{
url: "https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2", // “Share” > Copy link
tokens: [
{component: "alert", token: "icon.alert", type: "link", filename: "./icons/alert.svg"},
{component: "download", token: "icon.download", type: "link", filename: "./icons/download.svg"},
{component: "refresh", token: "icon.refresh", type: "link", filename: "./icons/refresh.svg"},
],
},
],
},
};
```

Note that the more icons you sync, the longer it may take to update your tokens. But even if this takes a few minutes, it still beats having to download them all manually.

#### Optimization

Sometimes you’ll find Figma exports needing a little cleanup. By adding the `figma.optimize` option, you can run optimizers over the downloaded files:

```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
docs: [
{
url: 'https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2',
tokens: [{component: 'alert', token: 'icon.alert', type: 'file', filename: './icons/alert.svg'}],
},
],
optimize: {
svgo: {
// SVGO options (to use defaults, leave object empty or set svgo: true)
removeTitle: false,
},
},
},
};
```

To disable optimizers, either omit them from the config, or set them to `false`.

##### Supported optimizers

- [SVGO](https://github.com/svg/svgo)
- Images coming soon?

##### Token overrides

If the default optimization settings are messing up a few tokens, you can override them by adding the same `optimize` settings on an individual token itself. This will override any defaults. Or, you may disable optimization by setting `optimize: false` or `optimize.svgo: false` on an individual token to skip optimization just for that token.

### Shadow

To pull a **Drop Shadow** from Figma, use the `shadow` type.

<!-- prettier-ignore -->
```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
docs: [
{
url: "https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2", // “Share” > Copy link
tokens: [
{style: 'Distance / Medium', token: 'shadow.distance-medium', type: 'shadow'},
{style: 'Distance / Far', token: 'shadow.distance-far', type: 'shadow'},
],
},
],
},
};
```

### Typography

![](/images/figma-typography.png)

To extract a **Text Style** from Figma, use the `fontFamily` or `typography` type.

| Type | Effect |
| :----------- | :----------------------------------------------------------- |
| `fontFamily` | Extract only the font family name from a style or component. |
| `typography` | Extract all text styles from a style or component. |

<!-- prettier-ignore -->
```js
/** @type import('@cobalt-ui/core').Config */
export default {
figma: {
docs: [
{
url: "https://www.figma.com/file/Mm0nTq0UXZKG1WXu7PeCmS/MyFile?node-id=2%3A2", // “Share” > Copy link
tokens: [
{style: "Brand Sans", token: "typography.family.brand-sans", type: "fontFamily"},
{style: "Typography / Body", token: "typography.body", type: "typography"},
{style: "Typography / Heading 1", token: "typography.heading-1", type: "typography"},
{style: "Typography / Heading 2", token: "typography.heading-2", type: "typography"},
],
},
],
},
};
```

## Troubleshooting

If you’re having trouble syncing from Figma, here are some quick tips:

- Make sure you are trying to extract a **style** or **component**. Normal layers aren’t supported for extraction.
- Every style/component must be located within the share link specified (if using shared components, **use the file they are defined in**, not _a_ file that uses them).
- Every style/component should have a unique name within its own Figma doc (using the same name in different docs is OK)
- Make sure `tokens.config.mjs` _perfectly_ matches your component name in Figma (watch for typos!)

[alias]: /docs/tokens#aliasing
[dotenv]: https://github.com/motdotla/dotenv
[env-system]: https://gist.github.com/iest/58692bf1001b0424c257
[issues]: https://github.com/drwpow/cobalt-ui/issues
[modes]: /docs/guides/modes
[figma-api]: /docs/reference/config#figma
[figma-api-key]: https://www.figma.com/developers/api#access-tokens
[types]: /docs/tokens#types
Syncing with Figma since `1.1.0` is now done via the [Tokens Studio plugin for Figma](/docs/guides/tokens-studio). The previous version of Figma syncing was buggy and incomplete.
2 changes: 1 addition & 1 deletion docs/src/pages/docs/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ layout: ../../../layouts/docs.astro

- [Best Practices](/docs/guides/best-practices/)
- [Thinking in Modes](/docs/guides/modes)
- [Syncing with Figma](/docs/figma)
- [Syncing with Figma using Tokens Studio](/docs/guides/tokens-studio)
27 changes: 27 additions & 0 deletions docs/src/pages/docs/guides/tokens-studio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Tokens Studio for Figma
layout: ../../../layouts/docs.astro
---

# Tokens Studio for Figma

![Tokens Studio for Figma](/images/tokens-studio-for-figma.png)

[Tokens Studio for Figma](https://tokens.studio/) is a free plugin that makes managing design tokens in Figma easy ([docs](https://docs.tokens.studio/)). While it doesn’t use the W3C Design Tokens format like Cobalt does, Cobalt supports most of Tokens Studio’s format.

To use Tokens Studio, first export a `tokens.json` file using [any of the approved sync methods](https://docs.tokens.studio/sync/sync). Then use Cobalt as you would normally:

```js
import pluginCSS from '@cobalt-ui/plugin-css';

/** @type import('@cobalt-ui/core').Config */
export default {
tokens: './tokens.json',
outDir: './tokens/',
plugins: [pluginCSS()],
};
```

> ⚠️ **Compatibility Warning**
>
> While Cobalt will do the best job it can converting from Tokens Studio for Figma’s format, some types may be incompatible. If you encounter any problems, please [file an issue](https://github.com/drwpow/cobalt-ui/issues).
Loading

0 comments on commit 52f898b

Please sign in to comment.