carbon scss or css variables? #18087
-
Hi, What is the recommended way to handle carbon variables in a React app? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I can't speak for the Carbon team but as a long time consumer, I would highly recommend using the Sass variables as they give you some advantages over the direct CSS custom properties: 1. ConfigurationAspects like the prefix can be configured globally. E.g.: @use '@carbon/react/scss/config' with (
$prefix: 'my-custom-prefix',
); All Sass variables will automatically adapt to that. 2. Error handlingSass will throw you a build error if for example 3. FallbacksAt least for colors, the Sass variables include an appropriate fallback value in case the CSS custom property isn't set correctly. I think the spacing tokens currently don't do this but could probably be set up that way as well. .my-component {
/* color: $text-primary; */
color: var(--cds-text-primary, #0f62fe);
} |
Beta Was this translation helpful? Give feedback.
I can't speak for the Carbon team but as a long time consumer, I would highly recommend using the Sass variables as they give you some advantages over the direct CSS custom properties:
1. Configuration
Aspects like the prefix can be configured globally. E.g.:
All Sass variables will automatically adapt to that.
2. Error handling
Sass will throw you a build error if for example
$spacing-03
wouldn't exist. Usingvar(--cds-spacing-03)
requires visual testing and can fail silently.3. Fallbacks
At least for colors, the Sass variables include an appropriate fallback value in case the CSS custom property isn't set correct…