Overview
PostCSS plugin that wraps Tailwind CSS group/ and peer/ classes in :global() pseudo-class for CSS Modules compatibility
PostCSS plugin that wraps Tailwind CSS group/ and peer/ classes in :global() pseudo-class to fix compatibility issues with CSS Modules.
Usage
// postcss.config.js
export default {
plugins: {
'@tailwindcss/postcss': {},
'@accelint/postcss-tailwind-css-modules': {},
},
};Reference
function tailwindCssModulesPlugin(): PluginReturns
Returns a PostCSS plugin instance configured to transform group/ and peer/ classes.
The plugin:
- Only processes files ending in
.module.css - Wraps Tailwind
group/andpeer/classes in:global()pseudo-class - Prevents duplicate transformations using symbol-based tracking
Plugin Behavior
The plugin automatically identifies and transforms selectors containing:
- Classes starting with
group/(e.g.,group/sidebar,group/header) - Classes starting with
peer/(e.g.,peer/checked,peer/focus)
Examples
Example: Basic configuration with Turbo and Webpack
// postcss.config.js
export default {
plugins: {
'@tailwindcss/postcss': {},
'@accelint/postcss-tailwind-css-modules': {},
},
};Example: Configuration with Vite
// postcss.config.js
export default {
plugins: {
'@accelint/postcss-tailwind-css-modules': {},
},
};Good to know: When using Vite, the Tailwind Vite plugin handles Tailwind's PostCSS processing, so you only need this plugin in your PostCSS config. Do not use array syntax (
plugins: ['...']) as it will cause errors with Vite—use the object syntax shown above.
Example: CSS transformation
Input:
/* styles.module.css */
.group/sidebar {
color: red;
}
.peer/checked {
color: blue;
}Output:
:global(.group/sidebar) {
color: red;
}
:global(.peer/checked) {
color: blue;
}Example: Complex selectors
The plugin handles complex selectors including pseudo-classes, pseudo-elements, and descendant selectors:
/* Input */
.container .group/sidebar:hover > .item { }
.peer/checked:focus::after { }
/* Output */
.container :global(.group/sidebar):hover > .item { }
:global(.peer/checked):focus::after { }Example: Multiple classes in one selector
/* Input */
.group/header.group/nav { }
/* Output */
:global(.group/header):global(.group/nav) { }Good to know: The plugin only transforms classes that start with
group/orpeer/followed by a forward slash. Classes like.groupor.peer(without the slash) are not transformed. Regular CSS Modules classes in the same selector remain unaffected.
Related
- PostCSS Documentation
- postcss-selector-parser - Used internally for selector transformation
- Tailwind CSS - Parent State
- Tailwind CSS - Sibling State