diff --git a/packages/client/src/components/SplitText.spec.tsx b/packages/client/src/components/SplitText.spec.tsx index 795daa5883..c4bce9abe2 100644 --- a/packages/client/src/components/SplitText.spec.tsx +++ b/packages/client/src/components/SplitText.spec.tsx @@ -12,6 +12,39 @@ beforeEach(() => { }); describe('SplitText', () => { + it.each(['سلام', 'می‌توانید', 'مرحبا', 'שלום'])( + 'keeps the RTL word %s together so the browser can shape it', + (word) => { + const { container } = render(); + const animatedSegments = container.querySelectorAll('p > span > span.inline-block'); + + expect(animatedSegments).toHaveLength(1); + expect(animatedSegments[0]).toHaveTextContent(word); + expect(container.querySelector('p')).toHaveAttribute('dir', 'auto'); + }, + ); + + it.each(['سلام OpenAI 123!', 'سلام John Smith', 'Hello שלום עולם', 'مرحبا بكم'])( + 'keeps %s in one wrapping text run so the browser can resolve bidi order', + (text) => { + const { container } = render(); + const animatedSegments = container.querySelectorAll('p > span > span.inline-block'); + + expect(animatedSegments).toHaveLength(1); + expect(animatedSegments[0].childNodes).toHaveLength(1); + expect(animatedSegments[0].firstChild?.nodeType).toBe(Node.TEXT_NODE); + expect(animatedSegments[0].textContent).toBe(text); + expect(animatedSegments[0].parentElement).toHaveClass('whitespace-normal'); + expect(container.querySelector('.sr-only')).toHaveTextContent(text); + }, + ); + + it('keeps per-grapheme animation for Latin-only text', () => { + const { container } = render(); + + expect(container.querySelectorAll('p > span > span.inline-block')).toHaveLength(6); + }); + it('renders emojis correctly', () => { const emojis = ['🚧', '❤️‍🔥', '💜', '🦎', '❌', '✅', '⚠️']; const originalText = emojis.join(''); @@ -35,7 +68,7 @@ describe('SplitText', () => { it.each([ ['rtl', 'Welcome to reeva::chat', ['Welcome', 'to', 'reeva::chat']], - ['ltr', 'שלום עולם', ['שלום', 'עולם']], + ['ltr', 'שלום עולם', ['שלום עולם']], ])('uses the accessible copy to resolve %s-document greeting %s', (dir, text, expectedWords) => { const previousDir = document.documentElement.dir; document.documentElement.dir = dir; diff --git a/packages/client/src/components/SplitText.tsx b/packages/client/src/components/SplitText.tsx index c4a565c8db..871362ab99 100644 --- a/packages/client/src/components/SplitText.tsx +++ b/packages/client/src/components/SplitText.tsx @@ -1,5 +1,6 @@ import { useEffect, useMemo, useRef, useState } from 'react'; -import { useSprings, animated, SpringConfig } from '@react-spring/web'; +import { useSprings, animated } from '@react-spring/web'; +import type { SpringConfig } from '@react-spring/web'; import useRemScale from '~/hooks/useRemScale'; const TEXT_ALIGN: Record, string> = { @@ -90,8 +91,9 @@ const SplitText: React.FC = ({ onLetterAnimationComplete, onLineCountChange, }) => { + const containsRtl = /[\p{Script=Arabic}\p{Script=Hebrew}]/u.test(text); const { words, letterCount, offsets } = useMemo(() => { - const split = text.split(' ').map(splitGraphemes); + const split = containsRtl ? [[text]] : text.split(' ').map(splitGraphemes); const starts: number[] = []; let total = 0; for (const w of split) { @@ -99,7 +101,7 @@ const SplitText: React.FC = ({ total += w.length; } return { words: split, letterCount: total, offsets: starts }; - }, [text]); + }, [text, containsRtl]); const [inView, setInView] = useState(false); const ref = useRef(null); const animatedCount = useRef(0); @@ -173,7 +175,7 @@ const SplitText: React.FC = ({ key={wordIndex} dir="auto" aria-hidden="true" - className="inline-block whitespace-nowrap" + className={`inline-block ${containsRtl ? 'whitespace-normal' : 'whitespace-nowrap'}`} > {word.map((letter, letterIndex) => { const index = offsets[wordIndex] + letterIndex;