Mask input
A text field that formats what is typed against a fixed pattern: a phone number, a date, a product key. The pattern's own characters appear by themselves and the caret stays put when the middle is edited. For a value with no fixed shape use the input; for a date with a calendar use a date picker.
Install
npx shadcn@latest add @thrum-pro/mask-inputThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
mask | string | Required | The pattern: # is a digit, A a letter, * any character, and everything else is written as it stands. A backslash makes the next character literal. |
value | string | – | The text when the parent holds it: hand back the masked text from onValueChange. Raw text is formatted too, as long as it holds none of the pattern's own characters. |
defaultValue | string | – | The text at first, masked or raw, when the field holds it itself. |
onValueChange | (masked: string, raw: string) => void | – | Called on every change with the text as shown and with only the typed characters. |
type | "text" | "tel" | Default "text" | tel suits a phone number. Other input types cannot hold a caret position. |
inputMode | string | – | The on-screen keyboard. It defaults to numeric when every slot of the mask is a digit, and to text otherwise. |
placeholder | string | – | It defaults to the mask with an underscore in each slot: (___) ___-____. |
sound | boolean | Default true | Play a soft completion chime when the last slot of the mask is filled, if the visitor has sound on. |
className | string | – | Extra classes, merged last. The field fills its container; set a width here. |
...props | ComponentProps<"input"> | – | Every other input attribute and event handler: name, id, disabled, aria-invalid, onChange. In a form the field's value is the masked text. |
maskValue() | (value: string, mask: string) => { masked: string; raw: string; complete: boolean } | – | A second export, a pure function: the same formatting for text that is not in a field, and whether every slot is filled. |
Motion and sound
Motion
- The border colour changes over 150 ms when the field becomes invalid or valid again.
- The text is rewritten in the same keystroke: nothing animates.
- Reduced motion: the border changes at once.
Sound
- A two-note rise when the final slot of the mask is filled. Earlier keystrokes are silent.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It is a native input element, so its role, selection and editing keys are the browser's. Name it with a label element.
- Say the expected format in the label or in a description linked with aria-describedby. The default placeholder shows it, and a placeholder is gone once typing starts.
- Pasted text is fitted to the mask: characters that fit no slot are dropped, so a number copied with or without its punctuation gives the same result.
- A character of the pattern typed in its own place counts as the pattern's. So a pattern character that could also fill the next slot, such as the 1 in +1 ###, cannot be typed as the first digit: keep such characters out of a mask.
- While an input method is composing a character the text is left alone, and it is formatted when the composition ends.
- Text is 16 pixels on phones, so the page does not zoom when the field takes focus, and 14 pixels on wider screens.
| Keys | Action |
|---|---|
| Tab | Focus the field. |
| Any character | It goes into the next slot it fits, and the pattern's own characters before that slot are written for you. One that fits no slot is dropped. |
| Backspace / Delete | Remove a character. On one of the pattern's own characters the caret steps over it, and the next press removes a typed one. |
| ← / → | Move the caret. Typing in the middle shifts the rest along the pattern and keeps the caret where you are. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/mask-input.tsx- Packages
- None
- Brings along
- InputSound engine
Changelog
- 1.0.0 · 2026-12-03First release.