Skip to content

Commit

Permalink
Add Tailwind plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Oct 20, 2023
1 parent 4e3262a commit f36b3f0
Show file tree
Hide file tree
Showing 17 changed files with 424 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"keywords": [
"design tokens",
"dtfm",
"dtcg",
"cli",
"w3c design tokens",
"design system",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-css/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
tsconfig.json
4 changes: 4 additions & 0 deletions packages/plugin-css/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ Generate CSS from your design tokens using [Cobalt](https://cobalt-ui.pages.dev)

## Setup

Install the plugin from npm:

```bash
npm i -D @cobalt-ui/plugin-css
```

Then add to your `tokens.config.mjs` file:

```js
// tokens.config.mjs
import pluginCSS from '@cobalt-ui/plugin-css';
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
},
"keywords": [
"cobalt",
"design tokens"
"design tokens",
"design system",
"dtfm",
"w3c",
"css"
],
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-js/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
tsconfig.json
5 changes: 5 additions & 0 deletions packages/plugin-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ Generate JSON and JS (with TypeScript types) from your design tokens using [Coba

**Features**

- ✅ Universal JSON format makes it easy to consume tokens in any platform (including native/compiled code)
- ✅ Access all your design tokens safely and programatically in any frontend or backend setup
- ✅ Full support for token modes (e.g. light/dark mode)
- ✅ Automatic TypeScript types for strong typechecking (never have a broken style)

## Setup

Install the plugin from npm:

```bash
npm i -D @cobalt-ui/plugin-js
```

Then add to your `tokens.config.mjs` file:

```js
// tokens.config.mjs
import pluginJS from '@cobalt-ui/plugin-js';
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
},
"keywords": [
"cobalt",
"design tokens"
"design tokens",
"design system",
"dtfm",
"dtcg",
"w3c"
],
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-sass/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
tsconfig.json
4 changes: 4 additions & 0 deletions packages/plugin-sass/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ Generate `.scss` and `.sass` output from your design tokens using [Cobalt](https

## Setup

Install the plugin (and its dependency) from npm:

```bash
npm i -D @cobalt-ui/plugin-sass @cobalt-ui/plugin-css
```

Then add to your `tokens.config.mjs` file:

```js
// tokens.config.mjs
import pluginSass from '@cobalt-ui/plugin-sass';
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-sass/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
},
"keywords": [
"cobalt",
"design tokens"
"design tokens",
"design system",
"dtfm",
"dtcg",
"w3c",
"css",
"sass"
],
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-tailwind/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test
tsconfig.json
77 changes: 77 additions & 0 deletions packages/plugin-tailwind/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# @cobalt-ui/plugin-tailwind

Generate a [Tailwind CSS](https://tailwindcss.com/) preset from your design tokens.

## Setup

Install the plugin from npm:

```bash
npm i -D @cobalt-ui/plugin-tailwind
```

Then add to your `tokens.config.mjs` file, configuring [theme](https://tailwindcss.com/docs/configuration#theme) as you would normally, except replacing the values with token IDs:

```js
// tokens.config.mjs
import pluginTailwind from '@cobalt-ui/plugin-tailwind';

/** @type import('@cobalt-ui/core').Config */
export default {
tokens: './tokens.json',
outDir: './tokens/',
plugins: [
pluginTailwind({
/** (optional) the path to the Tailwind preset */
output?: './tailwind-tokens.js',
/** (optional) module format to use (to match your Tailwind config) */
outputFormat?: 'esm' | 'cjs',
tailwind: {
theme: {
/** @see https://tailwindcss.com/docs/configuration#theme */
colors: {
blue: {
100: 'color.blue.100',
200: 'color.blue.200',
//
},
},
fontFamily: {
sans: 'typography.family.base',
//
},
extend: {
spacing: {
m: 'token.size.m.space',
//
},
borderRadius: {
m: 'token.size.m.borderRadius',
//
},
},
},
},
}),
],
};
```

Then, when you run `co build` in your project, it will generate a `./tokens/tailwind-tokens.js` file. Now add it to your Tailwind config under `presets` ([docs](https://tailwindcss.com/docs/configuration#presets)):

```diff
+ import tailwindTokens from './tokens/tailwind-tokens.js';

/** @type {import('tailwindcss').Config} */
export default {
+ presets: [
+ tailwindTokens,
+ ],
};
```

_Note: if using CommonJS, use the `outputFormat: 'cjs'` plugin option._

And you’re up and running! You’ll now have all your design tokens available in Tailwind.

_**Tip**: be sure to rerun `co build` to rebuild your Tailwind preset, or run `co build --watch` to rebuild your tokens every time they change!_
48 changes: 48 additions & 0 deletions packages/plugin-tailwind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@cobalt-ui/plugin-tailwind",
"version": "0.0.0",
"author": {
"name": "Drew Powers",
"email": "[email protected]"
},
"keywords": [
"cobalt",
"design tokens",
"design system",
"dtfm",
"dtcg",
"w3c",
"tailwind",
"css"
],
"repository": {
"type": "git",
"url": "https://github.com/drwpow/cobalt-ui.git",
"directory": "packages/plugin-tailwind"
},
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
"scripts": {
"build": "run-s -s build:*",
"build:clean": "del dist",
"build:ts": "tsc",
"build:license": "node ../../scripts/inject-license.js @cobalt-ui/plugin-tailwind dist/index.js",
"dev": "tsc -w",
"test": "run-p -s test:*",
"test:js": "vitest run",
"test:ts": "tsc --noEmit"
},
"peerDependencies": {
"@cobalt-ui/cli": "*",
"tailwindcss": "*"
},
"devDependencies": {
"@cobalt-ui/cli": "^1.6.0",
"@cobalt-ui/core": "^1.6.0",
"@types/node": "^20.5.7",
"npm-run-all": "^4.1.5",
"tailwindcss": "^3.3.3",
"vitest": "^0.34.3"
}
}
22 changes: 22 additions & 0 deletions packages/plugin-tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {Plugin} from '@cobalt-ui/core';
import type {} from 'tailwindcss';
import {ThemeConfig} from 'tailwindcss/types/config';

export interface Options {
/** The output path for the Tailwind preset (default: `./tailwind-tokens.js`) */
output?: string;
/** (optional) module format to use (to match your Tailwind config) */
outputFormat?: 'esm' | 'cjs';
tailwind: {
theme: ThemeConfig;
};
}

export default function pluginSass(options?: Options): Plugin {
return {
name: '@cobalt-ui/plugin-tailwind',
build(tokens) {
return [{filename: options?.output ?? './tailwind-tokens.js', src: ''}];
},
};
}
1 change: 1 addition & 0 deletions packages/plugin-tailwind/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
describe('@cobalt-ui/plugin-tailwind', () => {});
7 changes: 7 additions & 0 deletions packages/plugin-tailwind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist/"
},
"include": ["src"]
}
Loading

0 comments on commit f36b3f0

Please sign in to comment.