useStreamingText @astryx-svelte/core v0.3.0 · hooks

Usage

Smooths bursty streamed text into a steady character-by-character reveal using requestAnimationFrame. Decouples arrival rate from display rate. Advances on word and syntax boundaries to avoid slicing mid-markdown or mid-word, preventing visual glitches with markdown renderers. Animation timing derives from Astryx motion tokens via useTheme when available, with sensible fallbacks outside a theme provider. Snaps to full text when isStreaming becomes false.
ts
import { hooks } from '@astryx-svelte/core/hooks';

Best practices

GuidancePractices
DoPass the accumulated text (not individual chunks) as targetText; the hook handles incremental reveal internally.
DoSet isStreaming to false when the stream completes to snap to the final text.
DoUse speed='instant' for non-animated contexts like search results or when reduced motion is preferred.
Don'tUse for static text that does not change; the hook adds unnecessary overhead for non-streaming content.

Parameters

ParameterTypeDefaultDescription
targetText Required () => stringThe full target text to reveal. As new chunks arrive, update this value with the accumulated text.
isStreaming Required () => booleanWhether text is currently being streamed. When false, the hook returns the full targetText immediately.
options () => UseStreamingTextOptionsOptional configuration for streaming behavior.
options.speed 'natural' | 'fast' | 'instant''natural'Speed preset for text reveal. 'natural' is steady ~2 chars/frame, 'fast' scales with backlog ~4 chars/frame, 'instant' returns full text with no animation.

Returns

FieldTypeDefaultDescription
displayedText stringThe portion of targetText to render. Grows steadily toward the full targetText during streaming, or equals targetText when not streaming.

Called current here: the hook returns a live StreamingTextState, so read displayed.current. Upstream returns a bare string, which cannot stay reactive across a Svelte component’s lifetime — this is the one row where the port wraps a value upstream returns plain.

Examples

Common configurations, variations, and states.
useStreamingText — Streaming Response
Smooth bursty generated text into a steady reveal with useStreamingText.