Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

UIComponentsReact

Masked Input

A text field that formats what is typed to a fixed pattern, such as a phone number, and shows the part of the pattern still to fill.

Built on the headless Masked Input.See its API in Docs

Rendered by React 19.3.1

masked-input.tsx

Installation

npx shadcn@4.23.0 add https://hamur.dev/r/react/masked-input.json

It also adds utils. The app needs its setup first: Installation.

Usage

Put a MaskedInputInput and then a MaskedInputGuide inside MaskedInput, and give MaskedInput the mask (9 a digit, a a letter, * either) and the raw value in value or defaultValue. The Guide draws the rest of the template over the Input in the muted colour, each empty slot as placeholderChar, and is hidden while the Input shows a placeholder of the app's; it must come after the Input. Its content replaces the two spans of the template (React: render with the state's typed and remaining; Vue: the default slot { typed, remaining }; Svelte: the children snippet with the state; Solid: a function child; Angular: typed() and remaining() of #guide="uiMaskedInputGuide"). Name the field with a Field label or aria-label. The editing, the caret, the paste and autofill handling, the undo history, the events and the form value come from the headless Masked Input.

import { MaskedInput, MaskedInputGuide, MaskedInputInput } from '@/components/ui/masked-input'

Examples

Default

A phone number field in a Field: (555) 123 typed, the Guide drawing -____, a description.

Its preview and code are at the top of the page.

The Input is the Input's box with tabular figures, so the typed text and the template keep the same widths. The Guide has the Input's border width, padding and font, so its template lines up with the typed text; its typed part takes its width unseen. The Guide's muted colour is at least 4.5:1 against the Input's background in every preset theme, light and dark (WCAG 1.4.3); it is hidden from assistive technology, which reads the Input's value. The Input shows the focus ring (WCAG 2.4.7), and a disabled field draws the Input and the Guide at half opacity. This item takes Base UI's component ID as its slug and one name per Base UI part.

API reference

The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Masked Input's API reference.