TextTransformer
A short-text transition that morphs per character or crossfades.
Usage
Basic
When the text color changes with the value, set it on the root; under fade the outgoing layer fades out in its old color.
Loading demo...
Morph and Fade
The default morph animates per character and rolls digits by place value; fade is a whole-string blur crossfade, the only mode blurPx applies to.
Loading demo...
With AutoSizer
Inside TxAutoSizer, wrap the change in action(() => …) and the size follows the text; with wrap=false, overflow is clipped.
Loading demo...
Long Text / Chapter Switch
wrap breaks multi-line text with pre-line and forces fade.
Loading demo...
Title and Subtitle
Loading demo...
Status Text
Loading demo...
Best Practices
- Use it for labels, titles, badges, and occasional state changes, not for streaming tokens or per-frame counters.
- Use
TxTextMorphdirectly when you only need the morph; it adds springs,numbers,locale, andcursorIndex. - Keep the durations equal when paired with
TxAutoSizer;morphanimates its own size, so an outer sizer is usually redundant. - Keep
wrap=falsein compact buttons and badges; turnwrapon only for paragraph or chapter changes. - Keep slot content light and derived only from the provided
text: during afade, both layers render it.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | number | - | The current value, normalized with String(...). |
mode | 'morph' | 'fade' | morph | morph animates per character; fade crossfades the whole string with blur. |
durationMs | number | 240 | Transition duration in ms; under fade, also when the old layer is removed. |
blurPx | number | 8 | Blur distance of the crossfade; fade only. |
tag | string | span | Root element tag. |
wrap | boolean | false | Breaks multi-line text with pre-line and forces fade. |
Slots
| Slot | Props | Description |
|---|---|---|
default | { text: string } | Text renderer shared by the current and previous layers; forces fade. |
Overview
- The default slot or
wrapoverridesmodeand forcesfade: the engine morphs only plain text and measures on a single line. - The root is
aria-live="polite". Undermorphthe whole value sits in a visually hidden[tx-morph-sr]node; underfadethe old layer isaria-hidden, so only the current text is announced. fadewithwrap=falsetruncates to one line with an ellipsis; undermorphthe root isoverflow: visibleso exiting segments aren't clipped, and nothing is truncated.- Under
fade, the new layer lands in a transparent, blurred setup state, one style commit is forced, and the transition starts on the next frame; the old layer is removed afterdurationMs + 34ms. - A newer change cancels the previous timer and animation frame, so a stale transition never removes the current layer.
- Under reduced motion, neither layer transitions; the new text replaces the old at once.
Technologies
morphrendersTxTextMorph(see TextMorph);faderenders two layers whose CSS transitions read--tx-tt-durationand--tx-tt-blur.- Source:
packages/tuffex/packages/components/src/text-transformer/.
查看源码
packages/tuffex/packages/components/src/text-transformer/index.ts