Local Values and Variables
You'll learn how to define and share reusable constants across CSS Modules using @value declarations.
When working with CSS Modules, you often need to use the same colors, spacing values, or breakpoints across many component files. Instead of copying magic strings like #3b82f6 or 768px everywhere, you can define them once as local values and import them wherever needed.
The Core Idea
Think of @value as a way to create named constants in CSS Modules—like variables in programming. You define them in one file, export them, and import them into other modules. This keeps your design system consistent and makes updates effortless: change the value once, and it updates everywhere.
Defining and Exporting Values
Create a file (often called colors.module.css or constants.module.css) to hold your shared values:
/* colors.module.css */
@value primaryBlue: #3b82f6;
@value darkGray: #1f2937;
@value breakpointMd: 768px;
These values are automatically exported and ready to use in other CSS Modules.
Importing and Using Values
In any component's CSS Module file, import the values you need:
/* Button.module.css */
@value primaryBlue from './colors.module.css';
.button {
background-color: primaryBlue;
padding: 1rem 2rem;
}
Now primaryBlue acts like a local variable—scoped to this module, but sourced from your centralized constants file.
Why This Matters
This approach creates a single source of truth for design tokens, making your CSS architecture more maintainable and reducing the risk of inconsistent styling across your application.
Key Takeaway: Use @value in CSS Modules to define, export, and import reusable constants like colors and breakpoints, ensuring consistency and maintainability across your entire stylesheet architecture.