I want to use this jumping text effect (https://web.dev/patterns/animation/animated-words/) in my app and while I am creating the component in React I got into some problems. I created a styled component and put the css into it. Then I use the innerHTML for DOM manipulation in my React component and finally when I test no effect here. What did I wrong?
App.tsx
import styled from "styled-components";
const Jumper = styled.p`
@keyframes trampoline {
0% {
transform: translateY(100%);
animation-timing-function: ease-out;
}
50% {
transform: translateY(0);
animation-timing-function: ease-in;
}
}
@media (prefers-reduced-motion: no-preference) {
[word-animation] {
display: inline-flex;
flex-wrap: wrap;
gap: 1ch;
}
[word-animation="trampoline"] > span {
display: inline-block;
transform: translateY(100%);
animation: trampoline 3s ease calc(var(--index) * 150 * 1ms) infinite
alternate;
}
}
`;
export default function App() {
const span = (text, index) => {
const node = document.createElement("span");
node.textContent = text;
node.style.setProperty("--index", index);
return node;
};
const byWord = (text) => text.split(" ").map(span);
const { matches: motionOK } = window.matchMedia(
"(prefers-reduced-motion: no-preference)"
);
if (motionOK) {
const splitTargets = document.querySelectorAll("[split-by]");
splitTargets.forEach((node) => {
let nodes = byWord(node.innerText);
if (nodes) node.firstChild.replaceWith(...nodes);
});
}
return <Jumper>split a paragraph of content </Jumper>;
}