@ember-joinery/maskito 0.0.0-stage → 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.
Files changed (45) hide show
  1. package/CHANGELOG.md +178 -0
  2. package/LICENSE.md +9 -0
  3. package/NOTICE +532 -0
  4. package/README.md +55 -2
  5. package/addon-main.cjs +4 -0
  6. package/declarations/components/masked-input.d.ts +30 -0
  7. package/declarations/components/masked-input.d.ts.map +1 -0
  8. package/declarations/components/phone-input.d.ts +167 -0
  9. package/declarations/components/phone-input.d.ts.map +1 -0
  10. package/declarations/index.d.ts +9 -0
  11. package/declarations/index.d.ts.map +1 -0
  12. package/declarations/maskito.d.ts +42 -0
  13. package/declarations/maskito.d.ts.map +1 -0
  14. package/declarations/messages.d.ts +21 -0
  15. package/declarations/messages.d.ts.map +1 -0
  16. package/declarations/phone.d.ts +49 -0
  17. package/declarations/phone.d.ts.map +1 -0
  18. package/declarations/presets.d.ts +53 -0
  19. package/declarations/presets.d.ts.map +1 -0
  20. package/declarations/test-support/-private/typing.d.ts +28 -0
  21. package/declarations/test-support/-private/typing.d.ts.map +1 -0
  22. package/declarations/test-support/index.d.ts +3 -0
  23. package/declarations/test-support/index.d.ts.map +1 -0
  24. package/declarations/test-support/masked-input.d.ts +36 -0
  25. package/declarations/test-support/masked-input.d.ts.map +1 -0
  26. package/declarations/test-support/phone-input.d.ts +70 -0
  27. package/declarations/test-support/phone-input.d.ts.map +1 -0
  28. package/declarations/test-support.d.ts +2 -0
  29. package/dist/components/masked-input.js +31 -0
  30. package/dist/components/masked-input.js.map +1 -0
  31. package/dist/components/phone-input.js +374 -0
  32. package/dist/components/phone-input.js.map +1 -0
  33. package/dist/index.js +7 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/maskito.js +87 -0
  36. package/dist/maskito.js.map +1 -0
  37. package/dist/messages.js +23 -0
  38. package/dist/messages.js.map +1 -0
  39. package/dist/phone.js +99 -0
  40. package/dist/phone.js.map +1 -0
  41. package/dist/presets.js +122 -0
  42. package/dist/presets.js.map +1 -0
  43. package/dist/test-support.js +330 -0
  44. package/dist/test-support.js.map +1 -0
  45. package/package.json +117 -3
