@m3ui-vue/m3ui-vue 1.6.0 → 1.6.1

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 (151) hide show
  1. package/dist/API.md +940 -33
  2. package/dist/{MButton-CPghlKb0.js → MButton-OQ4wIERQ.js} +2 -2
  3. package/dist/{MButton-CPghlKb0.js.map → MButton-OQ4wIERQ.js.map} +1 -1
  4. package/dist/{MDialog-X50At1Gk.js → MDialog-DYMnaBlt.js} +3 -3
  5. package/dist/{MDialog-X50At1Gk.js.map → MDialog-DYMnaBlt.js.map} +1 -1
  6. package/dist/{MListItem-DVi1j1QI.js → MListItem-D2cNUyzP.js} +211 -168
  7. package/dist/MListItem-D2cNUyzP.js.map +1 -0
  8. package/dist/{MLoadingIndicator-vooA3110.js → MLoadingIndicator-DWUj0BAB.js} +2 -2
  9. package/dist/{MLoadingIndicator-vooA3110.js.map → MLoadingIndicator-DWUj0BAB.js.map} +1 -1
  10. package/dist/{MMenuItem-CRNZkVVr.js → MMenuItem-BuxYoASE.js} +133 -151
  11. package/dist/MMenuItem-BuxYoASE.js.map +1 -0
  12. package/dist/{MSeekBar-BL2W5_rD.js → MSeekBar-C4cn9RIF.js} +2 -2
  13. package/dist/{MSeekBar-BL2W5_rD.js.map → MSeekBar-C4cn9RIF.js.map} +1 -1
  14. package/dist/{MSlider-B9NcKTmZ.js → MSlider-BIkA6DiE.js} +2 -2
  15. package/dist/{MSlider-B9NcKTmZ.js.map → MSlider-BIkA6DiE.js.map} +1 -1
  16. package/dist/{MSpinner-_XpTRkAg.js → MSpinner-DZkXZJpc.js} +2 -2
  17. package/dist/{MSpinner-_XpTRkAg.js.map → MSpinner-DZkXZJpc.js.map} +1 -1
  18. package/dist/{MTextField-DpcPmR5B.js → MTextField-CNH-byir.js} +2 -2
  19. package/dist/{MTextField-DpcPmR5B.js.map → MTextField-CNH-byir.js.map} +1 -1
  20. package/dist/{MTooltip-BL3jPYPT.js → MTooltip-Dd5g2zhh.js} +4 -4
  21. package/dist/{MTooltip-BL3jPYPT.js.map → MTooltip-Dd5g2zhh.js.map} +1 -1
  22. package/dist/{_useEscapeStack-C5-BYr8y.js → _popupPosition-nL8SVKPK.js} +20 -2
  23. package/dist/_popupPosition-nL8SVKPK.js.map +1 -0
  24. package/dist/api.json +1 -1
  25. package/dist/code-editor.js +1 -1
  26. package/dist/components/MAddressField.vue.d.ts +54 -0
  27. package/dist/components/MAudioPlayer.vue.d.ts +1 -1
  28. package/dist/components/MAudioRecorder.vue.d.ts +51 -0
  29. package/dist/components/MAvailabilityCalendar.vue.d.ts +1 -1
  30. package/dist/components/MCameraCapture.vue.d.ts +51 -0
  31. package/dist/components/MCollapse.vue.d.ts +32 -0
  32. package/dist/components/MCountrySelect.vue.d.ts +41 -0
  33. package/dist/components/MCronField.vue.d.ts +26 -0
  34. package/dist/components/MDashboardGrid.vue.d.ts +65 -0
  35. package/dist/components/MDiffViewer.vue.d.ts +56 -0
  36. package/dist/components/MExpansionPanel.vue.d.ts +26 -3
  37. package/dist/components/MFilterBuilder.vue.d.ts +22 -0
  38. package/dist/components/MFlex.vue.d.ts +1 -1
  39. package/dist/components/MMapCluster.vue.d.ts +80 -0
  40. package/dist/components/MMapDraw.vue.d.ts +79 -0
  41. package/dist/components/MMentionField.vue.d.ts +102 -0
  42. package/dist/components/MNotificationCenter.vue.d.ts +57 -0
  43. package/dist/components/MQRScanner.vue.d.ts +52 -0
  44. package/dist/components/MQuiz.vue.d.ts +1 -1
  45. package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
  46. package/dist/components/MStack.vue.d.ts +1 -1
  47. package/dist/components/MTimezoneSelect.vue.d.ts +39 -0
  48. package/dist/components/MToolbar.vue.d.ts +1 -1
  49. package/dist/components/MVideoPlayer.vue.d.ts +1 -1
  50. package/dist/components/_MFilterGroup.vue.d.ts +17 -0
  51. package/dist/composables/_caretPosition.d.ts +6 -0
  52. package/dist/composables/_useCamera.d.ts +16 -0
  53. package/dist/composables/_useFollowAnchor.d.ts +15 -0
  54. package/dist/composables/cron.d.ts +29 -0
  55. package/dist/composables/dashboardLayout.d.ts +28 -0
  56. package/dist/composables/filterBuilder.d.ts +55 -0
  57. package/dist/composables/mapAdapters.d.ts +6 -1
  58. package/dist/composables/mapSearch.d.ts +1 -0
  59. package/dist/composables/textDiff.d.ts +24 -0
  60. package/dist/composables/useLocale.d.ts +108 -0
  61. package/dist/draw.js +3 -3
  62. package/dist/flow.js +1 -1
  63. package/dist/gantt.js +1 -1
  64. package/dist/index.d.ts +25 -0
  65. package/dist/locales/index.js +749 -0
  66. package/dist/locales/index.js.map +1 -1
  67. package/dist/m3ui-vue.css +1 -1
  68. package/dist/m3ui.js +9888 -6940
  69. package/dist/m3ui.js.map +1 -1
  70. package/dist/map.d.ts +5 -0
  71. package/dist/map.js +1462 -576
  72. package/dist/map.js.map +1 -1
  73. package/dist/media.js +5 -5
  74. package/dist/pdf.js +5 -5
  75. package/dist/rich-text-editor.js +5 -5
  76. package/dist/styles.css +1 -1
  77. package/dist/{useLocale-COav9m8C.js → useLocale-DaVSTdm0.js} +108 -1
  78. package/dist/useLocale-DaVSTdm0.js.map +1 -0
  79. package/package.json +1 -1
  80. package/src/__tests__/MAddressField.test.ts +149 -0
  81. package/src/__tests__/MAudioRecorder.test.ts +125 -0
  82. package/src/__tests__/MCameraCapture.test.ts +154 -0
  83. package/src/__tests__/MCountrySelect.test.ts +67 -0
  84. package/src/__tests__/MCronField.test.ts +100 -0
  85. package/src/__tests__/MDashboardGrid.test.ts +122 -0
  86. package/src/__tests__/MDiffViewer.test.ts +68 -0
  87. package/src/__tests__/MExpansionPanel.test.ts +107 -0
  88. package/src/__tests__/MFilterBuilder.test.ts +130 -0
  89. package/src/__tests__/MMapCluster.test.ts +127 -0
  90. package/src/__tests__/MMapDraw.test.ts +188 -0
  91. package/src/__tests__/MMapLayers.test.ts +56 -0
  92. package/src/__tests__/MMentionField.test.ts +163 -0
  93. package/src/__tests__/MNotificationCenter.test.ts +94 -0
  94. package/src/__tests__/_fakeMaplibre.ts +14 -3
  95. package/src/__tests__/attrsFallthrough.test.ts +4 -0
  96. package/src/__tests__/cron.test.ts +68 -0
  97. package/src/__tests__/dashboardLayout.test.ts +45 -0
  98. package/src/__tests__/filterBuilder.test.ts +91 -0
  99. package/src/__tests__/followAnchor.test.ts +162 -0
  100. package/src/__tests__/ssr.test.ts +3 -0
  101. package/src/__tests__/textDiff.test.ts +37 -0
  102. package/src/components/MAddressField.vue +301 -0
  103. package/src/components/MAudioRecorder.vue +334 -0
  104. package/src/components/MAutocomplete.vue +3 -7
  105. package/src/components/MCameraCapture.vue +156 -0
  106. package/src/components/MCollapse.vue +91 -0
  107. package/src/components/MColorPicker.vue +4 -8
  108. package/src/components/MCountrySelect.vue +89 -0
  109. package/src/components/MCronField.vue +251 -0
  110. package/src/components/MDashboardGrid.vue +382 -0
  111. package/src/components/MDatePicker.vue +10 -7
  112. package/src/components/MDateRangePicker.vue +10 -7
  113. package/src/components/MDiffViewer.vue +234 -0
  114. package/src/components/MExpansionPanel.vue +56 -48
  115. package/src/components/MFilterBuilder.vue +47 -0
  116. package/src/components/MMapCluster.vue +295 -0
  117. package/src/components/MMapDraw.vue +601 -0
  118. package/src/components/MMapSearch.vue +1 -9
  119. package/src/components/MMentionField.vue +373 -0
  120. package/src/components/MMultiAutocomplete.vue +3 -7
  121. package/src/components/MMultiSelect.vue +3 -7
  122. package/src/components/MNotificationCenter.vue +252 -0
  123. package/src/components/MPhoneInput.vue +3 -10
  124. package/src/components/MQRScanner.vue +177 -0
  125. package/src/components/MSelect.vue +3 -7
  126. package/src/components/MTimePicker.vue +10 -7
  127. package/src/components/MTimezoneSelect.vue +116 -0
  128. package/src/components/_MFilterGroup.vue +210 -0
  129. package/src/composables/_caretPosition.ts +44 -0
  130. package/src/composables/_useCamera.ts +66 -0
  131. package/src/composables/_useFollowAnchor.ts +109 -0
  132. package/src/composables/cron.ts +179 -0
  133. package/src/composables/dashboardLayout.ts +94 -0
  134. package/src/composables/filterBuilder.ts +195 -0
  135. package/src/composables/mapAdapters.ts +29 -15
  136. package/src/composables/mapSearch.ts +10 -0
  137. package/src/composables/textDiff.ts +106 -0
  138. package/src/composables/useLocale.ts +215 -0
  139. package/src/index.ts +25 -0
  140. package/src/locales/de.ts +107 -0
  141. package/src/locales/es.ts +107 -0
  142. package/src/locales/fr.ts +107 -0
  143. package/src/locales/ja.ts +107 -0
  144. package/src/locales/ko.ts +107 -0
  145. package/src/locales/pt.ts +107 -0
  146. package/src/locales/zh.ts +107 -0
  147. package/src/map.ts +5 -0
  148. package/dist/MListItem-DVi1j1QI.js.map +0 -1
  149. package/dist/MMenuItem-CRNZkVVr.js.map +0 -1
  150. package/dist/_useEscapeStack-C5-BYr8y.js.map +0 -1
  151. package/dist/useLocale-COav9m8C.js.map +0 -1
