@alikhalilll/a-tel-input 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +585 -72
- package/dist/_chunks/types.d.ts +661 -0
- package/dist/_chunks/types.js +52 -0
- package/dist/_chunks/types.js.map +1 -0
- package/dist/_chunks/usePhoneValidation.js +539 -0
- package/dist/_chunks/usePhoneValidation.js.map +1 -0
- package/dist/index.cjs +13859 -1240
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +120 -585
- package/dist/index.d.ts +120 -585
- package/dist/index.js +13752 -1113
- package/dist/index.js.map +1 -1
- package/dist/styles.css +3 -2
- package/dist/vee-validate/index.cjs +113 -0
- package/dist/vee-validate/index.cjs.map +1 -0
- package/dist/vee-validate/index.d.cts +86 -0
- package/dist/vee-validate/index.d.ts +86 -0
- package/dist/vee-validate/index.js +112 -0
- package/dist/vee-validate/index.js.map +1 -0
- package/dist/zod/index.cjs +211 -0
- package/dist/zod/index.cjs.map +1 -0
- package/dist/zod/index.d.cts +65 -0
- package/dist/zod/index.d.ts +65 -0
- package/dist/zod/index.js +208 -0
- package/dist/zod/index.js.map +1 -0
- package/package.json +41 -6
- package/src/components/ACountrySelect.vue +79 -1
- package/src/components/ATelInput.vue +206 -66
- package/src/composables/useCountryDetection.ts +28 -11
- package/src/composables/useCountryMatching.ts +160 -20
- package/src/composables/useCountrySelection.ts +71 -0
- package/src/composables/usePhoneValidation.ts +81 -18
- package/src/composables/useSyncedModel.ts +80 -0
- package/src/composables/useTelInputValidation.ts +50 -11
- package/src/index.ts +2 -0
- package/src/types.ts +80 -0
- package/src/vee-validate/index.ts +2 -0
- package/src/vee-validate/useTelField.ts +202 -0
- package/src/zod/index.ts +259 -0
- package/web-types.json +44 -1
package/README.md
CHANGED
|
@@ -1,41 +1,154 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@alikhalilll/a-tel-input`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
number, and offers a country picker that's a **popover on desktop / drawer on mobile**. RTL, i18n,
|
|
7
|
-
and alternative-numeral (Arabic-Indic, Persian, Devanagari, Bengali) input all work out of the box.
|
|
3
|
+
> A headless, shadcn-vue style **international telephone input** for Vue 3 / Nuxt 3+.
|
|
4
|
+
> Country auto-detect · libphonenumber-js validation · responsive picker (popover ⇆ drawer) ·
|
|
5
|
+
> RTL & i18n ready · first-class VeeValidate + Zod integration · server-side validation hook.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
[](https://www.npmjs.com/package/@alikhalilll/a-tel-input)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
[](https://www.npmjs.com/package/@alikhalilll/a-tel-input)
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# pnpm
|
|
13
|
+
pnpm add @alikhalilll/a-tel-input
|
|
14
|
+
|
|
15
|
+
# npm
|
|
16
|
+
npm install @alikhalilll/a-tel-input
|
|
17
|
+
|
|
18
|
+
# yarn
|
|
19
|
+
yarn add @alikhalilll/a-tel-input
|
|
20
|
+
|
|
21
|
+
# bun
|
|
22
|
+
bun add @alikhalilll/a-tel-input
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```vue
|
|
26
|
+
<!-- Single E.164 string — what VeeValidate's <Field v-slot="{ field }"> + native forms expect -->
|
|
27
|
+
<ATelInput v-model="phone" default-country="SA" show-validation />
|
|
28
|
+
|
|
29
|
+
<!-- Or split phone + country into two v-models -->
|
|
30
|
+
<ATelInput v-model:phone="phone" v-model:country="country" default-country="SA" show-validation />
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Why this component
|
|
36
|
+
|
|
37
|
+
- **Universal country detection** — debounced parse against the **full libphonenumber
|
|
38
|
+
metadata (~250 countries)**. Works with international format (`+201066105963`) AND
|
|
39
|
+
local format (`01066105963`), with NANP disambiguation and a hint-priority chain
|
|
40
|
+
(env → current → recents → popular → all). No "only the popular countries" caveats.
|
|
41
|
+
- **Validates and formats** — error reasons, format hint, E.164 output, every keystroke.
|
|
42
|
+
- **Responsive surface** — popover on desktop, bottom-sheet drawer on mobile, sticky-safe
|
|
43
|
+
scroll lock on **both**. The page underneath never scrolls; the inner picker list does.
|
|
44
|
+
- **Headless slots for every region** — trigger, chevron, flag, item, search, hint,
|
|
45
|
+
error. Restyle the field down to the pixel without forking the logic.
|
|
46
|
+
- **First-class form-library integration** — controlled `error` prop, `@blur` event,
|
|
47
|
+
`useTelField()` composable for VeeValidate, `zPhone()` factory for Zod schemas, and a
|
|
48
|
+
`validating` spinner for async server-side checks ("is this number already registered?").
|
|
49
|
+
- **Two binding contracts, your pick** — single default `v-model` (E.164 string, drops
|
|
50
|
+
into VeeValidate's `<Field v-slot="{ field }">` via `v-bind="field"`), or split
|
|
51
|
+
`v-model:phone` + `v-model:country`. Both stay in sync.
|
|
52
|
+
- **i18n + RTL out of the box** — country names localised via `Intl.DisplayNames`,
|
|
53
|
+
alternative numerals (Arabic-Indic, Persian, Devanagari, Bengali) folded to ASCII on
|
|
54
|
+
input, RTL inherited from the page or forced via `dir`.
|
|
55
|
+
- **Efficient by default** — REST Countries fetch + IP geolocation request deduped to
|
|
56
|
+
one network call per page across every `<ATelInput>` / `<ACountrySelect>` /
|
|
57
|
+
`useTelField()` / `zPhone()` instance. LRU-cached matcher. `FALLBACK_COUNTRIES`
|
|
58
|
+
pre-seeded into the lookup indexes so detection works synchronously from first paint.
|
|
59
|
+
- **SSR-safe** — country detection runs only after mount, hydration is clean.
|
|
60
|
+
- **TypeScript-first** — every prop, slot, and event fully typed; web-types ship for
|
|
61
|
+
JetBrains IDEs.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Table of contents
|
|
66
|
+
|
|
67
|
+
- [Install](#install)
|
|
68
|
+
- [Quick start](#quick-start)
|
|
69
|
+
- [Form integration](#form-integration)
|
|
70
|
+
- [VeeValidate + Zod](#veevalidate--zod)
|
|
71
|
+
- [Server-side validation](#server-side-validation-is-this-phone-already-registered)
|
|
72
|
+
- [Native HTML forms](#native-html-forms)
|
|
73
|
+
- [API reference](#api-reference)
|
|
74
|
+
- [Props](#props)
|
|
75
|
+
- [Events](#events)
|
|
76
|
+
- [Slots](#slots)
|
|
77
|
+
- [Exposed methods](#exposed-methods)
|
|
78
|
+
- [Composables](#composables)
|
|
79
|
+
- [Theming](#theming)
|
|
80
|
+
- [Accessibility](#accessibility)
|
|
81
|
+
- [Auto-import](#auto-import)
|
|
82
|
+
- [SSR](#ssr)
|
|
83
|
+
- [TypeScript](#typescript)
|
|
84
|
+
- [Browser support](#browser-support)
|
|
85
|
+
- [Troubleshooting](#troubleshooting)
|
|
86
|
+
- [License](#license)
|
|
87
|
+
|
|
88
|
+
---
|
|
10
89
|
|
|
11
90
|
## Install
|
|
12
91
|
|
|
92
|
+
Pick your package manager:
|
|
93
|
+
|
|
13
94
|
```bash
|
|
95
|
+
# pnpm
|
|
14
96
|
pnpm add @alikhalilll/a-tel-input
|
|
15
|
-
```
|
|
16
97
|
|
|
17
|
-
|
|
18
|
-
|
|
98
|
+
# npm
|
|
99
|
+
npm install @alikhalilll/a-tel-input
|
|
19
100
|
|
|
20
|
-
|
|
101
|
+
# yarn
|
|
102
|
+
yarn add @alikhalilll/a-tel-input
|
|
21
103
|
|
|
22
|
-
|
|
104
|
+
# bun
|
|
105
|
+
bun add @alikhalilll/a-tel-input
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**One CSS import — everything in.** The popover, drawer, and design tokens are all
|
|
109
|
+
bundled into the same stylesheet:
|
|
23
110
|
|
|
24
111
|
```ts
|
|
25
|
-
import '@alikhalilll/a-popover/styles.css';
|
|
26
|
-
import '@alikhalilll/a-drawer/styles.css';
|
|
27
112
|
import '@alikhalilll/a-tel-input/styles.css';
|
|
28
113
|
```
|
|
29
114
|
|
|
30
|
-
|
|
115
|
+
No separate `a-popover` / `a-drawer` / `a-ui-base` imports needed.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Quick start
|
|
120
|
+
|
|
121
|
+
The component supports **two binding contracts** — pick whichever fits your form code:
|
|
122
|
+
|
|
123
|
+
### Single v-model (E.164 string)
|
|
124
|
+
|
|
125
|
+
The friendliest with VeeValidate's `<Field v-slot="{ field }">`, native HTML `<form>`
|
|
126
|
+
submission, and anything else that expects one canonical value:
|
|
127
|
+
|
|
128
|
+
```vue
|
|
129
|
+
<script setup lang="ts">
|
|
130
|
+
import { ref } from 'vue';
|
|
131
|
+
import { ATelInput } from '@alikhalilll/a-tel-input';
|
|
132
|
+
|
|
133
|
+
const phone = ref(''); // → '+201066105963'
|
|
134
|
+
</script>
|
|
135
|
+
|
|
136
|
+
<template>
|
|
137
|
+
<ATelInput v-model="phone" default-country="SA" show-validation />
|
|
138
|
+
</template>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Split `v-model:phone` + `v-model:country`
|
|
142
|
+
|
|
143
|
+
When you want the raw national digits and the dial code as separate values:
|
|
31
144
|
|
|
32
145
|
```vue
|
|
33
146
|
<script setup lang="ts">
|
|
34
147
|
import { ref } from 'vue';
|
|
35
148
|
import { ATelInput } from '@alikhalilll/a-tel-input';
|
|
36
149
|
|
|
37
|
-
const phone = ref('');
|
|
38
|
-
const country = ref<number | null>(null);
|
|
150
|
+
const phone = ref(''); // → '1066105963'
|
|
151
|
+
const country = ref<number | null>(null); // → 20 (the dial code as a number)
|
|
39
152
|
</script>
|
|
40
153
|
|
|
41
154
|
<template>
|
|
@@ -43,82 +156,482 @@ const country = ref<number | null>(null);
|
|
|
43
156
|
</template>
|
|
44
157
|
```
|
|
45
158
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
159
|
+
| Binding | Type | Carries |
|
|
160
|
+
| ----------------- | ---------------- | --------------------------------------------------------------------- |
|
|
161
|
+
| `v-model` | `string` | Full E.164 string (`'+201066105963'`). Empty when invalid / blank. |
|
|
162
|
+
| `v-model:phone` | `string` | Digits-only national number (no `+`, no spaces). |
|
|
163
|
+
| `v-model:country` | `number \| null` | Dial-digit number (e.g. `20` for Egypt, `1` for NANP). `null` ≈ none. |
|
|
164
|
+
|
|
165
|
+
> The two contracts stay in sync — you can mix them, but most apps pick one and stick with it.
|
|
166
|
+
|
|
167
|
+
The picker is **hidden by default** until a leading dial code is detected from typing —
|
|
168
|
+
pass `default-country` to show it pre-selected, or `:detect-from-input="false"` for the
|
|
169
|
+
legacy always-visible picker.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Form integration
|
|
174
|
+
|
|
175
|
+
`@alikhalilll/a-tel-input` ships two thin subpath entries so the same validation engine
|
|
176
|
+
that powers the in-field UI is also available to your form layer:
|
|
177
|
+
|
|
178
|
+
- **`@alikhalilll/a-tel-input/vee-validate`** — `useTelField()` composable.
|
|
179
|
+
- **`@alikhalilll/a-tel-input/zod`** — `zPhone()` / `zPhoneObject()` schema factories.
|
|
180
|
+
|
|
181
|
+
Both `vee-validate` and `zod` are **optional peer dependencies** — install them yourself.
|
|
182
|
+
|
|
183
|
+
### Drop-in `<Field v-slot="{ field, errors }">` pattern
|
|
184
|
+
|
|
185
|
+
If you're already using VeeValidate's slot-style fields, **`v-bind="field"` just works**.
|
|
186
|
+
ATelInput's default `v-model` is the E.164 string, and Vue auto-spreads
|
|
187
|
+
`field.modelValue` + `field['onUpdate:modelValue']` + `field.name` + `field.onBlur`
|
|
188
|
+
from the slot prop directly onto the component:
|
|
189
|
+
|
|
190
|
+
```vue
|
|
191
|
+
<script setup lang="ts">
|
|
192
|
+
import { useForm, Field as VeeField } from 'vee-validate';
|
|
193
|
+
import { toTypedSchema } from '@vee-validate/zod';
|
|
194
|
+
import { z } from 'zod';
|
|
195
|
+
import { ATelInput } from '@alikhalilll/a-tel-input';
|
|
196
|
+
import { zPhone } from '@alikhalilll/a-tel-input/zod';
|
|
197
|
+
|
|
198
|
+
const { handleSubmit } = useForm({
|
|
199
|
+
validationSchema: toTypedSchema(z.object({ phone: zPhone() })),
|
|
200
|
+
});
|
|
201
|
+
</script>
|
|
202
|
+
|
|
203
|
+
<template>
|
|
204
|
+
<form @submit="handleSubmit(onSubmit)">
|
|
205
|
+
<VeeField v-slot="{ field, errors }" name="phone">
|
|
206
|
+
<label for="phone">Phone</label>
|
|
207
|
+
<ATelInput
|
|
208
|
+
id="phone"
|
|
209
|
+
v-bind="field"
|
|
210
|
+
:error="errors[0]"
|
|
211
|
+
:aria-invalid="!!errors.length"
|
|
212
|
+
default-country="SA"
|
|
213
|
+
show-validation
|
|
214
|
+
/>
|
|
215
|
+
</VeeField>
|
|
216
|
+
<button type="submit">Submit</button>
|
|
217
|
+
</form>
|
|
218
|
+
</template>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
That's it — no `useTelField()`, no manual wiring, no `handleBlur` to forward. `field`
|
|
222
|
+
provides everything; `:error="errors[0]"` surfaces the first error message in the
|
|
223
|
+
existing error region.
|
|
224
|
+
|
|
225
|
+
> Prefer `useTelField()` (below) when you also need async / server-side validation in
|
|
226
|
+
> flight, or when you want the helper to manage `defaultCountry` for you.
|
|
227
|
+
|
|
228
|
+
### VeeValidate + Zod (with useTelField)
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
# pnpm
|
|
232
|
+
pnpm add vee-validate @vee-validate/zod zod
|
|
233
|
+
|
|
234
|
+
# npm
|
|
235
|
+
npm install vee-validate @vee-validate/zod zod
|
|
236
|
+
|
|
237
|
+
# yarn
|
|
238
|
+
yarn add vee-validate @vee-validate/zod zod
|
|
239
|
+
|
|
240
|
+
# bun
|
|
241
|
+
bun add vee-validate @vee-validate/zod zod
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { useForm } from 'vee-validate';
|
|
246
|
+
import { toTypedSchema } from '@vee-validate/zod';
|
|
247
|
+
import { z } from 'zod';
|
|
248
|
+
import { useTelField } from '@alikhalilll/a-tel-input/vee-validate';
|
|
249
|
+
import { zPhone } from '@alikhalilll/a-tel-input/zod';
|
|
250
|
+
|
|
251
|
+
const { handleSubmit } = useForm({
|
|
252
|
+
validationSchema: toTypedSchema(z.object({ phone: zPhone() })),
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const { phone, country, error, handleBlur, fieldProps } = useTelField('phone', {
|
|
256
|
+
validateOn: 'blur',
|
|
257
|
+
defaultCountry: 'SA',
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
```vue
|
|
262
|
+
<form @submit="handleSubmit(onSubmit)">
|
|
263
|
+
<ATelInput
|
|
264
|
+
v-model:phone="phone"
|
|
265
|
+
v-model:country="country"
|
|
266
|
+
v-bind="fieldProps"
|
|
267
|
+
:error="error"
|
|
268
|
+
@blur="handleBlur"
|
|
269
|
+
/>
|
|
270
|
+
<button type="submit">Submit</button>
|
|
271
|
+
</form>
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
`useTelField` composes the digits-only `phone` + the dial-code `country` into an E.164
|
|
275
|
+
string under the hood, and feeds **that** to VeeValidate's schema — so your Zod schema
|
|
276
|
+
validates a single canonical value while the component still binds to two v-models.
|
|
277
|
+
|
|
278
|
+
### Server-side validation ("is this phone already registered?")
|
|
279
|
+
|
|
280
|
+
> **Important** — VeeValidate **ignores field-level `rules`** when `useForm` is given a
|
|
281
|
+
> `validationSchema`. To run an async server check, chain it onto the schema itself via
|
|
282
|
+
> `z.refine(async)`. `handleSubmit` awaits the schema, and `meta.pending` (which drives
|
|
283
|
+
> `useTelField`'s `validating` ref → the in-field spinner) follows the schema's async work.
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
import { useForm } from 'vee-validate';
|
|
287
|
+
import { toTypedSchema } from '@vee-validate/zod';
|
|
288
|
+
import { z } from 'zod';
|
|
289
|
+
import { useTelField } from '@alikhalilll/a-tel-input/vee-validate';
|
|
290
|
+
import { zPhone } from '@alikhalilll/a-tel-input/zod';
|
|
291
|
+
|
|
292
|
+
// Build the schema: sync zPhone() first (cheap — runs locally via libphonenumber-js),
|
|
293
|
+
// then an async refine that hits your server. Refines run AFTER the parent passes, so
|
|
294
|
+
// the server is only contacted when the value is syntactically valid.
|
|
295
|
+
const phoneSchema = zPhone().refine(
|
|
296
|
+
async (value) => {
|
|
297
|
+
if (!value) return true;
|
|
298
|
+
const { exists } = await $fetch('/api/phone/exists', { query: { phone: value } });
|
|
299
|
+
return !exists;
|
|
300
|
+
},
|
|
301
|
+
{ message: 'This phone number is already registered.' }
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
const { handleSubmit } = useForm({
|
|
305
|
+
validationSchema: toTypedSchema(z.object({ phone: phoneSchema })),
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const { phone, country, error, handleBlur, fieldProps, validating } = useTelField('phone', {
|
|
309
|
+
validateOn: 'blur',
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
```vue
|
|
314
|
+
<ATelInput
|
|
315
|
+
v-model:phone="phone"
|
|
316
|
+
v-model:country="country"
|
|
317
|
+
v-bind="fieldProps"
|
|
318
|
+
:error="error"
|
|
319
|
+
:validating="validating"
|
|
320
|
+
show-validation
|
|
321
|
+
@blur="handleBlur"
|
|
322
|
+
/>
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
- `error` displays the server message in the existing error region.
|
|
326
|
+
- `validating` is `true` while the request is in flight — renders a small spinner inside
|
|
327
|
+
the field and sets `aria-busy="true"`. It does **not** disable the input.
|
|
328
|
+
- `handleSubmit` awaits the async refine before invoking your callback, so a failing
|
|
329
|
+
server check blocks submission automatically.
|
|
330
|
+
|
|
331
|
+
### Native HTML forms
|
|
332
|
+
|
|
333
|
+
```vue
|
|
334
|
+
<form>
|
|
335
|
+
<ATelInput v-model:phone="phone" v-model:country="country" name="phone" />
|
|
336
|
+
</form>
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
`name` is forwarded to the inner `<input>` so `FormData` picks the value up. The submitted
|
|
340
|
+
value is the digits-only national number — compose the E.164 with `usePhoneValidation()`
|
|
341
|
+
in your submit handler if you want the international form.
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## API reference
|
|
346
|
+
|
|
347
|
+
### Props
|
|
348
|
+
|
|
349
|
+
| Prop | Type | Default | Description |
|
|
350
|
+
| ---------------------- | -------------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
351
|
+
| `modelValue` | `string` | `''` | Default `v-model` — full **E.164** string (`'+201066105963'`). Drops directly into VeeValidate's `<Field v-slot="{ field }">` via `v-bind="field"`. |
|
|
352
|
+
| `phone` | `string` | `''` | `v-model:phone` — digits-only national number. |
|
|
353
|
+
| `country` | `number \| null` | `null` | `v-model:country` — selected dial-digit number (e.g. `20`). |
|
|
354
|
+
| `name` | `string` | — | Forwarded to the inner `<input name="">` for native form submission / form libraries. |
|
|
355
|
+
| `error` | `string \| null` | — | Externally controlled error message. When non-empty, overrides internal validation. |
|
|
356
|
+
| `validating` | `boolean` | `false` | `true` while an async validation is in flight. Renders a spinner inside the field. |
|
|
357
|
+
| `validateOn` | `'change' \| 'blur' \| 'eager'` | `'change'` | When to surface validation in the UI. |
|
|
358
|
+
| `defaultCountry` | `string` | — | Initial country — ISO2 (`'EG'`) or dial code (`'20'` / `'+20'`). |
|
|
359
|
+
| `detectCountry` | `DetectionStrategy` | `'auto'` | Silent country hint chain: IP → timezone → `navigator.language`. |
|
|
360
|
+
| `detectFromInput` | `boolean` | `true` | Reveal the picker on first dial-code match while typing. |
|
|
361
|
+
| `detectDebounceMs` | `number` | `800` | Debounce window for `detectFromInput`. |
|
|
362
|
+
| `allowedDialCodes` | `string[]` | — | Whitelist of dial codes; others render disabled. |
|
|
363
|
+
| `size` | `'xs' \| 'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Control size — mirrors the shared `Size` scale. |
|
|
364
|
+
| `dir` | `'ltr' \| 'rtl' \| 'auto'` | `'auto'` | Text direction (inherits from the page by default). |
|
|
365
|
+
| `locale` | `string` | — | BCP-47 locale — localises country names + numerals in hints. |
|
|
366
|
+
| `messages` | `TelInputMessagesInput` | — | Bag of every UI string; merged onto English defaults. |
|
|
367
|
+
| `showValidation` | `boolean` | `false` | Colour the field border + error line by validity. |
|
|
368
|
+
| `showValidationIcon` | `boolean` | `false` | Show the valid / error icon at the field end. |
|
|
369
|
+
| `disabled` / `loading` | `boolean` | `false` | Field state. |
|
|
370
|
+
| `placeholder` | `string` | derived | Falls back to the country's `format_hint` when empty. |
|
|
371
|
+
| `flagUrl` | `(iso2, w) => string` | flagcdn | Override the flag image source. |
|
|
372
|
+
| `countries` | `CountryOption[]` | REST API | Provide your own country list (bypasses the REST Countries fetch). |
|
|
373
|
+
| `searcher` | `(q, c) => boolean` | substring | Custom search predicate. |
|
|
374
|
+
| `detector` | `async (opts) => string \| null` | built-in | Fully custom country detection. |
|
|
375
|
+
| `ipEndpoint` | `string` | `ipapi.co` | Override the IP geolocation endpoint. |
|
|
376
|
+
| `scrollLock` | `'events' \| 'body' \| 'none'` | `'events'` | How page-scroll is blocked while the picker is open. Applies on both desktop and mobile. |
|
|
377
|
+
| Class hooks | `string` | — | `class`, `fieldClass`, `inputClass`, `contentClass`, `popoverClass`, `drawerClass`, `hintClass`, `errorClass`. |
|
|
378
|
+
| Localised strings | `string` | — | `searchPlaceholder`, `emptyText`, `loadingText`, `errorMessages`. |
|
|
379
|
+
|
|
380
|
+
> The full prop / type reference (with every default and every JSDoc note) lives in
|
|
381
|
+
> [`src/types.ts`](./src/types.ts) and is published as part of the package types.
|
|
78
382
|
|
|
79
383
|
### Events
|
|
80
384
|
|
|
81
|
-
| Event
|
|
82
|
-
|
|
|
83
|
-
| `update:
|
|
84
|
-
| `update:
|
|
385
|
+
| Event | Payload | Fires when |
|
|
386
|
+
| ------------------- | ---------------- | ---------------------------------------------------------- |
|
|
387
|
+
| `update:modelValue` | `string` | Composed E.164 string changed (the default `v-model`). |
|
|
388
|
+
| `update:phone` | `string` | Digits-only national number changed. |
|
|
389
|
+
| `update:country` | `number \| null` | Dial-code number changed. |
|
|
390
|
+
| `blur` | `FocusEvent` | Inner input lost focus (also flips internal `hasBlurred`). |
|
|
391
|
+
| `focus` | `FocusEvent` | Inner input gained focus. |
|
|
85
392
|
|
|
86
393
|
### Slots
|
|
87
394
|
|
|
88
|
-
Every visual region is a slot
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
395
|
+
Every visual region is a slot — the component is fully recomposable.
|
|
396
|
+
|
|
397
|
+
| Slot | Props |
|
|
398
|
+
| -------------- | ------------------------------------------- |
|
|
399
|
+
| `prefix` | — |
|
|
400
|
+
| `suffix` | `{ validationState, validation }` |
|
|
401
|
+
| `trigger` | `{ selectedCountry, open, sizeClasses }` |
|
|
402
|
+
| `chevron` | `{ open }` |
|
|
403
|
+
| `flag` | `{ country, context: 'trigger' \| 'item' }` |
|
|
404
|
+
| `item` | `{ country, selected, disabled, select }` |
|
|
405
|
+
| `group-header` | `{ label, group: 'suggested' \| 'all' }` |
|
|
406
|
+
| `search` | `{ value, setValue, isSearching }` |
|
|
407
|
+
| `loading` | — |
|
|
408
|
+
| `empty` | `{ query }` |
|
|
409
|
+
| `detecting` | — (during country detection) |
|
|
410
|
+
| `validating` | — (during async form validation) |
|
|
411
|
+
| `valid-icon` | — |
|
|
412
|
+
| `error-icon` | `{ reason }` |
|
|
413
|
+
| `hint` | `{ country, formatHint, example }` |
|
|
414
|
+
| `error` | `{ message, reason, validation }` |
|
|
415
|
+
|
|
416
|
+
### Exposed methods
|
|
417
|
+
|
|
418
|
+
Reach these via `<ATelInput ref="tel" />` → `tel.value?.<method>()`:
|
|
419
|
+
|
|
420
|
+
| Member | Type | Notes |
|
|
421
|
+
| ------------------------ | ------------------------------------------- | ----------------------------------------------- |
|
|
422
|
+
| `validation` | `ComputedRef<PhoneValidationResult>` | Full validation result. |
|
|
423
|
+
| `required` | `ComputedRef<PhoneRequiredInfo \| null>` | Country format hint + example E.164. |
|
|
424
|
+
| `selectedDialCode` | `ComputedRef<string \| null>` | `+`-prefixed dial code of the selected country. |
|
|
425
|
+
| `validationState` | `ComputedRef<'idle' \| 'valid' \| 'error'>` | Raw state (no typing-pause gating). |
|
|
426
|
+
| `visibleValidationState` | `ComputedRef<'idle' \| 'valid' \| 'error'>` | UI-surfacing state (gated by `validateOn`). |
|
|
427
|
+
| `isDetecting` | `Readonly<Ref<boolean>>` | `true` during the first debounce window. |
|
|
428
|
+
| `hasFinishedTyping` | `Readonly<Ref<boolean>>` | Flips after the debounce settles. |
|
|
429
|
+
| `detectionAttempted` | `Readonly<Ref<boolean>>` | `true` after at least one detection pass. |
|
|
430
|
+
| `focus(options?)` | `() => void` | Focus the inner `<input>`. |
|
|
431
|
+
| `blur()` | `() => void` | Blur the inner `<input>`. |
|
|
432
|
+
| `select()` | `() => void` | Select the inner `<input>`'s text. |
|
|
433
|
+
|
|
434
|
+
### Composables
|
|
435
|
+
|
|
436
|
+
Re-exported from the main entry — compose your own field from the same primitives the
|
|
437
|
+
component uses.
|
|
438
|
+
|
|
439
|
+
| Symbol | Source path | Purpose |
|
|
440
|
+
| ------------------------- | --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
441
|
+
| `usePhoneValidation` | `@alikhalilll/a-tel-input` | The libphonenumber-js wrapper — `validate`, `getRequiredInfo`, `searchCountries`, `getCountryByValue`, `getCountriesByDial`. |
|
|
442
|
+
| `useCountryMatching` | `@alikhalilll/a-tel-input` | Longest-prefix dial-code matching with tier-3 NANP tie-break. |
|
|
443
|
+
| `detectCountry` | `@alikhalilll/a-tel-input` | The IP → timezone → locale → default chain (callable standalone). |
|
|
444
|
+
| `useTypingPhase` | `@alikhalilll/a-tel-input` | Debounced typing-pause state machine. |
|
|
445
|
+
| `useTelInputValidation` | `@alikhalilll/a-tel-input` | View-layer facade (visible state, error message, show flags). |
|
|
446
|
+
| `useCountrySelection` | `@alikhalilll/a-tel-input` | Picker selection state machine (`iso2` + `source` enum + `detectionLocked`). The single state machine the component uses internally — useful when composing your own field. |
|
|
447
|
+
| `useSyncedModel` | `@alikhalilll/a-tel-input` | Generic bidirectional sync between a `defineModel` ref and internal state, with the echo-loop guard built in. Reusable in any v-model bridge. |
|
|
448
|
+
| `useTelField` | `@alikhalilll/a-tel-input/vee-validate` | VeeValidate adapter — see [Form integration](#form-integration). |
|
|
449
|
+
| `zPhone` / `zPhoneObject` | `@alikhalilll/a-tel-input/zod` | Zod schema factories — see [Form integration](#form-integration). |
|
|
450
|
+
| `normalizeDigits` | `@alikhalilll/a-tel-input` | Fold Arabic-Indic / Persian / Devanagari / Bengali numerals → ASCII. |
|
|
451
|
+
| `defaultFlagUrl` | `@alikhalilll/a-tel-input` | Default flagcdn URL builder. |
|
|
452
|
+
|
|
453
|
+
---
|
|
454
|
+
|
|
455
|
+
## Theming
|
|
456
|
+
|
|
457
|
+
The component renders with neutral defaults and reads global design tokens — restyle from
|
|
458
|
+
your app's stylesheet without touching the component itself.
|
|
459
|
+
|
|
460
|
+
### CSS custom properties
|
|
461
|
+
|
|
462
|
+
Set these on `:root` (or any ancestor) to retint the component:
|
|
463
|
+
|
|
464
|
+
| Token | Used for |
|
|
465
|
+
| -------------------------- | ------------------------------------- |
|
|
466
|
+
| `--ak-ui-background` | Field + popover/drawer background. |
|
|
467
|
+
| `--ak-ui-foreground` | Field + popover/drawer text. |
|
|
468
|
+
| `--ak-ui-input` | Field border. |
|
|
469
|
+
| `--ak-ui-ring` | Focus ring colour. |
|
|
470
|
+
| `--ak-ui-muted-foreground` | Dial prefix, hint text, placeholder. |
|
|
471
|
+
| `--ak-ui-destructive` | Error border / ring / icon / message. |
|
|
472
|
+
| `--ak-ui-radius` | Field corner radius. |
|
|
473
|
+
|
|
474
|
+
Valid / error tints (green / red) read literal values — override via the class hooks below.
|
|
475
|
+
|
|
476
|
+
### Class hooks
|
|
477
|
+
|
|
478
|
+
Each visual region accepts a class prop you can target with utility classes (Tailwind,
|
|
479
|
+
your own utility framework, or plain CSS):
|
|
92
480
|
|
|
93
|
-
|
|
481
|
+
| Prop | Targets |
|
|
482
|
+
| -------------- | -------------------------------------------------------------------------- |
|
|
483
|
+
| `class` | The root wrapper (`.a-tel-input`). |
|
|
484
|
+
| `fieldClass` | The field row that contains input + dial + picker (`.a-tel-input__field`). |
|
|
485
|
+
| `inputClass` | The actual `<input>` element. |
|
|
486
|
+
| `hintClass` | The hint paragraph below the field. |
|
|
487
|
+
| `errorClass` | The error paragraph below the field. |
|
|
488
|
+
| `popoverClass` | The desktop popover surface. |
|
|
489
|
+
| `drawerClass` | The mobile drawer surface. |
|
|
490
|
+
| `contentClass` | Both branches (applied alongside `popoverClass` / `drawerClass`). |
|
|
94
491
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
492
|
+
### Stateful selectors
|
|
493
|
+
|
|
494
|
+
- `[data-state="idle" | "valid" | "error"]` on the root and on `.a-tel-input__field`.
|
|
495
|
+
- `[data-size="xs" | "sm" | "md" | "lg" | "xl"]` on the root.
|
|
496
|
+
- `[data-show-validation]` on the root when `showValidation` is on.
|
|
497
|
+
- `[aria-invalid="true"]` on the input when the surfaced state is error.
|
|
498
|
+
- `[aria-busy="true"]` on the input when `validating` is true.
|
|
499
|
+
|
|
500
|
+
### Dark mode
|
|
501
|
+
|
|
502
|
+
Light / dark is driven entirely by the `--ak-ui-*` tokens — set them to dark values under
|
|
503
|
+
`[data-theme="dark"]` (or however your app gates dark mode) and everything follows.
|
|
504
|
+
|
|
505
|
+
---
|
|
506
|
+
|
|
507
|
+
## Accessibility
|
|
508
|
+
|
|
509
|
+
- `aria-label` on the inner `<input>` (overrideable via `messages.phoneInputLabel`).
|
|
510
|
+
- `aria-invalid="true"` mirrors the surfaced error state.
|
|
511
|
+
- `aria-describedby` points at the hint / error line whenever it has content; the line is
|
|
512
|
+
an `aria-live="polite"` region so screen readers announce new errors.
|
|
513
|
+
- `aria-errormessage` points at the same line when the field is in error.
|
|
514
|
+
- `aria-busy="true"` on the input while `validating` is on.
|
|
515
|
+
- Country picker is keyboard-navigable — arrow keys, `/` to focus search, Enter to pick,
|
|
516
|
+
Esc to close.
|
|
517
|
+
- Focus management is handled by the underlying popover/drawer — focus returns to the
|
|
518
|
+
trigger on close.
|
|
519
|
+
|
|
520
|
+
---
|
|
99
521
|
|
|
100
522
|
## Auto-import
|
|
101
523
|
|
|
102
|
-
|
|
524
|
+
### Nuxt
|
|
103
525
|
|
|
104
526
|
```ts
|
|
105
527
|
export default defineNuxtConfig({
|
|
106
528
|
modules: ['@alikhalilll/a-tel-input/nuxt'],
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
'@alikhalilll/a-drawer/styles.css',
|
|
110
|
-
'@alikhalilll/a-tel-input/styles.css',
|
|
111
|
-
],
|
|
529
|
+
// The single tel-input stylesheet already bundles popover + drawer styles + design tokens.
|
|
530
|
+
css: ['@alikhalilll/a-tel-input/styles.css'],
|
|
112
531
|
});
|
|
113
532
|
```
|
|
114
533
|
|
|
115
|
-
|
|
534
|
+
### Vite + `unplugin-vue-components`
|
|
535
|
+
|
|
536
|
+
Register `@alikhalilll/a-tel-input/resolver`:
|
|
537
|
+
|
|
538
|
+
```ts
|
|
539
|
+
import Components from 'unplugin-vue-components/vite';
|
|
540
|
+
import { ATelInputResolver } from '@alikhalilll/a-tel-input/resolver';
|
|
541
|
+
|
|
542
|
+
export default { plugins: [Components({ resolvers: [ATelInputResolver()] })] };
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
---
|
|
116
546
|
|
|
117
547
|
## SSR
|
|
118
548
|
|
|
119
|
-
Country detection runs only
|
|
120
|
-
(or empty), and IP/timezone/locale
|
|
549
|
+
Country detection runs **only inside `onMounted`** — the field renders immediately with
|
|
550
|
+
`defaultCountry` (or empty) on the server, and the IP / timezone / locale chain patches
|
|
551
|
+
in on hydration. There are no SSR network calls, and `useEventScrollLock` short-circuits
|
|
552
|
+
when `document` is unavailable.
|
|
553
|
+
|
|
554
|
+
If `default-country` is set, the picker is visible at first paint and hydration is a
|
|
555
|
+
no-op visually. If you rely on `detect-from-input`, the picker stays hidden until the
|
|
556
|
+
client-side parser sees a leading dial code — also hydration-safe.
|
|
557
|
+
|
|
558
|
+
---
|
|
559
|
+
|
|
560
|
+
## TypeScript
|
|
561
|
+
|
|
562
|
+
Import the public types from the main entry:
|
|
563
|
+
|
|
564
|
+
```ts
|
|
565
|
+
import type {
|
|
566
|
+
ATelInputProps,
|
|
567
|
+
ATelInputEmits,
|
|
568
|
+
ATelInputSlots,
|
|
569
|
+
ATelInputSize,
|
|
570
|
+
ATelInputDir,
|
|
571
|
+
ATelInputValidateOn,
|
|
572
|
+
TelInputMessages,
|
|
573
|
+
TelInputMessagesInput,
|
|
574
|
+
PhoneValidationResult,
|
|
575
|
+
PhoneValidationReason,
|
|
576
|
+
PhoneRequiredInfo,
|
|
577
|
+
CountryOption,
|
|
578
|
+
} from '@alikhalilll/a-tel-input';
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
Slot props are inferable in templates:
|
|
582
|
+
|
|
583
|
+
```vue
|
|
584
|
+
<ATelInput #suffix="{ validationState, validation }"> … </ATelInput>
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
Or in script:
|
|
588
|
+
|
|
589
|
+
```ts
|
|
590
|
+
type SuffixProps = Parameters<NonNullable<ATelInputSlots['suffix']>>[0];
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
---
|
|
594
|
+
|
|
595
|
+
## Browser support
|
|
596
|
+
|
|
597
|
+
Modern evergreen browsers — last two versions of Chrome, Edge, Firefox, Safari, and the
|
|
598
|
+
matching mobile WebViews. Uses `Intl.DisplayNames` for localized country names (universal
|
|
599
|
+
since 2020). No polyfills required.
|
|
600
|
+
|
|
601
|
+
---
|
|
602
|
+
|
|
603
|
+
## Troubleshooting
|
|
604
|
+
|
|
605
|
+
**Why is the picker hidden until I type?**
|
|
606
|
+
`detectFromInput` is on by default — the field starts as a single clean input and the
|
|
607
|
+
picker reveals once a leading dial code is recognised. Pass `default-country="EG"` (or
|
|
608
|
+
any ISO2 / dial-code string) to show it pre-selected, or `:detect-from-input="false"`
|
|
609
|
+
for the legacy always-visible picker.
|
|
610
|
+
|
|
611
|
+
**How do I show validation only after blur?**
|
|
612
|
+
`validateOn="blur"`. Or use `useTelField()` — its `fieldProps` already includes that.
|
|
613
|
+
|
|
614
|
+
**I want a single E.164 value out of the field.**
|
|
615
|
+
Two options. From a form: `useTelField()` tracks the E.164 string as VeeValidate's value
|
|
616
|
+
(see [Form integration](#form-integration)). Without a form: `tellRef.value?.validation.full_phone`.
|
|
617
|
+
|
|
618
|
+
**My Zod schema rejects a valid number.**
|
|
619
|
+
Check `allowedDialCodes` and `country` — `zPhone({ country: 'EG' })` expects the **national**
|
|
620
|
+
digits (`'1066105963'`), while `zPhone()` (no `country`) expects the **E.164** form
|
|
621
|
+
(`'+201066105963'`). Use `zPhoneObject()` if you want to pass `{ phone, country }` directly.
|
|
622
|
+
|
|
623
|
+
**The page underneath the drawer scrolls.**
|
|
624
|
+
That was a bug in versions < 1.1.0 — the event scroll-lock was desktop-only. Upgrade.
|
|
625
|
+
If you need the legacy `body { overflow: hidden }` style instead, set `scroll-lock="body"`.
|
|
626
|
+
|
|
627
|
+
**Country auto-detect didn't work.**
|
|
628
|
+
The default `ipEndpoint` is `https://ipapi.co/json/` — it's free-tier rate-limited.
|
|
629
|
+
Either provide your own endpoint (`ip-endpoint="/api/geo"` returning `{ country_code }`),
|
|
630
|
+
swap the entire chain via the `detector` prop, or disable IP and fall through to timezone:
|
|
631
|
+
`detect-country="locale"`.
|
|
632
|
+
|
|
633
|
+
---
|
|
121
634
|
|
|
122
635
|
## License
|
|
123
636
|
|
|
124
|
-
MIT
|
|
637
|
+
[MIT](./LICENSE) © alikhalilll
|