Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔀 fix: Resolve SplitText Direction From Its Own Text Under RTL (#16454)

Shuki Vaknin42568a0077bc
diff --git a/packages/client/src/components/SplitText.spec.tsx b/packages/client/src/components/SplitText.spec.tsx
index daf6419bc9..3e7671c7f1 100644
--- a/packages/client/src/components/SplitText.spec.tsx
+++ b/packages/client/src/components/SplitText.spec.tsx
@@ -22,4 +22,35 @@ describe('SplitText', () => {
expect(Array.from(textSpans)[i].textContent).toBe(emojis[i]);
}
});
+
+ it.each([
+ ['rtl', 'Welcome to reeva::chat', ['Welcome', 'to', 'reeva::chat']],
+ ['ltr', 'שלום עולם', ['שלום', 'עולם']],
+ ])('uses the accessible copy to resolve %s-document greeting %s', (dir, text, expectedWords) => {
+ const previousDir = document.documentElement.dir;
+ document.documentElement.dir = dir;
+ try {
+ const { container } = render(<SplitText text={text} />);
+ const paragraph = container.querySelector('p');
+ expect(paragraph).toHaveAttribute('dir', 'auto');
+
+ const accessibleCopy = paragraph?.querySelector('.sr-only');
+ expect(paragraph?.firstElementChild).toBe(accessibleCopy);
+ expect(accessibleCopy).toHaveTextContent(text);
+ expect(accessibleCopy?.closest('[aria-hidden="true"]')).toBeNull();
+
+ const wordBoxes = container.querySelectorAll('p > span[dir="auto"]');
+ expect(wordBoxes).toHaveLength(expectedWords.length);
+ for (const [index, box] of Array.from(wordBoxes).entries()) {
+ expect(box).toHaveAttribute('aria-hidden', 'true');
+ expect(
+ Array.from(box.querySelectorAll('span.inline-block'))
+ .map((span) => span.textContent)
+ .join(''),
+ ).toBe(expectedWords[index]);
+ }
+ } finally {
+ document.documentElement.dir = previousDir;
+ }
+ });
});
diff --git a/packages/client/src/components/SplitText.tsx b/packages/client/src/components/SplitText.tsx
index 46dd757365..dfbc410139 100644
--- a/packages/client/src/components/SplitText.tsx
+++ b/packages/client/src/components/SplitText.tsx
@@ -1,5 +1,5 @@
-import { useSprings, animated, SpringConfig } from '@react-spring/web';
import { useEffect, useRef, useState } from 'react';
+import { useSprings, animated, SpringConfig } from '@react-spring/web';
interface SegmenterOptions {
granularity?: 'grapheme' | 'word' | 'sentence';
@@ -135,15 +135,21 @@ const SplitText: React.FC<SplitTextProps> = ({
return (
<>
- <span className="sr-only">{text}</span>
<p
ref={ref}
+ dir="auto"
className={`split-parent inline overflow-hidden ${className}`}
style={{ textAlign, whiteSpace: 'normal', wordWrap: 'break-word' }}
- aria-hidden="true"
>
+ {/* The paragraph's auto direction ignores word boxes with their own dir. */}
+ <span className="sr-only">{text}</span>
{words.map((word, wordIndex) => (
- <span key={wordIndex} style={{ display: 'inline-block', whiteSpace: 'nowrap' }}>
+ <span
+ key={wordIndex}
+ dir="auto"
+ aria-hidden="true"
+ style={{ display: 'inline-block', whiteSpace: 'nowrap' }}
+ >
{word.map((letter, letterIndex) => {
const index =
words.slice(0, wordIndex).reduce((acc, w) => acc + w.length, 0) + letterIndex;