@@ -0,0 +1,252 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * An inbox of notifications: grouped by day (Today, Yesterday, a date), newest first, with relative times in the
4
+ * app's language, unread ones marked, an All / Unread filter and Mark all as read. A click opens one (and marks it
5
+ * read); a sideways swipe (finger or mouse), its dismiss button or Delete dismisses it. The list is the v-model:
6
+ * reading and dismissing change it. Put it in a menu, a side sheet or a page; `MNotificationCenter` doesn't float.
7
+ * For passing toasts, use `useNotification()`.
8
+ *
9
+ * @slot item One notification's content instead of the default: `{ item }`
10
+ * @slot empty What shows with nothing to list
11
+ * @event update:modelValue The list changed (read, unread, dismissed)
12
+ * @event open A notification was opened (clicked, or Enter)
13
+ * @event dismiss A notification was dismissed
14
+ */
15
+ import { computed, ref } from 'vue'
16
+ import MButton from './MButton.vue'
17
+ import MChip from './MChip.vue'
18
+ import MIcon from './MIcon.vue'
19
+ import MIconButton from './MIconButton.vue'
20
+ import { useLocale } from '../composables/useLocale'
21
+
22
+ /** One notification of `MNotificationCenter`. */
23
+ export interface InboxNotification {
24
+ id: string | number
25
+ title: string
26
+ body?: string
27
+ /** When: a Date, an ISO string or a timestamp */
28
+ time: Date | string | number
29
+ read?: boolean
30
+ /** A Material Symbol */
31
+ icon?: string
32
+ /** An image URL instead of the icon */
33
+ avatar?: string
34
+ /** The icon's color: a theme role */
35
+ color?: 'primary' | 'secondary' | 'tertiary' | 'error' | 'success'
36
+ }
37
+
38
+ const props = withDefaults(
39
+ defineProps<{
40
+ /** The notifications (v-model) */
41
+ modelValue: InboxNotification[]
42
+ /** The header's title (the locale's "Notifications" by default); empty hides the header */
43
+ title?: string
44
+ /** Show the All / Unread filter */
45
+ filter?: boolean
46
+ /** Let people dismiss notifications */
47
+ dismissible?: boolean
48
+ /** Limit the height (CSS), scrolling inside */
49
+ maxHeight?: string
50
+ }>(),
51
+ { filter: true, dismissible: true },
52
+ )
53
+
54
+ const emit = defineEmits<{
55
+ 'update:modelValue': [items: InboxNotification[]]
56
+ open: [item: InboxNotification]
57
+ dismiss: [item: InboxNotification]
58
+ }>()
59
+
60
+ const t = useLocale()
61
+ const showUnread = ref(false)
62
+ const list = computed(() => (Array.isArray(props.modelValue) ? props.modelValue : []))
63
+ const unread = computed(() => list.value.filter((n) => !n.read).length)
64
+
65
+ const when = (n: InboxNotification) => new Date(n.time)
66
+ const dayKey = (d: Date) => `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`
67
+
68
+ /** Newest first, grouped by day */
69
+ const groups = computed(() => {
70
+ const shown = [...list.value].filter((n) => !showUnread.value || !n.read).sort((a, b) => when(b).getTime() - when(a).getTime())
71
+ const today = new Date()
72
+ const yesterday = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1)
73
+ const fmt = new Intl.DateTimeFormat(t.lang || undefined, { weekday: 'long', day: 'numeric', month: 'long' })
74
+ const out: { label: string; items: InboxNotification[] }[] = []
75
+ for (const n of shown) {
76
+ const d = when(n)
77
+ const label = dayKey(d) === dayKey(today) ? t.notifToday : dayKey(d) === dayKey(yesterday) ? t.notifYesterday : fmt.format(d)
78
+ const last = out[out.length - 1]
79
+ if (last?.label === label) last.items.push(n)
80
+ else out.push({ label, items: [n] })
81
+ }
82
+ return out
83
+ })
84
+
85
+ /** "5 min ago", "3 h ago", or the time for older ones */
86
+ function relative(n: InboxNotification) {
87
+ const d = when(n)
88
+ const s = (d.getTime() - Date.now()) / 1000
89
+ const rtf = new Intl.RelativeTimeFormat(t.lang || undefined, { numeric: 'auto', style: 'short' })
90
+ if (Math.abs(s) < 60) return rtf.format(0, 'second')
91
+ if (Math.abs(s) < 3600) return rtf.format(Math.round(s / 60), 'minute')
92
+ if (Math.abs(s) < 6 * 3600) return rtf.format(Math.round(s / 3600), 'hour')
93
+ return new Intl.DateTimeFormat(t.lang || undefined, { hour: 'numeric', minute: '2-digit' }).format(d)
94
+ }
95
+
96
+ function update(id: InboxNotification['id'], patch: Partial<InboxNotification>) {
97
+ emit('update:modelValue', list.value.map((n) => (n.id === id ? { ...n, ...patch } : n)))
98
+ }
99
+ function open(n: InboxNotification) {
100
+ if (!n.read) update(n.id, { read: true })
101
+ emit('open', { ...n, read: true })
102
+ }
103
+ function markAll() {
104
+ emit('update:modelValue', list.value.map((n) => (n.read ? n : { ...n, read: true })))
105
+ }
106
+ function dismiss(n: InboxNotification) {
107
+ emit('update:modelValue', list.value.filter((x) => x.id !== n.id))
108
+ emit('dismiss', n)
109
+ }
110
+
111
+ // ── Swipe to dismiss (finger or mouse); the row follows, then flies out or springs back ─────────
112
+
113
+ let swipe: { id: InboxNotification['id']; x: number; y: number; dx: number; active: boolean; el: HTMLElement } | null = null
114
+ const swiping = ref<InboxNotification['id'] | null>(null)
115
+
116
+ function onPointerDown(e: PointerEvent, n: InboxNotification) {
117
+ if (!props.dismissible || e.button > 0 || (e.target as HTMLElement).closest('button, a')) return
118
+ swipe = { id: n.id, x: e.clientX, y: e.clientY, dx: 0, active: false, el: e.currentTarget as HTMLElement }
119
+ window.addEventListener('pointermove', onPointerMove)
120
+ window.addEventListener('pointerup', onPointerUp)
121
+ window.addEventListener('pointercancel', onPointerUp)
122
+ }
123
+ function onPointerMove(e: PointerEvent) {
124
+ if (!swipe) return
125
+ const dx = e.clientX - swipe.x
126
+ const dy = e.clientY - swipe.y
127
+ if (!swipe.active) {
128
+ // A vertical move is a scroll: let it go
129
+ if (Math.abs(dy) > 10 && Math.abs(dy) > Math.abs(dx)) return end()
130
+ if (Math.abs(dx) < 10) return
131
+ swipe.active = true
132
+ swiping.value = swipe.id
133
+ }
134
+ swipe.dx = dx
135
+ swipe.el.style.transform = `translateX(${dx}px)`
136
+ swipe.el.style.opacity = String(Math.max(0.2, 1 - Math.abs(dx) / swipe.el.offsetWidth))
137
+ }
138
+ function onPointerUp() {
139
+ if (!swipe) return
140
+ const { el, dx, active, id } = swipe
141
+ const n = list.value.find((x) => x.id === id)
142
+ end()
143
+ if (!active) return
144
+ const width = el.offsetWidth || 1
145
+ if (n && Math.abs(dx) > width * 0.35) {
146
+ // Off it goes, then out of the list
147
+ const out = el.animate([{ transform: `translateX(${dx}px)`, opacity: el.style.opacity }, { transform: `translateX(${Math.sign(dx) * width}px)`, opacity: 0 }], { duration: 180, easing: 'cubic-bezier(0.3, 0, 0.8, 0.15)' })
148
+ el.style.transform = `translateX(${Math.sign(dx) * width}px)`
149
+ el.style.opacity = '0'
150
+ const done = () => dismiss(n)
151
+ if (out?.finished) out.finished.then(done, done)
152
+ else done()
153
+ } else {
154
+ el.animate?.([{ transform: `translateX(${dx}px)` }, { transform: 'translateX(0)' }], { duration: 250, easing: 'cubic-bezier(0.2, 0, 0, 1)' })
155
+ el.style.transform = ''
156
+ el.style.opacity = ''
157
+ }
158
+ // The click that ends a swipe isn't an open
159
+ setTimeout(() => (swiping.value = null))
160
+ }
161
+ function end() {
162
+ swipe = null
163
+ window.removeEventListener('pointermove', onPointerMove)
164
+ window.removeEventListener('pointerup', onPointerUp)
165
+ window.removeEventListener('pointercancel', onPointerUp)
166
+ }
167
+
168
+ function onClick(n: InboxNotification) {
169
+ if (swiping.value === n.id) return
170
+ open(n)
171
+ }
172
+ function onKey(e: KeyboardEvent, n: InboxNotification) {
173
+ if (e.target !== e.currentTarget) return
174
+ if (e.key === 'Enter' || e.key === ' ') {
175
+ e.preventDefault()
176
+ open(n)
177
+ } else if ((e.key === 'Delete' || e.key === 'Backspace') && props.dismissible) {
178
+ e.preventDefault()
179
+ const row = e.currentTarget as HTMLElement
180
+ const next = (row.nextElementSibling ?? row.previousElementSibling) as HTMLElement | null
181
+ dismiss(n)
182
+ next?.focus?.()
183
+ }
184
+ }
185
+
186
+ const ROLE_BG = {
187
+ primary: 'bg-primary-container text-on-primary-container',
188
+ secondary: 'bg-secondary-container text-on-secondary-container',
189
+ tertiary: 'bg-tertiary-container text-on-tertiary-container',
190
+ error: 'bg-error-container text-on-error-container',
191
+ success: 'bg-success-container text-on-success-container',
192
+ } as const
193
+ </script>
194
+
195
+ <template>
196
+ <div class="m3-notification-center flex flex-col overflow-hidden bg-surface-container-low text-on-surface">
197
+ <div v-if="title !== ''" class="flex items-center gap-2 px-4 pb-2 pt-4">
198
+ <h2 class="flex-1 text-title-large">{{ title ?? t.notifTitle }}</h2>
199
+ <span v-if="unread" class="rounded-full bg-error px-2 text-label-small text-on-error">{{ unread }}</span>
200
+ <MButton v-if="unread" variant="text" size="sm" @click="markAll">{{ t.notifMarkAllRead }}</MButton>
201
+ </div>
202
+ <div v-if="filter" class="flex gap-2 px-4 pb-2" role="group">
203
+ <MChip type="filter" :selected="!showUnread" @click="showUnread = false">{{ t.notifAll }}</MChip>
204
+ <MChip type="filter" :selected="showUnread" @click="showUnread = true">{{ t.notifUnread }}<template v-if="unread"> ({{ unread }})</template></MChip>
205
+ </div>
206
+
207
+ <div class="flex-1 overflow-y-auto overflow-x-hidden pb-2" :style="{ maxHeight }">
208
+ <div v-if="!groups.length" class="flex flex-col items-center gap-2 px-6 py-10 text-center text-on-surface-variant">
209
+ <slot name="empty">
210
+ <MIcon name="notifications_off" :size="36" />
211
+ <p class="text-body-medium">{{ t.notifEmpty }}</p>
212
+ </slot>
213
+ </div>
214
+ <section v-for="g in groups" :key="g.label">
215
+ <h3 class="sticky top-0 z-[1] bg-surface-container-low px-4 py-2 text-label-large text-on-surface-variant first-letter:uppercase">{{ g.label }}</h3>
216
+ <ul role="list">
217
+ <li
218
+ v-for="n in g.items"
219
+ :key="n.id"
220
+ class="group relative flex cursor-pointer touch-pan-y items-start gap-3 px-4 py-3 outline-none transition-colors hover:bg-on-surface/8 focus-visible:bg-on-surface/10"
221
+ :class="n.read ? '' : 'bg-secondary-container/40'"
222
+ tabindex="0"
223
+ :aria-label="`${n.title}${n.read ? '' : ` (${t.notifUnread})`}`"
224
+ @pointerdown="onPointerDown($event, n)"
225
+ @click="onClick(n)"
226
+ @keydown="onKey($event, n)"
227
+ >
228
+ <slot name="item" :item="n">
229
+ <img v-if="n.avatar" :src="n.avatar" alt="" class="size-10 shrink-0 rounded-full object-cover">
230
+ <span v-else class="grid size-10 shrink-0 place-items-center rounded-full" :class="ROLE_BG[n.color ?? 'secondary']">
231
+ <MIcon :name="n.icon ?? 'notifications'" :size="22" />
232
+ </span>
233
+ <span class="flex min-w-0 flex-1 flex-col">
234
+ <span class="flex items-baseline gap-2">
235
+ <span class="min-w-0 flex-1 truncate text-body-large" :class="n.read ? '' : 'font-medium'">{{ n.title }}</span>
236
+ <span class="shrink-0 text-label-small text-on-surface-variant">{{ relative(n) }}</span>
237
+ </span>
238
+ <span v-if="n.body" class="line-clamp-2 text-body-medium text-on-surface-variant">{{ n.body }}</span>
239
+ </span>
240
+ </slot>
241
+ <span v-if="!n.read" class="mt-2 size-2 shrink-0 rounded-full bg-primary" aria-hidden="true" />
242
+ <!-- Floating over the row on hover / focus, so they take no room from the text -->
243
+ <span class="absolute right-2 top-1/2 flex -translate-y-1/2 rounded-full bg-surface-container-high opacity-0 shadow-elevation-1 transition-opacity focus-within:opacity-100 group-hover:opacity-100 max-md:hidden">
244
+ <MIconButton :icon="n.read ? 'mark_email_unread' : 'mark_email_read'" :label="n.read ? t.notifMarkUnread : t.notifMarkRead" size="sm" @click.stop="update(n.id, { read: !n.read })" />
245
+ <MIconButton v-if="dismissible" icon="close" :label="t.notifDismiss" size="sm" @click.stop="dismiss(n)" />
246
+ </span>
247
+ </li>
248
+ </ul>
249
+ </section>
250
+ </div>
251
+ </div>
252
+ </template>
@@ -16,6 +16,7 @@ import { computed, nextTick, onBeforeUnmount, ref, useId, watch } from 'vue'
16
16
  import MIcon from './MIcon.vue'
