CSS variables
In case you didn't know, CSS variables are reusable entities in CSS which contain specific values. Learn more about CSS variables on MDN Documentation.
Not only in CSS variables, Ravenbright design tokens are also available in other format such as: Tailwind CSS config, Figma Tokens, & JSON 👇.
Overview​
Ravenbright uses CSS variables as the primary design token format. It also has prefix: --rbrh-
to prevent clash with other frameworks or libraries. Ravenbright offers 4 different ways to customize its library:
- changing components style with Utility Classes
- overriding existing classes or existing styles with CSS Variables
- adding & modifying CSS Custom Properties
Styling with utility classes​
Let's begin by modifying .alert background-color from gray
to yellow
, we could update that style without leaving the markup like this:
<!--The legacy alert component-->
<div class="alert alert-neutral">This is a neutral alert</div>
<!--The new alert component-->
<div class="alert bg-yellow-200 dark-bg-yellow-800">
This is a warning alert
</div>
Overriding existing CSS classes​
Ravenbright styles and component can also be personalized with your own brand at high level (global scope). The advantage is, it will provide better control.
Here are the steps to customize the components using CSS variables. Let's start by changing button border-radius from 1rem
to 0.5rem
, we could update the button primary class by:
- Opening the button component file in
component folder
- Using the lower border radius CSS variables inside your CSS selector. This will only update the btn-primary class
.btn-primary {
/*This is old border radius variable*/
border-radius: var(--rbrh-radius-md);
/*Apply the new border radius variable*/
border-radius: var(--rbrh-radius-sm);
}
Modifying CSS variables values​
- Open the
css-variable.css
file intokens folder
- Change the value of border CSS variables in :root selector. Meanwhile, this action will change other component which connected with the variables.
:root {
/* Border radius */
--rbrh-radius-circle: 100rem;
--rbrh-radius-xl: 2rem;
--rbrh-radius-lg: 1rem;
--rbrh-radius-md: 0.8rem;
--rbrh-radius-sm: 0.5rem;
--rbrh-radius-xs: 0.3rem;
}
- Compile the CSS with PostCSS
postcss main.css --output output.css --watch
Adding new CSS variables​
- Open the
css-variable.css
file intokens folder
- Determine design tokens (colors, border-radius, spacing, etc.)
- Write the new variables inside :root selector
:root {
/* Border radius */
--rbrh-radius-circle: 100rem;
--rbrh-radius-xl: 2rem;
--rbrh-radius-lg: 1rem;
--rbrh-radius-md: 0.8rem;
--rbrh-radius-sm: 0.5rem;
--rbrh-radius-xs: 0.3rem;
/* New border-radius token */
--rbrh-radius-xxl: 2.5rem;
}
- Implement the new variables inside the CSS class
.card {
border-radius: var(--rbrh-radius-xxl);
}
- Compile the CSS with PostCSS
postcss main.css --output output.css --watch