vkalinichev / postcss-rtl

PostCSS plugin for RTL-adaptivity
MIT License
172 stars 33 forks source link
arabic css hebrew left-to-right postcss postcss-plugin postcss-rtl right-to-left rtl rtl-adaptivity rtlcss

PostCSS-RTL

npm Build Status npm Package Quality license

PostCSS-plugin for RTL-adaptivity

Generates RTL rules with flipped properties. Use one file for both directions!

Examples

Simple properties

In most cases all you need is flip property name or value from left to right or change values order in full-valued shorthand from top-right-bottom-left to top-left-bottom-right.

LTR input:

.foo {
    float: right;
    margin-left: 13px;
    text-align: right;
    font-size: 13px;
    border-color: lightgray;
    border-width: 2px 0 2px 2px;
    border-style: solid dashed solid solid
}

.foo {
    text-align: center;
}

LTR+RTL output:

.foo {
    font-size: 13px
}

[dir] .foo {
    border-color: lightgray
}

[dir="ltr"] .foo {
    float: right;
    margin-left: 13px;
    text-align: right;
    border-width: 2px 0 2px 2px;
    border-style: solid dashed solid solid
}

[dir="rtl"] .foo {
    float: left;
    margin-right: 13px;
    text-align: left;
    border-width: 2px 2px 2px 0;
    border-style: solid solid solid dashed
}

[dir] .foo {
    text-align: center
}

Animations

Flippable keyframes-animations will be splitted to two direction-based rules with -ltr or -rtl suffixes

LTR input:

.foo {
    animation: 1s slide 0s ease-in-out
}

@keyframes slide {
    from {
        transform: translate( -1000px )
    }
    to {
        transform: translate( 0 )
    }
}

LTR+RTL output:

[dir="ltr"] .foo {
    animation: 1s slide-ltr 0s ease-in-out
}

[dir="rtl"] .foo {
    animation: 1s slide-rtl 0s ease-in-out
}

@keyframes slide-ltr {
    from {
        transform: translate( -1000px )
    }
    to {
        transform: translate( 0 )
    }
}

@keyframes slide-rtl {
    from {
        transform: translate( 1000px )
    }
    to {
        transform: translate( 0 )
    }
}

Value directives

To transform declaration values use value directives:

Source

.foo {
    font-weight: bold;
    font-family: "Droid Sans", "Helvetica Neue", Arial, sans-serif/*rtl:prepend:"Droid Arabic Kufi",*/;
    transform: rotate(45deg)/* rtl:append: scaleX(-1) */;
    flex-direction: row/* rtl: row-reverse */;
}

Result

.foo {
    font-weight: bold;
}

[dir=ltr] .foo {
    font-family: "Droid Sans", "Helvetica Neue", Arial, sans-serif/*rtl:prepend:"Droid Arabic Kufi",*/;
    transform: rotate(45deg)/* rtl:append: scaleX(-1) */;
    flex-direction: row/* rtl: row-reverse */;
}

[dir=rtl] .foo {
    font-family: "Droid Arabic Kufi", "Droid Sans", "Helvetica Neue", Arial, sans-serif;
    transform: rotate(45deg) scaleX(-1);
    flex-direction: row-reverse;
}

Property directives

To transform declaration property name use property directives:

Source

:root {
    --padding /* rtl:as:padding */: 1rem 2rem 3rem 4rem;
}

Result

[dir=ltr]:root {
    --padding /* rtl:as:padding */: 1rem 2rem 3rem 4rem;
}

[dir=rtl]:root {
    --padding /* rtl:as:padding */: 1rem 4rem 3rem 2rem;
}

Ignoring specific declarations

To skip flipping specific declarations use some of supported directives:

Ignore one rule:

/* rtl:ignore */
.foo {
    padding-left: 0
}

Block-syntax to ignore rules within scope:

/* rtl:begin:ignore */
.foo {
    padding-left: 0
}
.bar {
    direction: ltr
}
/* rtl:end:ignore */

Value-syntax to ignore a single CSS declaration:

.foo {
    margin-left: 20px;
    padding-right: 20px /* rtl:ignore */;
}

/*! notation will work too:

/*! rtl:ignore */
.foo {
    padding-left: 0
}

Usage

  1. Plug it to PostCSS

    const postcss = require('postcss')
    const rtl = require('postcss-rtl')
    
    postcss([ rtl( options ) ])

    See PostCSS docs for examples for your environment.

  2. Manage direction by switching between dir="ltr" and dir="rtl" on <html> element.

With Webpack:

module.exports = {
  module: {
    rules: [ {
      test: /\.css$/,
      use: [
        { loader: 'style-loader' },
        { loader: 'css-loader' },
        { loader: 'postcss-loader',
          options: {
            plugins: function () {
              return [ require( 'postcss-rtl' )( options ) ]
            }
          }
        }
      ]
    } ]
  }
}

With Gulp:

gulp.src( 'style.css' )
    .pipe( postcss( [ rtl( options ) ]) )
    .pipe( gulp.dest( './dest' ) )

Options