Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(CodeEditor): code and its line numbers stay left-to-right in a right-to-left page (#9363)

Cindy Zhangf7a8f7085c19
diff --git a/packages/lab/src/CodeEditor/CodeEditor.test.tsx b/packages/lab/src/CodeEditor/CodeEditor.test.tsx
index 90845669d3..70a67b9075 100644
--- a/packages/lab/src/CodeEditor/CodeEditor.test.tsx
+++ b/packages/lab/src/CodeEditor/CodeEditor.test.tsx
@@ -51,6 +51,33 @@ function renderEditor(value = '', props: {escapeHint?: string} = {}) {
}
describe('CodeEditor', () => {
+ it('keeps the code left-to-right inside a right-to-left page', () => {
+ render(
+ <div dir="rtl">
+ <CodeEditor label="Code" value="const a = 1;" onChange={() => {}} />
+ </div>,
+ );
+ const editor = screen.getByRole('textbox', {name: 'Code'});
+ expect(editor.closest('[dir]')).toHaveAttribute('dir', 'ltr');
+ });
+
+ it('lets the placeholder take its own text direction', () => {
+ render(
+ <div dir="rtl">
+ <CodeEditor
+ label="Code"
+ value=""
+ onChange={() => {}}
+ placeholder="اكتب الكود هنا..."
+ />
+ </div>,
+ );
+ expect(screen.getByText('اكتب الكود هنا...')).toHaveAttribute(
+ 'dir',
+ 'auto',
+ );
+ });
+
it('uses the label prop as the accessible name', () => {
render(
<CodeEditor
diff --git a/packages/lab/src/CodeEditor/CodeEditor.tsx b/packages/lab/src/CodeEditor/CodeEditor.tsx
index 02d1f8aebb..4ca4fe5a9a 100644
--- a/packages/lab/src/CodeEditor/CodeEditor.tsx
+++ b/packages/lab/src/CodeEditor/CodeEditor.tsx
@@ -556,7 +556,13 @@ export function CodeEditor({
style,
)}
{...props}>
- <div {...stylex.props(styles.editorContainer)} style={containerStyle}>
+ {/* Code, with its line numbers, keeps its own left-to-right direction
+ inside a right-to-left page (design:bidirectionality DR2): lines read
+ and indent correctly, and a long line scrolls from its start. */}
+ <div
+ dir="ltr"
+ {...stylex.props(styles.editorContainer)}
+ style={containerStyle}>
{hasLineNumbers && (
<div
{...stylex.props(styles.gutter, gutterSizeStyle)}
@@ -570,7 +576,9 @@ export function CodeEditor({
)}
<div style={{position: 'relative', flex: 1}}>
{showPlaceholder && (
- <div {...stylex.props(styles.placeholder, sizeStyle)}>
+ // The placeholder is prose, not code: it takes its own text's
+ // direction, so an Arabic or Hebrew hint still reads correctly.
+ <div dir="auto" {...stylex.props(styles.placeholder, sizeStyle)}>
{placeholder}
</div>
)}