@eldrajs/ui 0.3.7 → 0.3.8

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE-country-flag-icons +25 -0
  3. package/README.md +230 -66
  4. package/dist/PhoneInput-CYGpERg4.js +749 -0
  5. package/dist/{VariantPicker-Cr_AkX47.js → VariantPicker-D_Gg80So.js} +88 -2302
  6. package/dist/components/chip/Chip.vue.d.ts +8 -0
  7. package/dist/components/dialog/Dialog.vue.d.ts +8 -0
  8. package/dist/components/drawer/Drawer.vue.d.ts +8 -0
  9. package/dist/components/filter-panel/FilterGroup.vue.d.ts +8 -0
  10. package/dist/components/filter-panel/FilterPanel.vue.d.ts +8 -0
  11. package/dist/components/filter-panel/facets/ColourFacet.vue.d.ts +8 -0
  12. package/dist/components/filter-panel/facets/ListFacet.vue.d.ts +8 -0
  13. package/dist/components/filter-panel/facets/RangeFacet.vue.d.ts +8 -0
  14. package/dist/components/filter-panel/facets/SizeFacet.vue.d.ts +8 -0
  15. package/dist/components/filter-panel/facets/ToggleFacet.vue.d.ts +8 -0
  16. package/dist/components/lightbox/Lightbox.vue.d.ts +8 -0
  17. package/dist/components/nav-menu/NavMenu.vue.d.ts +8 -0
  18. package/dist/components/pagination/Pagination.vue.d.ts +8 -0
  19. package/dist/components/range-slider/RangeSlider.vue.d.ts +8 -0
  20. package/dist/components/search-bar/SearchBar.vue.d.ts +8 -0
  21. package/dist/components/search-modal/SearchModal.vue.d.ts +8 -0
  22. package/dist/components/select/MultiSelect.vue.d.ts +8 -0
  23. package/dist/components/select/Select.vue.d.ts +8 -0
  24. package/dist/components/site-form/SiteForm.vue.d.ts +8 -0
  25. package/dist/components/toast/Toast.vue.d.ts +8 -0
  26. package/dist/components/toast/Toaster.vue.d.ts +8 -0
  27. package/dist/components.css +64 -0
  28. package/dist/index.js +3 -2
  29. package/dist/messages/en-US.d.ts +19 -0
  30. package/dist/messages/is-IS.js +9 -1
  31. package/dist/phone/PhoneFlag.vue.d.ts +6 -0
  32. package/dist/phone/PhoneInput.vue.d.ts +220 -0
  33. package/dist/phone/caret.d.ts +14 -0
  34. package/dist/phone/examples.d.ts +2 -0
  35. package/dist/phone/flags.d.ts +8 -0
  36. package/dist/phone/index.d.ts +13 -0
  37. package/dist/phone/phone.d.ts +62 -0
  38. package/dist/phone/phoneFlagData.d.ts +2 -0
  39. package/dist/phone/rule.d.ts +22 -0
  40. package/dist/phone/types.d.ts +63 -0
  41. package/dist/phone/vee-validate/FieldPhoneInput.vue.d.ts +24 -0
  42. package/dist/phone/vee-validate/index.d.ts +3 -0
  43. package/dist/phone/vee-validate/types.d.ts +3 -0
  44. package/dist/phone/vee-validate.js +96 -0
  45. package/dist/phone.js +1 -0
  46. package/dist/phoneFlagData-D9dDT2uC.js +261 -0
  47. package/dist/resolver.d.ts +9 -2
  48. package/dist/resolver.js +15 -4
  49. package/dist/style.css +64 -0
  50. package/dist/useFieldControl-D_b9SC9s.js +273 -0
  51. package/dist/{useMessages-DOHGfip_.js → useMessages-DQQPh1P_.js} +20 -1
  52. package/dist/usePopover-4CQR2hB5.js +2228 -0
  53. package/dist/utilities.css +37 -0
  54. package/dist/vee-validate.js +6 -273
  55. package/dist/vue-i18n.js +4 -1
  56. package/package.json +19 -2
