Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToggleGroupControl: Fix arrow key navigation in RTL #65735

Merged
merged 2 commits into from
Sep 30, 2024

Conversation

mirka
Copy link
Member

@mirka mirka commented Sep 30, 2024

Reported in #64963 (comment)

What?

Fixes arrow key navigation when ToggleGroupControl is in RTL mode.

Testing Instructions for Keyboard

See default Storybook story for ToggleGroupControl and toggle it to RTL mode in the toolbar. Test that arrow key navigation works as expected.

CleanShot.2024-09-30.at.19.03.16.mp4

@mirka mirka added [Type] Bug An existing feature does not function as intended [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). Internationalization (i18n) Issues or PRs related to internationalization efforts [Package] Components /packages/components labels Sep 30, 2024
@mirka mirka self-assigned this Sep 30, 2024
@mirka mirka requested a review from ajitbohra as a code owner September 30, 2024 10:04
Copy link

github-actions bot commented Sep 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Tests well with both left-right and up-down arrows 👍

@mirka mirka added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 30, 2024
@mirka mirka merged commit b7bf133 into trunk Sep 30, 2024
69 checks passed
@mirka mirka deleted the toggle-group-rtl-keyboard branch September 30, 2024 13:52
@github-actions github-actions bot added this to the Gutenberg 19.4 milestone Sep 30, 2024
@github-actions github-actions bot removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 30, 2024
gutenbergplugin pushed a commit that referenced this pull request Sep 30, 2024
* ToggleGroupControl: Fix arrow key navigation in RTL

* Add changelog

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
@github-actions github-actions bot added the Backported to WP Core Pull request that has been successfully merged into WP Core label Sep 30, 2024
Copy link

I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: aa59d85

@t-hamano
Copy link
Contributor

t-hamano commented Oct 1, 2024

Thanks for the fix! I would like to update #64963 and clarify the remaining tasks.

@DaniGuardiola
Copy link
Contributor

DaniGuardiola commented Oct 1, 2024

Looking at the implementation of isRTL, this seems highly WP-specific. That might be what we want, but I wonder if doing a more straightforward DOM check would work while being more context-agnostic (and lightweight, isRTL seems a bit chunky)?

For example, the dom package also exports an isRTL function implemented like this:

image

@ciampo
Copy link
Contributor

ciampo commented Oct 1, 2024

It would be good to unify the way we check for RTL. I don't know if there are any hidden implications (ie., could a user ever end up where the WP language is a RTL language BUT the document's direction is not rtl ), but:

  • if there are such differences, we should find out why and if they are intended. And we could probably rename the functions to better showcase what they effectively do — ie. isRtlDirection vs isRtlLanguage ?
  • if there's no difference, we could decide what's the best version and deprecate the other?

Doesn't seem high priority, though.

@mirka
Copy link
Member Author

mirka commented Oct 1, 2024

I don't currently see an issue with isRTL(). We're tied to @wordpress/i18n for string translations anyway, and a DOM check would be less ergonomic given that devs would need to pick out and supply a DOM element for every invocation.

@jsnajdr
Copy link
Member

jsnajdr commented Oct 1, 2024

For some reason the MDN page for the direction CSS attribute is discouraging people from using it at all:

Warning: Where possible, authors are encouraged to avoid using the direction CSS property and use the HTML dir global attribute instead.

According to that the right way to do WP-agnostic DOM-only RTL check is:

element.ownerDocument.documentElement.dir === 'rtl'

getdave pushed a commit that referenced this pull request Oct 1, 2024
* ToggleGroupControl: Fix arrow key navigation in RTL

* Add changelog

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
@DaniGuardiola
Copy link
Contributor

DaniGuardiola commented Oct 1, 2024

According to that the right way to do WP-agnostic DOM-only RTL check is:

element.ownerDocument.documentElement.dir === 'rtl'

Whether you use the CSS property or the HTML attribute, the computed result is the same. I made a demo to illustrate this: https://stackblitz.com/edit/stackblitz-starters-mc6hcb?file=script.js

I don't currently see an issue with isRTL(). We're tied to @wordpress/i18n for string translations anyway

That's true, though I still think it's worth removing extra dependencies when possible. Keeping a minimal dep footprint gives us more flexibility and, in this case, might allow us to tree-shake that part of the library away.

and a DOM check would be less ergonomic given that devs would need to pick out and supply a DOM element for every invocation.

That's not necessarily a problem if we're okay with checking document.body by default. E.g. this would work fine:

export default function isRTL( element = document.body ) {
	return getComputedStyle( element ).direction === 'rtl';
}
isRTL();

While still allowing an element in, which is useful for us to do something like:

isRTL(containerRef);

Which is more specific, and in the rare case that the direction is different in the subtree that component is in, it will correctly pick its value up. So, in a way, this would potentially improve detection and prevent some edge cases.

That is, in the case that the current isRTL function isn't doing something we don't know (like the example @ciampo gave: "could a user ever end up where the WP language is a RTL language BUT the document's direction is not rtl").

Another point in favor of doing this is that we might want to use :dir(rtl) in CSS (here's a current example), which is very useful, and this approach in JS land would bring it closer to that, preventing potential future inconsistencies.

huubl pushed a commit to huubl/gutenberg that referenced this pull request Oct 2, 2024
* ToggleGroupControl: Fix arrow key navigation in RTL

* Add changelog

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
huubl added a commit to huubl/gutenberg that referenced this pull request Oct 2, 2024
draganescu pushed a commit that referenced this pull request Oct 2, 2024
* ToggleGroupControl: Fix arrow key navigation in RTL

* Add changelog

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). Internationalization (i18n) Issues or PRs related to internationalization efforts [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants