Skip to content

Commit 7ecca28

Browse files
fix(rtl): use :dir() for rtl styles when the browser supports it (#31744)
Issue number: internal --------- ## What is the current behavior? The `rtl()` mixin outputs a `:host-context([dir=rtl])` selector for Chromium alongside a `:dir(rtl)` selector for Firefox and Safari. `:host-context([dir=rtl])` matches any ancestor with `dir="rtl"`, even when a closer ancestor sets `dir="ltr"`. In Chromium, an LTR section inside an RTL page gets the RTL styles from every shadow component that uses the RTL mixins. This is visible in the popover position test: in "LTR, Start" in `popover-position-ios-rtl-Mobile-Chrome-linux.png`, the arrow points away from its trigger. Firefox and Safari already render it correctly because they only use `:dir()`. ## What is the new behavior? - The `:host-context()` selectors are now only output inside `@supports not selector(:dir(rtl))`, so browsers that support `:dir()` use it exclusively. `:dir()` follows the nearest ancestor with a `dir` attribute. - Chrome 120+ now matches Firefox and Safari. Chrome 89 - 119 keep the `:host-context()` fallback, so their behavior does not change. - Updated the Chrome ground truth for `popover/test/position` (ios, rtl), where the "LTR, Start" arrow now points at its trigger. ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information - Only the Chromium output changes. The compiled CSS for 34 component stylesheets changes by wrapping the existing `:host-context()` rules in `@supports not selector(:dir(rtl))`, and nothing else. Global stylesheets are unchanged. - I ran every component whose CSS changed in Docker. The only screenshot that changed is the popover one above. - This fixes the arrow part of FW-7850. The top and bottom `alignment` issue on that ticket is calculated in JavaScript in `popover/utils.ts` and is not part of this PR. Co-authored-by: Brandy Smith <6577830+brandyscarney@users.noreply.github.com>
1 parent 17326e1 commit 7ecca28

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

50 Bytes
Loading

‎core/src/themes/ionic.mixins.scss‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,15 @@ $rtl-use-host-context: true !default;
258258
}
259259
}
260260

261-
// Supported by Chrome.
261+
// Fallback for Chrome 89 - 119, which support `:host-context()` but not
262+
// `:dir()`. `:host-context([dir=rtl])` matches any ancestor with
263+
// `dir="rtl"`, even when a closer ancestor sets `dir="ltr"`, so it is
264+
// only used when `:dir()` is not available.
262265
@if $rtl-use-host-context and length($hostContextSelectors) > 0 {
263-
@at-root #{$hostContextSelectors} {
264-
@content;
266+
@supports not selector(:dir(rtl)) {
267+
@at-root #{$hostContextSelectors} {
268+
@content;
269+
}
265270
}
266271
}
267272

@@ -272,21 +277,14 @@ $rtl-use-host-context: true !default;
272277
}
273278
}
274279

275-
// If browser can support `:dir()`, then add the `:dir()` selectors.
280+
// `:dir()` follows the direction of the nearest ancestor with a `dir`
281+
// attribute, so it is used whenever the browser supports it. This covers
282+
// Chrome 120+, Firefox and Safari 16.4+.
283+
// @link https://webkit.org/blog/13966/webkit-features-in-safari-16-4/
284+
// In Safari 16.4 - 16.x, `:dir()` does not update when the direction
285+
// changes after the page loads. Safari 17+ is not affected.
286+
// @link https://bugs.webkit.org/show_bug.cgi?id=257133
276287
@supports selector(:dir(rtl)) {
277-
// Adding :dir() in case the browser doesn't support `:host-context()` and does support `:dir()`.
278-
// `:host-context()` is added:
279-
// - through the `add-root-selector()` function.
280-
// - first so that it takes precedence over `:dir()`.
281-
// For example,
282-
// - Firefox doesn't support `:host-context()`, but does support `:dir()`.
283-
// - Safari doesn't support `:host-context()`, but Safari 16.4+ supports `:dir()`
284-
// @link https://webkit.org/blog/13966/webkit-features-in-safari-16-4/
285-
// -- However, there is a Webkit bug on v16 that prevents `:dir()` from working when
286-
// -- the app direction is changed dynamically. v17+ works fine.
287-
// -- @link https://bugs.webkit.org/show_bug.cgi?id=257133
288-
289-
// Supported by Firefox.
290288
@at-root #{add-root-selector($root, ":dir(rtl)", false)} {
291289
@content;
292290
}

0 commit comments

Comments
 (0)