Become a Pro / website · becomeapro.ai / src / components / home / split-words.tsx
| 1 | import { Fragment } from "react"; |
| 2 | |
| 3 | // Wraps each word (and optionally each letter) in spans so headings can be revealed and animated. |
| 4 | // The heading itself should carry the full text as its aria-label; these spans are hidden from screen readers. |
| 5 | export function SplitWords({ text, letters }: { text: string; letters?: boolean }) { |
| 6 | const words = text.trim().split(/\s+/); |
| 7 | return ( |
| 8 | <> |
| 9 | {words.map((word, i) => ( |
| 10 | <Fragment key={i}> |
| 11 | <span className="w" aria-hidden> |
| 12 | <span className="wi"> |
| 13 | {letters |
| 14 | ? [...word].map((c, j) => ( |
| 15 | <span key={j} className="ch"> |
| 16 | {c} |
| 17 | </span> |
| 18 | )) |
| 19 | : word} |
| 20 | </span> |
| 21 | </span> |
| 22 | {i < words.length - 1 ? " " : null} |
| 23 | </Fragment> |
| 24 | ))} |
| 25 | </> |
| 26 | ); |
| 27 | } |