|
| 1 | +# Tailwind preset |
| 2 | + |
| 3 | +Builds [Tailwind preset](https://tailwindcss.com/docs/presets#creating-a-preset) from tokens. |
| 4 | + |
| 5 | +## Building the preset |
| 6 | + |
| 7 | +Run `npm run build-tokens` to generate these files in `build/tailwind`: |
| 8 | + |
| 9 | +### cssVarPlugin.js |
| 10 | + |
| 11 | +A [Tailwind plugin](https://tailwindcss.com/docs/plugins) for registering new [base styles](https://tailwindcss.com/docs/plugins#adding-base-styles). |
| 12 | + |
| 13 | +The [rgbChannels](./config/transform.js) transform removes the color space function for compatability with [Tailwind's opacity modifier syntax](https://tailwindcss.com/docs/text-color#changing-the-opacity). |
| 14 | + |
| 15 | +```js |
| 16 | +import plugin from 'tailwindcss/plugin.js'; |
| 17 | + |
| 18 | +export default plugin(function ({ addBase }) { |
| 19 | + addBase({ |
| 20 | + ':root': { |
| 21 | + '--sd-text-small': '0.75', |
| 22 | + '--sd-text-base': '46 46 70', |
| 23 | + '--sd-text-secondary': '100 100 115', |
| 24 | + '--sd-text-tertiary': '129 129 142', |
| 25 | + '--sd-text-neutral': '0 0 0 / 0.55', |
| 26 | + '--sd-theme': '31 197 191', |
| 27 | + '--sd-theme-light': '153 235 226', |
| 28 | + '--sd-theme-dark': '0 179 172', |
| 29 | + '--sd-theme-secondary': '106 80 150', |
| 30 | + '--sd-theme-secondary-dark': '63 28 119', |
| 31 | + '--sd-theme-secondary-light': '196 178 225', |
| 32 | + }, |
| 33 | + }); |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +### themeColors.js |
| 38 | + |
| 39 | +Tailwind theme color values that reference the plugin [css vars](https://tailwindcss.com/docs/customizing-colors#using-css-variables). |
| 40 | + |
| 41 | +```js |
| 42 | +export default { |
| 43 | + 'sd-text-base': 'rgb(var(--sd-text-base))', |
| 44 | + 'sd-text-secondary': 'rgb(var(--sd-text-secondary))', |
| 45 | + 'sd-text-tertiary': 'rgb(var(--sd-text-tertiary))', |
| 46 | + 'sd-text-neutral': 'rgb(var(--sd-text-neutral))', |
| 47 | + 'sd-theme': 'rgb(var(--sd-theme))', |
| 48 | + 'sd-theme-light': 'rgb(var(--sd-theme-light))', |
| 49 | + 'sd-theme-dark': 'rgb(var(--sd-theme-dark))', |
| 50 | + 'sd-theme-secondary': 'rgb(var(--sd-theme-secondary))', |
| 51 | + 'sd-theme-secondary-dark': 'rgb(var(--sd-theme-secondary-dark))', |
| 52 | + 'sd-theme-secondary-light': 'rgb(var(--sd-theme-secondary-light))', |
| 53 | +}; |
| 54 | +``` |
| 55 | + |
| 56 | +### preset.js |
| 57 | + |
| 58 | +[Tailwind preset](https://tailwindcss.com/docs/presets) file that imports the colors and plugin. |
| 59 | + |
| 60 | +```js |
| 61 | +import themeColors from './themeColors.js'; |
| 62 | +import cssVarsPlugin from './cssVarsPlugin.js'; |
| 63 | + |
| 64 | +export default { |
| 65 | + theme: { |
| 66 | + extend: { |
| 67 | + colors: { |
| 68 | + ...themeColors, // <-- theme colors defined here |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + plugins: [cssVarsPlugin], // <-- plugin imported here |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +## Building the CSS |
| 77 | + |
| 78 | +The [Tailwind preset](https://tailwindcss.com/docs/presets#creating-a-preset) is imported from the build directory in `tailwind.config.js`. |
| 79 | + |
| 80 | +```js |
| 81 | +import tailwindPreset from './build/tailwind/preset.js'; |
| 82 | + |
| 83 | +/** @type {import('tailwindcss').Config} */ |
| 84 | +export default { |
| 85 | + theme: { |
| 86 | + extend: {}, |
| 87 | + }, |
| 88 | + presets: [tailwindPreset], |
| 89 | + content: ['./demo/**/*.{html,js}'], |
| 90 | + plugins: [], |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +Run `npm run build-css` to watch the `demo/index.html` file for changes -- any Tailwind classes used will be compiled into `demo/output.css`. |
0 commit comments