Skip to content

Solid numeric input

Numora is a Solid numeric input library. It handles thousand separators, decimal limits, paste sanitization, scientific-notation expansion and cursor preservation through a use:numora directive that mounts directly on an <input>. Write the raw value into a createSignal setter, and Solid's fine-grained reactivity updates whatever reads the signal. You need no numora-solid wrapper and no peer dependencies. The vanilla package is framework-agnostic TypeScript, 8.3 kB gzipped, with zero runtime dependencies.

Installation

pnpm add numora

Solid directive

NumoraInput attaches directly to an existing <input> element. Solid's use: directives bind that lifecycle to a DOM node. Add this helper to your project once, and every <input use:numora={opts}> works.

src/directives/numora.ts:

import { onCleanup } from 'solid-js';
import { NumoraInput, type NumoraInputOptions } from 'numora';

export function numora(el: HTMLInputElement, accessor: () => NumoraInputOptions) {
  // Options are read once, here. Later changes to the accessor are not applied.
  const instance = new NumoraInput(el, accessor() ?? {});
  // Removes Numora's listeners from the adopted <input>.
  onCleanup(() => instance.destroy());
}

// Tells Solid's JSX compiler that use:numora is a valid directive.
declare module 'solid-js' {
  namespace JSX {
    interface Directives {
      numora: NumoraInputOptions;
    }
  }
}

Component:

import { createSignal } from 'solid-js';
import { numora } from './directives/numora';
// 'numora' is only used as use:numora in the JSX, so TypeScript and bundlers see an
// unused import and may remove it. This reference keeps it.
void numora;

export function AmountInput() {
  const [value, setValue] = createSignal('');

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

The directive gets the <input> and an accessor for its options. 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 the element's value. That value is the display string ("1,234.56"). onValueChange gives you the raw value ("1234.56").

Animated example: Numora + Torph in Solid

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 Solid component.

import { onCleanup, onMount } from 'solid-js';
import { NumoraInput } from 'numora';
import { TextMorph } from 'torph';
import './numora-overlay.css'; // overlay CSS from the Torph page

export function AnimatedAmountInput() {
  let display!: HTMLSpanElement;
  let host!: HTMLDivElement;

  onMount(() => {
    // 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, {
      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');

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

  return (
    <label class="numora-overlay">
      <span ref={display} class="numora-overlay-display" aria-hidden="true">0</span>
      <div ref={host} class="numora-overlay-host" />
    </label>
  );
}

Solid does not scope CSS by default, so the overlay selectors from the Torph page work as-is.

FAQ

What is the best Solid numeric input library?

Numora is framework-agnostic TypeScript, 8.3 kB gzipped, with no runtime dependencies. In Solid you write a use:numora directive once, then use <input use:numora={opts} /> anywhere.

Is there a numora-solid package?

No, and you do not need one. The use:numora directive is the entire Solid adapter.

Does Numora work with SolidStart and SSR?

Yes. The use:numora directive only runs on the client. SolidStart renders the empty <input> on the server and Numora adopts it on hydration. You need no SolidStart-specific configuration.

How do I add thousand separators to a Solid 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 write straight into a createSignal setter.

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

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 a plain signal-bound <input>?

A plain signal-bound input 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 Solid?

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