Skip to content

Color variable best practices #485

Answered by nmn
fredrivett asked this question in Q&A
Mar 8, 2024 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

The cleanest answer here is to use the new native CSS feature Relative Color Syntax. With it, you'd be able to do something like this:

import * as stylex from "@stylexjs/stylex";

export const colors = stylex.defineVars({
  blue: '#0093ff',
});

And then use it like so:

import * as stylex from "@stylexjs/stylex";
import {vars} from './tokens';

const styles = stylex.create({
  blue: {
    color: vars.blue,
    backgroundColor: `rgb(from ${vars.blue} r g b / 0.2)`,
  }
});

This works natively in Chrome and Safari and hopefully, there is a polyfill for Firefox.


Another simple approach is to store the r, g, b values:

import * as stylex from "@stylexjs/stylex";

export const colors = stylex.d…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@fredrivett
Comment options

Comment options

You must be logged in to vote
1 reply
@fredrivett
Comment options

Answer selected by fredrivett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants