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

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

Open
t-hamano opened this issue Feb 9, 2024 · 7 comments
Labels
[Block] Media & Text Affects the Media & Text Block [Type] Enhancement A suggestion for improvement.

Comments

@t-hamano
Copy link
Contributor

t-hamano commented Feb 9, 2024

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.

21dd4cc3581bf3b74b966dd8078d2ed2.mp4

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

  • Detect changes in browser viewport width
  • Swap the order of media and content if the browser viewport width is less than the breakpoint of the Media & Text block and the "Show media on right" option is enabled.

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 t-hamano added [Type] Enhancement A suggestion for improvement. [Block] Media & Text Affects the Media & Text Block labels Feb 9, 2024
@t-hamano
Copy link
Contributor Author

t-hamano commented Feb 9, 2024

@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
Copy link
Member

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
Copy link
Contributor Author

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
Copy link
Contributor

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
Copy link
Contributor

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 #58881 (comment).

@t-hamano
Copy link
Contributor Author

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
Copy link
Contributor Author

It seems that there is a proposal to add a new CSS property, reading-flow: whatwg/html#10613

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Media & Text Affects the Media & Text Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants