From #2313783: Contribute a csslint rule to detect LTR properties:
In the Github issue someone recommended CSSJaunus PHP. Maybe this is something we could aim for in 8.1.x? We could include it using composer.
It looks like this was mentioned before but never discussed: https://groups.drupal.org/node/194488
This seems like a big maintenance improvement over handling this manually, it also means we could we could improve frontend performance without hurting DX. Right now we load RTL CSS all the time which increases the weight of the pages and the number of unused selectors that browsers have to parse.
Comments
Comment #1
Wim LeersVery interesting! :)
I think there are two problems in the IS though, and I think this issue only wants to solve the first:
I think solving the second is a nice-to-have — I think we load so incredibly (probably
is appropriate here) much CSS that this won't matter much. It can be solved though, by creating separate CSS aggregates for the different language directions, and having metadata for RTL-only assets that allows Drupal to know which assets not to load for RTL.Now, continuing the automation part.
I'd imagine we'd use
hook_library_info_alter()
to detect which CSS files live in the system, apply CSSJanus to all of them, and add a single "RTL CSS" CSS file to each asset library that has some RTL CSS.I think the only tricky bit is then dealing with modules that are being developed, i.e. whose CSS is actively being changed, and whose automatically generated RTL CSS hence needs to be regenerated on every page load. There's an easy answer: clear the libraries cache for every page load. The question is: is that good enough?
Comment #2
XanoI'm not bothered by any expertise in this field (this one's for you, Wim), so this might sound stupid, but what about developers for whom RTL is the primary case? Will there be a way for them to use this feature to generate LTR stylesheets?
Comment #3
Wim LeersI think this is a great question and I was wondering this too while I wrote the above.
AFAIK Drupal basically used to say: Drupal defaults to LTR, so you should too, and write RTL as a separate thing.
But then we got rid of
*-rtl.css
files in favor of[dir=rtl] .some-selector {}
in the same CSS files, so RTL as the primary use case implicitly became supported… but you'd still have to write[dir=rtl] …
(unless we advised those people to write RTL by default and then use[dir=ltr] …
for LTR instead? I don't know.).In this system, we'd have to have metadata about which CSS files are LTR, which are RTL and which are agnostic, so that we can generate the missing part automatically.
lewisnyman, can you enlighten us on the current policy/system for that?
Comment #4
LewisNymanhmm that's a good question. Yes I guess the current standards are just a recommendation. We've removed any logic that Drupal has over RTL and left the control in the hands of the developer.
Currently, Drupal has no opinion of the CSS that you write. This change would be a big different, as we would have more logic than we've had before.
I think the only tricky bit is then dealing with modules that are being developed, i.e. whose CSS is actively being changed, and whose automatically generated RTL CSS hence needs to be regenerated on every page load. There's an easy answer: clear the libraries cache for every page load. The question is: is that good enough?
That sounds acceptable for me, it's the same way we deal with Twig now.
Comment #5
XanoExactly. We write LTR CSS by default and manually create RTL overrides, but we do not prevent anyone from doing it the other way around. An automated tool would have to provide similar flexibility in order to prevent regressions, both technically and 'mentally', as not doing so would be a stab in the back of people from RTL cultures.
Also, +1 for the TX tag.
Comment #6
LewisNymanOr we just provide the ability to opt-out of the automatic generation on a per theme/library basis? Serve the 80% and all that.
Comment #7
YesCT CreditAttribution: YesCT commentedComment #8
tsi CreditAttribution: tsi commentedHi @LewisNyman and thanks for the heads-up in #1166886
I invite you to check out my https://www.drupal.org/project/css_flipper - it does exactly that - automate the creation of an RTL style sheet out of an LTR one (and vice-versa!) - it is better (for Drupal) than CSSJanus since CSSJanus will create a replacement style-sheet (e.g. flip all values but not reset existing values) while my css_flipper will create an overrides style sheet which is what drupal actually needs. see this example for instance:
Getting something like this into core will be truly amazing, but making something like this which is core-worthy will not be easy, my project will certainly need some work. mainly in the ease-of-use area.
Comment #9
LewisNyman@tsi Thanks this looks really good :) Let's do this in 8.1.x
Comment #10
joelpittetDoesn't need to be postponed, 8.1.x branch is active now
Comment #11
poiu CreditAttribution: poiu commentedI think replacement stylesheets are the only robust solution, since overriding and resetting CSS properties can cause the loss of inherited values.
Imagine a "* { left: 5px; right: 5px; }" rule at the top of tsi's example in #8 to see what I mean.
I keep a D7 module and a patched cssjanus at https://github.com/yonatan/rtl-toolkit if anyone's interested. I've been using it in production for about 3 years now, it works pretty well. Haven't looked into a D8 port yet.
Comment #26
mherchelClosing this as outdated since we are now using CSS Logical Properties wherever possible.