Skip to content

Number field ​

A number input with decrement and increment buttons that respect min, max and step.

Installation ​

bash
npx bootcn add number-field
bash
pnpm dlx bootcn add number-field
bash
yarn dlx bootcn add number-field
bash
bunx --bun bootcn add number-field

The CLI also installs the design tokens and these components: label, input. It prints the <link> and <script> tags to add to your layout.

Usage ​

It is a native <input type="number">, so forms submit it as usual and the browser validates min, max and step. The buttons help on touch screens; keyboard users use the arrow keys.

JavaScript API ​

  • data-bootcn-number-field on the wrapper; buttons carry data-bootcn-step="down" or "up". The input's own min, max and step apply (the script uses stepUp() / stepDown()), and buttons disable at the limits.
  • Clicking a button fires the input's input and change events.
  • Bootcn.numberField.init(root) / destroy().

Accessibility ​

  • The input keeps the native spinbutton role: Up/Down change the value and screen readers announce it.
  • The buttons are named ("Increase guests") and left out of the tab order, so Tab goes straight to the next field.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The label component.
  • The input component.

Source ​

The exact files the CLI installs (number-field@1.0.0). View on GitHub · Report an issue

html
<div class="bootcn-field">
  <label class="bootcn-label" for="bootcn-number-field-guests">Guests</label>
  <div class="bootcn-number-field" data-bootcn-number-field>
    <button
      type="button"
      class="bootcn-number-field__step"
      data-bootcn-step="down"
      aria-label="Decrease guests"
      aria-controls="bootcn-number-field-guests"
      tabindex="-1"
    >
      <svg
        class="bootcn-icon"
        width="16"
        height="16"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        focusable="false"
      >
        <path d="M5 12h14" />
      </svg>
    </button>
    <input
      class="bootcn-number-field__input"
      type="number"
      id="bootcn-number-field-guests"
      name="guests"
      value="2"
      min="1"
      max="12"
      step="1"
      inputmode="decimal"
    />
    <button
      type="button"
      class="bootcn-number-field__step"
      data-bootcn-step="up"
      aria-label="Increase guests"
      aria-controls="bootcn-number-field-guests"
      tabindex="-1"
    >
      <svg
        class="bootcn-icon"
        width="16"
        height="16"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        focusable="false"
      >
        <path d="M12 5v14M5 12h14" />
      </svg>
    </button>
  </div>
</div>

<div class="mt-3">
  <div class="bootcn-field">
    <label class="bootcn-label" for="bootcn-number-field-width">Width in centimetres</label>
    <div class="bootcn-number-field" data-bootcn-number-field>
      <button
        type="button"
        class="bootcn-number-field__step"
        data-bootcn-step="down"
        aria-label="Decrease width in centimetres"
        aria-controls="bootcn-number-field-width"
        tabindex="-1"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M5 12h14" />
        </svg>
      </button>
      <input
        class="bootcn-number-field__input"
        type="number"
        id="bootcn-number-field-width"
        name="width"
        value="42.5"
        min="10"
        max="200"
        step="0.5"
        inputmode="decimal"
      />
      <button
        type="button"
        class="bootcn-number-field__step"
        data-bootcn-step="up"
        aria-label="Increase width in centimetres"
        aria-controls="bootcn-number-field-width"
        tabindex="-1"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M12 5v14M5 12h14" />
        </svg>
      </button>
    </div>
  </div>
</div>
css
.bootcn-number-field {
  display: flex;
  align-items: center;
  min-height: var(--bootcn-control-height);
  border: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: var(--bootcn-radius);
  background-color: transparent;
  box-shadow: var(--bootcn-shadow-sm);
  transition:
    border-color var(--bootcn-transition),
    box-shadow var(--bootcn-transition);
  max-width: 12rem;
}

.bootcn-number-field:focus-within {
  border-color: var(--bootcn-ring);
  box-shadow: 0 0 0 3px var(--bs-focus-ring-color);
}

.bootcn-number-field__input {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  color: var(--bootcn-foreground);
  font-size: 1rem;
  outline: 0;
  padding: 0 0.25rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
  appearance: textfield;
}

/* The buttons replace the browser's spinner. */
.bootcn-number-field__input::-webkit-inner-spin-button,
.bootcn-number-field__input::-webkit-outer-spin-button {
  margin: 0;
  appearance: none;
}

@media (min-width: 768px) {
  .bootcn-number-field__input {
    font-size: var(--bootcn-control-font-size);
  }
}

.bootcn-number-field__step {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  align-self: stretch;
  width: 2.25rem;
  border: 0;
  background: transparent;
  color: var(--bootcn-foreground);
}

.bootcn-number-field__step:hover:not(:disabled) {
  background-color: var(--bootcn-accent);
}

.bootcn-number-field__step:first-child {
  border-right: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: var(--bootcn-radius) 0 0 var(--bootcn-radius);
}

.bootcn-number-field__step:last-child {
  border-left: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: 0 var(--bootcn-radius) var(--bootcn-radius) 0;
}

.bootcn-number-field__step:disabled {
  color: var(--bootcn-muted-foreground);
  opacity: 0.5;
  cursor: not-allowed;
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.numberField) return; // already loaded: idempotent

  const ROOT = '[data-bootcn-number-field]';
  let listening = false;

  function sync(root) {
    const input = root.querySelector('input[type="number"]');
    if (!input) return;
    const value = input.valueAsNumber;
    const min = input.min === '' ? -Infinity : Number(input.min);
    const max = input.max === '' ? Infinity : Number(input.max);
    root.querySelectorAll('[data-bootcn-step]').forEach((button) => {
      const up = button.getAttribute('data-bootcn-step') === 'up';
      button.disabled = !Number.isNaN(value) && (up ? value >= max : value <= min);
    });
  }

  function onClick(event) {
    const button = event.target.closest && event.target.closest('[data-bootcn-step]');
    const root = button && button.closest(ROOT);
    if (!root) return;
    const input = root.querySelector('input[type="number"]');
    if (!input) return;
    if (input.value === '') input.value = input.min || '0';
    else if (button.getAttribute('data-bootcn-step') === 'up') input.stepUp();
    else input.stepDown();
    input.dispatchEvent(new Event('input', { bubbles: true }));
    input.dispatchEvent(new Event('change', { bubbles: true }));
  }

  function onInput(event) {
    const root = event.target.closest && event.target.closest(ROOT);
    if (root) sync(root);
  }

  function init(root = document) {
    root.querySelectorAll(ROOT).forEach(sync);
    if (listening) return;
    document.addEventListener('click', onClick);
    document.addEventListener('input', onInput);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('click', onClick);
    document.removeEventListener('input', onInput);
    listening = false;
  }

  ns.numberField = { init: init, destroy: destroy };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => init());
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.