PostCSS plugin that scans your CSS declarations and looks for function calls that match the configured shortcuts. When it finds a match, it replaces the function call with a CSS variable reference that follows Tailwind v4's naming conventions. Works with both Tailwind and custom CSS files.
For example:
speed(slow)becomesvar(--transition-duration-slow)colorCode('primary')becomesvar(--color-primary)
npm install @locomotivemtl/postcss-tailwind-shortcuts --save-devTo use this plugin, include it in your PostCSS configuration file.
PostCSS Configuration:
import postcssTailwindShortcuts from '@locomotivemtl/postcss-tailwind-shortcuts';
export default {
plugins: [postcssTailwindShortcuts()]
};You can add your own custom shortcuts by passing them in the options:
import postcssTailwindShortcuts from '@locomotivemtl/postcss-tailwind-shortcuts';
export default {
plugins: [
postcssTailwindShortcuts({
shortcuts: [
{
functionIdent: 'shadow',
cssVariablePrefix: '--shadow'
},
{
functionIdent: 'radius',
cssVariablePrefix: '--radius'
}
]
})
]
};Important
Add static to your @theme directive to prevent Tailwind from purging CSS variables. This ensures they remain available for the plugin functions.
Source
And set them in your Tailwind theme configuration:
/* Using Tailwind @theme directive */
@theme static {
--shadow-large: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
--radius-medium: 0.375rem;
}| Option | Type | Description |
|---|---|---|
shortcuts |
Array |
Array of custom shortcut objects with functionIdent and cssVariablePrefix properties |
Each shortcut object should have:
functionIdent: The function name to use in CSS (e.g.,'shadow')cssVariablePrefix: The CSS variable prefix (e.g.,'--shadow')
This plugin comes with the following pre-configured shortcuts that are based on Tailwind CSS v4's variable naming conventions:
speed(value): Converts tovar(--transition-duration-{value})ease(value): Converts tovar(--ease-{value})z(value): Converts tovar(--z-index-{value})colorCode(value): Converts tovar(--color-{value})spacing(value): Converts tovar(--spacing-{value})radius(value): Converts tovar(--radius-{value})
These shortcuts are designed to work with Tailwind CSS v4's CSS variable system, where design tokens are exposed as CSS custom properties.
/* Input CSS */
.example {
transition-duration: speed(slow);
transition-timing-function: ease(fast);
z-index: z(modal);
color: colorCode(primary);
margin: spacing(4);
}
/* Output CSS */
.example {
transition-duration: var(--transition-duration-slow);
transition-timing-function: var(--ease-fast);
z-index: var(--z-index-modal);
color: var(--color-primary);
margin: var(--spacing-4);
}/* Input CSS */
.card {
box-shadow: shadow(large);
border-radius: radius(medium);
background-color: colorCode('accent');
}
/* Output CSS */
.card {
box-shadow: var(--shadow-large);
border-radius: var(--radius-medium);
background-color: var(--color-accent);
}The plugin supports both quoted and unquoted arguments:
/* All of these work the same way */
.element {
border-radius: radius(medium);
border-radius: radius('medium');
border-radius: radius('medium');
}If you're using custom shortcuts or want to extend the default behavior, you can define additional CSS variables:
/* Custom CSS */
:root {
--box-shadow-large: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
--border-radius-medium: 0.375rem;
}