This commit is contained in:
MiRHaDi 2026-11-18 14:50:30 +01:00 • committed by GitHub
commit 95386ab2f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 5 deletions

View file

@ -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(<SplitText text={word} />);
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(<SplitText text={text} />);
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(<SplitText text="OpenAI" />);
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;

View file

@ -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<NonNullable<SplitTextProps['textAlign']>, string> = {
@ -90,8 +91,9 @@ const SplitText: React.FC<SplitTextProps> = ({
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<SplitTextProps> = ({
total += w.length;
}
return { words: split, letterCount: total, offsets: starts };
}, [text]);
}, [text, containsRtl]);
const [inView, setInView] = useState(false);
const ref = useRef<HTMLParagraphElement>(null);
const animatedCount = useRef(0);
@ -173,7 +175,7 @@ const SplitText: React.FC<SplitTextProps> = ({
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;