@m3ui-vue/m3ui-vue 1.4.1 → 1.4.2
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/dist/API.md +251 -9
- package/dist/{MButton-DQMTIlaV.js → MButton-D7M324r9.js} +2 -2
- package/dist/{MButton-DQMTIlaV.js.map → MButton-D7M324r9.js.map} +1 -1
- package/dist/{MDialog-cdgapGfZ.js → MDialog-DuOnNF9s.js} +2 -2
- package/dist/{MDialog-cdgapGfZ.js.map → MDialog-DuOnNF9s.js.map} +1 -1
- package/dist/{MListItem-ZkSGIIrb.js → MListItem-Bb84X6ex.js} +144 -120
- package/dist/{MListItem-ZkSGIIrb.js.map → MListItem-Bb84X6ex.js.map} +1 -1
- package/dist/{MLoadingIndicator-U6_oGx8z.js → MLoadingIndicator-BUGRo2Tu.js} +2 -2
- package/dist/{MLoadingIndicator-U6_oGx8z.js.map → MLoadingIndicator-BUGRo2Tu.js.map} +1 -1
- package/dist/{MSeekBar-DTZVizZg.js → MSeekBar-BQUvUqb9.js} +2 -2
- package/dist/{MSeekBar-DTZVizZg.js.map → MSeekBar-BQUvUqb9.js.map} +1 -1
- package/dist/{MSlider-CqJkEYRZ.js → MSlider-SFdjE1Hy.js} +2 -2
- package/dist/{MSlider-CqJkEYRZ.js.map → MSlider-SFdjE1Hy.js.map} +1 -1
- package/dist/{MSpinner-Btgc-0nb.js → MSpinner-DKGNN64_.js} +2 -2
- package/dist/{MSpinner-Btgc-0nb.js.map → MSpinner-DKGNN64_.js.map} +1 -1
- package/dist/{MTextField-B6M0gJEO.js → MTextField-Utsef6Kk.js} +2 -2
- package/dist/{MTextField-B6M0gJEO.js.map → MTextField-Utsef6Kk.js.map} +1 -1
- package/dist/{MTooltip-Dspji4XG.js → MTooltip-BhTFQ9Z2.js} +4 -4
- package/dist/{MTooltip-Dspji4XG.js.map → MTooltip-BhTFQ9Z2.js.map} +1 -1
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAvailabilityCalendar.vue.d.ts +1 -1
- package/dist/components/MCalendarHeatmap.vue.d.ts +120 -0
- package/dist/components/MDatePicker.vue.d.ts +5 -0
- package/dist/components/MDatePickerModal.vue.d.ts +5 -0
- package/dist/components/MDateRangePicker.vue.d.ts +5 -0
- package/dist/components/MDateRangePickerModal.vue.d.ts +5 -0
- package/dist/components/MLightbox.vue.d.ts +1 -1
- package/dist/components/MListItem.vue.d.ts +10 -6
- package/dist/components/MMeterGroup.vue.d.ts +98 -0
- package/dist/components/MPhoneInput.vue.d.ts +72 -0
- package/dist/components/MSeekBar.vue.d.ts +1 -1
- package/dist/components/MTreeTable.vue.d.ts +1 -1
- package/dist/components/MTypewriter.vue.d.ts +57 -0
- package/dist/components/MVideoPlayer.vue.d.ts +1 -1
- package/dist/components/_MMonthYearButton.vue.d.ts +16 -0
- package/dist/components/_MYearGrid.vue.d.ts +19 -0
- package/dist/composables/_useCalendarMonth.d.ts +7 -0
- package/dist/composables/phoneNumber.d.ts +39 -0
- package/dist/composables/useLocale.d.ts +18 -0
- package/dist/data/phone-countries.d.ts +2 -0
- package/dist/draw.js +2 -2
- package/dist/flow.js +1 -1
- package/dist/gantt.js +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/locales/index.js +63 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3615 -1999
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +5 -5
- package/dist/media.js +4 -4
- package/dist/pdf.js +4 -4
- package/dist/rich-text-editor.js +4 -4
- package/dist/styles.css +1 -1
- package/dist/{useLocale-DLKJIRFi.js → useLocale-B8YKWg-H.js} +10 -1
- package/dist/useLocale-B8YKWg-H.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MCalendarHeatmap.test.ts +79 -0
- package/src/__tests__/MListItemNestedControl.test.ts +88 -0
- package/src/__tests__/MMeterGroup.test.ts +43 -0
- package/src/__tests__/MPhoneInput.test.ts +97 -0
- package/src/__tests__/MTypewriter.test.ts +50 -0
- package/src/__tests__/calendarMonth.test.ts +104 -0
- package/src/__tests__/ssr.test.ts +4 -0
- package/src/components/MCalendarHeatmap.vue +349 -0
- package/src/components/MDatePicker.vue +74 -34
- package/src/components/MDatePickerModal.vue +79 -36
- package/src/components/MDateRangePicker.vue +73 -33
- package/src/components/MDateRangePickerModal.vue +75 -35
- package/src/components/MListItem.vue +54 -8
- package/src/components/MMeterGroup.vue +196 -0
- package/src/components/MPhoneInput.vue +480 -0
- package/src/components/MTypewriter.vue +195 -0
- package/src/components/_MMonthYearButton.vue +36 -0
- package/src/components/_MYearGrid.vue +106 -0
- package/src/composables/_useCalendarMonth.ts +32 -2
- package/src/composables/phoneNumber.ts +137 -0
- package/src/composables/useLocale.ts +27 -0
- package/src/data/phone-countries.ts +62 -0
- package/src/index.ts +8 -0
- package/src/locales/de.ts +9 -0
- package/src/locales/es.ts +9 -0
- package/src/locales/fr.ts +9 -0
- package/src/locales/ja.ts +9 -0
- package/src/locales/ko.ts +9 -0
- package/src/locales/pt.ts +9 -0
- package/src/locales/zh.ts +9 -0
- package/dist/useLocale-DLKJIRFi.js.map +0 -1
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* MPhoneInput — a phone number field with a country selector: flag, calling code and a searchable list of every
|
|
4
|
+
* country (names from `Intl` in the app's language). The number is formatted as it's typed with the country's
|
|
5
|
+
* mask (Argentina's mobiles with the 9 too) and bound as E.164 (`+541123456789`), ready for an API or an SMS
|
|
6
|
+
* provider. Pasting or typing a full international number (`+34...`, `0034...`) switches the country. Without a
|
|
7
|
+
* `country`, it starts on the region of the app's locale (es-AR → Argentina). Filled or outlined, like MTextField.
|
|
8
|
+
*
|
|
9
|
+
* @slot flag Replaces the flag emoji (on the button and in the list), e.g. with an image set that also shows on
|
|
10
|
+
* Windows, which has no flag emoji; receives `iso2`
|
|
11
|
+
* @event update:modelValue Emitted with the number in E.164, or null when empty — powers v-model
|
|
12
|
+
* @event update:country Emitted with the ISO code when the country changes — powers v-model:country
|
|
13
|
+
* @event update:valid Emitted when the number becomes (in)valid for its country — powers v-model:valid
|
|
14
|
+
*/
|
|
15
|
+
import { computed, nextTick, onBeforeUnmount, ref, useId, watch } from 'vue'
|
|
16
|
+
import MIcon from './MIcon.vue'
|
|
17
|
+
import { useFieldBg } from '../composables/useFieldBg'
|
|
18
|
+
import { useLocale } from '../composables/useLocale'
|
|
19
|
+
import { POPUP_MARGIN, dropdownX, fitPopup } from '../composables/_popupPosition'
|
|
20
|
+
import { useEscapeStack } from '../composables/_useEscapeStack'
|
|
21
|
+
import {
|
|
22
|
+
flagEmoji,
|
|
23
|
+
formatNational,
|
|
24
|
+
isValidNational,
|
|
25
|
+
nationalDigits,
|
|
26
|
+
parsePhone,
|
|
27
|
+
phoneCountries,
|
|
28
|
+
phoneCountry,
|
|
29
|
+
toE164,
|
|
30
|
+
type PhoneCountry,
|
|
31
|
+
} from '../composables/phoneNumber'
|
|
32
|
+
|
|
33
|
+
const props = withDefaults(
|
|
34
|
+
defineProps<{
|
|
35
|
+
/** The number in E.164 (`+541123456789`), or null (v-model) */
|
|
36
|
+
modelValue?: string | null
|
|
37
|
+
/** Field label */
|
|
38
|
+
label: string
|
|
39
|
+
/** ISO 3166-1 alpha-2 code of the country (v-model:country) */
|
|
40
|
+
country?: string
|
|
41
|
+
/** Country to start on when there's neither a number nor `country`; defaults to the locale's region, else US */
|
|
42
|
+
defaultCountry?: string
|
|
43
|
+
/** Only these countries (ISO codes) in the list */
|
|
44
|
+
countries?: string[]
|
|
45
|
+
/** Countries pinned at the top of the list */
|
|
46
|
+
preferred?: string[]
|
|
47
|
+
/** Visual style */
|
|
48
|
+
variant?: 'filled' | 'outlined'
|
|
49
|
+
/** Puts the field in error state */
|
|
50
|
+
error?: boolean
|
|
51
|
+
/** Error message below the field (when `error`, or when `validate` finds the number incomplete) */
|
|
52
|
+
errorLabel?: string
|
|
53
|
+
/** Hint text below the field */
|
|
54
|
+
hint?: string
|
|
55
|
+
/** Disables the field */
|
|
56
|
+
disabled?: boolean
|
|
57
|
+
/** Marks as required — adds a red asterisk after the label */
|
|
58
|
+
required?: boolean
|
|
59
|
+
/** After leaving the field, show an error when the number doesn't fit its country's length */
|
|
60
|
+
validate?: boolean
|
|
61
|
+
/** Placeholder; defaults to an example in the country's format */
|
|
62
|
+
placeholder?: string
|
|
63
|
+
/** Show flag emoji */
|
|
64
|
+
flags?: boolean
|
|
65
|
+
/** Overrides the auto-detected background behind the outlined label */
|
|
66
|
+
fieldBg?: string
|
|
67
|
+
/** BCP-47 language for country names; defaults to the app's locale */
|
|
68
|
+
locale?: string
|
|
69
|
+
}>(),
|
|
70
|
+
{ modelValue: null, variant: 'filled', validate: true, flags: true },
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const emit = defineEmits<{
|
|
74
|
+
'update:modelValue': [string | null]
|
|
75
|
+
'update:country': [string]
|
|
76
|
+
'update:valid': [boolean]
|
|
77
|
+
}>()
|
|
78
|
+
|
|
79
|
+
const t = useLocale()
|
|
80
|
+
const lang = computed(() => props.locale ?? t.lang)
|
|
81
|
+
const id = useId()
|
|
82
|
+
const listId = `${id}-countries`
|
|
83
|
+
|
|
84
|
+
// ── Country and number ────────────────────────────────────────────
|
|
85
|
+
function localeRegion() {
|
|
86
|
+
const tags = [props.locale ?? t.lang, typeof navigator !== 'undefined' ? navigator.language : '']
|
|
87
|
+
for (const tag of tags) {
|
|
88
|
+
try {
|
|
89
|
+
const region = tag && new Intl.Locale(tag).maximize().region
|
|
90
|
+
if (region && phoneCountry(region)) return region
|
|
91
|
+
} catch { /* not a valid tag */ }
|
|
92
|
+
}
|
|
93
|
+
return 'US'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const initial = props.modelValue ? parsePhone(props.modelValue, props.country) : null
|
|
97
|
+
const iso = ref((props.country ?? initial?.country.iso2 ?? props.defaultCountry ?? localeRegion()).toUpperCase())
|
|
98
|
+
const country = computed<PhoneCountry>(() => phoneCountry(iso.value) ?? phoneCountry('US')!)
|
|
99
|
+
const national = ref(initial?.national ?? '')
|
|
100
|
+
const e164 = computed(() => toE164(national.value, country.value))
|
|
101
|
+
const valid = computed(() => isValidNational(national.value, country.value))
|
|
102
|
+
|
|
103
|
+
watch(() => props.country, (c) => { if (c && c.toUpperCase() !== iso.value) setCountry(c.toUpperCase(), false) })
|
|
104
|
+
watch(() => props.modelValue, (v) => {
|
|
105
|
+
if ((v ?? null) === e164.value) return
|
|
106
|
+
const p = v ? parsePhone(v, iso.value) : null
|
|
107
|
+
national.value = p?.national ?? ''
|
|
108
|
+
if (p && p.country.iso2 !== iso.value) {
|
|
109
|
+
iso.value = p.country.iso2
|
|
110
|
+
emit('update:country', iso.value)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
watch(valid, (v) => emit('update:valid', v), { immediate: true })
|
|
114
|
+
// v-model:country gets the country picked from the number or the locale when it didn't set one
|
|
115
|
+
if (props.country?.toUpperCase() !== iso.value) emit('update:country', iso.value)
|
|
116
|
+
|
|
117
|
+
function setCountry(code: string, notify = true) {
|
|
118
|
+
if (!phoneCountry(code)) return
|
|
119
|
+
iso.value = code
|
|
120
|
+
national.value = nationalDigits(national.value, country.value)
|
|
121
|
+
if (notify) emit('update:country', code)
|
|
122
|
+
emit('update:modelValue', e164.value)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const display = computed(() => formatNational(national.value, country.value))
|
|
126
|
+
const examplePlaceholder = computed(() => {
|
|
127
|
+
const mask = country.value.masks[0]
|
|
128
|
+
let n = 0
|
|
129
|
+
return mask ? mask.replace(/#/g, () => String((n++ % 9) + 1)) : ''
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
// ── Typing ────────────────────────────────────────────────────────
|
|
133
|
+
const inputEl = ref<HTMLInputElement>()
|
|
134
|
+
const touched = ref(false)
|
|
135
|
+
|
|
136
|
+
// Puts the caret after the n-th digit of the formatted text, so reformatting doesn't throw it to the end
|
|
137
|
+
function caretAfterDigits(text: string, n: number) {
|
|
138
|
+
if (n <= 0) return 0
|
|
139
|
+
let seen = 0
|
|
140
|
+
for (let i = 0; i < text.length; i++) if (/\d/.test(text[i]!) && ++seen === n) return i + 1
|
|
141
|
+
return text.length
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function commit(raw: string, digitsBeforeCaret: number) {
|
|
145
|
+
const trimmed = raw.trim()
|
|
146
|
+
let dropped = 0
|
|
147
|
+
if (trimmed.startsWith('+') || trimmed.startsWith('00')) {
|
|
148
|
+
// A full international number: the country comes from its calling code
|
|
149
|
+
const p = parsePhone(trimmed, iso.value)
|
|
150
|
+
if (p) {
|
|
151
|
+
if (p.country.iso2 !== iso.value) {
|
|
152
|
+
iso.value = p.country.iso2
|
|
153
|
+
emit('update:country', iso.value)
|
|
154
|
+
}
|
|
155
|
+
national.value = p.national
|
|
156
|
+
digitsBeforeCaret = p.national.length
|
|
157
|
+
} else national.value = ''
|
|
158
|
+
} else {
|
|
159
|
+
const before = raw.replace(/\D/g, '').length
|
|
160
|
+
national.value = nationalDigits(raw, country.value)
|
|
161
|
+
dropped = before - national.value.length
|
|
162
|
+
}
|
|
163
|
+
emit('update:modelValue', e164.value)
|
|
164
|
+
const el = inputEl.value
|
|
165
|
+
if (!el) return
|
|
166
|
+
el.value = display.value
|
|
167
|
+
const pos = caretAfterDigits(display.value, digitsBeforeCaret - dropped)
|
|
168
|
+
el.setSelectionRange(pos, pos)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function onInput(e: Event) {
|
|
172
|
+
const el = e.target as HTMLInputElement
|
|
173
|
+
const caret = el.selectionStart ?? el.value.length
|
|
174
|
+
commit(el.value, el.value.slice(0, caret).replace(/\D/g, '').length)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Backspace right after a separator ("(212) |") deletes the digit before it instead of a separator the mask puts back
|
|
178
|
+
function onKeydown(e: KeyboardEvent) {
|
|
179
|
+
const el = e.target as HTMLInputElement
|
|
180
|
+
if (e.key !== 'Backspace' || el.selectionStart !== el.selectionEnd || !el.selectionStart) return
|
|
181
|
+
const before = el.value.slice(0, el.selectionStart)
|
|
182
|
+
if (/\d$/.test(before)) return
|
|
183
|
+
e.preventDefault()
|
|
184
|
+
const digitsBefore = before.replace(/\D/g, '').length
|
|
185
|
+
if (!digitsBefore) return
|
|
186
|
+
const digits = el.value.replace(/\D/g, '')
|
|
187
|
+
commit(digits.slice(0, digitsBefore - 1) + digits.slice(digitsBefore), digitsBefore - 1)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const showInvalid = computed(() => props.validate && touched.value && !!national.value && !valid.value)
|
|
191
|
+
const isError = computed(() => props.error || showInvalid.value)
|
|
192
|
+
const errorText = computed(() => props.errorLabel ?? (showInvalid.value ? t.phoneInvalid : undefined))
|
|
193
|
+
|
|
194
|
+
// ── Country list ──────────────────────────────────────────────────
|
|
195
|
+
const open = ref(false)
|
|
196
|
+
const search = ref('')
|
|
197
|
+
const active = ref(0)
|
|
198
|
+
const fieldEl = ref<HTMLElement | null>(null)
|
|
199
|
+
const buttonEl = ref<HTMLButtonElement>()
|
|
200
|
+
const panelEl = ref<HTMLElement>()
|
|
201
|
+
const searchEl = ref<HTMLInputElement>()
|
|
202
|
+
const listEl = ref<HTMLElement>()
|
|
203
|
+
const panelPos = ref<Record<string, string>>({})
|
|
204
|
+
|
|
205
|
+
const { resolvedFieldBg } = useFieldBg(fieldEl, () => props.fieldBg)
|
|
206
|
+
|
|
207
|
+
const regionNames = computed(() => {
|
|
208
|
+
try {
|
|
209
|
+
return new Intl.DisplayNames([lang.value, 'en'], { type: 'region' })
|
|
210
|
+
} catch {
|
|
211
|
+
return null
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
const nameOf = (code: string) => {
|
|
215
|
+
try {
|
|
216
|
+
return regionNames.value?.of(code) ?? code
|
|
217
|
+
} catch {
|
|
218
|
+
return code
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const fold = (s: string) => s.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()
|
|
222
|
+
|
|
223
|
+
const allCountries = computed(() => {
|
|
224
|
+
const only = props.countries?.map((c) => c.toUpperCase())
|
|
225
|
+
const list = phoneCountries()
|
|
226
|
+
.filter((c) => !only || only.includes(c.iso2))
|
|
227
|
+
.map((c) => ({ ...c, name: nameOf(c.iso2) }))
|
|
228
|
+
return list.sort((a, b) => a.name.localeCompare(b.name, lang.value))
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
const options = computed(() => {
|
|
232
|
+
const q = fold(search.value.trim()).replace(/^\+/, '')
|
|
233
|
+
if (q) {
|
|
234
|
+
return allCountries.value
|
|
235
|
+
.filter((c) => fold(c.name).includes(q) || c.iso2.toLowerCase() === q || c.dial.startsWith(q))
|
|
236
|
+
.map((c) => ({ ...c, pinned: false }))
|
|
237
|
+
}
|
|
238
|
+
const pinned = (props.preferred ?? []).map((p) => allCountries.value.find((c) => c.iso2 === p.toUpperCase())).filter((c) => !!c)
|
|
239
|
+
return [...pinned.map((c) => ({ ...c, pinned: true })), ...allCountries.value.map((c) => ({ ...c, pinned: false }))]
|
|
240
|
+
})
|
|
241
|
+
const lastPinned = computed(() => {
|
|
242
|
+
let last = -1
|
|
243
|
+
options.value.forEach((o, i) => { if (o.pinned) last = i })
|
|
244
|
+
return last
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
function place() {
|
|
248
|
+
const anchor = fieldEl.value?.getBoundingClientRect()
|
|
249
|
+
const panel = panelEl.value
|
|
250
|
+
if (!anchor || !panel) return
|
|
251
|
+
const width = Math.max(anchor.width, 280)
|
|
252
|
+
panel.style.width = `${width}px`
|
|
253
|
+
const h = panel.offsetHeight
|
|
254
|
+
const left = dropdownX(anchor, width, window.innerWidth, 'start')
|
|
255
|
+
const top = fitPopup([anchor.bottom + 4, anchor.top - 4 - h], h, window.innerHeight, POPUP_MARGIN)
|
|
256
|
+
panelPos.value = { left: `${left}px`, top: `${top}px`, width: `${width}px` }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function openList() {
|
|
260
|
+
if (props.disabled) return
|
|
261
|
+
open.value = true
|
|
262
|
+
search.value = ''
|
|
263
|
+
await nextTick()
|
|
264
|
+
active.value = Math.max(0, options.value.findIndex((o) => o.iso2 === iso.value && !o.pinned))
|
|
265
|
+
place()
|
|
266
|
+
searchEl.value?.focus({ preventScroll: true })
|
|
267
|
+
scrollActive('center')
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function closeList(refocus = true) {
|
|
271
|
+
if (!open.value) return
|
|
272
|
+
open.value = false
|
|
273
|
+
if (refocus) buttonEl.value?.focus()
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function choose(code: string) {
|
|
277
|
+
setCountry(code)
|
|
278
|
+
closeList(false)
|
|
279
|
+
inputEl.value?.focus()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function scrollActive(block: ScrollLogicalPosition = 'nearest') {
|
|
283
|
+
nextTick(() => listEl.value?.querySelector<HTMLElement>(`[data-index="${active.value}"]`)?.scrollIntoView({ block }))
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function onSearchKey(e: KeyboardEvent) {
|
|
287
|
+
const n = options.value.length
|
|
288
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
289
|
+
e.preventDefault()
|
|
290
|
+
if (!n) return
|
|
291
|
+
active.value = (active.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
|
292
|
+
scrollActive()
|
|
293
|
+
} else if (e.key === 'Enter') {
|
|
294
|
+
e.preventDefault()
|
|
295
|
+
const o = options.value[active.value]
|
|
296
|
+
if (o) choose(o.iso2)
|
|
297
|
+
} else if (e.key === 'Tab') closeList(false)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
watch(search, () => { active.value = 0 })
|
|
301
|
+
|
|
302
|
+
useEscapeStack(open, () => closeList())
|
|
303
|
+
|
|
304
|
+
function onDocDown(e: MouseEvent) {
|
|
305
|
+
const target = e.target as Node
|
|
306
|
+
if (panelEl.value?.contains(target) || buttonEl.value?.contains(target)) return
|
|
307
|
+
closeList(false)
|
|
308
|
+
}
|
|
309
|
+
function onScroll(e: Event) {
|
|
310
|
+
if (panelEl.value?.contains(e.target as Node)) return
|
|
311
|
+
place()
|
|
312
|
+
}
|
|
313
|
+
watch(open, (v) => {
|
|
314
|
+
if (v) {
|
|
315
|
+
document.addEventListener('mousedown', onDocDown)
|
|
316
|
+
window.addEventListener('scroll', onScroll, true)
|
|
317
|
+
window.addEventListener('resize', place)
|
|
318
|
+
} else {
|
|
319
|
+
document.removeEventListener('mousedown', onDocDown)
|
|
320
|
+
window.removeEventListener('scroll', onScroll, true)
|
|
321
|
+
window.removeEventListener('resize', place)
|
|
322
|
+
}
|
|
323
|
+
})
|
|
324
|
+
onBeforeUnmount(() => {
|
|
325
|
+
document.removeEventListener('mousedown', onDocDown)
|
|
326
|
+
window.removeEventListener('scroll', onScroll, true)
|
|
327
|
+
window.removeEventListener('resize', place)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
defineExpose({
|
|
331
|
+
focus: (options?: FocusOptions) => inputEl.value?.focus(options),
|
|
332
|
+
/** Whether the number fits its country's length */
|
|
333
|
+
valid,
|
|
334
|
+
})
|
|
335
|
+
</script>
|
|
336
|
+
|
|
337
|
+
<template>
|
|
338
|
+
<div class="m3-phone-input flex flex-col gap-1">
|
|
339
|
+
<div
|
|
340
|
+
ref="fieldEl"
|
|
341
|
+
class="group relative flex h-14 transition-[border-color,border-width] duration-150"
|
|
342
|
+
:class="[
|
|
343
|
+
variant === 'outlined'
|
|
344
|
+
? [
|
|
345
|
+
'mt-2 items-center rounded-xs border',
|
|
346
|
+
disabled
|
|
347
|
+
? 'border-on-surface/12'
|
|
348
|
+
: isError
|
|
349
|
+
? 'border-error focus-within:border-2 focus-within:border-error'
|
|
350
|
+
: 'border-outline hover:border-on-surface focus-within:border-2 focus-within:border-primary',
|
|
351
|
+
]
|
|
352
|
+
: [
|
|
353
|
+
'items-end rounded-t-xs border-b bg-surface-container-highest pb-2',
|
|
354
|
+
disabled
|
|
355
|
+
? 'border-b-on-surface/38 bg-[color-mix(in_srgb,var(--color-on-surface)_4%,var(--color-surface-container-highest))]'
|
|
356
|
+
: isError
|
|
357
|
+
? 'border-b-error focus-within:border-b-2 focus-within:border-b-error'
|
|
358
|
+
: 'border-b-on-surface-variant hover:border-b-on-surface focus-within:border-b-2 focus-within:border-b-primary',
|
|
359
|
+
],
|
|
360
|
+
]"
|
|
361
|
+
:style="variant === 'outlined' ? { '--field-bg': resolvedFieldBg } : undefined"
|
|
362
|
+
>
|
|
363
|
+
<!-- The label stays floated: the calling code is always there -->
|
|
364
|
+
<label
|
|
365
|
+
:for="id"
|
|
366
|
+
class="pointer-events-none absolute truncate text-label-small"
|
|
367
|
+
:class="[
|
|
368
|
+
variant === 'outlined' ? 'left-3 -top-2.5 max-w-[calc(100%-1.5rem)] bg-[var(--field-bg)] px-1' : 'left-4 top-2 right-4',
|
|
369
|
+
disabled ? 'text-on-surface/38' : isError ? 'text-error' : 'text-on-surface-variant group-focus-within:text-primary',
|
|
370
|
+
]"
|
|
371
|
+
>
|
|
372
|
+
{{ label }}<span v-if="required" class="text-error"> *</span>
|
|
373
|
+
</label>
|
|
374
|
+
|
|
375
|
+
<!-- Country -->
|
|
376
|
+
<button
|
|
377
|
+
ref="buttonEl"
|
|
378
|
+
type="button"
|
|
379
|
+
class="m3-phone-country ml-1 flex h-8 shrink-0 cursor-pointer items-center gap-1 rounded-full pr-1 pl-3 text-body-large text-on-surface outline-none transition-colors hover:bg-on-surface/8 focus-visible:ring-2 focus-visible:ring-secondary disabled:cursor-not-allowed disabled:text-on-surface/38"
|
|
380
|
+
:class="variant === 'filled' && '-mb-1'"
|
|
381
|
+
:disabled="disabled"
|
|
382
|
+
:aria-label="`${t.phoneCountry}: ${nameOf(country.iso2)} +${country.dial}`"
|
|
383
|
+
aria-haspopup="listbox"
|
|
384
|
+
:aria-expanded="open"
|
|
385
|
+
:aria-controls="open ? listId : undefined"
|
|
386
|
+
@click="open ? closeList() : openList()"
|
|
387
|
+
>
|
|
388
|
+
<slot name="flag" :iso2="country.iso2">
|
|
389
|
+
<span v-if="flags" class="text-[20px] leading-none" aria-hidden="true">{{ flagEmoji(country.iso2) }}</span>
|
|
390
|
+
</slot>
|
|
391
|
+
<span class="tabular-nums">+{{ country.dial }}</span>
|
|
392
|
+
<MIcon name="arrow_drop_down" :size="20" class="text-on-surface-variant transition-transform duration-200" :class="open && 'rotate-180'" />
|
|
393
|
+
</button>
|
|
394
|
+
|
|
395
|
+
<input
|
|
396
|
+
:id="id"
|
|
397
|
+
ref="inputEl"
|
|
398
|
+
type="tel"
|
|
399
|
+
inputmode="tel"
|
|
400
|
+
autocomplete="tel"
|
|
401
|
+
:value="display"
|
|
402
|
+
:disabled="disabled"
|
|
403
|
+
:required="required"
|
|
404
|
+
:placeholder="placeholder ?? examplePlaceholder"
|
|
405
|
+
:aria-invalid="isError || undefined"
|
|
406
|
+
class="min-w-0 flex-1 bg-transparent pr-4 pl-1 text-body-large text-on-surface outline-none placeholder:text-on-surface-variant/60 disabled:cursor-not-allowed disabled:text-on-surface/38"
|
|
407
|
+
:class="isError ? 'caret-error' : 'caret-primary'"
|
|
408
|
+
@input="onInput"
|
|
409
|
+
@keydown="onKeydown"
|
|
410
|
+
@blur="touched = true"
|
|
411
|
+
/>
|
|
412
|
+
</div>
|
|
413
|
+
|
|
414
|
+
<p v-if="isError && errorText" class="px-4 text-body-small text-error">{{ errorText }}</p>
|
|
415
|
+
<p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
|
|
416
|
+
|
|
417
|
+
<!-- Country list: a menu surface (surface-container, elevation 2) with a search field on top -->
|
|
418
|
+
<Teleport to="body">
|
|
419
|
+
<Transition
|
|
420
|
+
enter-active-class="transition duration-150 ease-out"
|
|
421
|
+
enter-from-class="opacity-0 -translate-y-1"
|
|
422
|
+
leave-active-class="transition duration-100 ease-in"
|
|
423
|
+
leave-to-class="opacity-0"
|
|
424
|
+
>
|
|
425
|
+
<div
|
|
426
|
+
v-if="open"
|
|
427
|
+
ref="panelEl"
|
|
428
|
+
class="fixed z-500 flex max-h-[min(360px,calc(100vh-16px))] flex-col overflow-hidden rounded-xs bg-surface-container py-2 shadow-elevation-2"
|
|
429
|
+
:style="panelPos"
|
|
430
|
+
>
|
|
431
|
+
<div class="px-3 pb-2">
|
|
432
|
+
<div class="flex h-10 items-center gap-2 rounded-full bg-surface-container-highest px-3 text-on-surface-variant focus-within:ring-2 focus-within:ring-primary">
|
|
433
|
+
<MIcon name="search" :size="20" />
|
|
434
|
+
<input
|
|
435
|
+
ref="searchEl"
|
|
436
|
+
v-model="search"
|
|
437
|
+
type="text"
|
|
438
|
+
class="min-w-0 flex-1 bg-transparent text-body-large text-on-surface outline-none placeholder:text-on-surface-variant"
|
|
439
|
+
:placeholder="t.phoneSearchCountry"
|
|
440
|
+
:aria-label="t.phoneSearchCountry"
|
|
441
|
+
role="combobox"
|
|
442
|
+
aria-expanded="true"
|
|
443
|
+
:aria-controls="listId"
|
|
444
|
+
:aria-activedescendant="options[active] ? `${listId}-${active}` : undefined"
|
|
445
|
+
autocomplete="off"
|
|
446
|
+
@keydown="onSearchKey"
|
|
447
|
+
/>
|
|
448
|
+
</div>
|
|
449
|
+
</div>
|
|
450
|
+
<ul :id="listId" ref="listEl" role="listbox" :aria-label="t.phoneCountry" class="min-h-0 flex-1 overflow-y-auto">
|
|
451
|
+
<template v-for="(o, i) in options" :key="`${o.iso2}-${o.pinned}`">
|
|
452
|
+
<li
|
|
453
|
+
:id="`${listId}-${i}`"
|
|
454
|
+
role="option"
|
|
455
|
+
:data-index="i"
|
|
456
|
+
:aria-selected="o.iso2 === iso"
|
|
457
|
+
class="flex h-12 cursor-pointer items-center gap-3 px-4 text-body-large"
|
|
458
|
+
:class="[
|
|
459
|
+
o.iso2 === iso ? 'bg-secondary-container text-on-secondary-container' : 'text-on-surface',
|
|
460
|
+
i === active && o.iso2 !== iso && 'bg-on-surface/8',
|
|
461
|
+
i === active && o.iso2 === iso && 'ring-2 ring-inset ring-secondary',
|
|
462
|
+
]"
|
|
463
|
+
@mousemove="active = i"
|
|
464
|
+
@click="choose(o.iso2)"
|
|
465
|
+
>
|
|
466
|
+
<slot name="flag" :iso2="o.iso2">
|
|
467
|
+
<span v-if="flags" class="text-[20px] leading-none" aria-hidden="true">{{ flagEmoji(o.iso2) }}</span>
|
|
468
|
+
</slot>
|
|
469
|
+
<span class="min-w-0 flex-1 truncate">{{ o.name }}</span>
|
|
470
|
+
<span class="tabular-nums text-on-surface-variant">+{{ o.dial }}</span>
|
|
471
|
+
</li>
|
|
472
|
+
<li v-if="i === lastPinned" role="separator" class="my-2 h-px bg-outline-variant" />
|
|
473
|
+
</template>
|
|
474
|
+
<li v-if="!options.length" class="px-4 py-3 text-body-medium text-on-surface-variant">{{ t.noResults }}</li>
|
|
475
|
+
</ul>
|
|
476
|
+
</div>
|
|
477
|
+
</Transition>
|
|
478
|
+
</Teleport>
|
|
479
|
+
</div>
|
|
480
|
+
</template>
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* MTypewriter — text that types itself out and, with several phrases, erases and types the next in a loop, behind a
|
|
4
|
+
* blinking caret. Only the end that differs is erased ("faster" → "prettier" keeps nothing, "build faster apps" →
|
|
5
|
+
* "build prettier apps" erases back to "build "). Works by grapheme, so emoji and accents count as one
|
|
6
|
+
* character. Starts when it scrolls into view; with reduced motion the phrases swap whole. Screen readers get every
|
|
7
|
+
* phrase once, not each keystroke.
|
|
8
|
+
*
|
|
9
|
+
* @slot cursor Replaces the caret
|
|
10
|
+
* @event typed Emitted with the phrase's index when a phrase is fully typed
|
|
11
|
+
* @event done Emitted when the last phrase is typed and `loop` is off
|
|
12
|
+
*/
|
|
13
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
14
|
+
|
|
15
|
+
const props = withDefaults(
|
|
16
|
+
defineProps<{
|
|
17
|
+
/** A phrase, or phrases typed one after another */
|
|
18
|
+
text: string | string[]
|
|
19
|
+
/** Milliseconds per character typed */
|
|
20
|
+
typeSpeed?: number
|
|
21
|
+
/** Milliseconds per character erased */
|
|
22
|
+
deleteSpeed?: number
|
|
23
|
+
/** Milliseconds a typed phrase stays before it's erased */
|
|
24
|
+
pause?: number
|
|
25
|
+
/** Milliseconds before the first character */
|
|
26
|
+
startDelay?: number
|
|
27
|
+
/** Start over after the last phrase; off stops on it */
|
|
28
|
+
loop?: boolean
|
|
29
|
+
/** Erase only the end that differs from the next phrase */
|
|
30
|
+
smartBackspace?: boolean
|
|
31
|
+
/** Vary each keystroke's delay a little, like a person typing */
|
|
32
|
+
humanize?: boolean
|
|
33
|
+
/** Show the caret */
|
|
34
|
+
cursor?: boolean
|
|
35
|
+
/** Wait until it's on screen to start */
|
|
36
|
+
startOnVisible?: boolean
|
|
37
|
+
/** Element rendered */
|
|
38
|
+
tag?: string
|
|
39
|
+
}>(),
|
|
40
|
+
{
|
|
41
|
+
typeSpeed: 70,
|
|
42
|
+
deleteSpeed: 35,
|
|
43
|
+
pause: 1600,
|
|
44
|
+
startDelay: 300,
|
|
45
|
+
loop: true,
|
|
46
|
+
smartBackspace: true,
|
|
47
|
+
humanize: true,
|
|
48
|
+
cursor: true,
|
|
49
|
+
startOnVisible: true,
|
|
50
|
+
tag: 'span',
|
|
51
|
+
},
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
const emit = defineEmits<{ typed: [index: number]; done: [] }>()
|
|
55
|
+
|
|
56
|
+
const phrases = computed(() => (Array.isArray(props.text) ? props.text : [props.text]))
|
|
57
|
+
|
|
58
|
+
// Graphemes: an emoji with a skin tone, a flag or a letter with its accent is one keystroke
|
|
59
|
+
const segmenter = typeof Intl !== 'undefined' && 'Segmenter' in Intl ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null
|
|
60
|
+
const split = (s: string) => (segmenter ? Array.from(segmenter.segment(s), (g) => g.segment) : Array.from(s))
|
|
61
|
+
|
|
62
|
+
const shown = ref<string[]>([])
|
|
63
|
+
const index = ref(0)
|
|
64
|
+
const reduced = typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
65
|
+
|
|
66
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
67
|
+
let running = false
|
|
68
|
+
|
|
69
|
+
const jitter = (ms: number) => (props.humanize ? ms * (0.6 + Math.random() * 0.8) : ms)
|
|
70
|
+
|
|
71
|
+
function commonPrefix(a: string[], b: string[]) {
|
|
72
|
+
let i = 0
|
|
73
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++
|
|
74
|
+
return i
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function schedule(fn: () => void, ms: number) {
|
|
78
|
+
clearTimeout(timer)
|
|
79
|
+
timer = setTimeout(fn, ms)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function typePhrase() {
|
|
83
|
+
const target = split(phrases.value[index.value] ?? '')
|
|
84
|
+
if (reduced) {
|
|
85
|
+
shown.value = target
|
|
86
|
+
return afterTyped()
|
|
87
|
+
}
|
|
88
|
+
if (shown.value.length < target.length) {
|
|
89
|
+
shown.value = target.slice(0, shown.value.length + 1)
|
|
90
|
+
schedule(typePhrase, jitter(props.typeSpeed))
|
|
91
|
+
} else afterTyped()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function afterTyped() {
|
|
95
|
+
emit('typed', index.value)
|
|
96
|
+
const last = index.value === phrases.value.length - 1
|
|
97
|
+
if (last && !props.loop) {
|
|
98
|
+
emit('done')
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
// A single phrase has nothing to loop to: it stays typed
|
|
102
|
+
if (phrases.value.length === 1) return
|
|
103
|
+
schedule(erase, props.pause)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function erase() {
|
|
107
|
+
const nextIndex = (index.value + 1) % phrases.value.length
|
|
108
|
+
const next = split(phrases.value[nextIndex] ?? '')
|
|
109
|
+
const keep = props.smartBackspace ? commonPrefix(shown.value, next) : 0
|
|
110
|
+
if (reduced) {
|
|
111
|
+
shown.value = []
|
|
112
|
+
index.value = nextIndex
|
|
113
|
+
return typePhrase()
|
|
114
|
+
}
|
|
115
|
+
if (shown.value.length > keep) {
|
|
116
|
+
shown.value = shown.value.slice(0, -1)
|
|
117
|
+
schedule(erase, jitter(props.deleteSpeed))
|
|
118
|
+
} else {
|
|
119
|
+
index.value = nextIndex
|
|
120
|
+
schedule(typePhrase, props.typeSpeed * 3)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function start() {
|
|
125
|
+
if (running) return
|
|
126
|
+
running = true
|
|
127
|
+
schedule(typePhrase, props.startDelay)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function restart() {
|
|
131
|
+
clearTimeout(timer)
|
|
132
|
+
running = false
|
|
133
|
+
shown.value = []
|
|
134
|
+
index.value = 0
|
|
135
|
+
start()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const el = ref<HTMLElement>()
|
|
139
|
+
let observer: IntersectionObserver | undefined
|
|
140
|
+
onMounted(() => {
|
|
141
|
+
if (props.startOnVisible && typeof IntersectionObserver !== 'undefined' && el.value) {
|
|
142
|
+
observer = new IntersectionObserver((entries) => {
|
|
143
|
+
if (entries.some((e) => e.isIntersecting)) {
|
|
144
|
+
observer?.disconnect()
|
|
145
|
+
start()
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
observer.observe(el.value)
|
|
149
|
+
} else start()
|
|
150
|
+
})
|
|
151
|
+
onBeforeUnmount(() => {
|
|
152
|
+
clearTimeout(timer)
|
|
153
|
+
observer?.disconnect()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
watch(() => phrases.value.join('\u0000'), () => { if (running) restart() })
|
|
157
|
+
|
|
158
|
+
defineExpose({ restart })
|
|
159
|
+
|
|
160
|
+
const typed = computed(() => shown.value.join(''))
|
|
161
|
+
</script>
|
|
162
|
+
|
|
163
|
+
<template>
|
|
164
|
+
<component :is="tag" ref="el" class="m3-typewriter">
|
|
165
|
+
<span class="sr-only">{{ phrases.join(', ') }}</span>
|
|
166
|
+
<span aria-hidden="true">{{ typed }}</span>
|
|
167
|
+
<span v-if="cursor" aria-hidden="true" class="m3-typewriter-cursor" :class="!$slots.cursor && 'm3-typewriter-caret'"><slot name="cursor" /></span>
|
|
168
|
+
</component>
|
|
169
|
+
</template>
|
|
170
|
+
|
|
171
|
+
<style>
|
|
172
|
+
/* Like an M3 text field's caret: 2px wide, primary, blinking */
|
|
173
|
+
.m3-typewriter-cursor {
|
|
174
|
+
display: inline-block;
|
|
175
|
+
min-width: 2px;
|
|
176
|
+
height: 1em;
|
|
177
|
+
margin-left: 0.06em;
|
|
178
|
+
vertical-align: -0.12em;
|
|
179
|
+
animation: m3-typewriter-blink 1.06s steps(1) infinite;
|
|
180
|
+
}
|
|
181
|
+
.m3-typewriter-caret {
|
|
182
|
+
width: 2px;
|
|
183
|
+
background-color: var(--color-primary);
|
|
184
|
+
}
|
|
185
|
+
@keyframes m3-typewriter-blink {
|
|
186
|
+
50% {
|
|
187
|
+
opacity: 0;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
@media (prefers-reduced-motion: reduce) {
|
|
191
|
+
.m3-typewriter-cursor {
|
|
192
|
+
animation: none;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
</style>
|