@@ -0,0 +1,374 @@
1
+ import Component from '@glimmer/component';
2
+ import { tracked, cached } from '@glimmer/tracking';
3
+ import { on } from '@ember/modifier';
4
+ import { renderSettled } from '@ember/renderer';
5
+ import { waitForPromise } from '@ember/test-waiters';
6
+ import { modifier } from 'ember-modifier';
7
+ import { ChevronDown } from 'lucide';
8
+ import { maskitoTransform } from '@maskito/core';
9
+ import { AsYouType, parsePhoneNumberFromString } from 'libphonenumber-js/core';
10
+ import { ControllableState } from '@ember-joinery/primitives/controllable-state';
11
+ import { idFor } from '@ember-joinery/primitives/id';
12
+ import { nativeValue } from '@ember-joinery/primitives/native-value';
13
+ import { VisuallyHidden } from '@ember-joinery/primitives/visually-hidden';
14
+ import Icon from '@ember-joinery/ember/components/icon';
15
+ import { inputGroupAddonClasses, inputGroupButtonClasses } from '@ember-joinery/ember/components/input-group';
16
+ import { inputClasses } from '@ember-joinery/ember/components/input';
17
+ import Select from '@ember-joinery/ember/components/select';
18
+ import { maskito } from '../maskito.js';
19
+ import { countryOfPhone, phoneMetadata, callingCode, countryFlag, phoneCountries, phoneMask } from '../phone.js';
20
+ import { translator, translate, t } from '@ember-joinery/primitives/i18n';
21
+ import { messages } from '../messages.js';
22
+ import { precompileTemplate } from '@ember/template-compilation';
23
+ import { setComponentTemplate } from '@ember/component';
24
+ import { g, i, n } from 'decorator-transforms/runtime';
25
+
26
+ /** `display: contents`, set through the CSSOM so a strict Content Security Policy allows it. */const displayContents = modifier(element => {
27
+ element.style.setProperty('display', 'contents');
28
+ });
29
+ /**
30
+ * The number in `text` when it is an international one, `+` or `00` and then digits, written
31
+ * `+…` with its spaces and punctuation gone; else `undefined`.
32
+ */
33
+ function internationalNumber(text) {
34
+ const number = text.trim().replace(/^00/, '+').replace(/[^\d+]/g, '');
35
+ return /^\+\d+$/.test(number) ? number : undefined;
36
+ }
37
+ /** The `beforeinput` types that bring a whole number at once, read before the mask sees them. */
38
+ const WHOLE_INSERTS = new Set(['insertFromDrop', 'insertFromPaste', 'insertReplacementText']);
39
+ /** A press on the addon, but not on its button, focuses the input, as Input Group's does. */
40
+ function focusInput(event) {
41
+ if (event.target?.closest('button')) return;
42
+ const addon = event.currentTarget;
43
+ addon.parentElement?.querySelector('input')?.focus();
44
+ }
45
+ const INPUT_CLASSES = `${inputClasses()} jn-input-group-input jn-phone-input-input`;
46
+ const ADDON_CLASSES = `${inputGroupAddonClasses({
47
+ align: 'inline-start'
48
+ })} jn-phone-input-addon`;
49
+ const TRIGGER_CLASSES = `${inputGroupButtonClasses({
50
+ variant: 'ghost',
51
+ size: 'xs'
52
+ })} jn-phone-input-trigger`;
53
+ /**
54
+ * A phone number input with a country picker, in shadcn's look: an Input Group whose addon is
55
+ * a Select of countries (each with its flag and calling code), and whose input
56
+ * `@maskito/phone` masks as it is typed in, by the rules libphonenumber-js has for the chosen
57
+ * country. Its value is the number in E.164 form, `+12125550123`, or `null` while it is empty:
58
+ *
59
+ * <PhoneInput @value={{this.phone}} @onValueChange={{this.setPhone}} />
60
+ * <PhoneInput @defaultCountry="GB" @national={{true}} />
61
+ *
62
+ * The picker shows the country of the value: a number from another country, set, pasted,
63
+ * dropped or autofilled, switches it, and choosing a country keeps the national number typed so
64
+ * far. `@countries` narrows the list.
65
+ */
66
+ class PhoneInput extends Component {
67
+ static {
68
+ g(this.prototype, "typed", [tracked]);
69
+ }
70
+ #typed = (i(this, "typed"), void 0);
71
+ /** What was last typed; shown while it still stands for the value. */
72
+ /** Whether the picker closed because a country was chosen, so focus goes to the input. */#chose = false;
73
+ #input;
74
+ #mask;
75
+ valueState = new ControllableState({
76
+ name: 'value',
77
+ value: () => this.args.value,
78
+ defaultValue: () => this.args.defaultValue,
79
+ onChange: () => this.args.onValueChange,
80
+ fallback: null
81
+ });
82
+ countryState = new ControllableState({
83
+ name: 'country',
84
+ value: () => this.args.country,
85
+ defaultValue: () => this.args.defaultCountry ?? countryOfPhone(this.valueState.value, undefined, this.metadata) ?? this.args.countries?.[0] ?? 'US',
86
+ onChange: () => this.args.onCountryChange,
87
+ fallback: 'US'
88
+ });
89
+ get metadata() {
90
+ return this.args.metadata ?? phoneMetadata;
91
+ }
92
+ get national() {
93
+ return this.args.national ?? false;
94
+ }
95
+ /** The number, in E.164 form, or `null`. */
96
+ get value() {
97
+ return this.valueState.value;
98
+ }
99
+ /** The country shown: the value's, whose calling code decides, else the one chosen. */
100
+ get country() {
101
+ const chosen = this.countryState.value;
102
+ return countryOfPhone(this.value, chosen, this.metadata) ?? chosen;
103
+ }
104
+ get callingCode() {
105
+ return callingCode(this.country, this.metadata);
106
+ }
107
+ get flag() {
108
+ return countryFlag(this.country);
109
+ }
110
+ get countryName() {
111
+ return this.nameOf(this.country);
112
+ }
113
+ /** The hidden `country` that follows the input's label in the picker's default name. */
114
+ get countryWordId() {
115
+ return idFor(this, 'country-word');
116
+ }
117
+ get locale() {
118
+ return this.args.locale ?? translator(this)?.locale;
119
+ }
120
+ get displayNames() {
121
+ return new Intl.DisplayNames(this.locale ? [this.locale] : undefined, {
122
+ type: 'region'
123
+ });
124
+ }
125
+ static {
126
+ n(this.prototype, "displayNames", [cached]);
127
+ }
128
+ nameOf(code) {
129
+ return this.displayNames.of(code) ?? code;
130
+ }
131
+ /** The countries in the picker. */
132
+ get countries() {
133
+ const metadata = this.metadata;
134
+ const given = this.args.countries;
135
+ const countries = (given ?? phoneCountries(metadata)).map(code => ({
136
+ code,
137
+ name: this.nameOf(code),
138
+ callingCode: callingCode(code, metadata),
139
+ flag: countryFlag(code)
140
+ }));
141
+ if (given) return countries;
142
+ const collator = new Intl.Collator(this.locale);
143
+ return countries.sort((a, b) => collator.compare(a.name, b.name));
144
+ }
145
+ /** The mask for the country shown, made again only when the country or format changes. */
146
+ static {
147
+ n(this.prototype, "countries", [cached]);
148
+ }
149
+ get options() {
150
+ return this.maskFor(this.country, this.national, this.metadata);
151
+ }
152
+ /** The last mask made, kept while it is for the same country, format and metadata. */
153
+ maskFor(country, national, metadata) {
154
+ const key = `${country}:${national}`;
155
+ const mask = this.#mask;
156
+ if (mask?.key === key && mask.metadata === metadata) return mask.options;
157
+ const options = phoneMask(country, {
158
+ national,
159
+ metadata
160
+ });
161
+ this.#mask = {
162
+ key,
163
+ metadata,
164
+ options
165
+ };
166
+ return options;
167
+ }
168
+ /** The E.164 form of what the input shows, or `null` when it shows no digits. */
169
+ toE164(text, country) {
170
+ const digits = text.replace(/[^\d+]/g, '');
171
+ if (!/\d/.test(digits)) return null;
172
+ const formatter = new AsYouType(country, this.metadata);
173
+ formatter.input(digits);
174
+ // A calling code on its own, which the international mask keeps in front, is no number.
175
+ if (!formatter.getNumber()?.nationalNumber) return null;
176
+ return formatter.getNumberValue() ?? null;
177
+ }
178
+ /** A number in E.164 form, as the input shows it for `country`. */
179
+ format(value, country) {
180
+ const options = phoneMask(country, {
181
+ national: this.national,
182
+ metadata: this.metadata
183
+ });
184
+ if (!this.national) return maskitoTransform(value, options);
185
+ const parsed = parsePhoneNumberFromString(value, this.metadata);
186
+ const national = parsed ? parsed.formatNational() : this.nationalNumberOf(value);
187
+ return maskitoTransform(national.replace(/\D/g, ''), options);
188
+ }
189
+ /** The national part of a number in E.164 form, whole or begun. */
190
+ nationalNumberOf(value) {
191
+ const formatter = new AsYouType(undefined, this.metadata);
192
+ formatter.input(value);
193
+ return formatter.getNumber()?.nationalNumber ?? '';
194
+ }
195
+ /** What the input shows: what was typed, while it stands for the value, else the value. */
196
+ get shown() {
197
+ const value = this.value;
198
+ const typed = this.typed;
199
+ if (typed !== undefined && this.toE164(typed, this.country) === value) return typed;
200
+ return value ? this.format(value, this.country) : '';
201
+ }
202
+ /** The input changed (the mask has shaped it already). */
203
+ onText = text => {
204
+ const country = this.country;
205
+ // A number set from outside may show another country than the one chosen: typing keeps it.
206
+ if (country !== this.countryState.value) this.countryState.set(country);
207
+ this.typed = text;
208
+ this.valueState.set(this.toE164(text, country));
209
+ };
210
+ /** A country was chosen: the national number typed so far moves to its calling code. */
211
+ chooseCountry = code => {
212
+ const country = code;
213
+ this.#chose = true;
214
+ if (country === this.country) return;
215
+ const value = this.value;
216
+ this.countryState.set(country);
217
+ // A controlled country that didn't change leaves the number alone.
218
+ if (this.countryState.value !== country) return;
219
+ this.typed = undefined;
220
+ if (!value) return;
221
+ const national = this.nationalNumberOf(value);
222
+ const moved = national ? `+${callingCode(country, this.metadata)}${national}` : null;
223
+ this.valueState.set(moved ? this.toE164(this.format(moved, country), country) : null);
224
+ };
225
+ /**
226
+ * A whole number that arrives at once: pasted, dropped, autofilled, or filled in by a test.
227
+ * One that starts with `+` (or `00`) is read in its own country, which the picker switches
228
+ * to, and replaces what the input holds: the mask of the country shown would read its calling
229
+ * code as the first digits of a number of its own, and store another number. A number from a
230
+ * country `@countries` leaves out, or whose calling code is unknown, is turned away. Anything
231
+ * else is left to the mask (`undefined`).
232
+ */
233
+ takeNumber(text) {
234
+ const number = internationalNumber(text);
235
+ if (!number) return undefined;
236
+ const country = countryOfPhone(number, this.country, this.metadata);
237
+ const allowed = this.args.countries;
238
+ if (!country || allowed && !allowed.includes(country)) return 'refused';
239
+ if (country !== this.countryState.value) this.countryState.set(country);
240
+ this.typed = undefined;
241
+ this.valueState.set(this.toE164(this.format(number, country), country));
242
+ return 'taken';
243
+ }
244
+ /** Whether the mask of the country shown leaves `text` as it is: text it shaped itself. */
245
+ fitsMask(text) {
246
+ const options = phoneMask(this.country, {
247
+ national: this.national,
248
+ metadata: this.metadata
249
+ });
250
+ return maskitoTransform(text, options) === text;
251
+ }
252
+ /**
253
+ * Reads a whole number before the mask does, in the capture phase, so it goes before
254
+ * `{{maskito}}` in the template: a paste (its clipboard), a drop or a writing suggestion (its
255
+ * `beforeinput`), and an `input` no `beforeinput` announced, as Chrome's autofill and
256
+ * `fillIn()` send (with no `inputType`) and Firefox's (`insertReplacementText`). Typing has an
257
+ * `inputType` and is left alone, and so is text the mask shaped itself (the calling code its
258
+ * focus plugin puts in, with an `input` of no type). A number taken or turned away goes no
259
+ * further, and the input shows what the value now is.
260
+ */
261
+ wholeNumbers = modifier(element => {
262
+ const read = (text, event) => {
263
+ if (!this.takeNumber(text)) return;
264
+ event.preventDefault();
265
+ event.stopImmediatePropagation();
266
+ element.value = this.shown;
267
+ };
268
+ const onPaste = event => read(event.clipboardData?.getData('text/plain') ?? '', event);
269
+ const onBeforeInput = event => {
270
+ if (!WHOLE_INSERTS.has(event.inputType)) return;
271
+ // One that can't be cancelled is read from its `input`, once it is in.
272
+ if (!event.cancelable) return;
273
+ read(event.data ?? event.dataTransfer?.getData('text/plain') ?? '', event);
274
+ };
275
+ const onInput = event => {
276
+ const {
277
+ inputType
278
+ } = event;
279
+ if (inputType && inputType !== 'insertReplacementText') return;
280
+ if (this.fitsMask(element.value)) return;
281
+ read(element.value, event);
282
+ };
283
+ const options = {
284
+ capture: true
285
+ };
286
+ element.addEventListener('paste', onPaste, options);
287
+ element.addEventListener('beforeinput', onBeforeInput, options);
288
+ element.addEventListener('input', onInput, options);
289
+ return () => {
290
+ element.removeEventListener('paste', onPaste, options);
291
+ element.removeEventListener('beforeinput', onBeforeInput, options);
292
+ element.removeEventListener('input', onInput, options);
293
+ };
294
+ });
295
+ /**
296
+ * Names the picker after its input, `Phone country`, so a screen reader tells apart the
297
+ * pickers of a form with several phone inputs: the input's `aria-labelledby`, else its
298
+ * `<label>`s (given an id if they have none), then a hidden `country`; else its `aria-label`
299
+ * and `country`. `@countryLabel` names it instead.
300
+ */
301
+ nameTrigger = modifier((trigger, [label]) => {
302
+ if (label) {
303
+ trigger.setAttribute('aria-label', label);
304
+ return () => trigger.removeAttribute('aria-label');
305
+ }
306
+ const input = trigger.closest('[data-slot="input-group"]')?.querySelector('input[data-slot="input-group-control"]');
307
+ const given = new Map();
308
+ let ids = input?.getAttribute('aria-labelledby')?.trim();
309
+ if (!ids && input?.labels?.length) {
310
+ ids = [...input.labels].map((element, index) => {
311
+ if (element.id) return element.id;
312
+ element.id = idFor(this, `label-${index}`);
313
+ given.set(element, element.id);
314
+ return element.id;
315
+ }).join(' ');
316
+ }
317
+ if (ids) {
318
+ trigger.setAttribute('aria-labelledby', `${ids} ${this.countryWordId}`);
319
+ } else {
320
+ const name = input?.getAttribute('aria-label')?.trim();
321
+ trigger.setAttribute('aria-label', name ? translate(this, messages.namedCountry, {
322
+ name
323
+ }) : translate(this, messages.country));
324
+ }
325
+ return () => {
326
+ trigger.removeAttribute('aria-labelledby');
327
+ trigger.removeAttribute('aria-label');
328
+ for (const [element, id] of given) if (element.id === id) element.removeAttribute('id');
329
+ };
330
+ });
331
+ /**
332
+ * Choosing a country sends focus to the input, to type the number; Escape, to the picker.
333
+ * It goes once the input shows the new country's mask, so that mask's calling code goes in.
334
+ */
335
+ onCloseAutoFocus = event => {
336
+ if (!this.#chose) return;
337
+ this.#chose = false;
338
+ event.preventDefault();
339
+ void waitForPromise(renderSettled().then(() => this.#input?.focus()));
340
+ };
341
+ onOpenChange = open => {
342
+ if (open) this.#chose = false;
343
+ };
344
+ registerInput = modifier(element => {
345
+ this.#input = element;
346
+ return () => {
347
+ if (this.#input === element) this.#input = undefined;
348
+ };
349
+ });
350
+ static {
351
+ setComponentTemplate(precompileTemplate("<div class=\"jn-phone-input\" data-test-jn=\"phone-input\" data-test-jn-name={{@name}} data-test-jn-value={{this.country}} {{displayContents}}>\n <Select @value={{this.country}} @onValueChange={{this.chooseCountry}} @onOpenChange={{this.onOpenChange}} @disabled={{@disabled}} @name=\"country\" as |s|>\n <div class=\"group/input-group jn-input-group\" role=\"group\" data-slot=\"input-group\" data-country={{this.country}} data-test-jn-part=\"group\">\n <input class={{INPUT_CLASSES}} data-slot=\"input-group-control\" data-test-jn=\"input\" type=\"tel\" autocomplete=\"tel\" inputmode=\"tel\" dir=\"ltr\" disabled={{@disabled}} {{this.registerInput}} {{this.wholeNumbers}} {{nativeValue value=this.shown onValueChange=this.onText}} {{maskito this.options}} ...attributes />\n <div class={{ADDON_CLASSES}} role=\"group\" data-slot=\"input-group-addon\" data-align=\"inline-start\" data-test-jn-part=\"addon\" {{on \"click\" focusInput}}>\n <button class={{TRIGGER_CLASSES}} data-slot=\"button\" data-variant=\"ghost\" data-size=\"xs\" {{this.nameTrigger @countryLabel}} {{s.trigger}}>\n <span class=\"jn-phone-input-flag\" aria-hidden=\"true\">{{this.flag}}</span>\n <VisuallyHidden data-test-jn-part=\"country-name\">{{this.countryName}}</VisuallyHidden>\n {{#if this.national}}\n <span class=\"jn-phone-input-calling-code\" data-test-jn-part=\"calling-code\">\n +{{this.callingCode}}\n </span>\n {{/if}}\n <Icon @icon={{if @icon @icon ChevronDown}} class=\"jn-phone-input-chevron\" aria-hidden=\"true\" />\n </button>\n </div>\n </div>\n <s.Content @position=\"popper\" @align=\"start\" @onCloseAutoFocus={{this.onCloseAutoFocus}} class=\"jn-phone-input-content\">\n {{#each this.countries key=\"code\" as |country|}}\n <s.Item @value={{country.code}} @textValue={{country.name}} class=\"jn-phone-input-item\">\n <span class=\"jn-phone-input-flag\" aria-hidden=\"true\">{{country.flag}}</span>\n <span class=\"jn-phone-input-item-name\">{{country.name}}</span>\n <span class=\"jn-phone-input-item-code\">+{{country.callingCode}}</span>\n </s.Item>\n {{/each}}\n </s.Content>\n </Select>\n <span id={{this.countryWordId}} hidden>{{t messages.countryWord}}</span>\n {{#if @formName}}\n <input type=\"hidden\" name={{@formName}} value={{if this.value this.value \"\"}} />\n {{/if}}\n</div>", {
352
+ strictMode: true,
353
+ scope: () => ({
354
+ displayContents,
355
+ Select,
356
+ INPUT_CLASSES,
357
+ nativeValue,
358
+ maskito,
359
+ ADDON_CLASSES,
360
+ on,
361
+ focusInput,
362
+ TRIGGER_CLASSES,
363
+ VisuallyHidden,
364
+ Icon,
365
+ ChevronDown,
366
+ t,
367
+ messages
368
+ })
369
+ }), this);
370
+ }
371
+ }
372
+
373
+ export { PhoneInput as default };
374
+ //# sourceMappingURL=phone-input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"phone-input.js","sources":["../../src/components/phone-input.gts"],"sourcesContent":["import Component from '@glimmer/component'\nimport { cached, tracked } from '@glimmer/tracking'\nimport { on } from '@ember/modifier'\nimport { renderSettled } from '@ember/renderer'\nimport { waitForPromise } from '@ember/test-waiters'\nimport { modifier } from 'ember-modifier'\nimport { ChevronDown } from 'lucide'\nimport { maskitoTransform, type MaskitoOptions } from '@maskito/core'\nimport { AsYouType, parsePhoneNumberFromString } from 'libphonenumber-js/core'\nimport { ControllableState } from '@ember-joinery/primitives/controllable-state'\nimport { idFor } from '@ember-joinery/primitives/id'\nimport { nativeValue } from '@ember-joinery/primitives/native-value'\nimport { VisuallyHidden } from '@ember-joinery/primitives/visually-hidden'\nimport Icon, { type IconNode } from '@ember-joinery/ember/components/icon'\nimport {\n inputGroupAddonClasses,\n inputGroupButtonClasses,\n} from '@ember-joinery/ember/components/input-group'\nimport { inputClasses } from '@ember-joinery/ember/components/input'\nimport Select from '@ember-joinery/ember/components/select'\nimport { maskito } from '../maskito.ts'\nimport {\n callingCode,\n countryFlag,\n countryOfPhone,\n phoneCountries,\n phoneMask,\n phoneMetadata,\n type CountryCode,\n type MetadataJson,\n} from '../phone.ts'\nimport { t, translate, translator } from '@ember-joinery/primitives/i18n'\nimport { messages } from '../messages.ts'\n\n/** A country in the picker. */\nexport interface PhoneCountry {\n /** Its ISO 3166 code, such as `GB`. */\n code: CountryCode\n /** Its name, in `@locale`'s language. */\n name: string\n /** Its calling code, without the `+`. */\n callingCode: string\n /** Its flag, as an emoji. */\n flag: string\n}\n\nexport interface PhoneInputSignature {\n Args: {\n /**\n * The number in E.164 form (`+12125550123`), or `null` while it is empty, when you control\n * it. While a number is being typed it holds the digits so far, with the `+`.\n */\n value?: string | null\n /** The number it starts with when you don't control it. */\n defaultValue?: string | null\n /** Called with the new number (E.164, or `null`) whenever it changes. */\n onValueChange?: (value: string | null) => void\n /** The country whose numbers are typed (ISO 3166), when you control it. */\n country?: CountryCode\n /**\n * The country it starts with: by default the country of its value, else the first of\n * `@countries`, else `US`.\n */\n defaultCountry?: CountryCode\n /** Called with the new country whenever it changes. */\n onCountryChange?: (country: CountryCode) => void\n /** The countries the picker offers, in this order. Default: every country, by name. */\n countries?: readonly CountryCode[]\n /** Show numbers in the national format, `(212) 555-0123`; the calling code sits by the flag. */\n national?: boolean\n /** libphonenumber-js metadata. Default its `min` metadata. */\n metadata?: MetadataJson\n /** The language the countries are named in. Default the app's translator's, or the browser's. */\n locale?: string\n /**\n * The country picker's whole accessible name. By default it is named after the input: its\n * label and `country` (`Phone country`), so several phone inputs' pickers differ.\n */\n countryLabel?: string\n /** The picker's chevron. Default lucide's `chevron-down`. */\n icon?: IconNode\n /** Disables the input and the country picker. */\n disabled?: boolean\n /** Puts a hidden input by this name in a form, holding the number in E.164 form. */\n formName?: string\n /** Its name, for `PhoneInputHarness` (`data-test-jn-name`). */\n name?: string\n }\n /** The `<input type=\"tel\">`: give it `id`, `placeholder`, `aria-invalid`, `aria-describedby`… */\n Element: HTMLInputElement\n}\n\n/** `display: contents`, set through the CSSOM so a strict Content Security Policy allows it. */\nconst displayContents = modifier((element: HTMLElement) => {\n element.style.setProperty('display', 'contents')\n})\n\n/**\n * The number in `text` when it is an international one, `+` or `00` and then digits, written\n * `+…` with its spaces and punctuation gone; else `undefined`.\n */\nfunction internationalNumber(text: string): string | undefined {\n const number = text\n .trim()\n .replace(/^00/, '+')\n .replace(/[^\\d+]/g, '')\n return /^\\+\\d+$/.test(number) ? number : undefined\n}\n\n/** The `beforeinput` types that bring a whole number at once, read before the mask sees them. */\nconst WHOLE_INSERTS = new Set(['insertFromDrop', 'insertFromPaste', 'insertReplacementText'])\n\n/** A press on the addon, but not on its button, focuses the input, as Input Group's does. */\nfunction focusInput(event: MouseEvent): void {\n if ((event.target as Element | null)?.closest('button')) return\n const addon = event.currentTarget as HTMLElement\n addon.parentElement?.querySelector('input')?.focus()\n}\n\nconst INPUT_CLASSES = `${inputClasses()} jn-input-group-input jn-phone-input-input`\nconst ADDON_CLASSES = `${inputGroupAddonClasses({ align: 'inline-start' })} jn-phone-input-addon`\nconst TRIGGER_CLASSES = `${inputGroupButtonClasses({ variant: 'ghost', size: 'xs' })} jn-phone-input-trigger`\n\n/**\n * A phone number input with a country picker, in shadcn's look: an Input Group whose addon is\n * a Select of countries (each with its flag and calling code), and whose input\n * `@maskito/phone` masks as it is typed in, by the rules libphonenumber-js has for the chosen\n * country. Its value is the number in E.164 form, `+12125550123`, or `null` while it is empty:\n *\n * <PhoneInput @value={{this.phone}} @onValueChange={{this.setPhone}} />\n * <PhoneInput @defaultCountry=\"GB\" @national={{true}} />\n *\n * The picker shows the country of the value: a number from another country, set, pasted,\n * dropped or autofilled, switches it, and choosing a country keeps the national number typed so\n * far. `@countries` narrows the list.\n */\nexport default class PhoneInput extends Component<PhoneInputSignature> {\n /** What was last typed; shown while it still stands for the value. */\n @tracked private typed: string | undefined\n /** Whether the picker closed because a country was chosen, so focus goes to the input. */\n #chose = false\n #input: HTMLInputElement | undefined\n #mask: { key: string; metadata: MetadataJson; options: MaskitoOptions } | undefined\n\n valueState = new ControllableState<string | null>({\n name: 'value',\n value: () => this.args.value,\n defaultValue: () => this.args.defaultValue,\n onChange: () => this.args.onValueChange,\n fallback: null,\n })\n\n countryState = new ControllableState<CountryCode>({\n name: 'country',\n value: () => this.args.country,\n defaultValue: () =>\n this.args.defaultCountry ??\n countryOfPhone(this.valueState.value, undefined, this.metadata) ??\n this.args.countries?.[0] ??\n 'US',\n onChange: () => this.args.onCountryChange,\n fallback: 'US',\n })\n\n get metadata(): MetadataJson {\n return this.args.metadata ?? phoneMetadata\n }\n\n get national(): boolean {\n return this.args.national ?? false\n }\n\n /** The number, in E.164 form, or `null`. */\n get value(): string | null {\n return this.valueState.value\n }\n\n /** The country shown: the value's, whose calling code decides, else the one chosen. */\n get country(): CountryCode {\n const chosen = this.countryState.value\n return countryOfPhone(this.value, chosen, this.metadata) ?? chosen\n }\n\n get callingCode(): string {\n return callingCode(this.country, this.metadata)\n }\n\n get flag(): string {\n return countryFlag(this.country)\n }\n\n get countryName(): string {\n return this.nameOf(this.country)\n }\n\n /** The hidden `country` that follows the input's label in the picker's default name. */\n get countryWordId(): string {\n return idFor(this, 'country-word')\n }\n\n get locale(): string | undefined {\n return this.args.locale ?? translator(this)?.locale\n }\n\n @cached\n get displayNames(): Intl.DisplayNames {\n return new Intl.DisplayNames(this.locale ? [this.locale] : undefined, {\n type: 'region',\n })\n }\n\n nameOf(code: CountryCode): string {\n return this.displayNames.of(code) ?? code\n }\n\n /** The countries in the picker. */\n @cached\n get countries(): PhoneCountry[] {\n const metadata = this.metadata\n const given = this.args.countries\n const countries = (given ?? phoneCountries(metadata)).map(code => ({\n code,\n name: this.nameOf(code),\n callingCode: callingCode(code, metadata),\n flag: countryFlag(code),\n }))\n if (given) return countries\n const collator = new Intl.Collator(this.locale)\n return countries.sort((a, b) => collator.compare(a.name, b.name))\n }\n\n /** The mask for the country shown, made again only when the country or format changes. */\n get options(): MaskitoOptions {\n return this.maskFor(this.country, this.national, this.metadata)\n }\n\n /** The last mask made, kept while it is for the same country, format and metadata. */\n maskFor(country: CountryCode, national: boolean, metadata: MetadataJson): MaskitoOptions {\n const key = `${country}:${national}`\n const mask = this.#mask\n if (mask?.key === key && mask.metadata === metadata) return mask.options\n const options = phoneMask(country, { national, metadata })\n this.#mask = { key, metadata, options }\n return options\n }\n\n /** The E.164 form of what the input shows, or `null` when it shows no digits. */\n toE164(text: string, country: CountryCode): string | null {\n const digits = text.replace(/[^\\d+]/g, '')\n if (!/\\d/.test(digits)) return null\n const formatter = new AsYouType(country, this.metadata)\n formatter.input(digits)\n // A calling code on its own, which the international mask keeps in front, is no number.\n if (!formatter.getNumber()?.nationalNumber) return null\n return formatter.getNumberValue() ?? null\n }\n\n /** A number in E.164 form, as the input shows it for `country`. */\n format(value: string, country: CountryCode): string {\n const options = phoneMask(country, { national: this.national, metadata: this.metadata })\n if (!this.national) return maskitoTransform(value, options)\n const parsed = parsePhoneNumberFromString(value, this.metadata)\n const national = parsed ? parsed.formatNational() : this.nationalNumberOf(value)\n return maskitoTransform(national.replace(/\\D/g, ''), options)\n }\n\n /** The national part of a number in E.164 form, whole or begun. */\n nationalNumberOf(value: string): string {\n const formatter = new AsYouType(undefined, this.metadata)\n formatter.input(value)\n return formatter.getNumber()?.nationalNumber ?? ''\n }\n\n /** What the input shows: what was typed, while it stands for the value, else the value. */\n get shown(): string {\n const value = this.value\n const typed = this.typed\n if (typed !== undefined && this.toE164(typed, this.country) === value) return typed\n return value ? this.format(value, this.country) : ''\n }\n\n /** The input changed (the mask has shaped it already). */\n onText = (text: string): void => {\n const country = this.country\n // A number set from outside may show another country than the one chosen: typing keeps it.\n if (country !== this.countryState.value) this.countryState.set(country)\n this.typed = text\n this.valueState.set(this.toE164(text, country))\n }\n\n /** A country was chosen: the national number typed so far moves to its calling code. */\n chooseCountry = (code: string): void => {\n const country = code as CountryCode\n this.#chose = true\n if (country === this.country) return\n const value = this.value\n this.countryState.set(country)\n // A controlled country that didn't change leaves the number alone.\n if (this.countryState.value !== country) return\n this.typed = undefined\n if (!value) return\n const national = this.nationalNumberOf(value)\n const moved = national ? `+${callingCode(country, this.metadata)}${national}` : null\n this.valueState.set(moved ? this.toE164(this.format(moved, country), country) : null)\n }\n\n /**\n * A whole number that arrives at once: pasted, dropped, autofilled, or filled in by a test.\n * One that starts with `+` (or `00`) is read in its own country, which the picker switches\n * to, and replaces what the input holds: the mask of the country shown would read its calling\n * code as the first digits of a number of its own, and store another number. A number from a\n * country `@countries` leaves out, or whose calling code is unknown, is turned away. Anything\n * else is left to the mask (`undefined`).\n */\n takeNumber(text: string): 'taken' | 'refused' | undefined {\n const number = internationalNumber(text)\n if (!number) return undefined\n const country = countryOfPhone(number, this.country, this.metadata)\n const allowed = this.args.countries\n if (!country || (allowed && !allowed.includes(country))) return 'refused'\n if (country !== this.countryState.value) this.countryState.set(country)\n this.typed = undefined\n this.valueState.set(this.toE164(this.format(number, country), country))\n return 'taken'\n }\n\n /** Whether the mask of the country shown leaves `text` as it is: text it shaped itself. */\n fitsMask(text: string): boolean {\n const options = phoneMask(this.country, { national: this.national, metadata: this.metadata })\n return maskitoTransform(text, options) === text\n }\n\n /**\n * Reads a whole number before the mask does, in the capture phase, so it goes before\n * `{{maskito}}` in the template: a paste (its clipboard), a drop or a writing suggestion (its\n * `beforeinput`), and an `input` no `beforeinput` announced, as Chrome's autofill and\n * `fillIn()` send (with no `inputType`) and Firefox's (`insertReplacementText`). Typing has an\n * `inputType` and is left alone, and so is text the mask shaped itself (the calling code its\n * focus plugin puts in, with an `input` of no type). A number taken or turned away goes no\n * further, and the input shows what the value now is.\n */\n wholeNumbers = modifier((element: HTMLInputElement) => {\n const read = (text: string, event: Event): void => {\n if (!this.takeNumber(text)) return\n event.preventDefault()\n event.stopImmediatePropagation()\n element.value = this.shown\n }\n const onPaste = (event: ClipboardEvent) =>\n read(event.clipboardData?.getData('text/plain') ?? '', event)\n const onBeforeInput = (event: InputEvent) => {\n if (!WHOLE_INSERTS.has(event.inputType)) return\n // One that can't be cancelled is read from its `input`, once it is in.\n if (!event.cancelable) return\n read(event.data ?? event.dataTransfer?.getData('text/plain') ?? '', event)\n }\n const onInput = (event: Event) => {\n const { inputType } = event as InputEvent\n if (inputType && inputType !== 'insertReplacementText') return\n if (this.fitsMask(element.value)) return\n read(element.value, event)\n }\n const options = { capture: true }\n element.addEventListener('paste', onPaste, options)\n element.addEventListener('beforeinput', onBeforeInput, options)\n element.addEventListener('input', onInput, options)\n return () => {\n element.removeEventListener('paste', onPaste, options)\n element.removeEventListener('beforeinput', onBeforeInput, options)\n element.removeEventListener('input', onInput, options)\n }\n })\n\n /**\n * Names the picker after its input, `Phone country`, so a screen reader tells apart the\n * pickers of a form with several phone inputs: the input's `aria-labelledby`, else its\n * `<label>`s (given an id if they have none), then a hidden `country`; else its `aria-label`\n * and `country`. `@countryLabel` names it instead.\n */\n nameTrigger = modifier((trigger: HTMLElement, [label]: [string | undefined]) => {\n if (label) {\n trigger.setAttribute('aria-label', label)\n return () => trigger.removeAttribute('aria-label')\n }\n const input = trigger\n .closest('[data-slot=\"input-group\"]')\n ?.querySelector<HTMLInputElement>('input[data-slot=\"input-group-control\"]')\n const given = new Map<HTMLLabelElement, string>()\n let ids = input?.getAttribute('aria-labelledby')?.trim()\n if (!ids && input?.labels?.length) {\n ids = [...input.labels]\n .map((element, index) => {\n if (element.id) return element.id\n element.id = idFor(this, `label-${index}`)\n given.set(element, element.id)\n return element.id\n })\n .join(' ')\n }\n if (ids) {\n trigger.setAttribute('aria-labelledby', `${ids} ${this.countryWordId}`)\n } else {\n const name = input?.getAttribute('aria-label')?.trim()\n trigger.setAttribute(\n 'aria-label',\n name ? translate(this, messages.namedCountry, { name }) : translate(this, messages.country),\n )\n }\n return () => {\n trigger.removeAttribute('aria-labelledby')\n trigger.removeAttribute('aria-label')\n for (const [element, id] of given) if (element.id === id) element.removeAttribute('id')\n }\n })\n\n /**\n * Choosing a country sends focus to the input, to type the number; Escape, to the picker.\n * It goes once the input shows the new country's mask, so that mask's calling code goes in.\n */\n onCloseAutoFocus = (event: Event): void => {\n if (!this.#chose) return\n this.#chose = false\n event.preventDefault()\n void waitForPromise(renderSettled().then(() => this.#input?.focus()))\n }\n\n onOpenChange = (open: boolean): void => {\n if (open) this.#chose = false\n }\n\n registerInput = modifier((element: HTMLInputElement) => {\n this.#input = element\n return () => {\n if (this.#input === element) this.#input = undefined\n }\n })\n\n <template>\n <div\n class=\"jn-phone-input\"\n data-test-jn=\"phone-input\"\n data-test-jn-name={{@name}}\n data-test-jn-value={{this.country}}\n {{displayContents}}\n >\n <Select\n @value={{this.country}}\n @onValueChange={{this.chooseCountry}}\n @onOpenChange={{this.onOpenChange}}\n @disabled={{@disabled}}\n @name=\"country\"\n as |s|\n >\n <div\n class=\"group/input-group jn-input-group\"\n role=\"group\"\n data-slot=\"input-group\"\n data-country={{this.country}}\n data-test-jn-part=\"group\"\n >\n <input\n class={{INPUT_CLASSES}}\n data-slot=\"input-group-control\"\n data-test-jn=\"input\"\n type=\"tel\"\n autocomplete=\"tel\"\n inputmode=\"tel\"\n dir=\"ltr\"\n disabled={{@disabled}}\n {{this.registerInput}}\n {{this.wholeNumbers}}\n {{nativeValue value=this.shown onValueChange=this.onText}}\n {{maskito this.options}}\n ...attributes\n />\n <div\n class={{ADDON_CLASSES}}\n role=\"group\"\n data-slot=\"input-group-addon\"\n data-align=\"inline-start\"\n data-test-jn-part=\"addon\"\n {{on \"click\" focusInput}}\n >\n <button\n class={{TRIGGER_CLASSES}}\n data-slot=\"button\"\n data-variant=\"ghost\"\n data-size=\"xs\"\n {{this.nameTrigger @countryLabel}}\n {{s.trigger}}\n >\n <span class=\"jn-phone-input-flag\" aria-hidden=\"true\">{{this.flag}}</span>\n <VisuallyHidden data-test-jn-part=\"country-name\">{{this.countryName}}</VisuallyHidden>\n {{#if this.national}}\n <span class=\"jn-phone-input-calling-code\" data-test-jn-part=\"calling-code\">\n +{{this.callingCode}}\n </span>\n {{/if}}\n <Icon\n @icon={{if @icon @icon ChevronDown}}\n class=\"jn-phone-input-chevron\"\n aria-hidden=\"true\"\n />\n </button>\n </div>\n </div>\n <s.Content\n @position=\"popper\"\n @align=\"start\"\n @onCloseAutoFocus={{this.onCloseAutoFocus}}\n class=\"jn-phone-input-content\"\n >\n {{#each this.countries key=\"code\" as |country|}}\n <s.Item @value={{country.code}} @textValue={{country.name}} class=\"jn-phone-input-item\">\n <span class=\"jn-phone-input-flag\" aria-hidden=\"true\">{{country.flag}}</span>\n <span class=\"jn-phone-input-item-name\">{{country.name}}</span>\n <span class=\"jn-phone-input-item-code\">+{{country.callingCode}}</span>\n </s.Item>\n {{/each}}\n </s.Content>\n </Select>\n <span id={{this.countryWordId}} hidden>{{t messages.countryWord}}</span>\n {{#if @formName}}\n <input type=\"hidden\" name={{@formName}} value={{if this.value this.value \"\"}} />\n {{/if}}\n </div>\n </template>\n}\n"],"names":["displayContents","modifier","element","style","setProperty","internationalNumber","text","number","trim","replace","test","undefined","WHOLE_INSERTS","Set","focusInput","event","target","closest","addon","currentTarget","parentElement","querySelector","focus","INPUT_CLASSES","inputClasses","ADDON_CLASSES","inputGroupAddonClasses","align","TRIGGER_CLASSES","inputGroupButtonClasses","variant","size","PhoneInput","Component","g","prototype","tracked","i","valueState","ControllableState","name","value","args","defaultValue","onChange","onValueChange","fallback","countryState","country","defaultCountry","countryOfPhone","metadata","countries","onCountryChange","phoneMetadata","national","chosen","callingCode","flag","countryFlag","countryName","nameOf","countryWordId","idFor","locale","translator","displayNames","Intl","DisplayNames","type","n","cached","code","of","given","phoneCountries","map","collator","Collator","sort","a","b","compare","options","maskFor","key","mask","phoneMask","toE164","digits","formatter","AsYouType","input","getNumber","nationalNumber","getNumberValue","format","maskitoTransform","parsed","parsePhoneNumberFromString","formatNational","nationalNumberOf","shown","typed","onText","set","chooseCountry","moved","takeNumber","allowed","includes","fitsMask","wholeNumbers","read","preventDefault","stopImmediatePropagation","onPaste","clipboardData","getData","onBeforeInput","has","inputType","cancelable","data","dataTransfer","onInput","capture","addEventListener","removeEventListener","nameTrigger","trigger","label","setAttribute","removeAttribute","Map","ids","getAttribute","labels","length","index","id","join","translate","messages","namedCountry","onCloseAutoFocus","waitForPromise","renderSettled","then","onOpenChange","open","registerInput","setComponentTemplate","precompileTemplate","strictMode","scope","Select","nativeValue","maskito","on","VisuallyHidden","Icon","ChevronDown","t"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA4FA,gGACA,MAAMA,eAAA,GAAkBC,QAAA,CAAUC,OAAS,IAAA;EACzCA,OAAA,CAAQC,KAAK,CAACC,WAAW,CAAC,SAAA,EAAW,UAAA,CAAA;AACvC,CAAA,CAAA;AAEA;;;AAGC;AACD,SAASC,mBAAAA,CAAoBC,IAAY,EAAqB;EAC5D,MAAMC,MAAA,GAASD,IAAA,CACZE,IAAI,EAAA,CACJC,OAAO,CAAC,KAAA,EAAO,G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/masked-input.js';
2
+ export { default as PhoneInput } from './components/phone-input.js';
3
+ export { maskText, maskito } from './maskito.js';
4
+ export { messages } from './messages.js';
5
+ export { callingCode, callingCodeOf, countryFlag, countryOfPhone, formatPhone, isValidPhone, phoneCountries, phoneMask, phoneMetadata } from './phone.js';
6
+ export { dateMask, dateRangeMask, dateTimeMask, formatDate, formatNumber, formatTime, numberMask, parseDate, parseNumber, parseTime, timeMask } from './presets.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
@@ -0,0 +1,87 @@
1
+ import { registerDestructor } from '@ember/destroyable';
2
+ import Modifier from 'ember-modifier';
3
+ import { Maskito, maskitoTransform } from '@maskito/core';
4
+
5
+ /** The input or textarea a mask goes on. */
6
+
7
+ /**
8
+ * Puts what the element holds through the mask now, as Maskito does after an `input` it didn't
9
+ * see coming (a paste or autofill without `beforeinput`, or a test's `fillIn`). Maskito does the
10
+ * same once the event reaches its own listener, and then sends a second `input` with the masked
11
+ * text; doing it here, in the capture phase, first, means every `input` listener on the element,
12
+ * wherever it was added, reads the masked text.
13
+ */
14
+ function fitToMask(element, options, event) {
15
+ if (event.inputType === 'insertCompositionText') return;
16
+ const {
17
+ value,
18
+ selectionStart,
19
+ selectionEnd
20
+ } = element;
21
+ const state = maskitoTransform({
22
+ value,
23
+ selection: [selectionStart ?? 0, selectionEnd ?? 0]
24
+ }, options);
25
+ if (state.value === value) return;
26
+ element.value = state.value;
27
+ if (element.matches(':focus')) element.setSelectionRange(...state.selection);
28
+ }
29
+
30
+ /**
31
+ * Masks an `<input>` or `<textarea>` of your own as it is typed in, with
32
+ * [Maskito](https://maskito.dev): give it any Maskito options, such as those of a preset
33
+ * (`numberMask`, `dateMask`, `timeMask`…) or your own `{ mask: [...] }`.
34
+ *
35
+ * <input {{asInput}} {{maskito this.cardMask}} {{on "input" this.update}} />
36
+ *
37
+ * The mask shapes what is typed, pasted, dropped and autofilled, and every `input` listener on
38
+ * the element reads the masked text. New options replace the mask; none (`undefined`) takes it
39
+ * off; the same options object keeps the mask as it is, history and all, so build them once
40
+ * (a getter with `@cached`, a constant, or a helper whose arguments don't change). A value set
41
+ * from code isn't masked: put it through `maskText()` first, as `MaskedInput` does.
42
+ *
43
+ * It isn't named after the element: a binding named `input` would turn every `<input>` in the
44
+ * template into a call to it.
45
+ */
46
+ class maskito extends Modifier {
47
+ #element;
48
+ #options;
49
+ #teardown;
50
+ modify(element, [options]) {
51
+ // The same options keep the same mask, with its history and its caret.
52
+ if (element === this.#element && options === this.#options) return;
53
+ if (!this.#element) registerDestructor(this, () => this.#teardown?.());
54
+ this.#teardown?.();
55
+ this.#teardown = undefined;
56
+ this.#element = element;
57
+ this.#options = options;
58
+ if (!options) return;
59
+ const instance = new Maskito(element, options);
60
+ const onInput = event => fitToMask(element, options, event);
61
+ element.addEventListener('input', onInput, {
62
+ capture: true
63
+ });
64
+ this.#teardown = () => {
65
+ element.removeEventListener('input', onInput, {
66
+ capture: true
67
+ });
68
+ instance.destroy();
69
+ };
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Text as the mask would shape it: what a value set from code looks like masked.
75
+ *
76
+ * maskText('5551234567', phoneMask) // '(555) 123-4567'
77
+ *
78
+ * Without options, or without text (`''`, `undefined`), it is returned as it is.
79
+ */
80
+
81
+ function maskText(text, options) {
82
+ if (!options || !text) return text;
83
+ return maskitoTransform(text, options);
84
+ }
85
+
86
+ export { maskText, maskito };
87
+ //# sourceMappingURL=maskito.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maskito.js","sources":["../src/maskito.ts"],"sourcesContent":["import { registerDestructor } from '@ember/destroyable'\nimport Modifier from 'ember-modifier'\nimport { Maskito, maskitoTransform, type MaskitoOptions } from '@maskito/core'\n\nexport type { MaskitoOptions }\n\n/** The input or textarea a mask goes on. */\nexport type MaskedElement = HTMLInputElement | HTMLTextAreaElement\n\nexport interface MaskitoSignature {\n Element: MaskedElement\n Args: {\n /** The Maskito options, such as a preset's (`numberMask()`); none leaves the element alone. */\n Positional: [options: MaskitoOptions | null | undefined]\n }\n}\n\n/**\n * Puts what the element holds through the mask now, as Maskito does after an `input` it didn't\n * see coming (a paste or autofill without `beforeinput`, or a test's `fillIn`). Maskito does the\n * same once the event reaches its own listener, and then sends a second `input` with the masked\n * text; doing it here, in the capture phase, first, means every `input` listener on the element,\n * wherever it was added, reads the masked text.\n */\nfunction fitToMask(element: MaskedElement, options: MaskitoOptions, event: Event): void {\n if ((event as InputEvent).inputType === 'insertCompositionText') return\n const { value, selectionStart, selectionEnd } = element\n const state = maskitoTransform(\n { value, selection: [selectionStart ?? 0, selectionEnd ?? 0] },\n options,\n )\n if (state.value === value) return\n element.value = state.value\n if (element.matches(':focus')) element.setSelectionRange(...state.selection)\n}\n\n/**\n * Masks an `<input>` or `<textarea>` of your own as it is typed in, with\n * [Maskito](https://maskito.dev): give it any Maskito options, such as those of a preset\n * (`numberMask`, `dateMask`, `timeMask`…) or your own `{ mask: [...] }`.\n *\n * <input {{asInput}} {{maskito this.cardMask}} {{on \"input\" this.update}} />\n *\n * The mask shapes what is typed, pasted, dropped and autofilled, and every `input` listener on\n * the element reads the masked text. New options replace the mask; none (`undefined`) takes it\n * off; the same options object keeps the mask as it is, history and all, so build them once\n * (a getter with `@cached`, a constant, or a helper whose arguments don't change). A value set\n * from code isn't masked: put it through `maskText()` first, as `MaskedInput` does.\n *\n * It isn't named after the element: a binding named `input` would turn every `<input>` in the\n * template into a call to it.\n */\nexport class maskito extends Modifier<MaskitoSignature> {\n #element: MaskedElement | undefined\n #options: MaskitoOptions | null | undefined\n #teardown: (() => void) | undefined\n\n override modify(element: MaskedElement, [options]: [MaskitoOptions | null | undefined]): void {\n // The same options keep the same mask, with its history and its caret.\n if (element === this.#element && options === this.#options) return\n if (!this.#element) registerDestructor(this, () => this.#teardown?.())\n this.#teardown?.()\n this.#teardown = undefined\n this.#element = element\n this.#options = options\n if (!options) return\n const instance = new Maskito(element, options)\n const onInput = (event: Event) => fitToMask(element, options, event)\n element.addEventListener('input', onInput, { capture: true })\n this.#teardown = () => {\n element.removeEventListener('input', onInput, { capture: true })\n instance.destroy()\n }\n }\n}\n\n/**\n * Text as the mask would shape it: what a value set from code looks like masked.\n *\n * maskText('5551234567', phoneMask) // '(555) 123-4567'\n *\n * Without options, or without text (`''`, `undefined`), it is returned as it is.\n */\nexport function maskText(text: string, options?: MaskitoOptions | null): string\nexport function maskText(\n text: string | undefined,\n options?: MaskitoOptions | null,\n): string | undefined\nexport function maskText(\n text: string | undefined,\n options?: MaskitoOptions | null,\n): string | undefined {\n if (!options || !text) return text\n return maskitoTransform(text, options)\n}\n"],"names":["fitToMask","element","options","event","inputType","value","selectionStart","selectionEnd","state","maskitoTransform","selection","matches","setSelectionRange","maskito","Modifier","modify","registerDestructor","undefined","instance","Maskito","onInput","addEventListener","capture","removeEventListener","destroy","maskText","text"],"mappings":";;;;AAMA;;AAWA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,SAASA,CAACC,OAAsB,EAAEC,OAAuB,EAAEC,KAAY,EAAQ;AACtF,EAAA,IAAKA,KAAK,CAAgBC,SAAS,KAAK,uBAAuB,EAAE;EACjE,MAAM;IAAEC,KAAK;IAAEC,cAAc;AAAEC,IAAAA;AAAa,GAAC,GAAGN,OAAO;EACvD,MAAMO,KAAK,GAAGC,gBAAgB,CAC5B;IAAEJ,KAAK;IAAEK,SAAS,EAAE,CAACJ,cAAc,IAAI,CAAC,EAAEC,YAAY,IAAI,CAAC;GAAG,EAC9DL,OACF,CAAC;AACD,EAAA,IAAIM,KAAK,CAACH,KAAK,KAAKA,KAAK,EAAE;AAC3BJ,EAAAA,OAAO,CAACI,KAAK,GAAGG,KAAK,CAACH,KAAK;AAC3B,EAAA,IAAIJ,OAAO,CAACU,OAAO,CAAC,QAAQ,CAAC,EAAEV,OAAO,CAACW,iBAAiB,CAAC,GAAGJ,KAAK,CAACE,SAAS,CAAC;A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B,OAA+B,EACX;AACpB,EAAA,IAAI,CAACA,OAAO,IAAI,CAACwB,IAAI,EAAE,OAAOA,IAAI;AAClC,EAAA,OAAOjB,gBAAgB,CAACiB,IAAI,EAAExB,OAAO,CAAC;AACxC;;"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Every string `@ember-joinery/maskito` shows or announces itself, in English, for a
3
+ * translation catalog. An argument (`@countryLabel`) still beats each one.
4
+ */
5
+ const messages = {
6
+ country: {
7
+ id: 'joinery.phone-input.country',
8
+ message: 'Country'
9
+ },
10
+ namedCountry: {
11
+ id: 'joinery.phone-input.namedCountry',
12
+ message: '{name} country',
13
+ description: "The country picker's name, after its input's: {name} is the input's name."
14
+ },
15
+ countryWord: {
16
+ id: 'joinery.phone-input.countryWord',
17
+ message: 'country',
18
+ description: "Read after the input's label to name its country picker: “Phone country”."
19
+ }
20
+ };
21
+
22
+ export { messages };
23
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sources":["../src/messages.ts"],"sourcesContent":["import type { Message } from '@ember-joinery/primitives/i18n'\n\n/**\n * Every string `@ember-joinery/maskito` shows or announces itself, in English, for a\n * translation catalog. An argument (`@countryLabel`) still beats each one.\n */\nexport const messages = {\n country: { id: 'joinery.phone-input.country', message: 'Country' },\n namedCountry: {\n id: 'joinery.phone-input.namedCountry',\n message: '{name} country',\n description: \"The country picker's name, after its input's: {name} is the input's name.\",\n },\n countryWord: {\n id: 'joinery.phone-input.countryWord',\n message: 'country',\n description: \"Read after the input's label to name its country picker: “Phone country”.\",\n },\n} satisfies Record<string, Message>\n"],"names":["messages","country","id","message","namedCountry","description","countryWord"],"mappings":"AAEA;AACA;AACA;AACA;AACO,MAAMA,QAAQ,GAAG;AACtBC,EAAAA,OAAO,EAAE;AAAEC,IAAAA,EAAE,EAAE,6BAA6B;AAAEC,IAAAA,OAAO,EAAE;GAAW;AAClEC,EAAAA,YAAY,EAAE;AACZF,IAAAA,EAAE,EAAE,kCAAkC;AACtCC,IAAAA,OAAO,EAAE,gBAAgB;AACzBE,IAAAA,WAAW,EAAE;GACd;AACDC,EAAAA,WAAW,EAAE;AACXJ,IAAAA,EAAE,EAAE,iCAAiC;AACrCC,IAAAA,OAAO,EAAE,SAAS;AAClBE,IAAAA,WAAW,EAAE;AACf;AACF;;"}