Communitygithub.com

text-morph

Animate text changes the way torph (torph.lochie.me) does, on iOS and React Native. Letters that survive a change slide to their new place, new ones scale up and fade in, removed ones scale down and fade out, and digits in numbers roll by place value. Use when someone wants a label, button title, counter, price, timer, status or any string to morph or animate between values in SwiftUI or React Native / Expo, mentions torph, text morph, morphing text, animated text change, rolling numbers or a number ticker in an app, or wants "Save → Saved" or "Add to cart → Added" to animate instead of snapping. Ships a SwiftUI view, a Reanimated 4 component, a dependency-free diff core shared by both, and parity tests against torph itself. On the web, use the torph npm package directly.

What is text-morph?

text-morph is a Claude Code agent skill that animate text changes the way torph (torph.lochie.me) does, on iOS and React Native. Letters that survive a change slide to their new place, new ones scale up and fade in, removed ones scale down and fade out, and digits in numbers roll by place value. Use when someone wants a label, button title, counter, price, timer, status or any string to morph or animate between values in SwiftUI or React Native / Expo, mentions torph, text morph, morphing text, animated text change, rolling numbers or a number ticker in an app, or wants "Save → Saved" or "Add to cart → Added" to animate instead of snapping. Ships a SwiftUI view, a Reanimated 4 component, a dependency-free diff core shared by both, and parity tests against torph itself. On the web, use the torph npm package directly.

Works with✓Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/cranberryrum/text-morph/tree/HEAD

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

Text morph (torph for SwiftUI and React Native)

torph (MIT, Lochie Axon) animates a string changing into another string. It's web-only: it measures DOM nodes and drives the Web Animations API. This skill ports torph to SwiftUI and React Native. Its matching logic is ported exactly, and the rendering is redone with each platform's own animation system.

Platform check first. On the web (React, Vue, Svelte, vanilla), install torph and use it; don't port anything. Use this skill for SwiftUI (iOS 17+ / macOS 14+) and React Native with Reanimated ≥ 3.6 (written against 4.x).

How it works (the 30-second version)

  1. Diff (pure logic, identical on both platforms). Every piece of text on screen has a stable id. The next string is matched against what's showing:
    • Words are matched first, using a longest-common-subsequence. A word whose letters are at least 40% shared with an old word in the same slot morphs letter by letter (Following → Unfollow keeps "ollow").
    • Numeric words ("$1,204", "50%", "-0.5") match by place value, aligned on the decimal point. A digit that stays the same keeps its id. A digit that changes gets a new id, so it rolls out and its replacement rolls in.
    • An id that survives stays on screen; a new id enters; a missing id exits.
  2. Render. Each grapheme is its own view, keyed by its id. Then:
What happensMotion (fractions of the duration)
Kept characterSlides to its new position. Default 400ms cubic-bezier(0.19, 1, 0.22, 1)
New characterScales 0.95 → 1. Opacity 0 → 1 over 50%, after a 25% delay
Removed characterStays where it was, scales to 0.95, opacity → 0 over 25%
Run of ≥ 6 new/removed charactersScales 0.8 ↔ 1 as one group around its centre. Fades over 35% in / 45% out
New digitDrops in from one line above, fades in over 25%
Removed digitFalls one line down, fades out over 45%
ContainerWidth animates. Digits roll through a soft top and bottom edge (~0.15em)

Details and the reasoning behind each choice are in references/how-it-works.md.

Files

FileWhat it is
assets/swiftui/TextMorph.swiftA single file to drop in: the view, the flow layout, the transitions and the Swift core. No dependencies
assets/react-native/textMorphCore.tsThe diff core. Pure TS that runs on Hermes, with its own segmenter for when Intl.Segmenter is missing
assets/react-native/TextMorph.tsxThe Reanimated 4 component
assets/react-native/TextMorphFade.tsxOptional wrapper that adds torph's soft edge for rolling digits (needs @react-native-masked-view/masked-view and react-native-linear-gradient; use expo-linear-gradient in Expo)
scripts/parity.mjs + sequences.jsonRuns update sequences through torph and through the TS port, and diffs the segments
scripts/SwiftParity.swift + fixtures.jsonThe same check for the Swift port
scripts/TextMorph.test.tsxJest test for the RN component (exit plans are set before unmount, digits roll, no render loop)

Using it

SwiftUI

Copy TextMorph.swift into the target.

TextMorph(isSaved ? "Saved" : "Save")
    .font(.headline)

TextMorph(value: total, decimals: 2, locale: .init(identifier: "en_US"))
    .font(.title.monospacedDigit())          // tabular digits: places don't jiggle

TextMorph(status, config: .init(timing: .spring(stiffness: 200, damping: 20),
                                alignment: .center, fadeEdge: 4))
  • Font, colour and other styling come from the environment, the same as Text.
  • Set fadeEdge to about 0.15 × the font's point size (the default is 3pt).
  • The text wraps at spaces when the width is constrained, and \n starts a new line.
  • With Reduce Motion on, the whole string crossfades in 150ms: no slide, scale or roll. disabled swaps text with no animation at all.
  • VoiceOver reads the whole string once.

