Stylelint plugin that validates CSS custom properties in CSS modules to ensure they follow a component-based naming convention.
When working with CSS modules, components often expose custom properties as a public API that can be overridden by parent components. This plugin validates that these public custom properties follow a consistent naming convention based on the component name.
For example, if your CSS module is named Button.module.css
:
.button {
/* Public API - validated */
background-color: var(--Button-primary-color, #007bff);
/* Private properties - not validated */
--internal-state: active;
}
This convention helps with:
- Consistency: All public custom properties follow the same naming pattern
- Clarity: It's immediately clear which properties are part of the component's public API
- Avoiding conflicts: Prevents naming collisions when components are nested
- API documentation: Fallback values serve as default values and documentation
npm install --save-dev @simonsmith/stylelint-component-custom-property
yarn add --dev @simonsmith/stylelint-component-custom-property
pnpm add --save-dev @simonsmith/stylelint-component-custom-property
Add the plugin to your stylelint configuration:
{
"plugins": ["@simonsmith/stylelint-component-custom-property"],
"rules": {
"@simonsmith/stylelint-component-custom-property": true
}
}
The rule accepts different validation types:
Validates only that custom properties match the component name from the filename:
{
"plugins": ["@simonsmith/stylelint-component-custom-property"],
"rules": {
"@simonsmith/stylelint-component-custom-property": true
}
}
Validates SUIT CSS naming conventions:
{
"plugins": ["@simonsmith/stylelint-component-custom-property"],
"rules": {
"@simonsmith/stylelint-component-custom-property": {
"validationType": "suitcss"
}
}
}
Due to the potential ambiguity of the validation pattern in SUIT CSS this option prefers to lean on being more relaxed rather than incorrectly flagging properties as invalid. See the unit tests for what it covers currently
Use your own regular expression for suffix validation:
{
"plugins": ["@simonsmith/stylelint-component-custom-property"],
"rules": {
"@simonsmith/stylelint-component-custom-property": {
"validationType": /^-[a-z][a-zA-Z]*$/
}
}
}
{
"rules": {
"@simonsmith/stylelint-component-custom-property": false
}
}
The plugin:
- Only applies to CSS modules - files ending with
.module.css
- Validates public API custom properties - ensures they start with
--ComponentName-
- Ignores private custom properties - allows any naming for internal use
- Autofix - can automatically correct invalid prefixes in API declarations
The plugin validates custom properties only when they're used as public API declarations.
Custom properties used in var()
functions with fallback values are treated as component API declarations:
.container {
gap: var(--Button-spacing, 1rem);
width: var(--Button-max-width, 320px);
}
Direct custom property assignments are considered private implementation details:
.button {
--internal-state: hover;
--computed-size: calc(100% - 2rem);
}
Custom properties used without fallbacks are considered consumption of existing properties:
.button {
color: var(--theme-primary);
margin: var(--Button-spacing);
}
Button.module.css
.button {
/* Public API declarations - validated */
background-color: var(--Button-primary-color, #007bff);
padding: var(--Button-padding, 0.5rem 1rem);
/* Private properties - not validated */
--internal-state: default;
--computed-width: calc(100% - 2rem);
/* Consuming existing properties - not validated */
margin: var(--global-spacing);
font-family: var(--theme-font);
}
/* Overriding child component APIs - not validated */
.button-container {
--Icon-size: 16px;
--Tooltip-background: var(--Button-primary-color);
}
Button.module.css
.button {
/* Wrong prefix in API declaration */
background: var(--wrong-color, red);
/* No prefix in API declaration */
padding: var(--spacing, 1rem);
}
The plugin supports stylelint's --fix
option and will automatically correct invalid custom property prefixes in API declarations:
stylelint "**/*.module.css" --fix
MIT