useSplitText is a small React hook that makes it simple to create complex, beautiful split text animations with Motion.
It breaks apart text into individual characters, words, and lines, and hands them to you to animate. It also handles the parts that are easy to get wrong in React: it waits for fonts to load, splits again when the element changes width, and puts the original text back on unmount.
useSplitText is exclusive to Motion+ members. Motion+ is a one-time payment, lifetime membership that unlocks Motion UI, premium examples and access to a private Discord community.
Features
-
Animate anything: Split text into characters, words and lines to animate each independently.
-
Handles resize: Splits again when the element's width changes or a font loads, and the animation carries on from where it was.
-
Cleans up: Stops the animation and puts the original text back on unmount. Safe in
StrictMode. -
Preserves markup: Links,
<br>and other inline elements stay where they are. -
Respects kerning: Each character stays exactly where the browser rendered it.
-
Every language: Emoji, Arabic, Indic, Chinese, Japanese and Thai text split correctly.
-
Accessible: Screen readers read whole words, and copied text stays the same.
-
Lightweight: About 2.8kb, including splitText.
Install
First, install Motion UI in your project. You need to be a Motion+ member to generate an access token.
Once installed, useSplitText can be imported via motion-plus/react:
import { useSplitText } from "motion-plus/react"Usage
useSplitText accepts a ref to the element with the text, and a callback.
Once fonts have loaded, it replaces the element's text with <span> tags that wrap individual characters, words and lines. It then makes the element visible, and calls the callback with chars, words and lines as arrays of elements, so they can be animated right away with Motion's animate and stagger functions.
import { animate, stagger } from "motion"
import { useSplitText } from "motion-plus/react"
import { useRef } from "react"
export function Heading() {
const ref = useRef(null)
useSplitText(ref, ({ chars }) =>
animate(
chars,
{ opacity: [0, 1], y: [10, 0] },
{ duration: 1, delay: stagger(0.05) }
)
)
return (
<h1 ref={ref} style={{ visibility: "hidden" }}>
Split text in React
</h1>
)
}The returned arrays are regular elements so you're not limited to animating them with Motion. You could animate them with CSS, or attach gesture recognisers to each element individually, for instance with Motion's hover function.
Enter animations
Start the element with visibility: hidden. Otherwise we might see a flash of unsplit text before fonts load. useSplitText makes the element visible once it has split.
Handle resize
To split text, we first have to measure the current layout. Therefore, if the element changes width, or a font changes, the lines will be wrong.
useSplitText handles this for you. It splits again and calls the callback with the new chars, words and lines.
The callback runs in a scopeMotion scope rooted at the element. Before each new split, and on unmount, everything the callback started is stopped: animations, gestures like hover and press, and inView or scroll listeners.
Return the animation and the new animation also continues from the same elapsed time, so a resize mid-animation doesn't restart it.
Gradient text
Text with background-clip: text usually disappears once its characters are transformed. Each character gets its own slice of the background, so the gradient stays continuous while the characters move, and when the text splits again on resize.
Masked reveals
To slide words or lines in, within a clipped box, clip each line with CSS. No extra wrapper is needed. Because each line is clipped on its own, the reveal still works after the text splits again on resize:
.split-line {
clip-path: inset(0 -0.1em -0.15em);
}useSplitText(ref, ({ words }) =>
animate(words, { y: ["110%", "0%"] }, { delay: stagger(0.04) })
)The small negative insets leave room for italic overhangs and descenders.
Languages & emoji
Chinese, Japanese, Thai, Lao, Khmer and Myanmar text has no spaces between words. useSplitText finds the words with the browser's Intl.Segmenter. Emoji made from several parts, such as skin tones, flags and families, stay whole.
Arabic, Indic and other scripts where letters join are split into words but not characters, so their letters keep their shape.
Lines are read from the browser's layout, so vertical writing modes work too. Here, Japanese text set with writing-mode: vertical-rl is revealed word by word:
Targeting with CSS
Each split element receives a split-char, split-word or split-line class so you can select it with CSS. Each also receives a data-index of its position within the overall character, word or line lists.
.split-char[data-index="3"] {
color: red;
}The classes are configurable with the charClass, wordClass and lineClass options.
Accessibility
Some split text implementations break screen readers, which end up reading characters, words or lines separately. useSplitText ensures that screen readers read copy as it's originally written.