Skip to content

Vue numeric input

Numora is a Vue numeric input library. It handles thousand separators, decimal limits, paste sanitization, scientific-notation expansion and cursor preservation through a v-numora custom directive that mounts directly on an <input>. The vanilla package is framework-agnostic TypeScript, 8.3 kB gzipped, with zero runtime dependencies. The same engine works in Vue 3, Nuxt 3, VeeValidate, FormKit and Pinia, with no wrapper package.

Installation

pnpm add numora

Vue 3 directive

NumoraInput attaches directly to an existing <input> element. A custom directive is the usual Vue 3 adapter. Add this helper to your project once, and every <input v-numora="opts"> works.

src/directives/numora.ts:

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

const instances = new WeakMap<HTMLInputElement, NumoraInput>();

export const vNumora: Directive<HTMLInputElement, NumoraInputOptions> = {
  // Options are read once, here. Later changes to the binding are not applied.
  mounted(el, binding) {
    instances.set(el, new NumoraInput(el, binding.value ?? {}));
  },
  unmounted(el) {
    // Removes Numora's listeners from the adopted <input>.
    instances.get(el)?.destroy();
  },
};

// Types v-numora in templates when the directive is registered globally (Vue 3.5+).
declare module 'vue' {
  interface GlobalDirectives {
    vNumora: typeof vNumora;
  }
}

Register globally in src/main.ts (or import and declare locally in any <script setup>):

import { createApp } from 'vue';
import App from './App.vue';
import { vNumora } from './directives/numora';

createApp(App).directive('numora', vNumora).mount('#app');

Component <script setup lang="ts">:

import { ref } from 'vue';

const value = ref('');

Component <template>:

<label>
  Amount
  <input
    v-numora="{
      decimalMaxLength: 2,
      onValueChange: ({ value: v }) => (value = v),
    }"
  />
</label>
<p>Raw value: {{ value }}</p>

Numora adopts the <input> and sets the attributes it needs (type, inputmode, autocomplete and others). 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 v-model. 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 Vue 3

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 Vue 3 lifecycle and the scoped-style note.

Script block (inside <script setup lang="ts">):

import { onBeforeUnmount, onMounted, ref } from 'vue';
import { NumoraInput } from 'numora';
import { TextMorph } from 'torph';

const display = ref<HTMLSpanElement | null>(null);
const host = ref<HTMLDivElement | null>(null);

let cleanup: (() => void) | null = null;

onMounted(() => {
  if (!display.value || !host.value) return;

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

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

  const numora = new NumoraInput(host.value, {
    decimalMaxLength: 2,
    // 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),
  });
  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');

  cleanup = () => {
    morph.destroy();
    numora.destroy();
    input.remove();
  };
});

onBeforeUnmount(() => cleanup?.());

Template (inside <template>):

<label class="numora-overlay">
  <span ref="display" class="numora-overlay-display" aria-hidden="true">0</span>
  <div ref="host" class="numora-overlay-host" />
</label>

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

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

FAQ

What is the best Vue numeric input library?

Numora is framework-agnostic TypeScript, 8.3 kB gzipped, with no runtime dependencies. In Vue you register a v-numora directive once, then write <input v-numora="opts" /> anywhere.

Is there a numora-vue package?

No, and you do not need one. The v-numora directive is the entire Vue adapter.

Does Numora work with Nuxt 3 and SSR?

Yes. NumoraInput touches the DOM, so the v-numora directive only initializes in the mounted hook, which only runs in the browser. Nuxt renders the empty <input> on the server and Numora adopts it on hydration. Nuxt has no main.ts, so register the directive in a plugin with nuxtApp.vueApp.directive('numora', vNumora), or import vNumora in the component.

How do I add thousand separators to a Vue 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 ref, Pinia, VeeValidate or FormKit.

Does it work with VeeValidate, FormKit or Pinia in Vue?

Yes. onValueChange receives { value, formattedValue }, and value is the raw string. Pass it to VeeValidate, FormKit, Pinia or any other state library as you would a v-model value.

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

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

Why not v-model on a plain <input>?

A plain v-model 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 Vue?

Yes. The Torph + Numora overlay pattern is vanilla DOM, so it works inside any Vue component. Torph respects prefers-reduced-motion by default.