Torph integration
Torph by Lochie Axon is an animated text-morphing library. This guide layers a Torph TextMorph on top of a transparent-text vanilla NumoraInput so the editable surface itself appears animated. The real <input> still handles every keystroke, and Torph renders what the user sees.
Using React? See the Numora React Torph integration.
With 'change', separators animate on every keystroke.
With 'blur', the separators animate out on focus and back in on blur. The caret appears once the digits have settled. See “The caret during the focus strip” below.
How the overlay works
Native <input> elements render their value as a string with no child DOM, so animation libraries can't inject animated spans into them directly. The overlay stacks two layers in the same box to get around that:
- Visible layer: a
TextMorphbound to a sibling span that animates the formatted string. - Keyboard layer: the vanilla
NumoraInputpositioned on top withcolor: transparentand a visible caret. It still owns focus, keystrokes, selection, IME and mobileinputmode, so paste and native form submission keep working too.
Both layers render the same formatted string. The vanilla NumoraInput class calls its onFormattedValueChange option every time it rewrites the field: typing (from beforeinput), paste, the focus strip and the blur regroup. The bridge is a single call: morph.update(text || '0', numora.element.selectionStart ?? undefined). The second argument is the caret, which tells Torph where the edit was. Torph diffs the old and new strings and animates each digit and separator into place. The input itself never animates, and because its text is transparent, you only see the Torph layer.
Installation
pnpm add numora torphBuilding the overlay
Why each line matters
display.textContent = ''empties a placeholder, such as an SSR0, before Torph takes over the span. The line is optional, because Torph removes leftover text nodes itself when it renders.onFormattedValueChangefires every time Numora rewrites the field, so you need no DOMinput,focusorblurlisteners. It does not fire on creation, so callmorph.update()once after construction (withnumora.formattedValueif you pass an initial value).formatOn: 'change'(the default, used here) keeps the input's display as the formatted string at all times. With'blur', the focus strip and the blur regroup only add or remove separators.onValueChangedoes not report them, because the raw value is unchanged, butonFormattedValueChangedoes, so the same line covers both modes. Blur mode also needs a few lines of CSS for the caret, shown in the next section.- Match typography. Set
font: inheritplusmargin: 0; padding: 0; border: 0on the input. The browser computes the caret position from the input's invisible text layout, so any difference in font metrics or text origin shows up as caret drift.
The caret during the focus strip
With 'blur', focusing the field rewrites 1,234,567 to 1234567 in one step. Numora calls this rewrite the focus strip. The native caret is at its final position at once, but Torph needs about 400ms to move the digits there, so the caret looks misplaced until they arrive. Keep the caret transparent while the digits move. It takes CSS only:
The selector targets the input that NumoraInput creates inside the host. The caret stays transparent for the first 280ms and fades in over the last 120ms. The keyframes have no to step, so it fades into the colour the field already has.
- The 400ms matches the spring used on this page (
stiffness: 400, damping: 30). If you changeeaseorduration, change the 400ms to match. - The hold runs on every focus, also when the value has no separators to remove. The caret is then 280ms late for no visible reason.
- Under
prefers-reduced-motionTorph swaps the text instantly, so the hold is off.
Reducing motion
Torph respects prefers-reduced-motion by default. If a user opts out of animations at the OS level, the morph becomes an instant swap. You need no extra code.
Cleanup
On teardown, call morph.destroy() to detach Torph's observers and numora.destroy() to detach Numora's listeners, then remove the input element. Calling destroy() twice is safe.
FAQ
What is the Torph library?
Torph is an animated text-morphing library by Lochie Axon. It diffs an old string and a new string and animates each character between them. Torph has both a React component and a vanilla TextMorph class. This page uses the vanilla one.
Does Torph work without React?
Yes. The code on this page uses the vanilla TextMorph class from the torph package together with the vanilla NumoraInput class. No React in the runtime.
Where can I install the Torph library?
The Torph library lives at torph.lochie.me and is published on npm as torph. Install both packages with npm install numora torph.