RTLCSS
repository·master·Indexed 23 days ago
https://github.com/mohammadyounes/rtlcssA framework for transforming cascading style sheets (CSS) from left-to-right (LTR) to right-to-left (RTL). Version 4.3.0 features a CLI for processing files and directories, a process() method for CSS strings, and a configure() method for PostCSS plugin integration. It supports control and value directives to manipulate the transformation process and includes built-in processors to automate mirroring CSS properties for RTL languages.
What's inside rtlcss
- RTLCSS is a framework designed to convert Left-To-Right (LTR) Cascading Style Sheets (CSS) into Right-To-Left (RTL) versions. It automates the process of mirroring CSS properties to support RTL languages.
Explore RTLCSS via Documentation and Playground
masterFor in-depth learning and practical experimentation, use the following resources:
- Documentation: Detailed guides and learning materials can be found at https://rtlcss.com/learn/.
- Playground: Test your CSS transformations in real-time using the online playground at https://rtlcss.com/playground/.
Directives in RTLCSS
masterRTLCSS uses directives to control how the CSS is processed. Directives are categorized into
controldirectives (which affect the parsing and transformation flow) andvaluedirectives (which modify declaration values).Control Directives
Control directives allow you to manipulate the transformation process using specific syntax in your CSS:
ignore: Prevents certain parts of the CSS from being processed. It can be used as a block or a self-closing directive.rename: Renames selectors.raw: Allows inserting raw CSS nodes.remove: Removes specific at-rules, rules, or declarations.options: Allows passing a JSON object to change the RTLCSS configuration mid-file.config: Allows passing a configuration object (evaluated as a JS object) to change the RTLCSS configuration mid-file.
Value Directives
Value directives are used within declarations to modify how values are handled:
prepend: Prepends a value to the declaration.append: Appends a value to the declaration.insert: Inserts a value into the declaration based on a match.- (Unnamed): Replaces the declaration value with a new one.
Use the RTLCSS CLI
masterThertlcsscommand line interface allows you to convert CSS files to RTL (Right-to-Left) format. You can process a single file, a directory of files, or read fromstdin.Configure RTLCSS via CLI
masterYou can provide a custom configuration file using the-cor--configflag. This allows you to define specific RTL transformation rules and settings. If no config is provided,rtlcsswill attempt to load one from the current working directory or the input file's directory, otherwise it uses defaults.Integrate RTLCSS as a PostCSS plugin with configure()
masterIf you are using PostCSS, you can use
configure()to create a PostCSS plugin instance. This method accepts a single configuration object containingoptions,plugins, andhooks.const rtlcss = require('rtlcss'); const plugin = rtlcss.configure({ options: { /* ... */ }, plugins: [ /* ... */ ], hooks: { /* ... */ } }); // Use the returned plugin in your PostCSS pipeline // postcss.use(plugin)Use RTLCSS to process CSS strings with process()
masterThe
processmethod is the primary way to transform a CSS string from LTR to RTL. It accepts the input CSS, an options object, an array of plugins, and a hooks object. It returns the transformed CSS as a string.const rtlcss = require('rtlcss'); const css = '.foo { margin-left: 10px; }'; const options = { /* RTLCSS options */ }; const plugins = [ /* RTLCSS plugins */ ]; const hooks = { /* pre/post hooks */ }; const rtlCss = rtlcss.process(css, options, plugins, hooks); // rtlCss will be '.foo { margin-right: 10px; }'Configure RTLCSS via the configure() method
masterThe
configuremethod returns a PostCSS plugin instance based on the provided configuration object. The configuration object can contain:options: RTLCSS settings.plugins: An array of RTLCSS plugins.hooks: An object containingpreandposthooks.
This is useful when you want to integrate RTLCSS directly into a PostCSS workflow rather than using the standalone
processstring method.module.exports.configure = function (config = {}) { return postcss([this(config.options, config.plugins, config.hooks)]) }Reference: RTLCSS Processors
masterRTLCSS uses processors to identify specific CSS properties and apply RTL transformations to their values. The following processors are built into the core engine:RTLCSS CLI Options Reference
masterThe following options are available for the
rtlcsscommand:Option Description -h, --helpPrint help (this message) and exit. -v, --versionPrint version number and exit. -c, --config <path>Path to configuration settings file. - , --stdinRead from stdin stream. -d, --directoryProcess all *.cssfiles from input directory (recursive).-e, --ext <ext>Used with -doption to set the output files extension. Default: ".rtl.css".-s, --silentSilent mode, no warnings or errors are printed. Note on Output: If no destination is specified, output will be written to the same input folder as
{source}.rtl.{ext}.Option Description -------------- ---------------------------------------------- -h,--help Print help (this message) and exit. -v,--version Print version number and exit. -c,--config Path to configuration settings file. - ,--stdin Read from stdin stream. -d,--directory Process all *.css files from input directory (recursive). -e,--ext Used with -d option to set the output files extension. Default: ".rtl.css". -s,--silent Silent mode, no warnings or errors are printed.Reference: RTLCSS Control Directives
masterThe following control directives are available for managing the transformation lifecycle and configuration within CSS files.Reference: RTLCSS Value Directives
masterThe following value directives can be used to manipulate declaration values during processing.