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
Prev Previous commit
Next Next commit
style: Add direction ltr for email and url for rtl languages
  • Loading branch information
im3dabasia committed Jan 7, 2025
commit 24f0dac1b8cab50ab859816f7fb985ccec90f28f
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import type { SerializedStyles } from '@emotion/react';
import { css } from '@emotion/react';
import styled from '@emotion/styled';
import type { CSSProperties, ReactNode, HTMLInputTypeAttribute } from 'react';
import type { CSSProperties, ReactNode } from 'react';

/**
* Internal dependencies
@@ -141,7 +141,6 @@ type InputProps = {
dragCursor?: CSSProperties[ 'cursor' ];
paddingInlineStart?: CSSProperties[ 'paddingInlineStart' ];
paddingInlineEnd?: CSSProperties[ 'paddingInlineEnd' ];
type?: HTMLInputTypeAttribute;
};

const disabledStyles = ( { disabled }: InputProps ) => {
@@ -154,15 +153,6 @@ const disabledStyles = ( { disabled }: InputProps ) => {
} );
};

const directionStyles = ( { type }: InputProps ) => {
if ( type !== 'url' && type !== 'email' ) {
return '';
}
return css`
direction: ltr;
`;
};

export const fontSizeStyles = ( { inputSize: size }: InputProps ) => {
const sizes = {
default: '13px',
@@ -293,11 +283,16 @@ export const Input = styled.input< InputProps >`
${ fontSizeStyles }
${ sizeStyles }
${ customPaddings }
${ directionStyles }

&::-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.

}
`;