npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-digit-mask

v0.7.0

Published

Headless React hook for digit-only masked inputs — phone numbers, cards, dates, PINs and more

Readme

use-digit-mask

npm version npm downloads bundle size license TypeScript

Headless React hooks for digit-only masked inputs — phone numbers, cards, dates, PINs and more.

Unlike traditional masking libraries, it gives you full control over UI while handling all the hard parts: formatting, caret positioning, and parsing.

  • Zero runtime dependencies — only react as a peer
  • Headless — bring your own UI (works with any design system)
  • SSR-safe — no window access during render
  • Form-friendly — works with react-hook-form, Formik, etc.
  • Smart caret — never jumps into invalid positions
  • Dynamic masks — switch masks on the fly
  • Undo / redo history — Ctrl+Z / Ctrl+Y out of the box, configurable depth
  • Phone masking built-in — auto-detect country, E.164 fallback
  • Country selector hook — headless useCountrySelect for building country dropdowns

Install

npm install use-digit-mask

Quick start

Basic mask

import { useState } from 'react';
import { useMask } from 'use-digit-mask';

function CardInput() {
  const [value, setValue] = useState('');

  const { props } = useMask({
    mask: '#### #### #### ####',
    value,
    onChange: (next) => setValue(next),
  });

  return <input {...props} placeholder="Card number" />;
}

Phone mask with auto-detection

import { useState } from 'react';
import { usePhoneMask } from 'use-digit-mask';

function PhoneInput() {
  const [value, setValue] = useState('');

  const { props, id, prefix } = usePhoneMask({
    value,
    onChange: (next) => setValue(next),
  });

  return (
    <div>
      <input {...props} placeholder="Phone number" />
      <span>{id ?? '—'} {prefix}</span>
    </div>
  );
}

Phone with country selector

import { useState } from 'react';
import { usePhoneMask, useCountrySelect } from 'use-digit-mask';

function PhoneWithCountry() {
  const [value, setValue] = useState('');

  const { props, id, candidates, allPlans, selectPlan } = usePhoneMask({
    value,
    onChange: (next) => setValue(next),
  });

  const {
    isOpen, toggle, items, dividerAfter,
    containerRef, searchRef,
    query, setQuery, currentPlan, select,
  } = useCountrySelect({
    allPlans,
    currentId: id,
    onSelect: selectPlan,
    candidates,
    priorityIds: ['US', 'GB', 'RU'],
  });

  return (
    <div>
      <div ref={containerRef}>
        <button onClick={toggle}>
          {currentPlan ? `+${currentPlan.cc}` : '+'}
        </button>

        {isOpen && (
          <ul role="listbox">
            <li><input ref={searchRef} value={query} onChange={e => setQuery(e.target.value)} /></li>
            {items.map((plan, i) => (
              <>
                {i === dividerAfter && <li role="separator" />}
                <li key={plan.id} role="option" onClick={() => select(plan)}>
                  {plan.label} +{plan.cc}
                </li>
              </>
            ))}
          </ul>
        )}
      </div>
      <input {...props} />
    </div>
  );
}

Undo / redo

useMask keeps an edit history. Ctrl+Z / Ctrl+Y (and Cmd+Z / Cmd+Shift+Z on Mac) work out of the box. You can also call api.undo() / api.redo() programmatically and check api.canUndo / api.canRedo to enable/disable custom buttons.

import { useState } from 'react';
import { useMask } from 'use-digit-mask';

function CardInput() {
  const [value, setValue] = useState('');

  const { props, api } = useMask({
    mask: '#### #### #### ####',
    value,
    onChange: (next) => setValue(next),
    historyLimit: 50, // default: 100
  });

  return (
    <div>
      <input {...props} />
      <button onClick={api.undo} disabled={!api.canUndo}>Undo</button>
      <button onClick={api.redo} disabled={!api.canRedo}>Redo</button>
    </div>
  );
}

With react-hook-form

import { useController } from 'react-hook-form';
import { useMask } from 'use-digit-mask';

function PhoneField({ control }) {
  const { field } = useController({ name: 'phone', control });

  const { props } = useMask({
    mask: '+7 (###) ###-##-##',
    value: field.value,
    allowedPrefixes: ['+7', '8'],
    onChange: field.onChange,
  });

  return <input {...props} />;
}

Full API docs

→ franzzzz1.github.io/use-digit-mask

The live demo includes interactive examples and complete API reference for all three hooks: useMask · usePhoneMask · useCountrySelect

License

ISC © Vyacheslav Nesterenko (GitHub: FranzZZz1)