Coder Social home page Coder Social logo

rtl-css-js's Introduction

rtl-css-js

RTL conversion for CSS in JS objects

Build Status Code Coverage Dependencies version downloads MIT License

All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

Sponsor

The problem

For some locales, it's necessary to change padding-left to padding-right when your text direction is right to left. There are tools like this for CSS (cssjanus for example) which manipulate strings of CSS to do this, but none for CSS in JS where your CSS is represented by an object.

This solution

This is a function which accepts a CSS in JS object and can convert padding-left to padding-right as well as all other properties where it makes sense to do that (at least, that's what it's going to be when it's done... This is a work in progress).

Table of Contentss

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save rtl-css-js

Usage

This module is exposed via CommonJS as well as UMD with the global as rtlCSSJS

CommonJS:

const rtlCSSJS = require('rtl-css-js')
const styles = rtlCSSJS({paddingLeft: 23})
console.log(styles) // logs {paddingRight: 23}

You can also just include a script tag in your browser and use the rtlCSSJS variable:

<script src="https://unpkg.com/rtl-css-js"></script>
<script>
  const styles = rtlCSSJS({paddingRight: 23})
  console.log(styles) // logs {paddingLeft: 23}
</script>

You can also control which rules you don't want to flip by adding a /* @noflip */ CSS comment to your rule

const rtlCSSJS = require('rtl-css-js')
const styles = rtlCSSJS({paddingLeft: '20px /* @noflip */'})
console.log(styles) // logs {paddingLeft: '20px /* @noflip */' }

kebab-case

This library support kebab-case properties too.

const rtlCSSJS = require('rtl-css-js')
const styles = rtlCSSJS({'padding-right': 23})
console.log(styles) // logs {'padding-left': 23}

core

rtl-css-js also exposes its internal helpers and utilities so you can deal with certain scenarios yourself. To use these you can use the rtlCSSJSCore global with the UMD build, require('rtl-css-js/core'), or use import {propertyValueConverters, arrayToObject} from 'rtl-css-js/core'.

You can import anything that's exported from src/core. Please see the code comments for documentation on how to use these.

Caveats

background

Right now background and backgroundImage just replace all instances of ltr with rtl and right with left. This is so you can have a different image for your LTR and RTL, and in order to flip linear gradients. Note that this is case sensitive! Must be lower case. Note also that it will not change bright to bleft. It's a little smarter than that. But this is definitely something to consider with your URLs.

Inspiration

CSSJanus was a major inspiration.

Ecosystem

Other Solutions

I'm not aware of any, if you are please make a pull request and add it here!

Contributors

Thanks goes to these people (emoji key):

Kent C. Dodds
Kent C. Dodds

๐Ÿ’ป โš ๏ธ ๐Ÿš‡
Ahmed El Gabri
Ahmed El Gabri

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Maja Wichrowska
Maja Wichrowska

๐Ÿ’ป โš ๏ธ
Yaniv
Yaniv

๐Ÿ’ป โš ๏ธ
Jonathan Pollak
Jonathan Pollak

๐Ÿ’ป โš ๏ธ
Ali Taheri Moghaddar
Ali Taheri Moghaddar

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
garrettberg
garrettberg

๐Ÿ’ป โš ๏ธ
Miles Johnson
Miles Johnson

๐Ÿ’ป โš ๏ธ
Kevin Weber
Kevin Weber

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

rtl-css-js's People

Contributors

txhawks avatar alitaheri avatar majapw avatar yzimet avatar andarist avatar milesj avatar ahmedelgabri avatar christiango avatar garrettberg avatar jdorfman avatar kevinweber avatar palashmon avatar lastw avatar allcontributors[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.