17
17
  import { useFieldBg } from '../composables/useFieldBg'
18
18
  import { useLocale } from '../composables/useLocale'
19
+ import { useFollowAnchor } from '../composables/_useFollowAnchor'
19
20
  import { POPUP_MARGIN, dropdownX, fitPopup } from '../composables/_popupPosition'
20
21
  import { useEscapeStack } from '../composables/_useEscapeStack'
21
22
  import {
@@ -306,25 +307,17 @@ function onDocDown(e: MouseEvent) {
306
307
  if (panelEl.value?.contains(target) || buttonEl.value?.contains(target)) return
307
308
  closeList(false)
308
309
  }
309
- function onScroll(e: Event) {
310
- if (panelEl.value?.contains(e.target as Node)) return
311
- place()
312
- }
310
+ // Stays on its field while the page or a container scrolls, the window/zoom changes or the field resizes
311
+ useFollowAnchor({ open: () => open.value, anchor: fieldEl, popup: panelEl, place: place, close: () => closeList(false) })
313
312
  watch(open, (v) => {
314
313
  if (v) {
315
314
  document.addEventListener('mousedown', onDocDown)
316
- window.addEventListener('scroll', onScroll, true)
317
- window.addEventListener('resize', place)
318
315
  } else {
319
316
  document.removeEventListener('mousedown', onDocDown)
320
- window.removeEventListener('scroll', onScroll, true)
321
- window.removeEventListener('resize', place)
322
317
  }
323
318
  })
324
319
  onBeforeUnmount(() => {
325
320
  document.removeEventListener('mousedown', onDocDown)
326
- window.removeEventListener('scroll', onScroll, true)
327
- window.removeEventListener('resize', place)
328
321
  })
329
322
 
330
323
  defineExpose({
@@ -0,0 +1,177 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Reads QR codes (and barcodes) with the device's camera: a live preview with an M3 viewfinder, reading a few times a
4
+ * second. Uses the browser's `BarcodeDetector` where there is one (Chrome on Android and desktop, Edge, Samsung
5
+ * Internet; not Firefox or Safari on desktop); elsewhere, pass a `detector` of yours (jsQR, ZXing) that reads an
6
+ * `ImageData`, or it says the browser can't. Each code read is emitted once (`detect`) and is the v-model; the same
7
+ * code isn't repeated for 2 seconds. `once` stops the camera after the first. Needs a secure page (https or localhost).
8
+ *
9
+ * @event update:modelValue A code was read (its text)
10
+ * @event detect A code was read: `{ rawValue, format }`
11
+ * @event error The camera failed: `'denied' | 'unavailable' | 'unsupported' | 'failed'`
12
+ */
13
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
14
+ import MButton from './MButton.vue'
15
+ import MIcon from './MIcon.vue'
16
+ import MIconButton from './MIconButton.vue'
17
+ import MSpinner from './MSpinner.vue'
18
+ import { useLocale } from '../composables/useLocale'
19
+ import { useCamera, type CameraError, type CameraFacing } from '../composables/_useCamera'
20
+
21
+ /** A code read by `MQRScanner`. */
22
+ export interface ScanResult {
23
+ rawValue: string
24
+ /** `qr_code`, `ean_13`, `code_128`… */
25
+ format: string
26
+ }
27
+
28
+ /** Your own reader, for browsers without BarcodeDetector: a frame in, a code (or null) out. */
29
+ export type ScanDetector = (frame: ImageData) => ScanResult | null | Promise<ScanResult | null>
30
+
31
+ const props = withDefaults(
32
+ defineProps<{
33
+ /** The last code read (v-model) */
34
+ modelValue?: string | null
35
+ /** What BarcodeDetector looks for */
36
+ formats?: string[]
37
+ /** Your reader instead of BarcodeDetector (e.g. jsQR on the frame) */
38
+ detector?: ScanDetector
39
+ /** Which camera: the back one (`environment`) or the selfie one (`user`) */
40
+ facing?: CameraFacing
41
+ /** How often to look, in ms */
42
+ interval?: number
43
+ /** Stop the camera after the first code */
44
+ once?: boolean
45
+ /** Start the camera on mount instead of on a button press */
46
+ autostart?: boolean
47
+ /** The preview's aspect ratio (CSS `aspect-ratio`) */
48
+ aspect?: string
49
+ }>(),
50
+ { modelValue: null, formats: () => ['qr_code'], facing: 'environment', interval: 250, once: false, autostart: false, aspect: '1 / 1' },
51
+ )
52
+
53
+ const emit = defineEmits<{
54
+ 'update:modelValue': [value: string]
55
+ detect: [result: ScanResult]
56
+ error: [error: CameraError]
57
+ }>()
58
+
59
+ const t = useLocale()
60
+ const video = ref<HTMLVideoElement>()
61
+ const cam = useCamera(video, { facing: () => props.facing, resolution: () => 1280 })
62
+
63
+ type NativeDetector = { detect: (source: HTMLVideoElement) => Promise<{ rawValue: string; format: string }[]> }
64
+ // Looked up when needed, not at import: a polyfill loaded later still counts
65
+ const nativeClass = () => (typeof window !== 'undefined' ? (window as unknown as { BarcodeDetector?: new (o: { formats: string[] }) => NativeDetector }).BarcodeDetector : undefined)
66
+ const supported = computed(() => !!props.detector || !!nativeClass())
67
+
68
+ let native: NativeDetector | null = null
69
+ let timer: ReturnType<typeof setInterval> | undefined
70
+ let busy = false
71
+ let canvas: HTMLCanvasElement | null = null
72
+ let last = { value: '', at: 0 }
73
+ const hit = ref(false)
74
+
75
+ async function read(): Promise<ScanResult | null> {
76
+ const v = video.value
77
+ if (!v || !v.videoWidth) return null
78
+ if (props.detector) {
79
+ canvas ??= document.createElement('canvas')
80
+ canvas.width = v.videoWidth
81
+ canvas.height = v.videoHeight
82
+ const ctx = canvas.getContext('2d', { willReadFrequently: true })!
83
+ ctx.drawImage(v, 0, 0)
84
+ return props.detector(ctx.getImageData(0, 0, canvas.width, canvas.height))
85
+ }
86
+ native ??= new (nativeClass()!)({ formats: props.formats })
87
+ const [first] = await native.detect(v)
88
+ return first ? { rawValue: first.rawValue, format: first.format } : null
89
+ }
90
+
91
+ async function tick() {
92
+ if (busy || cam.status.value !== 'live') return
93
+ busy = true
94
+ try {
95
+ const result = await read()
96
+ if (result?.rawValue && (result.rawValue !== last.value || Date.now() - last.at > 2000)) {
97
+ last = { value: result.rawValue, at: Date.now() }
98
+ hit.value = true
99
+ setTimeout(() => (hit.value = false), 400)
100
+ emit('update:modelValue', result.rawValue)
101
+ emit('detect', result)
102
+ if (props.once) stop()
103
+ }
104
+ } catch {
105
+ // A frame that couldn't be read: try the next one
106
+ } finally {
107
+ busy = false
108
+ }
109
+ }
110
+
111
+ function startScan() {
112
+ clearInterval(timer)
113
+ timer = setInterval(tick, props.interval)
114
+ }
115
+ function stop() {
116
+ clearInterval(timer)
117
+ cam.stop()
118
+ }
119
+
120
+ watch(cam.status, (s) => (s === 'live' ? startScan() : clearInterval(timer)))
121
+ watch(cam.error, (e) => { if (e) emit('error', e) })
122
+ watch(() => props.formats.join(), () => { native = null })
123
+
124
+ const errorText = computed(() => ({
125
+ denied: t.cameraDenied, unavailable: t.cameraUnavailable, unsupported: t.cameraUnsupported, failed: t.cameraFailed,
126
+ })[cam.error.value ?? 'failed'])
127
+
128
+ onMounted(() => { if (props.autostart && supported.value) void cam.start() })
129
+ onBeforeUnmount(() => clearInterval(timer))
130
+
131
+ defineExpose({
132
+ /** Starts the camera */
133
+ start: () => cam.start(),
134
+ /** Stops the camera */
135
+ stop,
136
+ })
137
+ </script>
138
+
139
+ <template>
140
+ <div class="m3-qr-scanner relative overflow-hidden rounded-xl bg-[#000] text-[#fff]" :style="{ aspectRatio: aspect }">
141
+ <video ref="video" class="absolute inset-0 size-full object-cover" :class="{ invisible: cam.status.value !== 'live' }" playsinline muted />
142
+
143
+ <!-- The viewfinder: the outside dimmed, the corners marked, a flash of primary on a read -->
144
+ <div v-if="cam.status.value === 'live'" class="pointer-events-none absolute inset-0 grid place-items-center">
145
+ <div
146
+ class="relative aspect-square w-3/5 rounded-2xl shadow-[0_0_0_9999px_rgb(0_0_0/0.45)] transition-[outline-color] duration-200"
147
+ :class="hit ? 'outline-4 outline-primary' : 'outline-0 outline-transparent'"
148
+ >
149
+ <span v-for="c in ['left-0 top-0 border-l-4 border-t-4 rounded-tl-2xl', 'right-0 top-0 border-r-4 border-t-4 rounded-tr-2xl', 'bottom-0 left-0 border-b-4 border-l-4 rounded-bl-2xl', 'bottom-0 right-0 border-b-4 border-r-4 rounded-br-2xl']" :key="c" class="absolute size-8 border-[#fff]" :class="c" />
150
+ </div>
151
+ <p class="absolute bottom-4 rounded-full bg-[#000]/50 px-3 py-1 text-body-small">{{ t.qrHint }}</p>
152
+ </div>
153
+ <MIconButton
154
+ v-if="cam.status.value === 'live' && cam.canSwitch.value"
155
+ icon="cameraswitch"
156
+ :label="t.cameraSwitch"
157
+ class="absolute right-2 top-2 text-[#fff]"
158
+ @click="cam.switchCamera()"
159
+ />
160
+
161
+ <div v-if="!supported" class="absolute inset-0 flex flex-col items-center justify-center gap-3 p-6 text-center" role="alert">
162
+ <MIcon name="qr_code_scanner" :size="36" />
163
+ <p class="max-w-72 text-body-medium">{{ t.qrUnsupported }}</p>
164
+ </div>
165
+ <div v-else-if="cam.status.value === 'idle'" class="absolute inset-0 grid place-items-center">
166
+ <MButton variant="tonal" icon="qr_code_scanner" @click="cam.start()">{{ t.cameraStart }}</MButton>
167
+ </div>
168
+ <div v-else-if="cam.status.value === 'starting'" class="absolute inset-0 flex flex-col items-center justify-center gap-3 text-body-medium" role="status">
169
+ <MSpinner :size="32" />{{ t.cameraStarting }}
170
+ </div>
171
+ <div v-else-if="cam.status.value === 'error'" class="absolute inset-0 flex flex-col items-center justify-center gap-3 p-6 text-center" role="alert">
172
+ <MIcon :name="cam.error.value === 'denied' ? 'no_photography' : 'videocam_off'" :size="36" />
173
+ <p class="max-w-72 text-body-medium">{{ errorText }}</p>
174
+ <MButton v-if="cam.error.value !== 'unsupported'" variant="tonal" icon="refresh" @click="cam.start()">{{ t.cameraStart }}</MButton>
175
+ </div>
176
+ </div>
177
+ </template>
@@ -9,6 +9,7 @@ import MIcon from './MIcon.vue'
9
9
  import MIconButton from './MIconButton.vue'
10
10
  import { useFieldBg } from '../composables/useFieldBg'
11
11
  import { useLocale } from '../composables/useLocale'
12
+ import { useFollowAnchor } from '../composables/_useFollowAnchor'
12
13
  import { useScrollLock } from '../composables/_useScrollLock'
13
14
  import { useEscapeStack } from '../composables/_useEscapeStack'
14
15
  import { FIELD_PADDING_RIGHT, FIELD_VALUE, fieldLabelClasses, vOverflowTitle, type FieldRightInset } from '../composables/_fieldLayout'
@@ -135,11 +136,8 @@ function onOutsideClick(e: MouseEvent) {
135
136
  if (!fieldEl.value?.contains(t) && !dropdownEl.value?.contains(t)) open.value = false
136
137
  }
137
138
 
138
- function onScroll(e: Event) {
139
- if (!open.value) return
140
- if (dropdownEl.value?.contains(e.target as Node)) return
141
- open.value = false
142
- }
139
+ // Stays on its field while the page or a container scrolls, the window/zoom changes or the field resizes
140
+ useFollowAnchor({ open: () => open.value, anchor: fieldEl, popup: dropdownEl, place: computeDropPos, close: () => (open.value = false) })
143
141
 
144
142
  function onKeydown(e: KeyboardEvent) {
145
143
  if (e.key === 'Escape') {
@@ -173,11 +171,9 @@ function closeModal() { modalOpen.value = false }
173
171
 
174
172
  onMounted(() => {
175
173
  document.addEventListener('mousedown', onOutsideClick)
176
- window.addEventListener('scroll', onScroll, true)
177
174
  })
178
175
  onUnmounted(() => {
179
176
  document.removeEventListener('mousedown', onOutsideClick)
180
- window.removeEventListener('scroll', onScroll, true)
181
177
  })
182
178
 
183
179
  // The value and the label stop before the clear button and the arrow
@@ -4,10 +4,11 @@
4
4
  *
5
5
  * @event update:modelValue Emitted with the picked time.
6
6
  */
7
- import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
7
+ import { computed, onUnmounted, ref, watch } from 'vue'
8
8
  import MIcon from './MIcon.vue'
9
9
  import { useFieldBg } from '../composables/useFieldBg'
10
10
  import { useLocale } from '../composables/useLocale'
11
+ import { useFollowAnchor } from '../composables/_useFollowAnchor'
11
12
  import { POPUP_MARGIN, dropdownX, fitPopup } from '../composables/_popupPosition'
12
13
  import { OUTLINED_FLOATED_LABEL, vOverflowTitle } from '../composables/_fieldLayout'
13
14
 
@@ -123,11 +124,15 @@ function onOut(e: MouseEvent) {
123
124
  open.value = false
124
125
  }
125
126
 
126
- function onScroll(e: Event) {
127
- if (!open.value) return
128
- if (panelEl.value?.contains(e.target as Node)) return
129
- open.value = false
127
+ // Following the field: re-measure it and re-run placeDrop() from an unconstrained left edge (see above)
128
+ function followAnchor() {
129
+ if (!triggerEl.value || !panelEl.value) return
130
+ anchorRect = triggerEl.value.getBoundingClientRect()
131
+ panelEl.value.style.left = '0px'
132
+ placeDrop(panelEl.value)
130
133
  }
134
+ // Stays on its field while the page or a container scrolls, the window/zoom changes or the field resizes
135
+ useFollowAnchor({ open: () => open.value, anchor: triggerEl, popup: panelEl, place: followAnchor, close: () => (open.value = false) })
131
136
 
132
137
  watch(open, (v) => {
133
138
  if (v) {
@@ -141,9 +146,7 @@ watch(open, (v) => {
141
146
  }
142
147
  })
143
148
 
144
- onMounted(() => window.addEventListener('scroll', onScroll, true))
145
149
  onUnmounted(() => {
146
- window.removeEventListener('scroll', onScroll, true)
147
150
  document.removeEventListener('mousedown', onOut)
148
151
  })
149
152
  </script>
@@ -0,0 +1,116 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * A time zone picker: every IANA time zone the browser knows (`Intl.supportedValuesOf('timeZone')`), labelled with
4
+ * its current UTC offset and its city ("UTC−03:00 · Buenos Aires"), sorted by offset and searchable by city, region
5
+ * or offset. The v-model is the IANA name (`'America/Argentina/Buenos_Aires'`); `detect` starts it on the user's own.
6
+ * Offsets are today's (they move with daylight saving). Built on `MAutocomplete`.
7
+ *
8
+ * @event update:modelValue A time zone was picked (its IANA name), or cleared (null)
9
+ */
10
+ import { computed, onMounted } from 'vue'
11
+ import MAutocomplete from './MAutocomplete.vue'
12
+ import { useLocale } from '../composables/useLocale'
13
+
14
+ const props = withDefaults(
15
+ defineProps<{
16
+ /** The IANA time zone name (v-model), or null */
17
+ modelValue?: string | null
18
+ /** Field label (the locale's "Time zone" by default) */
19
+ label?: string
20
+ /** Only these zones, instead of every one */
21
+ only?: string[]
22
+ /** Start on the user's own zone when the v-model is empty */
23
+ detect?: boolean
24
+ /** Field style */
25
+ variant?: 'filled' | 'outlined'
26
+ /** Dropdown, or a dialog on small screens */
27
+ mode?: 'docked' | 'modal'
28
+ /** Show a clear button */
29
+ clearable?: boolean
30
+ /** Helper text below the field */
31
+ hint?: string
32
+ /** Error state */
33
+ error?: boolean
34
+ /** Error message shown when `error` is true */
35
+ errorLabel?: string
36
+ /** Marks it as required */
37
+ required?: boolean
38
+ /** Disables the field */
39
+ disabled?: boolean
40
+ }>(),
41
+ { modelValue: null, detect: false, variant: 'filled', mode: 'docked', clearable: false },
42
+ )
43
+
44
+ const emit = defineEmits<{ 'update:modelValue': [zone: string | null] }>()
45
+
46
+ const t = useLocale()
47
+
48
+ /** Every zone the browser knows; a few common ones where it can't list them */
49
+ function allZones(): string[] {
50
+ const intl = Intl as typeof Intl & { supportedValuesOf?: (key: string) => string[] }
51
+ try {
52
+ const zones = intl.supportedValuesOf?.('timeZone')
53
+ if (zones?.length) return zones
54
+ } catch { /* older engines */ }
55
+ return ['UTC', 'America/New_York', 'America/Chicago', 'America/Denver', 'America/Los_Angeles', 'America/Mexico_City',
56
+ 'America/Bogota', 'America/Sao_Paulo', 'America/Argentina/Buenos_Aires', 'Europe/London', 'Europe/Madrid',
57
+ 'Europe/Paris', 'Europe/Berlin', 'Europe/Moscow', 'Africa/Cairo', 'Asia/Dubai', 'Asia/Kolkata', 'Asia/Shanghai',
58
+ 'Asia/Tokyo', 'Asia/Seoul', 'Australia/Sydney', 'Pacific/Auckland']
59
+ }
60
+
61
+ /** The zone's offset from UTC right now, in minutes */
62
+ function offsetOf(zone: string, at: Date): number {
63
+ try {
64
+ const parts = new Intl.DateTimeFormat('en-US', { timeZone: zone, timeZoneName: 'longOffset' }).formatToParts(at)
65
+ const name = parts.find((p) => p.type === 'timeZoneName')?.value ?? 'GMT'
66
+ const m = /GMT([+-])(\d{2}):?(\d{2})?/.exec(name)
67
+ return m ? (m[1] === '-' ? -1 : 1) * (Number(m[2]) * 60 + Number(m[3] ?? 0)) : 0
68
+ } catch {
69
+ return 0
70
+ }
71
+ }
72
+
73
+ const fmtOffset = (min: number) => {
74
+ const sign = min < 0 ? '−' : '+'
75
+ const a = Math.abs(min)
76
+ return `UTC${sign}${String(Math.floor(a / 60)).padStart(2, '0')}:${String(a % 60).padStart(2, '0')}`
77
+ }
78
+ /** America/Argentina/Buenos_Aires → Buenos Aires (America) */
79
+ function cityOf(zone: string) {
80
+ const parts = zone.split('/')
81
+ const city = parts[parts.length - 1]!.replace(/_/g, ' ')
82
+ return parts.length > 1 ? `${city} (${parts[0]!.replace(/_/g, ' ')})` : city
83
+ }
84
+
85
+ const options = computed(() => {
86
+ const now = new Date()
87
+ return (props.only ?? allZones())
88
+ .map((zone) => ({ zone, offset: offsetOf(zone, now) }))
89
+ .sort((a, b) => a.offset - b.offset || a.zone.localeCompare(b.zone))
90
+ .map(({ zone, offset }) => ({ value: zone, label: `${fmtOffset(offset)} · ${cityOf(zone)}` }))
91
+ })
92
+
93
+ onMounted(() => {
94
+ if (!props.detect || props.modelValue) return
95
+ const own = Intl.DateTimeFormat().resolvedOptions().timeZone
96
+ if (own) emit('update:modelValue', own)
97
+ })
98
+ </script>
99
+
100
+ <template>
101
+ <MAutocomplete
102
+ :model-value="modelValue"
103
+ :options="options"
104
+ :label="label ?? t.timeZone"
105
+ :variant="variant"
106
+ :mode="mode"
107
+ :clearable="clearable"
108
+ :hint="hint"
109
+ :error="error"
110
+ :error-label="errorLabel"
111
+ :required="required"
112
+ :disabled="disabled"
113
+ leading-icon="schedule"
114
+ @update:model-value="(v: unknown) => emit('update:modelValue', (v as string | null) ?? null)"
115
+ />
116
+ </template>