Skip to content

Svelte numeric input

Numora is a Svelte numeric input library. It handles thousand separators, decimal limits, paste sanitization, scientific-notation expansion and cursor preservation through a use:numora action. Svelte's use: actions suit libraries that own a DOM node, as NumoraInput does. You need no numora-svelte wrapper, no peer dependencies and no SSR special-casing. The vanilla package is framework-agnostic TypeScript, 8.3 kB gzipped, with zero runtime dependencies.

Installation

pnpm add numora

Svelte action

NumoraInput attaches directly to an existing <input> element. A Svelte action binds that lifecycle to a DOM node. Add this helper to your project once and reuse it everywhere.

src/lib/numora.ts:

import { NumoraInput, type NumoraInputOptions } from 'numora';

export function numora(node: HTMLInputElement, options: NumoraInputOptions) {
  const instance = new NumoraInput(node, options);
  return {
    // Svelte calls this when the options passed to use:numora change.
    update: (next: NumoraInputOptions) => instance.update(next),
    // Removes Numora's listeners from the adopted <input>.
    destroy: () => instance.destroy(),
  };
}

Component <script lang="ts">:

import { numora } from '$lib/numora';

let value = $state('');

Component markup:

<label>
  Amount
  <input
    use:numora={{
      decimalMaxLength: 2,
      onValueChange: ({ value: v }) => (value = v),
    }}
  />
</label>

<p>Raw value: {value}</p>

Numora adopts the <input> and sets the attributes it needs. onValueChange receives { value, formattedValue }, where value is the raw string (see value types). By default, Numora formats on every keystroke (see formatting).

Read the value from onValueChange, not from bind:value. The element's own value is the display string ("1,234.56"). onValueChange gives you the raw value ("1234.56").

Animated example: Numora + Torph in Svelte

Stack a Torph TextMorph over a transparent-text NumoraInput. The Torph integration explains the overlay and has the shared CSS and install steps. Below is the Svelte 5 action.

src/lib/numora-torph.ts is an action that mounts NumoraInput and TextMorph in the same wrapper. Its destroy hook tears down both:

import { NumoraInput, type NumoraInputOptions } from 'numora';
import { TextMorph } from 'torph';

export function numoraTorph(node: HTMLElement, options: NumoraInputOptions) {
  const display = node.querySelector<HTMLElement>('.numora-overlay-display')!;
  const host = node.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 },
  });

  const numora = new NumoraInput(host, {
    ...options,
    // onFormattedValueChange reports every rewrite of the field, in both formatOn modes.
    // The caret is the second argument: Torph then matches digits around the edit.
    onFormattedValueChange: (text) => {
      morph.update(text || '0', numora.element.selectionStart ?? undefined);
      options.onFormattedValueChange?.(text);
    },
  });
  const input = numora.element;
  input.setAttribute('aria-label', 'Amount');

  // The callback does not fire on creation, so prime the display.
  morph.update(numora.formattedValue || '0');

  return {
    destroy: () => {
      morph.destroy();
      numora.destroy();
      input.remove();
    },
  };
}

Component <script lang="ts">:

import { numoraTorph } from '$lib/numora-torph';

Markup:

<!-- The action creates the <input> inside this label at runtime. -->
<!-- svelte-ignore a11y_label_has_associated_control -->
<label
  class="numora-overlay"
  use:numoraTorph={{ decimalMaxLength: 2 }}
>
  <span class="numora-overlay-display" aria-hidden="true">0</span>
  <div class="numora-overlay-host"></div>
</label>

Use the overlay CSS from the Torph page in the component's <style> block. Wrap selectors that target the runtime-created <input> in :global(), because Svelte's scoped styles cannot reach it otherwise:

.numora-overlay-host :global(input) { /* ... */ }

FAQ

What is the best Svelte numeric input library?

Numora is framework-agnostic TypeScript, 8.3 kB gzipped, with no runtime dependencies. In Svelte you wrap NumoraInput in a small use:numora action and mount it directly on an <input>.

Is there a numora-svelte package?

No, and you do not need one. The Svelte action is the entire adapter.

Does Numora work with SvelteKit and SSR?

Yes. NumoraInput touches the DOM, so initialize it inside an action or onMount. Both only run in the browser. SvelteKit renders the empty <input> on the server and the action adopts it in the browser. You need no SSR-specific configuration.

How do I add thousand separators to a Svelte number input?

Nothing to configure. Grouping on every keystroke is the default. Numora formats as the user types and keeps the cursor between the same two digits when it inserts a comma. onValueChange receives { value, formattedValue }, where value is the raw value without separators, which you can store in a Svelte store or pass to a form library.

Does it support i18n (decimal commas, locale grouping) in Svelte?

Yes. Set decimalSeparator: ',' and thousandSeparator: '.' (or use the locale option) for European locales. The raw value is a plain dot-decimal string in every locale.

Why not use a normal <input> with bind:value?

A plain bind:value has no thousand separators, decimal limits or paste sanitization, and you would have to write the cursor-positioning logic yourself. Numora handles all of it.

Can I animate digits in Svelte?

Yes. The Torph integration is also framework-agnostic. The same vanilla TextMorph overlay pattern works inside a Svelte component, and Torph respects prefers-reduced-motion by default.