RTLCSS: LTR to RTL CSS Transformer

4.3.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `rtlcss` as a PostCSS plugin to transform an LTR CSS string into an RTL equivalent, including basic options and directives.

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();

view raw JSON →