A splitter that never tears a glyph
Splitting text with Array.from or split('') breaks anything built from more than one code point: family emoji joined with zero-width joiners, flags made of two regional indicators, accented letters typed as base + combining mark. Animate those per character and half a flag flies off on its own.
The splitter segments by grapheme cluster with Intl.Segmenter, falling back to Array.from where it's missing. Lines are detected after layout by grouping words on offsetTop with a ±2px tolerance. Every span is aria-hidden while the container keeps an aria-label with the original string, so screen readers read a sentence, not a list of letters. A ResizeObserver re-splits on resize, and teardown kills the GSAP tweens and restores plain text.
function splitGraphemes(text: string): string[] {
if (typeof Intl !== 'undefined' && 'Segmenter' in Intl) {
try {
const IntlAny = Intl as any
const segmenter = new IntlAny.Segmenter(undefined, { granularity: 'grapheme' })
const segments = segmenter.segment(text)
const result: string[] = []
for (const seg of segments) {
result.push(seg.segment)
}
return result
} catch {
return Array.from(text)
}
}
return Array.from(text)
}