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

Grid: In RTL languages, the resize handles point in the opposite direction #64995

Merged
merged 5 commits into from
Sep 13, 2024

Conversation

rohitmathur-7
Copy link
Contributor

@rohitmathur-7 rohitmathur-7 commented Sep 3, 2024

What?

Why?

  • To fix horizontal resizing for RTL languages.

How?

  • Fix the direction for RTL languages.

Testing Instructions

  • Insert a Grid block.
  • Switch to a RTL language.
  • Resize the inner block horizontally.

Copy link

github-actions bot commented Sep 3, 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: rohitmathur-7 <rohitmathur7@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

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

@t-hamano t-hamano self-requested a review September 3, 2024 13:16
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the review!

This PR works fine horizontally, but seems to cause unintended issues vertically. The resizable box stretches from the center, not the top or bottom:

28794d3c28e6aeea417de5b4f2605c3d.mp4

I think the reason is that none of the conditions here are met when the direction is top or bottom. I think it will work if we make the following changes:

diff --git a/packages/block-editor/src/components/grid/grid-item-resizer.js b/packages/block-editor/src/components/grid/grid-item-resizer.js
index 0b63580149..7bd5fae530 100644
--- a/packages/block-editor/src/components/grid/grid-item-resizer.js
+++ b/packages/block-editor/src/components/grid/grid-item-resizer.js
@@ -137,6 +137,8 @@ function GridItemResizerInner( {
                                                        setResizeDirection( 'right' );
                                                } else if ( 'right' === direction ) {
                                                        setResizeDirection( 'left' );
+                                               } else {
+                                                       setResizeDirection( direction );
                                                }
                                        } else {
                                                /*

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Layout Layout block support, its UI controls, and style output. l10n Localization and translations best practices labels Sep 3, 2024
@rohitmathur-7
Copy link
Contributor Author

Thanks for the review!

This PR works fine horizontally, but seems to cause unintended issues vertically. The resizable box stretches from the center, not the top or bottom:

28794d3c28e6aeea417de5b4f2605c3d.mp4
I think the reason is that none of the conditions here are met when the direction is top or bottom. I think it will work if we make the following changes:

diff --git a/packages/block-editor/src/components/grid/grid-item-resizer.js b/packages/block-editor/src/components/grid/grid-item-resizer.js
index 0b63580149..7bd5fae530 100644
--- a/packages/block-editor/src/components/grid/grid-item-resizer.js
+++ b/packages/block-editor/src/components/grid/grid-item-resizer.js
@@ -137,6 +137,8 @@ function GridItemResizerInner( {
                                                        setResizeDirection( 'right' );
                                                } else if ( 'right' === direction ) {
                                                        setResizeDirection( 'left' );
+                                               } else {
+                                                       setResizeDirection( direction );
                                                }
                                        } else {
                                                /*

Hii @t-hamano ,
Yeah sorry I missed adding the else condition. 🙇🏻‍♂️
Have added the else condition.
Can you please check now.
Thanks.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM! Just to be sure, I would appreciate additional feedback from @noisysocks and @tellthemachines.

@t-hamano t-hamano mentioned this pull request Sep 10, 2024
37 tasks
@andrewserong
Copy link
Contributor

Just to be sure, I would appreciate additional feedback

I haven't tested the change, but conceptually this PR looks like it's in the right direction to me! The resizeDirection then sets the justifyContent here, so handling rtl in this way seems correct to me:

...( justification[ resizeDirection ] && {
justifyContent: justification[ resizeDirection ],
} ),
...( alignment[ resizeDirection ] && {

@t-hamano
Copy link
Contributor

@andrewserong Thanks for the feedback!

The resizeDirection then sets the justifyContent here, so handling rtl in this way seems correct to me

I have considered this approach as well. In the trunk branch, we can make the following changes to solve the problem as well:

diff --git a/packages/block-editor/src/components/grid/grid-item-resizer.js b/packages/block-editor/src/components/grid/grid-item-resizer.js
index da3eb824fe..ffa93d8c01 100644
--- a/packages/block-editor/src/components/grid/grid-item-resizer.js
+++ b/packages/block-editor/src/components/grid/grid-item-resizer.js
@@ -3,6 +3,7 @@
  */
 import { ResizableBox } from '@wordpress/components';
 import { useState, useEffect } from '@wordpress/element';
+import { isRTL } from '@wordpress/i18n';
 
 /**
  * Internal dependencies
@@ -73,8 +74,8 @@ function GridItemResizerInner( {
        }, [ blockElement, rootBlockElement ] );
 
        const justification = {
-               right: 'flex-start',
-               left: 'flex-end',
+               right: isRTL() ? 'flex-end' : 'flex-start',
+               left: isRTL() ? 'flex-start' : 'flex-end',
        };
 
        const alignment = {

However, flex-{start|end} in a flex layout should automatically take into account the logical direction of the document. So, I feel we need to keep this as flex-start, regardless of whether it's an RTL language or not.

I suspect the root cause is that the drag handle of the ResizableBox component is based on the physical direction rather than the logical direction (the source that fixes the drag handle to the physical direction).

Therefore, I feel that the direction detected by the ResistableBox component needs to be inverted to match the logical direction of the document.

@andrewserong
Copy link
Contributor

I suspect the root cause is that the drag handle of the ResizableBox component is based on the physical direction rather than the logical direction (the source that fixes the drag handle to the physical direction).
Therefore, I feel that the direction detected by the ResistableBox component needs to be inverted to match the logical direction of the document.

Good point, that makes sense to me now. Since the onResizeStart gets a direction with left and right values, do you think this PR would be an okay fix for now, or would you prefer to handle it in ResizableBox?

@t-hamano
Copy link
Contributor

Since the ResizableBox component itself is designed to change the visual size of an element, I don't think it's a good idea to change the behavior of the onResizeStart prop itself.

I considered a different approach, but found that the problem could be solved by making the following changes in trunk:

diff --git a/packages/block-editor/src/components/grid/grid-item-resizer.js b/packages/block-editor/src/components/grid/grid-item-resizer.js
index da3eb824fe..1277ac2a23 100644
--- a/packages/block-editor/src/components/grid/grid-item-resizer.js
+++ b/packages/block-editor/src/components/grid/grid-item-resizer.js
@@ -73,8 +73,8 @@ function GridItemResizerInner( {
        }, [ blockElement, rootBlockElement ] );
 
        const justification = {
-               right: 'flex-start',
-               left: 'flex-end',
+               right: 'left',
+               left: 'right',
        };
 
        const alignment = {

This means that the GridItemResizerInner component will consistently use logic based on physical direction instead of logical direction, as shown below:

  • The active side is determined here based on the physical position.
  • When we operate the resize handle, which handle was operated is determined via the onResizeStart prop based on the physical placement.
  • The justification variable determines the direction in which the element is physically placed.

This seems like a simpler approach than having to add logic to consider whether the language is RTL or not. What do you think?

@andrewserong
Copy link
Contributor

andrewserong commented Sep 12, 2024

This seems like a simpler approach than having to add logic to consider whether the language is RTL or not. What do you think?

Oh, I really like that idea. This makes sense, too, because we're dealing with a component that's about moving something visually, which is conceptually different to RTL language support 👍

@t-hamano
Copy link
Contributor

@andrewserong Thanks for the feedback!

@rohitmathur-7 Making the changes suggested in this comment seems the best option for now, could you update this PR?

@rohitmathur-7
Copy link
Contributor Author

@andrewserong Thanks for the feedback!

@rohitmathur-7 Making the changes suggested in this comment seems the best option for now, could you update this PR?

Yes sure @t-hamano ,
I will update the PR soon!
Thanks for the feedback everyone. 🙇

@rohitmathur-7
Copy link
Contributor Author

Hii @t-hamano ,
I have updated the PR to add changes of this comment.
Can you please check now.
Thanks. 🙇

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM! Thank for the update 👍

@t-hamano t-hamano merged commit 1664022 into WordPress:trunk Sep 13, 2024
61 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.3 milestone Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Layout Layout block support, its UI controls, and style output. l10n Localization and translations best practices [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grid: In RTL languages, the resize handles point in the opposite direction
3 participants