Typed Text
Types new text in, one character at a time, when it changes. Use it for short text an agent rewrites while someone watches, such as a chat title. The first render shows the text at once.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/typed-text.jsonCopies Typed Text into your app, installs the packages, and adds the styles. Then import it from @/components/ui/typed-text. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-mergeAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
import { useLayoutEffect, useRef, useState, type ComponentProps } from 'react'; import { cn } from '@/lib/sprout/classes'; export const typedTextCharacterDuration = 25; export const typedTextMaxDuration = 800; let segmenter: Intl.Segmenter | undefined; function graphemes(text: string) { segmenter ??= new Intl.Segmenter('und', { granularity: 'grapheme' }); return Array.from(segmenter.segment(text), item => item.segment); } export interface TypedTextProps extends Omit<ComponentProps<'span'>, 'children'> { text: string; animate?: boolean | undefined; } export function TypedText({ text, animate = true, className, ...rest }: TypedTextProps) { const previous = useRef(text); const [visible, setVisible] = useState(text); useLayoutEffect(() => { const changed = previous.current !== text; previous.current = text; const motion = window.matchMedia('(prefers-reduced-motion: reduce)'); if (!changed || !animate || motion.matches) { setVisible(text); return; } const characters = graphemes(text); const start = performance.now(); const duration = Math.min(typedTextMaxDuration, characters.length * typedTextCharacterDuration); let frame = 0; setVisible(characters[0] ?? ''); function tick(now: number) { const progress = duration ? Math.min(1, (now - start) / duration) : 1; setVisible(characters.slice(0, Math.max(1, Math.ceil(progress * characters.length))).join('')); if (progress < 1) frame = requestAnimationFrame(tick); } function reduceMotion() { if (!motion.matches) return; cancelAnimationFrame(frame); setVisible(text); } frame = requestAnimationFrame(tick); motion.addEventListener('change', reduceMotion); return () => { cancelAnimationFrame(frame); motion.removeEventListener('change', reduceMotion); }; }, [text, animate]); return ( <span data-slot="typed-text" data-state={visible === text ? 'idle' : 'typing'} className={cn('sprout-typed-text', className)} {...rest} > <span className="sprout-typed-text-sr-only">{text}</span> <span aria-hidden="true">{visible}</span> </span> ); }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-typed-text { color: inherit; } .sprout-typed-text-sr-only { clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; width: 1px; height: 1px; margin: -1px; padding: 0; position: absolute; overflow: hidden; } }Import it from
@/components/ui/typed-text. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/typed-text.
Chat title
An agent names the chat. Its first title appears at once; later agent renames type in. A title someone typed by hand never animates.
New chat
Animated changes
Each change types the new sentence in, 25ms a character and never longer than 800ms.
Let’s make a garden plan.
Emoji and accents
Typing steps by what a reader sees as one character, so emoji families and accented letters never split.
🌱 Seed order sent to Jürgen
Without animation
animate={false} shows every change at once.
This sentence appears immediately.
Usage
import { TypedText } from "@tulipfarm/sprout/typed-text";
<h1 tabIndex={-1} title={chat.title} className="min-w-0 flex-1 truncate text-sm font-medium">
<TypedText text={chat.title} animate={!chat.titleManual && chat.titleRevision > 1} />
</h1>text is the full text. When it changes, the new text types in at 25ms a character, and never takes longer than 800ms (typedTextCharacterDuration andtypedTextMaxDuration). It steps by graphemes, so emoji and accented letters never split.
animate defaults to true. Passfalse to show changes at once, such as a title someone typed by hand. The text is inline, so a truncating parent still adds an ellipsis. className, data-*, and other span attributes pass through.
Without animation
<TypedText text="This sentence appears immediately." animate={false} />Accessibility
Screen readers get the full text at once from a visually hidden copy; the typing letters are hidden from them, so nothing is read in pieces. With reduced motion the text changes at once, and turning reduced motion on mid-change finishes it right away. While typing, the root hasdata-state="typing"; otherwise "idle".
Theme tokens
Typed Text has no color or font of its own. It inherits from the heading or paragraph it sits in, in light, dark, and brand themes.