WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.32k stars 4.12k forks source link

Media & Text block: Incorrect focus order in mobile viewport when "Show media on right" is enabled #58881

Open t-hamano opened 7 months ago

t-hamano commented 7 months ago

What problem does this address?

The Media & Text block has an option to set the media to the right. Depending on whether the media is on the left or right, the order in which media and content are rendered changes as follows:

<!-- Show media on left -->
<div class="wp-block-media-text is-stacked-on-mobile">
    <figure class="wp-block-media-text__media">
        <a href="#"><img /></a>
    </figure>
    <div class="wp-block-media-text__content">Contents</div>
</div>

<!-- Show media on right -->
<div class="wp-block-media-text has-media-on-the-right is-stacked-on-mobile">
    <div class="wp-block-media-text__content">Contents</div>
    <figure class="wp-block-media-text__media">
        <a href="#"><img /></a>
    </figure>
</div>

Focus ordering works well in the desktop viewport when the Media and Text in the content have links. However, when vertically stacked in the mobile layout, the order is simply visually reversed via the CSS grid, so the focus order is incorrect as shown below.

https://github.com/WordPress/gutenberg/assets/54422211/c3106183-618f-4e3e-9ea1-c77eb09cd4ba

What is your proposed solution?

I don't know if this is possible, but I'm imagining the following approach using JS (Possibly interactivity API).

If this can be achieved, I think it will also solve accessibility concerns that arise when an option to change the vertical stacking order is added in the mobile layout, as attempted in the PR below.

Additionally, while this block is very similar to the Column block, the advantage of the Media & Text block is that even if the media is to the right in the desktop layout, it can be displayed above the content in the mobile layout.

If the Columns block can be reverse stacked in the mobile layout with accessibility cleared, as attempted in #55763, this block might be deprecated as suggested in this discussion.

t-hamano commented 7 months ago

@luisherranz @SantosGuillamot I would appreciate any advice on whether it is possible to reorder elements within a block depending on the width of the screen's viewport using the current interactive API šŸ™‡

luisherranz commented 7 months ago

It'd be possible to do a focus trap, but can't this be achieved with just CSS/HTML? Things like flex-direction: column-reverse; or tabindex?

t-hamano commented 6 months ago

Sorry for get in touch with you so late. Indeed, CSS and a focus trap may be able to solve the problem. However, I think we need to match the visual order and the actual DOM order, as mentioned in this comment.

In other words, I imagine it would be possible to dynamically change the order of the sources as shown below....

Desktop:

<!-- Show media on right -->
<div class="wp-block-media-text has-media-on-the-right is-stacked-on-mobile">
    <div class="wp-block-media-text__content">Contents</div>
    <figure class="wp-block-media-text__media">
        <a href="#"><img /></a>
    </figure>
</div>

desktop

Mobile:

<!-- Show media on right -->
<div class="wp-block-media-text has-media-on-the-right is-stacked-on-mobile">
    <figure class="wp-block-media-text__media">
        <a href="#"><img /></a>
    </figure>
    <div class="wp-block-media-text__content">Contents</div>
</div>

mobile

SantosGuillamot commented 6 months ago

It seems that this is a common issue with CSS and they are working on solving it: link 1 & link 2.

In the meantime, if we want to solve it with JS, I would say we don't have a directive to change the order. First thing that comes to my mind is duplicating the wp-block-media-text__content, one before the image and another one after. If we wrap it with something like wp-show we could show one in mobile and other in desktop.

However, it doesn't seem ideal. So I am not sure it is the correct approach.

cc @DAreRodz @c4rl0sbr4v0 as they might have more context and ideas.

DAreRodz commented 6 months ago

I think we can use some logic that detects whether the elements are stacked, and then modify the tabindex attribute for those elements through data-wp-bind directives. That way, there's no need to duplicate/rearrange/create/remove elements. šŸ¤”

I guess it's what Luis proposed in https://github.com/WordPress/gutenberg/issues/58881#issuecomment-1936168655.

t-hamano commented 6 months ago

Thank you everyone for your advice. I checked the link mentioned in this comment. In the future, when all browsers support CSS like focus-order/reading-order, it might be an ideal approach to implement it with that CSS šŸ¤”

t-hamano commented 1 day ago

It seems that there is a proposal to add a new CSS property, reading-flow: https://github.com/whatwg/html/pull/10613

If this CSS is implemented in browsers, it may solve this issue.