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

* 🔀 fix: Resolve SplitText Direction From Its Own Text Under RTL

SplitText wraps every grapheme and every word in its own inline-block box, so
the bidi algorithm never sees the text as one run. Under an RTL document the
boxes lay out right to left, and a Latin greeting or agent name on the landing
page renders mirrored, letter by letter and word by word.

The paragraph and each word box now carry dir="auto", so direction resolves
from the first strong character of the text itself: the paragraph decides word
order and each word decides its own letter order. RTL text still lays out
right to left, and mixed greetings render each part correctly.

Claude-Session: https://claude.ai/code/session_01EhJ8HUVBZ53ArUzgXDnxom

* 🐛 fix: Preserve RTL Greeting Word Order

---------

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
Shuki Vaknin 2026-09-29 03:59:28 +03:00 • committed by GitHub
parent 7e786baa22
commit 42568a0077
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 4 deletions

View file

@ -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;
}
});
});

View file

@ -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;