@m3ui-vue/m3ui-vue 1.4.1 → 1.4.3
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 +3662 -2028
- 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__/MTabs.test.ts +100 -2
- 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/MTabs.vue +33 -6
- 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>
|
package/src/components/MTabs.vue
CHANGED
|
@@ -40,6 +40,7 @@ const leading = computed(() => (props.iconPosition ?? (props.variant === 'primar
|
|
|
40
40
|
const emit = defineEmits<{ 'update:modelValue': [string | number] }>()
|
|
41
41
|
|
|
42
42
|
const tabEls = ref<HTMLElement[]>([])
|
|
43
|
+
const rowEl = ref<HTMLElement | null>(null)
|
|
43
44
|
const indicatorLeft = ref(0)
|
|
44
45
|
const indicatorWidth = ref(0)
|
|
45
46
|
|
|
@@ -98,11 +99,37 @@ function ensureRaf() {
|
|
|
98
99
|
rafId = requestAnimationFrame(tick)
|
|
99
100
|
}
|
|
100
101
|
|
|
101
|
-
|
|
102
|
+
// ScrollableTabRow keeps the selected tab in view: on every selection change it
|
|
103
|
+
// animates the row's scroll so the tab is centered, clamped to the row's ends
|
|
104
|
+
// (Compose's `calculateTabOffset`). Only the row scrolls, never the page, so
|
|
105
|
+
// no scrollIntoView.
|
|
106
|
+
function scrollToTab(rect: { left: number; width: number }, smooth: boolean) {
|
|
107
|
+
const row = rowEl.value
|
|
108
|
+
if (!props.scrollable || !row) return
|
|
109
|
+
const max = row.scrollWidth - row.clientWidth
|
|
110
|
+
if (max <= 0) return
|
|
111
|
+
const left = Math.round(Math.min(Math.max(rect.left + rect.width / 2 - row.clientWidth / 2, 0), max))
|
|
112
|
+
if (Math.abs(left - row.scrollLeft) < 1) return
|
|
113
|
+
const reduce = typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
114
|
+
const behavior: ScrollBehavior = smooth && !reduce ? 'smooth' : 'auto'
|
|
115
|
+
if (typeof row.scrollTo === 'function') row.scrollTo({ left, behavior })
|
|
116
|
+
else row.scrollLeft = left
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The value whose tab was last brought into view: tabs that arrive after mount
|
|
120
|
+
// (loaded async) still get scrolled to once, without a later `:tabs` literal
|
|
121
|
+
// re-render pulling the row back from wherever the user scrolled it.
|
|
122
|
+
let shownValue: unknown = Symbol('none')
|
|
123
|
+
|
|
124
|
+
function updateIndicator(animate = true, scroll: 'none' | 'instant' | 'smooth' = 'none') {
|
|
102
125
|
nextTick(() => {
|
|
103
126
|
const idx = props.tabs.findIndex((t) => t.value === props.modelValue)
|
|
104
127
|
const rect = getTabRect(idx)
|
|
105
128
|
if (!rect) return
|
|
129
|
+
if (scroll !== 'none') {
|
|
130
|
+
shownValue = props.modelValue
|
|
131
|
+
scrollToTab(rect, scroll === 'smooth')
|
|
132
|
+
}
|
|
106
133
|
|
|
107
134
|
if (props.variant === 'primary') {
|
|
108
135
|
const inset = Math.min(rect.width * 0.2, 16)
|
|
@@ -124,15 +151,15 @@ function updateIndicator(animate = true) {
|
|
|
124
151
|
})
|
|
125
152
|
}
|
|
126
153
|
|
|
127
|
-
onMounted(() => updateIndicator(false))
|
|
154
|
+
onMounted(() => updateIndicator(false, 'instant'))
|
|
128
155
|
onUnmounted(() => { if (rafId) cancelAnimationFrame(rafId) })
|
|
129
|
-
watch(() => props.modelValue, () => updateIndicator(true))
|
|
156
|
+
watch(() => props.modelValue, () => updateIndicator(true, 'smooth'))
|
|
130
157
|
// Animated (not a hard snap) here too: a consumer passing an inline `:tabs="[...]"`
|
|
131
158
|
// literal gets a brand-new array reference on every unrelated parent re-render,
|
|
132
159
|
// which would otherwise race with — and instantly override — an in-flight
|
|
133
160
|
// modelValue-triggered animation. Re-running the same animated update is a
|
|
134
161
|
// no-op when the target hasn't actually moved, so this is safe either way.
|
|
135
|
-
watch(() => props.tabs, () => updateIndicator(true), { deep: true })
|
|
162
|
+
watch(() => props.tabs, () => updateIndicator(true, shownValue === props.modelValue ? 'none' : 'instant'), { deep: true })
|
|
136
163
|
watch(leading, () => updateIndicator(true))
|
|
137
164
|
|
|
138
165
|
function select(tab: Tab) {
|
|
@@ -143,7 +170,7 @@ function select(tab: Tab) {
|
|
|
143
170
|
<template>
|
|
144
171
|
<!-- ── Primary: sliding short indicator with stretch ──────────────── -->
|
|
145
172
|
<div v-if="variant === 'primary'" class="border-b border-outline-variant">
|
|
146
|
-
<div class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
173
|
+
<div ref="rowEl" class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
147
174
|
<button
|
|
148
175
|
v-for="tab in tabs"
|
|
149
176
|
:key="tab.value"
|
|
@@ -175,7 +202,7 @@ function select(tab: Tab) {
|
|
|
175
202
|
|
|
176
203
|
<!-- ── Secondary: full-width underline indicator ─────────────────── -->
|
|
177
204
|
<div v-else class="border-b border-outline-variant">
|
|
178
|
-
<div class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
205
|
+
<div ref="rowEl" class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
179
206
|
<button
|
|
180
207
|
v-for="tab in tabs"
|
|
181
208
|
:key="tab.value"
|