React Native

Copy textMorphCore.ts and TextMorph.tsx (plus TextMorphFade.tsx if the text has numbers and the app has the mask libraries).

<TextMorph textStyle={styles.cta}>{added ? 'Added to cart' : 'Add to cart'}</TextMorph>

<TextMorphFade textStyle={[styles.price, { fontVariant: ['tabular-nums'] }]} decimals={2}>
  {price}
</TextMorphFade>

<TextMorph ease={{ stiffness: 200, damping: 20 }} onAnimationComplete={haptic}>{label}</TextMorph>
  • textStyle styles each character's <Text>, and style styles the row.
  • Reduce Motion works the same as on iOS: a 150ms crossfade of the whole string. TextMorphFade's soft edge grows with the user's text size.
  • All motion is Reanimated layout animations on the UI thread; React renders twice per change and never per frame. Judge the feel in a release build on a device, not in the simulator or a dev build.
  • Accepted props: duration, ease (a bezier tuple or spring params), scale, numbers, decimals, locale, cursorIndex, disabled, respectReducedMotion, onAnimationComplete, and onAnimationCancel (fired instead of complete when the next change interrupts).
  • To centre the text, centre the component in its parent. The row animates its own frame, so the characters glide instead of jumping.
  • For a field someone is typing in, pass cursorIndex (the caret position). A typed digit is then inserted instead of renumbering the whole column, the same as torph.

Rules that keep it feeling right

  • Short, changing labels only. Buttons, statuses, counters, prices, timers, tab titles. Don't use it for paragraphs: each grapheme is a view.

  • Tabular digits for numbers (.monospacedDigit() / fontVariant: ['tabular-nums']). Without them the places shift width and the numbers wobble.

  • Pick the duration by how often it changes. Keep the curve; change only the duration.

    The textDurationWhy
    Changes now and then: a button after an action, a status, a tab title400ms (default)torph's tuned feel; the change is the news
    Steppers, counters, chips tapped in quick runs200msA small state change. 400ms lags behind repeated taps
    A timer ticking every second200–300msMust settle well before the next tick

    Avoid bouncy springs on text; damping ≥ 20. Fire any haptic from the press handler, in the same frame as the change, never from onAnimationComplete.

  • 120fps on iOS. ProMotion iPhones cap third-party animation at 60fps unless CADisableMinimumFrameDurationOnPhone is true in Info.plist. Check for it in RN apps; recent Expo SDKs set it by default.

  • Don't animate keyboard-driven or high-frequency text. For a live search count or a per-keystroke field, morph only when the value settles, or set disabled.

  • One morph per change. Changing the text mid-morph is fine: kept letters retarget from where they are. Don't chain timers to fake sequences.

  • Pure-number counters in SwiftUI. Text(value, format:) with .contentTransition(.numericText(value:)) is the system's own roll, and it's fine for a bare number. Use TextMorph when the number sits inside other text or when words change too.

Known differences from torph

  • Kerning: each glyph is a separate view, so kerning between letters is lost. This is invisible at UI sizes in most fonts. It can show on large display type with tight pairs (AV, To).
  • Anchoring: torph moves a new character along with its nearest surviving neighbour, and a leaving character along with its old neighbour. Here they appear and leave in place. The difference only shows when a big shift happens at the same time as an enter or exit.
  • Group scaling assumes glyphs of similar width, because the group's centre is estimated from character counts.
  • React Native: single line unless the text contains \n. Without TextMorphFade, rolling digits aren't clipped, so they show slightly outside the line while they fade.
  • Reduced motion: torph sets the text instantly. The ports crossfade it in 150ms instead, since reduced motion should mean gentler, not none.
  • Graphemes, not UTF-16: words are split by grapheme cluster, so emoji survive a morph. torph splits by UTF-16 code unit. Ids match torph for all BMP text, so the parity tests cover it.

Verifying a change to the core

cd ~/.claude/skills/text-morph
# needs torph installed somewhere (npm i torph); pass its dist/index.mjs
node --experimental-strip-types scripts/parity.mjs ~/node_modules/torph/dist/index.mjs --write-fixtures
swiftc -parse-as-library -swift-version 6 assets/swiftui/TextMorph.swift scripts/SwiftParity.swift -o /tmp/tm-parity \
  && /tmp/tm-parity scripts/fixtures.json

Add a sequence to scripts/sequences.json for any case you change. The TS port is checked twice: once with Intl.Segmenter, and once with the Hermes fallback segmenter.

To check the motion itself, film it, because parity only proves the ids. On iOS, run it in the simulator with a slowed duration (timing: .curve(3)) and take screenshots at 5%, 15%, 30%, 50% and 100%. On RN, run the app and use the TextMorph.test.tsx checks plus a slowed duration.

When building with it, also apply

  • emil-design-eng / animate for whether the change should animate at all.
  • animate-expo for RN specifics (UI-thread work, reduced motion).
  • apple-design for SwiftUI motion conventions.

Related Skills