swissup/module-rtl

Magento2 RTL detection

Installs: 293 831

Dependents: 3

Suggesters: 0

Security: 0

Stars: 4

Watchers: 8

Forks: 3

Open Issues: 0

Type:magento2-module

1.3.7 2024-11-11 07:19 UTC

This package is auto-updated.

Last update: 2024-11-11 07:20:20 UTC


README

Magento module that helps to add RTL support to your modules and themes PHP and LESS sources:

@import '_modrtl.less';

.sidebar-popup {
    .left(0);
    .modrtl(transform, translateX(-100%), translateX(100%));

    &.shown {
        transform: translateX(0);
    }

    .close {
        .right(0);
    }
}

Contents

  1. Installation
    1. Get the sources
    2. Add a dependency
    3. Create inject point for mixins
  2. Usage
    1. CSS class
    2. Less mixins
    3. PHP helper
  3. License
  4. Credits

Installation

Get the sources

composer require swissup/module-rtl
bin/magento setup:upgrade

Add a dependency

If you want to use RTL mixins in your module sources, you must add a requirement into your module or theme:

{
    "require": {
        "swissup/module-rtl": "^1.3.0"
    }
}

Create inject point for mixins

This step is required if you want to use RTL mixins in your source code.

  1. Create _modrtl.less inside your module or theme with the following content:

    // @modrtl
  2. Open your main less file, import created _modrtl.less file, and save it:

    @import '_modrtl.less';
    

Usage

CSS class

Module automatically adds rtl class name to the body element when current language is detected as RTL. This allows you to write plain RTL-specific styles in your css/less files:

.my-element {
    right: 20px;
}
.rtl .my-element {
    right: auto;
    left: 20px;
}

While this approach is nice for the small files, it becomes a headache when dealing with large portion of css that should be adjusted. Additionally, this approach make your style files larger.

We recommend to use mixins for the best experience.

Less mixins

Inject point is required to use mixins.

A set of useful mixins can be injected into your less styles! This approach allows to keep original size of generated css file and doesn't bloat your sources with separate styles for RTL languages:

@import '_modrtl.less';

.my-element {
    .right(20px); // Will generate "right: 20px;" for LTR, and "left: 20px;" for RTL.
}

Don't like mixins? Use @modrtl-dir variable in & when statements:

@import '_modrtl.less';

.my-element {
    right: 20px;
}
& when (@modrtl-dir = rtl) {
    .my-element {
        right: auto;
        left: 20px;
    }
}

Mixins list

PHP helper

Need a serverside RTL detection? Inject \Swissup\Rtl\Helper\Data into your class and use isRtl method:

// Check current locale
$helper->isRtl();

// Check custom locale
$helper->isRtl('en_US');

License

Distributed under the MIT license

Credits

Mixins are taken from anasnakawa/bi-app-sass. Thanks!