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

Fix text direction for URL and email fields in block editor for RTL languages #68188

Merged
merged 12 commits into from
Jan 8, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const InsertFromURLPopover = ( {
<InputControl
__next40pxDefaultSize
label={ __( 'URL' ) }
type="url"
hideLabelFromVision
placeholder={ __( 'Paste or type URL' ) }
onChange={ onChange }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/rss/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function RSSEdit( { attributes, setAttributes } ) {
<InputControl
__next40pxDefaultSize
label={ label }
type="url"
hideLabelFromVision
placeholder={ __( 'Enter URL here…' ) }
value={ feedURL }
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `Components`: Standardize reduced motion handling using media queries ([#68421](https://github.com/WordPress/gutenberg/pull/68421)).

### Bug Fixes

- `InputControl`: Ensures email and url inputs have consistent LTR alignment in RTL languages ([#68188](https://github.com/WordPress/gutenberg/pull/68188)).

## 29.1.0 (2025-01-02)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ export const Input = styled.input< InputProps >`
&::-webkit-input-placeholder {
line-height: normal;
}
&[type='email'],
&[type='url'] {
/* rtl:ignore */
direction: ltr;
}
Comment on lines +291 to +295
Copy link
Contributor

Choose a reason for hiding this comment

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

If you reference the props directly, you should be able to output CSS conditionally. A possible approach would be something like this:

Details
diff --git a/packages/components/src/input-control/styles/input-control-styles.tsx b/packages/components/src/input-control/styles/input-control-styles.tsx
index 39eea8fdb0..ece01451db 100644
--- a/packages/components/src/input-control/styles/input-control-styles.tsx
+++ b/packages/components/src/input-control/styles/input-control-styles.tsx
@@ -4,7 +4,7 @@
 import type { SerializedStyles } from '@emotion/react';
 import { css } from '@emotion/react';
 import styled from '@emotion/styled';
-import type { CSSProperties, ReactNode } from 'react';
+import type { CSSProperties, ReactNode, HTMLInputTypeAttribute } from 'react';
 
 /**
  * Internal dependencies
@@ -141,6 +141,7 @@ type InputProps = {
        dragCursor?: CSSProperties[ 'cursor' ];
        paddingInlineStart?: CSSProperties[ 'paddingInlineStart' ];
        paddingInlineEnd?: CSSProperties[ 'paddingInlineEnd' ];
+       type?: HTMLInputTypeAttribute;
 };
 
 const disabledStyles = ( { disabled }: InputProps ) => {
@@ -262,6 +263,15 @@ const dragStyles = ( { isDragging, dragCursor }: InputProps ) => {
        `;
 };
 
+const directionStyles = ( { type }: InputProps ) => {
+       if ( type !== 'url' && type !== 'email' ) {
+               return '';
+       }
+       return css`
+               direction: ltr;
+       `;
+};
+
 // TODO: Resolve need to use &&& to increase specificity
 // https://github.com/WordPress/gutenberg/issues/18483
 
@@ -283,6 +293,7 @@ export const Input = styled.input< InputProps >`
                ${ fontSizeStyles }
                ${ sizeStyles }
                ${ customPaddings }
+               ${ directionStyles }
 
                &::-webkit-input-placeholder {
                        line-height: normal;

Copy link
Member

Choose a reason for hiding this comment

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

Any reason why we would prefer this over plain CSS selectors? I think in general we should use plain CSS instead of JS when possible, since it will be simpler both in terms of lines of code and runtime work. Another reason is that we are actually planning to move off of CSS-in-JS (#66806).

Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why we would prefer this over plain CSS selectors?

I did it for consistency with existing approaches (sizeStyles, customPaddings, etc.). But if we are planning to move off of CSS-in-JS, I think the plain CSS is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @t-hamano , I have reverted back to using CSS. Please review my work once again.

}
`;

Expand Down
Loading