styled-jsx icon indicating copy to clipboard operation
styled-jsx copied to clipboard

RTL support

Open Pro542 opened this issue 8 years ago • 5 comments

What would be the best way to go about RTL css? I was expecting something like below to work, and was trying to use postcss-rtl to get the required styles. (I believe it doesn't work since the selectors are not matched at compile time)

[dir] h1 {
    margin-left: 10px;
}
[dir=ltr] h1 {
    margin-left: 10px;
}
[dir=rtl] h1 {
    margin-right: 10px;
}

Otherwise, would using a toggle in JS to select the appropriate style the way to go? This would mean I won't be able to auto generate the styles and have to write RTL css manually.

Pro542 avatar Dec 12 '17 11:12 Pro542

What exactly doesn't work about your example above? Can you share your experimental code? Maybe make a Next.js app

giuseppeg avatar Dec 16 '17 12:12 giuseppeg

The above style can be written like this to make it work:

:global(html[dir=rtl]) h1 {
    margin-right: 10px;
}

I'm setting dir on html based on a URL key and deciding the styles to be applied from that. Will share a temp app soon

Pro542 avatar Dec 18 '17 05:12 Pro542

Sorry got a bit late on this, made a temp repo here. Seems to work alright but I'm not sure if prefetching is handled correctly, or how styled-jsx handles styles when prefetched. Please let me know your thoughts. Thanks!

Pro542 avatar Dec 26 '17 05:12 Pro542

Looks like a fine approach. With v3 you will be able to do this https://github.com/zeit/styled-jsx/issues/383#issuecomment-358287330 i.e. get the scoped className and styles which should help in case like this

giuseppeg avatar Feb 09 '18 08:02 giuseppeg

Another idea.

While RTL idea meant to change left to right and vice versa, I think you can have your own bi-variables based on the selected site language.

Example bi-variables.js

const siteLang = 'en'; // // You probably need this to come from a GLOBAL variable.
export const directions = {
  left: siteLang === 'en' ? 'left' : 'right'
};

styles.js

import css from 'styled-jsx/css';
import { directions } from './settings/bi-variables';
export const form = css`
  .form {
      padding-${directions.left}: 10px;
      float: ${directions.left};
  }
`;

So, the directions.left will be left if the siteLang = en and right if the siteLang = ar.

And, if you want to have it left on both English and Arabic site, then you don't have to use the bi-variables, and just use it normally like float: left; instead of float: ${directions.left};

OmarZeidan avatar Feb 25 '18 20:02 OmarZeidan