Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How to implement design tokens in a Tailwind CSS project

Hardcoded values like #3b82f6 or 16px scattered through your codebase cause color drift, rebranding headaches, and design-dev mismatches. Design tokens solve this by centralizing values like colors, spacing, and typography under named variables. Why Use Design Tokens? Consistency: One source of truth prevents color drift across a large codebase. Rebranding: Change a few token values to update the…

Design tokens provide a centralized source of truth for values like colors, spacing, and typography. Instead of hardcoding these values throughout your codebase, design tokens enable consistency across a large codebase, simplify rebranding, ensure design-dev parity, and support multi-platform styling. To implement design tokens in a Tailwind CSS project:

1. Define CSS custom properties in a separate file. For instance, you could define a primary color as `--color-primary: 37 99 235;` (blue-600 in RGB) and a radius as `--radius-md: 0.375rem;`.

2. For dark mode, redefine the token values accordingly. In the dark mode example, the primary color becomes `--color-primary: 96 165 250;` (blue-400).

3. In your Tailwind configuration file (tailwind.config.js), map these tokens to the Tailwind theme. Use the `extend` property to add custom colors and border radii.

```javascript

module.exports = {

theme: {

extend: {

colors: {

primary: `rgb(var(--color-primary) / alpha-value)`,

},

borderRadius: {

md: 'var(--radius-md)',

},

},

},

};

```

4. Use the tokens in your components. For example, create a button component that uses the `primary` token for its background color.

```javascript

function Button() {

return (

<button

className="bg-primary rounded-md px-4 py-2"

>

Click me

</button>

);

}

```

Key considerations when implementing design tokens include starting small, using semantic names for clarity, documenting token purposes to avoid confusion, and ensuring alignment between design and code. By following these guidelines, design tokens can help maintain consistency, support rebranding, and simplify multi-platform development when used with a utility-first framework like Tailwind.

Rewritten by urgent.news; may contain errors — check the original.

Read the original at dev.to →

Report a problem with this story

More in Tech

Journald logs: ship them to Dynatrace with OneAgent

You open a recent Linux node to look for an error and there is no file to read: everything goes through systemd. The journald logs stay on the machine in a binary format, and they vanish with it if…

  • Journald logs collected via OneAgent log module
  • OneAgent reads journal through systemd API
  • Enable Journald log detector and "Ingest Journald logs" rule

Flutter 3.50 is out: Material and Cupertino are deprecated - here's the migration path

Flutter 3.50 shipped on 11 November with Dart 3.14. The headline change is not a widget or a performance graph - it is a structural one: the Material and Cupertino libraries bundled with the framework…

  • Flutter 3.50 released on November 11th deprecates Material and Cupertino libraries
  • MaterialUiCompatibilityBridge aids transition for code straddling both worlds
  • Dart fix tool applies majority of migration changes, official guide available

More from Saturday 21 November →