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

Wim Leers’s picture

Very interesting! :)

I think there are two problems in the IS though, and I think this issue only wants to solve the first:

  1. Automate RTL CSS generation, to avoid regressions.
  2. Front-end perf is worse because we always load RTL CSS.

I think solving the second is a nice-to-have — I think we load so incredibly (probably excessively 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?

Xano’s picture

I'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?

Wim Leers’s picture

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?

I 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?

LewisNyman’s picture

Issue tags: +TX (Themer Experience)

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?

hmm 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.

Xano’s picture

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.

Exactly. 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.

LewisNyman’s picture

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.

Or we just provide the ability to opt-out of the automatic generation on a per theme/library basis? Serve the 80% and all that.

YesCT’s picture

Issue tags: +D8MI
tsi’s picture

Hi @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:

/* Original CSS */
.left {
  font-weight: bold;
  left: 10px;
}

/* With CSSJanus */
.left {
  font-weight: bold; /* This is not needed in an overrides style sheet */
  right: 10px;  /* we didn't reset 'left' */
}

/* With css_flipper */
.left {
  right: 10px;
  left: auto; /* we must reset 'left' */
}

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.

LewisNyman’s picture

@tsi Thanks this looks really good :) Let's do this in 8.1.x

joelpittet’s picture

Status: Postponed » Active

Doesn't need to be postponed, 8.1.x branch is active now

poiu’s picture

I 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.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mherchel’s picture

Status: Active » Closed (outdated)

Closing this as outdated since we are now using CSS Logical Properties wherever possible.