RTLCSS: LTR to RTL CSS Transformer
RTLCSS is a robust framework for transforming Left-To-Right (LTR) Cascading Style Sheets (CSS) into Right-To-Left (RTL) stylesheets. It enables developers to internationalize web interfaces for RTL languages with minimal manual effort by automatically flipping directional properties (e.g., `left` to `right`, `margin-left` to `margin-right`). The current stable version is 4.3.0, with a focus on stability and compatibility. It operates as a PostCSS plugin, leveraging PostCSS's AST manipulation capabilities, and supports various control directives and a plugin architecture for custom transformations. Key differentiators include its extensive directive support for fine-grained control over transformations, a flexible plugin API, and a clear upgrade path across major versions, primarily driven by PostCSS updates.
Common errors
-
TypeError: (0, _postcss.default) is not a function
cause Attempting to import `postcss` incorrectly in an ESM context (e.g., `import { postcss } from 'postcss';`) when it's typically a default export.fixUse `import postcss from 'postcss';` for ESM or `const postcss = require('postcss');` for CommonJS. -
Error: Cannot find module 'postcss'
cause The `postcss` package, a peer dependency for `rtlcss`, is not installed in the project.fixInstall PostCSS: `npm install postcss` or `yarn add postcss`. -
Error: Command failed with exit code 1
cause Running a build process involving `rtlcss` on an unsupported Node.js version, especially after upgrading to `rtlcss` v3.0 or later.fixEnsure your Node.js environment meets the minimum requirement of `node >=12.0.0` as specified in `rtlcss` v3.0.0 and above. Upgrade Node.js if necessary.
Warnings
- breaking Version 3.0.0 introduced a breaking change by upgrading to PostCSS 8 and dropping support for Node.js versions 6.x, 8.x, 11.x, and 13.x. Users on these Node.js versions or older PostCSS versions will experience compatibility issues.
- breaking When upgrading from version 1.0.0 to 2.0.0, the options and configuration settings underwent significant changes. While CSS directives remain backward-compatible, direct programmatic configuration will likely need updates.
- gotcha RTLCSS is a PostCSS plugin. Attempting to use it directly on CSS strings without integrating it into a PostCSS processing pipeline will result in errors or unexpected behavior.
Install
-
npm install rtlcss -
yarn add rtlcss -
pnpm add rtlcss
Imports
- rtlcss
import rtlcss from 'rtlcss'; // Or for CommonJS: const rtlcss = require('rtlcss'); - postcss
import { postcss } from 'postcss';import postcss from 'postcss'; // Or for CommonJS: const postcss = require('postcss'); - PostCSS plugin usage
rtlcss.process(cssString);
postcss([rtlcss(/* options */)]).process(cssString, { from: undefined });
Quickstart
import postcss from 'postcss';
import rtlcss from 'rtlcss';
const ltrCss = `
.container {
text-align: left;
margin-left: 10px;
padding: 5px 10px 15px 20px;
border-radius: 5px 10px 0 0;
}
/*rtl:ignore*/
.no-flip {
left: 0; /* Should not be flipped */
}
/*rtl:begin:ignore*/
.block-ignore {
right: 0;
left: 10px;
}
/*rtl:end:ignore*/
.flex-justify {
justify-content: flex-start;
}
`;
async function transformCss() {
try {
const result = await postcss([rtlcss({
autoRename: true, // Example option
processUrls: true // Example option
})]).process(ltrCss, { from: undefined });
console.log('LTR CSS:\n', ltrCss);
console.log('\nRTL CSS:\n', result.css);
} catch (error) {
console.error('Error transforming CSS:', error);
}
}
transformCss();