@@ -0,0 +1,749 @@
1
+ import { $ as useEldraScope, G as FIELD_KEY, P as FIELD_TEXT, W as useEldraUiLocale, X as useControllableModel, c as useOptionList, d as SelectPanel_default, et as joinIds, k as FIELD_RADIUS, t as usePopover, tt as useUiId, u as useListbox } from "./usePopover-4CQR2hB5.js";
2
+ import { i as enUS, r as useMessages } from "./useMessages-DQQPh1P_.js";
3
+ import { Fragment, Teleport, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, inject, mergeProps, nextTick, normalizeClass, onMounted, openBlock, ref, renderSlot, shallowRef, toDisplayString, toValue, unref, watch, withCtx } from "vue";
4
+ import { comparator } from "@eldrajs/sdk/format";
5
+ import { AsYouType, Metadata, getCountries, getCountryCallingCode, getExampleNumber, isSupportedCountry, parseIncompletePhoneNumber, parsePhoneNumberFromString } from "libphonenumber-js/min";
6
+ import examples from "libphonenumber-js/mobile/examples";
7
+ import { displayNames } from "@eldrajs/sdk/format/display-names";
8
+ // src/phone/caret.ts
9
+ /**
10
+ * Caret bookkeeping for a field that is re-spaced on every keystroke. Writing an input's `value`
11
+ * moves the caret to the end, so the caret is put back by what the customer typed, not by offset:
12
+ * the digits (and a leading `+`) before it are counted in the old text, and it lands after the same
13
+ * number of them in the new one. Spaces, brackets and dashes belong to the format and count for
14
+ * nothing.
15
+ */
16
+ /** Whether the character at `index` is one the customer typed: a digit, or a `+` that leads. */
17
+ function significant(text, index, seen) {
18
+ const char = text[index];
19
+ if (char >= "0" && char <= "9") return true;
20
+ return char === "+" && seen === 0;
21
+ }
22
+ /** How many typed characters sit before `caret` in `text`. */
23
+ function significantBefore(text, caret) {
24
+ let count = 0;
25
+ const end = Math.min(caret, text.length);
26
+ for (let index = 0; index < end; index += 1) if (significant(text, index, count)) count += 1;
27
+ return count;
28
+ }
29
+ /**
30
+ * Where the caret goes in `text` so that `count` typed characters sit before it. Past the last one
31
+ * it goes to the very end, so a closing bracket the format added is behind the caret, not ahead.
32
+ */
33
+ function caretAfter(text, count) {
34
+ if (count <= 0) return 0;
35
+ let seen = 0;
36
+ for (let index = 0; index < text.length; index += 1) {
37
+ if (!significant(text, index, seen)) continue;
38
+ seen += 1;
39
+ if (seen === count) return significantBefore(text, text.length) === count ? text.length : index + 1;
40
+ }
41
+ return text.length;
42
+ }
43
+ // src/phone/examples.ts
44
+ /** A number the country could have, spaced as it writes one (`IS` → `611 1234`): a placeholder. */
45
+ function phoneExample(country) {
46
+ if (!isSupportedCountry(country)) return void 0;
47
+ return getExampleNumber(country, examples)?.formatNational();
48
+ }
49
+ // src/phone/flags.ts
50
+ /**
51
+ * The flag artwork is 46 kB gzipped, so it is one lazy chunk, fetched when the first `PhoneInput`
52
+ * mounts in a browser and shared by every one after it. Until it arrives a flag is an empty box of
53
+ * the same size, so nothing moves when it does. A server render never loads it: the server and the
54
+ * client's first render both draw the empty box, and agree.
55
+ */
56
+ var flags = shallowRef(null);
57
+ var loading;
58
+ /** Loads the flags. Safe to call more than once, and ahead of a `PhoneInput` to draw them sooner. */
59
+ function preloadPhoneFlags() {
60
+ loading ??= import("./phoneFlagData-D9dDT2uC.js").then((module) => {
61
+ flags.value = module.default;
62
+ }, () => {
63
+ loading = void 0;
64
+ });
65
+ return loading;
66
+ }
67
+ /** A country's flag once the chunk is in; reactive, so a flag drawn before then fills in. */
68
+ function phoneFlag(country) {
69
+ const entry = flags.value?.[country];
70
+ if (entry === void 0) return void 0;
71
+ const cut = entry.indexOf("|");
72
+ return {
73
+ viewBox: entry.slice(0, cut),
74
+ body: entry.slice(cut + 1)
75
+ };
76
+ }
77
+ // src/phone/phone.ts
78
+ /**
79
+ * Everything `@eldrajs/ui/phone` knows about phone numbers, with no Vue in it. This file and
80
+ * `./examples.ts` are the only places in the package that import `libphonenumber-js`, an optional
81
+ * peer: the root entry and `./vee-validate` never reach them
82
+ * (`src/__tests__/phoneIsolation.spec.ts`).
83
+ *
84
+ * The `min` metadata is the one imported. It accepts landlines and mobiles and rejects a number
85
+ * whose prefix the country does not use; `mobile` would refuse every landline.
86
+ */
87
+ var supported = (country) => country !== void 0 && isSupportedCountry(country);
88
+ var allCountries;
89
+ /** Every country a number can be written for, as ISO 3166-1 alpha-2 codes. */
90
+ function phoneCountries() {
91
+ allCountries ??= getCountries();
92
+ return allCountries;
93
+ }
94
+ /** A country's dialling code with no `+` (`IS` → `354`), or `''` for a country it does not know. */
95
+ function phoneCallingCode(country) {
96
+ return supported(country) ? getCountryCallingCode(country) : "";
97
+ }
98
+ var metadata;
99
+ /** The countries sharing a dialling code, the one the code belongs to first (`1` → `US`, …). */
100
+ function countriesForCallingCode(code) {
101
+ metadata ??= new Metadata();
102
+ return metadata.getCountryCodesForCallingCode(code) ?? [];
103
+ }
104
+ /**
105
+ * Takes a number apart. `country` is where a number written without a dialling code is from.
106
+ * `null` for text with no number in it.
107
+ */
108
+ function parsePhone(value, country) {
109
+ const parsed = parsePhoneNumberFromString(value, supported(country) ? country : void 0);
110
+ if (parsed === void 0) return null;
111
+ return {
112
+ e164: parsed.number,
113
+ country: parsed.country,
114
+ callingCode: parsed.countryCallingCode,
115
+ nationalNumber: parsed.nationalNumber,
116
+ national: parsed.formatNational(),
117
+ valid: parsed.isValid()
118
+ };
119
+ }
120
+ /**
121
+ * Whether `value` is a number that exists. With a `country` it must also be that country's: a
122
+ * Danish number is not valid for Iceland, however well formed.
123
+ */
124
+ function isValidPhone(value, country) {
125
+ const parts = parsePhone(value, country);
126
+ if (parts === null || !parts.valid) return false;
127
+ return country === void 0 || parts.country === country;
128
+ }
129
+ /** The country a dialling code selects: see `typePhone`. */
130
+ function countryForCode(code, detected, current, allowed) {
131
+ if (detected !== void 0 && allowed(detected)) return detected;
132
+ const sharing = countriesForCallingCode(code).filter(allowed);
133
+ return sharing.includes(current) ? current : sharing[0];
134
+ }
135
+ function national(digits, country, allowed, stripped) {
136
+ const code = supported(country) ? country : void 0;
137
+ const typed = new AsYouType(code);
138
+ let text = typed.input(digits);
139
+ const number = typed.getNumber();
140
+ const e164 = number?.number ?? "";
141
+ const actual = e164 === "" ? void 0 : parsePhoneNumberFromString(e164)?.country;
142
+ let next = country;
143
+ if (actual !== void 0 && actual !== country && allowed(actual)) {
144
+ next = actual;
145
+ text = new AsYouType(actual).input(digits);
146
+ }
147
+ return {
148
+ text,
149
+ country: next,
150
+ e164,
151
+ nationalNumber: number?.nationalNumber ?? "",
152
+ stripped
153
+ };
154
+ }
155
+ /**
156
+ * One edit of the field: `raw` is whatever the `<input>` now holds, `country` the one chosen.
157
+ *
158
+ * - The text is reduced to digits and re-spaced the way the country writes a number.
159
+ * - Text that starts with `+` picks its own country. As soon as the dialling code is complete the
160
+ * `+` and the code leave the text (the country button shows them) and the rest is the national
161
+ * number. A code several countries share keeps the current country if it is one of them, and
162
+ * otherwise takes the country the code belongs to, until the digits identify another.
163
+ * - `allowed` limits which countries the text may switch to. A dialling code of none of them stays
164
+ * in the text as typed.
165
+ */
166
+ function typePhone(raw, country, allowed = () => true) {
167
+ const chars = parseIncompletePhoneNumber(raw);
168
+ if (!chars.startsWith("+")) return national(chars, country, allowed, 0);
169
+ const typed = new AsYouType();
170
+ const text = typed.input(chars);
171
+ const code = typed.getCallingCode();
172
+ if (code !== void 0) {
173
+ const next = countryForCode(code, typed.getCountry(), country, allowed);
174
+ if (next !== void 0) return national(chars.slice(1 + code.length), next, allowed, 1 + code.length);
175
+ }
176
+ const number = typed.getNumber();
177
+ return {
178
+ text,
179
+ country,
180
+ e164: number?.number ?? "",
181
+ nationalNumber: number?.nationalNumber ?? "",
182
+ stripped: 0
183
+ };
184
+ }
185
+ /**
186
+ * The field's state for a value that arrives from outside (a bound `modelValue`, a saved address).
187
+ * A complete number is spaced as the country writes it in full, national prefix included
188
+ * (`07400 123456`), which is more than the as-you-type spacing knows for a number typed without it.
189
+ */
190
+ function readPhone(value, country, allowed = () => true) {
191
+ const state = typePhone(value, country, allowed);
192
+ const parts = state.e164 === "" ? null : parsePhone(state.e164);
193
+ if (parts !== null && parts.valid && parts.country === state.country) return {
194
+ ...state,
195
+ text: parts.national
196
+ };
197
+ return state;
198
+ }
199
+ // src/phone/PhoneFlag.vue?vue&type=script&setup=true&lang.ts
200
+ var _hoisted_1$1 = ["data-eldra-scope"];
201
+ // src/phone/PhoneFlag.vue
202
+ var PhoneFlag_default = /* @__PURE__ */ defineComponent({
203
+ __name: "PhoneFlag",
204
+ props: { country: {} },
205
+ setup(__props) {
206
+ const props = __props;
207
+ const eldra = useEldraScope();
208
+ const art = computed(() => phoneFlag(props.country));
209
+ const drawn = computed(() => art.value === void 0 ? {} : {
210
+ viewBox: art.value.viewBox,
211
+ innerHTML: art.value.body
212
+ });
213
+ return (_ctx, _cache) => {
214
+ return openBlock(), createElementBlock("svg", mergeProps({
215
+ "data-eldra-ui": "",
216
+ "data-eldra-scope": unref(eldra).scope,
217
+ "data-part": "flag",
218
+ class: "eldra:h-3.5 eldra:w-5 eldra:shrink-0 eldra:eldra-phone-flag",
219
+ preserveAspectRatio: "none",
220
+ "aria-hidden": "true",
221
+ focusable: "false"
222
+ }, drawn.value), null, 16, _hoisted_1$1);
223
+ };
224
+ }
225
+ });
226
+ // src/phone/rule.ts
227
+ /** A country's name in `locale` (`IS`, `is-IS` → `Ísland`); the code itself when it has none. */
228
+ function phoneCountryName(country, locale) {
229
+ try {
230
+ return displayNames(locale, { type: "region" }).of(country) ?? country;
231
+ } catch {
232
+ return country;
233
+ }
234
+ }
235
+ /**
236
+ * A validation rule for a `PhoneInput`'s value: `true`, or the message to show.
237
+ *
238
+ * An empty value passes: whether the field is required is another rule's question. The shape is
239
+ * vee-validate's rule function, and nothing here imports vee-validate, so it serves any form code.
240
+ */
241
+ function phoneRule(options = {}) {
242
+ return (value) => {
243
+ if (value === void 0 || value === null || value === "") return true;
244
+ const text = String(value);
245
+ const country = toValue(options.country);
246
+ if (isValidPhone(text, country)) return true;
247
+ const messages = {
248
+ ...enUS,
249
+ ...toValue(options.messages)
250
+ };
251
+ const named = country ?? parsePhone(text)?.country;
252
+ if (named === void 0) return messages.phoneInvalidNumber;
253
+ return messages.phoneInvalid(phoneCountryName(named, toValue(options.locale) ?? "en-US"));
254
+ };
255
+ }
256
+ // src/phone/PhoneInput.vue?vue&type=script&setup=true&lang.ts
257
+ var _hoisted_1 = ["data-eldra-scope"];
258
+ var _hoisted_2 = [
259
+ "aria-expanded",
260
+ "aria-controls",
261
+ "aria-label",
262
+ "aria-disabled",
263
+ "aria-readonly",
264
+ "disabled"
265
+ ];
266
+ var _hoisted_3 = ["data-eldra-slot"];
267
+ var _hoisted_4 = [
268
+ "id",
269
+ "value",
270
+ "placeholder",
271
+ "autocomplete",
272
+ "required",
273
+ "readonly",
274
+ "disabled",
275
+ "aria-invalid",
276
+ "aria-describedby"
277
+ ];
278
+ var _hoisted_5 = [
279
+ "name",
280
+ "value",
281
+ "disabled"
282
+ ];
283
+ var _hoisted_6 = { class: "eldra:eldra-select-match" };
284
+ var rootClass = "eldra:relative eldra:block eldra:w-full";
285
+ /**
286
+ * The field box: `Input`'s box (its border variable, its radius variable, its heights, its state
287
+ * colours), drawn on a wrapper because two controls sit in it. The one focus ring is the box's:
288
+ * `eldra-focus-proxy` turns it on while the number or the button is `:focus-visible`, and
289
+ * `eldra-focus-has-open` keeps it on while the list is open and focus is in the list's search
290
+ * field. No `transition-*` utility beside `eldra-focus`, which owns the transition list.
291
+ */
292
+ var LIVE_FILL = "eldra:bg-background eldra:text-text";
293
+ var REST = "eldra:border-border-strong eldra:hover:border-text eldra:focus-within:border-text";
294
+ var OPEN = "eldra:border-text";
295
+ var INVALID = "eldra:border-danger";
296
+ var DISABLED = "eldra:bg-surface-strong eldra:border-border eldra:border-dashed eldra:text-muted eldra:cursor-not-allowed";
297
+ var READONLY_FILL = "eldra:bg-surface eldra:text-text";
298
+ var BUTTON_BASE = "eldra:flex eldra:shrink-0 eldra:items-center eldra:gap-1.5 eldra:ps-2.5 eldra:pe-2 eldra:border-e eldra:border-border eldra:tabular-nums eldra:outline-none eldra:rounded-s-[calc(var(--eldra-input-radius,var(--eldra-radius-md))-var(--eldra-field-border-width,1px))]";
299
+ var BUTTON_LIVE = "eldra:cursor-pointer eldra:hover:bg-[color-mix(in_oklab,var(--eldra-color-text),transparent_94%)] eldra:focus-visible:bg-[color-mix(in_oklab,var(--eldra-color-text),transparent_94%)]";
300
+ // src/phone/PhoneInput.vue
301
+ var PhoneInput_default = /* @__PURE__ */ defineComponent({
302
+ inheritAttrs: false,
303
+ __name: "PhoneInput",
304
+ props: {
305
+ modelValue: { default: void 0 },
306
+ country: { default: void 0 },
307
+ defaultCountry: { default: void 0 },
308
+ preferredCountries: { default: void 0 },
309
+ countries: { default: void 0 },
310
+ locale: { default: void 0 },
311
+ size: { default: "md" },
312
+ id: { default: void 0 },
313
+ name: { default: void 0 },
314
+ placeholder: {
315
+ type: [String, Boolean],
316
+ default: void 0
317
+ },
318
+ autocomplete: { default: "tel-national" },
319
+ invalid: {
320
+ type: Boolean,
321
+ default: void 0
322
+ },
323
+ describedBy: { default: void 0 },
324
+ required: {
325
+ type: Boolean,
326
+ default: void 0
327
+ },
328
+ readonly: {
329
+ type: Boolean,
330
+ default: false
331
+ },
332
+ disabled: {
333
+ type: Boolean,
334
+ default: false
335
+ },
336
+ messages: { default: void 0 },
337
+ classes: { default: void 0 }
338
+ },
339
+ emits: [
340
+ "update:modelValue",
341
+ "update:country",
342
+ "change",
343
+ "focus",
344
+ "blur",
345
+ "open",
346
+ "close"
347
+ ],
348
+ setup(__props, { expose: __expose, emit: __emit }) {
349
+ const eldra = useEldraScope();
350
+ const props = __props;
351
+ const emit = __emit;
352
+ const m = useMessages(() => props.messages);
353
+ const providedLocale = useEldraUiLocale();
354
+ const locale = computed(() => props.locale ?? providedLocale.value);
355
+ const field = inject(FIELD_KEY, null);
356
+ const controlId = useUiId("phone", () => props.id ?? (field?.value.labelsControl === true ? field.value.id : void 0));
357
+ const describedBy = computed(() => joinIds(props.describedBy, field?.value.describedBy));
358
+ const isInvalid = computed(() => props.invalid ?? field?.value.invalid ?? false);
359
+ const isRequired = computed(() => props.required ?? field?.value.required ?? false);
360
+ const showsInvalid = computed(() => isInvalid.value && !props.disabled);
361
+ const panelId = computed(() => `${controlId.value}-panel`);
362
+ const listboxId = computed(() => `${controlId.value}-listbox`);
363
+ const known = new Set(phoneCountries());
364
+ /** The countries on offer, in the order given: every one, or the `countries` this field is limited to. */
365
+ const offered = computed(() => props.countries === void 0 ? phoneCountries() : [...new Set(props.countries)].filter((country) => known.has(country)));
366
+ const offeredSet = computed(() => new Set(offered.value));
367
+ const allowed = (country) => offeredSet.value.has(country);
368
+ const preferred = computed(() => [...new Set(props.preferredCountries ?? [])].filter((country) => allowed(country)));
369
+ function startingCountry() {
370
+ const limited = props.countries === void 0 ? void 0 : offered.value[0];
371
+ for (const country of [
372
+ props.country,
373
+ props.defaultCountry,
374
+ preferred.value[0],
375
+ limited,
376
+ "US"
377
+ ]) if (country !== void 0 && allowed(country)) return country;
378
+ return offered.value[0] ?? "US";
379
+ }
380
+ /** Controlled when the parent binds `modelValue`, self-managing when it does not. */
381
+ const model = useControllableModel(props, emit, () => "");
382
+ /**
383
+ * The field's own state: the text it shows and the country it is on. The text is not derived from
384
+ * the model, because the model cannot hold it: `07400` and `7400` are the same E.164 digits, and a
385
+ * field re-derived from the model on every keystroke would eat the `0` as it was typed. The model
386
+ * is read only when it holds something this state did not produce (`syncFromModel`).
387
+ */
388
+ const state = ref(readPhone(model.value, startingCountry(), allowed));
389
+ const text = computed(() => state.value.text);
390
+ const country = computed(() => state.value.country);
391
+ const callingCode = computed(() => phoneCallingCode(country.value));
392
+ function describe(next) {
393
+ return {
394
+ e164: next.e164,
395
+ country: next.country,
396
+ callingCode: phoneCallingCode(next.country),
397
+ nationalNumber: next.nationalNumber,
398
+ valid: isValidPhone(next.e164, next.country)
399
+ };
400
+ }
401
+ /** Takes a new state and tells the parent what changed. */
402
+ function commit(next) {
403
+ const previous = state.value;
404
+ state.value = next;
405
+ if (next.country !== previous.country) emit("update:country", next.country);
406
+ if (next.e164 !== model.value) model.value = next.e164;
407
+ if (next.country !== previous.country || next.e164 !== previous.e164) emit("change", describe(next));
408
+ nextTick(syncFromModel);
409
+ }
410
+ function syncFromModel() {
411
+ if (model.value === state.value.e164) return;
412
+ const previous = state.value;
413
+ state.value = readPhone(model.value, previous.country, allowed);
414
+ if (state.value.country !== previous.country) emit("update:country", state.value.country);
415
+ }
416
+ watch(model, syncFromModel);
417
+ /** The digits to carry over to another country: what was typed, without a dialling code. */
418
+ function carriedDigits() {
419
+ const current = state.value;
420
+ return current.text.trimStart().startsWith("+") ? current.nationalNumber : current.text;
421
+ }
422
+ /** A country chosen from the list, or bound from outside: the digits stay, re-spaced for it. */
423
+ function setCountry(next) {
424
+ if (next === country.value || !allowed(next)) return;
425
+ commit(typePhone(carriedDigits(), next, (candidate) => candidate === next));
426
+ }
427
+ watch(() => props.country, (next) => {
428
+ if (next !== void 0) setCountry(next);
429
+ });
430
+ onMounted(() => {
431
+ if (props.country !== void 0 && props.country !== country.value) emit("update:country", country.value);
432
+ preloadPhoneFlags();
433
+ });
434
+ const controlRef = ref(null);
435
+ function onInput(event) {
436
+ if (event.isComposing) return;
437
+ const element = event.target;
438
+ const raw = element.value;
439
+ const before = significantBefore(raw, element.selectionStart ?? raw.length);
440
+ const next = typePhone(raw, country.value, allowed);
441
+ element.value = next.text;
442
+ const caret = caretAfter(next.text, before - next.stripped);
443
+ element.setSelectionRange(caret, caret);
444
+ commit(next);
445
+ }
446
+ const placeholderText = computed(() => {
447
+ if (props.placeholder === false) return void 0;
448
+ return props.placeholder ?? phoneExample(country.value);
449
+ });
450
+ const countryName = (code) => phoneCountryName(code, locale.value);
451
+ /**
452
+ * Suggested countries first, in the order given, then every other one from A to Z in the locale's
453
+ * own order. A country is listed once: a suggested one is not repeated below. The dialling code
454
+ * and the ISO code are keywords, so "+45", "45" and "dk" all find Denmark.
455
+ */
456
+ const options = computed(() => {
457
+ const compare = comparator(locale.value);
458
+ const grouped = preferred.value.length > 0;
459
+ const entry = (code, group) => {
460
+ const dial = phoneCallingCode(code);
461
+ return {
462
+ value: code,
463
+ label: countryName(code),
464
+ meta: `+${dial}`,
465
+ keywords: [
466
+ `+${dial}`,
467
+ dial,
468
+ code
469
+ ],
470
+ group
471
+ };
472
+ };
473
+ const suggested = preferred.value.map((code) => entry(code, grouped ? m.value.phoneSuggested : void 0));
474
+ const rest = offered.value.filter((code) => !preferred.value.includes(code)).map((code) => entry(code, grouped ? m.value.phoneAllCountries : void 0)).sort((a, b) => compare(a.label, b.label));
475
+ return [...suggested, ...rest];
476
+ });
477
+ const indexOfValue = computed(() => new Map(options.value.map((option, index) => [option.value, index])));
478
+ const optionId = (value) => `${controlId.value}-o${indexOfValue.value.get(value) ?? 0}`;
479
+ const query = ref("");
480
+ const fieldRef = ref(null);
481
+ const buttonRef = ref(null);
482
+ const panelComponent = ref(null);
483
+ const panelRef = computed(() => panelComponent.value?.root ?? null);
484
+ const searchRef = computed(() => panelComponent.value?.searchInput ?? null);
485
+ const { sections, listOptions, hasOptions, highlights, renderMore, reveal, resetBatch } = useOptionList({
486
+ options: () => options.value,
487
+ query: () => query.value,
488
+ searchable: () => true,
489
+ controlId: () => controlId.value
490
+ });
491
+ const canOpen = () => !props.disabled && !props.readonly;
492
+ const { isOpen, panelStyle, placement, teleportTo, teleportDisabled, open: openPopover, close: closePopover, onTriggerPointerDown, onTriggerClick } = usePopover({
493
+ trigger: buttonRef,
494
+ content: panelRef,
495
+ anchor: fieldRef,
496
+ matchWidth: true,
497
+ canOpen,
498
+ onOpen: () => {
499
+ listbox.resetTypeahead();
500
+ emit("open");
501
+ },
502
+ onClose: () => {
503
+ listbox.resetTypeahead();
504
+ resetBatch();
505
+ query.value = "";
506
+ emit("close");
507
+ },
508
+ afterOpen: async () => {
509
+ await nextTick();
510
+ searchRef.value?.focus();
511
+ },
512
+ openFromPointer: () => openPanel()
513
+ });
514
+ const listbox = useListbox({
515
+ options: () => listOptions.value,
516
+ isOpen: () => isOpen.value,
517
+ canOpen,
518
+ searchable: () => true,
519
+ optionId,
520
+ open: () => openPanel(),
521
+ close: () => closePopover(),
522
+ select: (option) => choose(option),
523
+ startQuery: (character) => {
524
+ query.value = character;
525
+ },
526
+ escape: () => {
527
+ if (query.value === "") return false;
528
+ query.value = "";
529
+ return true;
530
+ }
531
+ });
532
+ watch(listbox.activeValue, reveal, { flush: "sync" });
533
+ watch(query, () => {
534
+ if (isOpen.value) listbox.activateFrom(void 0);
535
+ });
536
+ function openPanel() {
537
+ openPopover(() => listbox.activateFrom(country.value));
538
+ }
539
+ /** Choosing a country hands the keyboard to the number: that is what is typed next. */
540
+ function choose(option) {
541
+ setCountry(option.value);
542
+ closePopover(false);
543
+ controlRef.value?.focus();
544
+ }
545
+ const selectedValues = computed(() => [country.value]);
546
+ const emptyText = computed(() => query.value.trim() === "" ? m.value.noResults : m.value.phoneNoMatch(query.value.trim()));
547
+ const buttonLabel = computed(() => m.value.phoneCountry(countryName(country.value), `+${callingCode.value}`));
548
+ const FIELD_BOX = `eldra:flex eldra:w-full eldra:min-w-0 eldra:items-stretch eldra:eldra-field-border ${FIELD_RADIUS} eldra:eldra-focus eldra:eldra-focus-proxy eldra:eldra-focus-has-open`;
549
+ const FIELD_HEIGHT = {
550
+ sm: "eldra:control-h-sm",
551
+ md: "eldra:control-h",
552
+ lg: "eldra:control-h-lg"
553
+ };
554
+ function fieldState() {
555
+ if (props.disabled) return DISABLED;
556
+ if (props.readonly) return `${READONLY_FILL} ${showsInvalid.value ? INVALID : "eldra:border-border"}`;
557
+ return `${LIVE_FILL} ${showsInvalid.value ? INVALID : isOpen.value ? OPEN : REST}`;
558
+ }
559
+ const fieldClass = computed(() => [
560
+ FIELD_BOX,
561
+ FIELD_HEIGHT[props.size],
562
+ fieldState(),
563
+ showsInvalid.value && "eldra:eldra-field-invalid",
564
+ props.classes?.field
565
+ ]);
566
+ /** The start corners follow the box's, one border-width in, so the hover fill never pokes out. */
567
+ const buttonClass = computed(() => [
568
+ BUTTON_BASE,
569
+ FIELD_TEXT[props.size],
570
+ props.disabled ? "eldra:cursor-not-allowed" : props.readonly ? "eldra:cursor-default" : BUTTON_LIVE,
571
+ props.classes?.countryButton
572
+ ]);
573
+ const chevronClass = computed(() => [
574
+ "eldra:size-3.5 eldra:shrink-0 eldra:text-muted eldra:transition-transform eldra:duration-base",
575
+ isOpen.value && "eldra:rotate-180",
576
+ props.classes?.chevron
577
+ ]);
578
+ const controlClass = computed(() => [
579
+ "eldra:block eldra:min-w-0 eldra:flex-1 eldra:bg-transparent eldra:px-2.75 eldra:tabular-nums eldra:outline-none eldra:placeholder:text-muted",
580
+ FIELD_TEXT[props.size],
581
+ props.disabled && "eldra:cursor-not-allowed",
582
+ props.classes?.control
583
+ ]);
584
+ /** `control` is the number `<input>`; `country` the one the field is on, whoever chose it. */
585
+ __expose({
586
+ control: controlRef,
587
+ focus: () => controlRef.value?.focus(),
588
+ country
589
+ });
590
+ return (_ctx, _cache) => {
591
+ return openBlock(), createElementBlock("div", {
592
+ "data-eldra-ui": "",
593
+ "data-eldra-scope": unref(eldra).scope,
594
+ "data-part": "root",
595
+ class: normalizeClass([rootClass, _ctx.$attrs.class])
596
+ }, [
597
+ createElementVNode("div", {
598
+ ref_key: "fieldRef",
599
+ ref: fieldRef,
600
+ "data-part": "field",
601
+ class: normalizeClass(fieldClass.value)
602
+ }, [createElementVNode("button", {
603
+ ref_key: "buttonRef",
604
+ ref: buttonRef,
605
+ "data-part": "countryButton",
606
+ type: "button",
607
+ role: "combobox",
608
+ class: normalizeClass(buttonClass.value),
609
+ "aria-haspopup": "listbox",
610
+ "aria-expanded": unref(isOpen) ? "true" : "false",
611
+ "aria-controls": listboxId.value,
612
+ "aria-label": buttonLabel.value,
613
+ "aria-disabled": __props.disabled ? "true" : void 0,
614
+ "aria-readonly": __props.readonly ? "true" : void 0,
615
+ disabled: __props.disabled || void 0,
616
+ onPointerdown: _cache[0] || (_cache[0] = (...args) => unref(onTriggerPointerDown) && unref(onTriggerPointerDown)(...args)),
617
+ onClick: _cache[1] || (_cache[1] = (...args) => unref(onTriggerClick) && unref(onTriggerClick)(...args)),
618
+ onKeydown: _cache[2] || (_cache[2] = (...args) => unref(listbox).onKeydown && unref(listbox).onKeydown(...args))
619
+ }, [
620
+ createElementVNode("span", {
621
+ "data-eldra-slot": unref(eldra).slotHost("flag"),
622
+ class: "eldra:flex eldra:shrink-0"
623
+ }, [renderSlot(_ctx.$slots, "flag", { country: country.value }, () => [createVNode(PhoneFlag_default, {
624
+ country: country.value,
625
+ class: normalizeClass(__props.classes?.flag)
626
+ }, null, 8, ["country", "class"])])], 8, _hoisted_3),
627
+ createElementVNode("span", {
628
+ "data-part": "callingCode",
629
+ class: normalizeClass(__props.classes?.callingCode)
630
+ }, "+" + toDisplayString(callingCode.value), 3),
631
+ !__props.readonly ? (openBlock(), createElementBlock("span", {
632
+ key: 0,
633
+ "data-part": "chevron",
634
+ class: normalizeClass(chevronClass.value),
635
+ "aria-hidden": "true"
636
+ }, [..._cache[6] || (_cache[6] = [createElementVNode("svg", {
637
+ class: "eldra:size-3.5",
638
+ viewBox: "0 0 24 24",
639
+ fill: "none",
640
+ stroke: "currentColor",
641
+ "stroke-width": "2",
642
+ "stroke-linecap": "round",
643
+ "stroke-linejoin": "round",
644
+ focusable: "false"
645
+ }, [createElementVNode("path", { d: "M6 9l6 6l6 -6" })], -1)])], 2)) : createCommentVNode("", true)
646
+ ], 42, _hoisted_2), createElementVNode("input", mergeProps({
647
+ ref_key: "controlRef",
648
+ ref: controlRef
649
+ }, {
650
+ ..._ctx.$attrs,
651
+ class: void 0
652
+ }, {
653
+ "data-part": "control",
654
+ type: "tel",
655
+ inputmode: "tel",
656
+ class: controlClass.value,
657
+ id: unref(controlId),
658
+ value: text.value,
659
+ placeholder: placeholderText.value,
660
+ autocomplete: __props.autocomplete,
661
+ required: isRequired.value || void 0,
662
+ readonly: __props.readonly || void 0,
663
+ disabled: __props.disabled || void 0,
664
+ "aria-invalid": isInvalid.value ? "true" : void 0,
665
+ "aria-describedby": describedBy.value,
666
+ onInput,
667
+ onFocus: _cache[3] || (_cache[3] = ($event) => emit("focus", $event)),
668
+ onBlur: _cache[4] || (_cache[4] = ($event) => emit("blur", $event))
669
+ }), null, 16, _hoisted_4)], 2),
670
+ __props.name ? (openBlock(), createElementBlock("input", {
671
+ key: 0,
672
+ "data-part": "native",
673
+ type: "hidden",
674
+ name: __props.name,
675
+ value: unref(model),
676
+ disabled: __props.disabled || void 0
677
+ }, null, 8, _hoisted_5)) : createCommentVNode("", true),
678
+ unref(isOpen) ? (openBlock(), createBlock(Teleport, {
679
+ key: 1,
680
+ to: unref(teleportTo),
681
+ disabled: unref(teleportDisabled)
682
+ }, [createVNode(SelectPanel_default, {
683
+ ref_key: "panelComponent",
684
+ ref: panelComponent,
685
+ "panel-id": panelId.value,
686
+ "listbox-id": listboxId.value,
687
+ sections: unref(sections),
688
+ "has-options": unref(hasOptions),
689
+ highlights: unref(highlights),
690
+ "option-id": optionId,
691
+ "active-value": unref(listbox).activeValue.value,
692
+ "selected-values": selectedValues.value,
693
+ searchable: true,
694
+ query: query.value,
695
+ "search-placeholder": unref(m).phoneSearch,
696
+ "empty-text": emptyText.value,
697
+ "fallback-label": unref(m).phoneCountryList,
698
+ "panel-style": unref(panelStyle),
699
+ placement: unref(placement),
700
+ classes: __props.classes,
701
+ onSearch: _cache[5] || (_cache[5] = ($event) => query.value = $event),
702
+ onSelect: choose,
703
+ onActivate: unref(listbox).setActive,
704
+ onScrollEnd: unref(renderMore),
705
+ onKeydown: unref(listbox).onKeydown
706
+ }, {
707
+ optionContent: withCtx(({ option, match }) => [
708
+ renderSlot(_ctx.$slots, "flag", { country: option.value }, () => [createVNode(PhoneFlag_default, {
709
+ country: option.value,
710
+ class: normalizeClass(__props.classes?.flag)
711
+ }, null, 8, ["country", "class"])]),
712
+ createElementVNode("span", {
713
+ "data-part": "optionLabel",
714
+ class: normalizeClass(["eldra:min-w-0 eldra:flex-1", __props.classes?.optionLabel])
715
+ }, [match ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
716
+ createTextVNode(toDisplayString(match.before), 1),
717
+ createElementVNode("span", _hoisted_6, toDisplayString(match.match), 1),
718
+ createTextVNode(toDisplayString(match.after), 1)
719
+ ], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [createTextVNode(toDisplayString(option.label), 1)], 64))], 2),
720
+ createElementVNode("span", {
721
+ "data-part": "optionMeta",
722
+ class: normalizeClass(["eldra:ms-3 eldra:shrink-0 eldra:text-caption eldra:tabular-nums eldra:text-[color:var(--eldra-select-panel-muted,var(--eldra-color-muted))]", __props.classes?.optionMeta])
723
+ }, toDisplayString(option.meta), 3)
724
+ ]),
725
+ _: 3
726
+ }, 8, [
727
+ "panel-id",
728
+ "listbox-id",
729
+ "sections",
730
+ "has-options",
731
+ "highlights",
732
+ "active-value",
733
+ "selected-values",
734
+ "query",
735
+ "search-placeholder",
736
+ "empty-text",
737
+ "fallback-label",
738
+ "panel-style",
739
+ "placement",
740
+ "classes",
741
+ "onActivate",
742
+ "onScrollEnd",
743
+ "onKeydown"
744
+ ])], 8, ["to", "disabled"])) : createCommentVNode("", true)
745
+ ], 10, _hoisted_1);
746
+ };
747
+ }
748
+ });
749
+ export { parsePhone as a, preloadPhoneFlags as c, isValidPhone as i, phoneExample as l, phoneCountryName as n, phoneCallingCode as o, phoneRule as r, phoneCountries as s, PhoneInput_default as t };