Skip to content

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.


Blur the field to format.

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 TextMorph bound to a sibling span that animates the formatted string.
  • Keyboard layer: the vanilla NumoraInput positioned on top with color: transparent and a visible caret. It still owns focus, keystrokes, selection, IME and mobile inputmode, 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 torph

Building the overlay

<label class="numora-overlay">
  <span class="numora-overlay-display" aria-hidden="true">0</span>
  <span class="numora-overlay-host"></span>
</label>
.numora-overlay {
  position: relative;
  display: inline-flex;
  align-items: center;
  font: 2.25rem/1 ui-monospace, SFMono-Regular, monospace;
  color: white;
}

.numora-overlay-display {
  pointer-events: none;
  white-space: pre;
}

.numora-overlay-host {
  position: absolute;
  inset: 0;
}

/* Style the <input> that NumoraInput creates inside the host */
.numora-overlay-host input {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: transparent;
  caret-color: white;
  outline: none;
  font: inherit;
}

.numora-overlay-host input::placeholder {
  color: transparent;
}
import { NumoraInput } from 'numora'
import { TextMorph } from 'torph'

const wrap = document.querySelector<HTMLElement>('.numora-overlay')!
const display = wrap.querySelector<HTMLElement>('.numora-overlay-display')!
const host = wrap.querySelector<HTMLElement>('.numora-overlay-host')!

// Optional: empty the SSR placeholder up front. Torph also removes it itself.
display.textContent = ''

const morph = new TextMorph({
  element: display,
  ease: { stiffness: 400, damping: 30 },
})

// onFormattedValueChange gives the display string, which is what Torph should show.
const numora = new NumoraInput(host, {
  decimalMaxLength: 2,
  // The caret is the second argument: Torph then matches digits around the edit.
  onFormattedValueChange: (text) =>
    morph.update(text || '0', numora.element.selectionStart ?? undefined),
})
const input = numora.element
input.setAttribute('aria-label', 'Amount')

morph.update('0')

Why each line matters

  • display.textContent = '' empties a placeholder, such as an SSR 0, before Torph takes over the span. The line is optional, because Torph removes leftover text nodes itself when it renders.
  • onFormattedValueChange fires every time Numora rewrites the field, so you need no DOM input, focus or blur listeners. It does not fire on creation, so call morph.update() once after construction (with numora.formattedValue if 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. onValueChange does not report them, because the raw value is unchanged, but onFormattedValueChange does, 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: inherit plus margin: 0; padding: 0; border: 0 on 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:

@keyframes caret-hold {
  0%, 70% { caret-color: transparent; }
}

@media (prefers-reduced-motion: no-preference) {
  .numora-overlay-host input:focus { animation: caret-hold 400ms; }
}

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 change ease or duration, 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-motion Torph 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.

morph.destroy()
numora.destroy()
input.remove()

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.