Inputs
MaskedField
Text input with live formatting for Brazilian documents, phone numbers, CEP and dates.
<script>
import { MaskedField, maskCPF, isValidCPF } from "@codeatlasdev/ui/forms";
let cpf = $state("");
</script>
<MaskedField
label="CPF"
mask={maskCPF}
validate={(v) => (isValidCPF(v) ? undefined : "Invalid CPF")}
bind:value={cpf}
/>
These components live in the @codeatlasdev/ui/forms subpath. A mask is a pure function from raw text to formatted text, so you can write your own. MaskedField renders a TextField and forwards its props. Digit-only masks set inputmode="numeric" automatically.
The package never makes network requests. CepField only calls the onlookup function you inject (for example a ViaCEP client) once 8 digits are typed.
Documents try these values: CPF 123.456.789-09 is valid and 111.111.111-11 is invalid. Validation runs on blur.
Documents and dates
CPF, CNPJ, a mask that switches after 11 digits, a birth date and RG.
Switches mask after 11 digits.
Code
Phone and CEP
PhoneField binds E.164 (+55...) while showing the masked form. CepField resolves an address through onlookup. Try 01310-100.
value = –
Try 01310-100.
–
Code
use:mask action
Apply any pure mask to a plain input. The caret stays on the same digit.
value = –
Code
MaskedField props
| Prop | Type | Default | Description |
|---|---|---|---|
mask | (raw: string) => string | – | Required. Pure formatter applied while typing, for example maskCPF. |
validate | (value: string) => string | undefined | – | Returns an error message on blur for non-empty values, or undefined when valid. |
inputmode | HTMLInputAttributes['inputmode'] | auto | Defaults to 'numeric' for digit-only masks and 'text' otherwise. |
value | string | '' | Bindable masked value (what the user sees). |
error | string | – | External error. Takes precedence over the validate result. |
label | string | – | Label text. |
placeholder | string | – | Input placeholder. |
description | string | – | Hint shown below the field. |
disabled | boolean | false | Disables the input. |
required | boolean | false | Marks the field as required. |
readonly | boolean | false | Makes the input read-only. |
autocomplete | HTMLInputAttributes['autocomplete'] | – | Native autocomplete hint. |
leading / trailing | Snippet | – | Content before or after the input. |
size | 'sm' | 'md' | 'md' | Field height. |
name | string | – | Native name, for form submission. |
oninput / onchange | (value: string) => void | – | Called with the masked value. |
onblur | (e: FocusEvent) => void | – | Called when the input loses focus. |
PhoneField and CepField props
| Prop | Type | Default | Description |
|---|---|---|---|
PhoneField.value | string | '' | Bindable E.164 value (+5511987654321). Empty while the number is incomplete or empty. |
PhoneField.country | 'BR' | 'BR' | Only BR is supported today. |
PhoneField.placeholder | string | '(11) 98765-4321' | Input placeholder. |
CepField.value | string | '' | Bindable masked CEP (01310-100). |
CepField.onlookup | (cep: string) => Promise<CepAddress | null> | – | Injected lookup. Receives 8 digits. Return null for an unknown CEP. |
CepField.onresolve | (address: CepAddress) => void | – | Called with the address after a successful lookup. |
CepField.lookupPending | boolean | false | Bindable. True while onlookup is in flight. |
CepField.placeholder | string | '00000-000' | Input placeholder. |
Exports
| Prop | Type | Default | Description |
|---|---|---|---|
maskCPF / maskCNPJ / maskCPFCNPJ | (raw: string) => string | – | Document masks. maskCPFCNPJ switches after 11 digits. |
maskDateBR / maskRG / maskCEP / maskPhoneBR | (raw: string) => string | – | Date (dd/mm/yyyy), RG, CEP and Brazilian phone masks. |
isValidCPF / isValidCNPJ / isValidDateBR | (value: string) => boolean | – | Validators for use in validate. |
mask | Action<HTMLInputElement, (raw: string) => string> | – | Svelte action that applies a mask to any input. |