@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.
Files changed (91) hide show
  1. package/dist/API.md +251 -9
  2. package/dist/{MButton-DQMTIlaV.js → MButton-D7M324r9.js} +2 -2
  3. package/dist/{MButton-DQMTIlaV.js.map → MButton-D7M324r9.js.map} +1 -1
  4. package/dist/{MDialog-cdgapGfZ.js → MDialog-DuOnNF9s.js} +2 -2
  5. package/dist/{MDialog-cdgapGfZ.js.map → MDialog-DuOnNF9s.js.map} +1 -1
  6. package/dist/{MListItem-ZkSGIIrb.js → MListItem-Bb84X6ex.js} +144 -120
  7. package/dist/{MListItem-ZkSGIIrb.js.map → MListItem-Bb84X6ex.js.map} +1 -1
  8. package/dist/{MLoadingIndicator-U6_oGx8z.js → MLoadingIndicator-BUGRo2Tu.js} +2 -2
  9. package/dist/{MLoadingIndicator-U6_oGx8z.js.map → MLoadingIndicator-BUGRo2Tu.js.map} +1 -1
  10. package/dist/{MSeekBar-DTZVizZg.js → MSeekBar-BQUvUqb9.js} +2 -2
  11. package/dist/{MSeekBar-DTZVizZg.js.map → MSeekBar-BQUvUqb9.js.map} +1 -1
  12. package/dist/{MSlider-CqJkEYRZ.js → MSlider-SFdjE1Hy.js} +2 -2
  13. package/dist/{MSlider-CqJkEYRZ.js.map → MSlider-SFdjE1Hy.js.map} +1 -1
  14. package/dist/{MSpinner-Btgc-0nb.js → MSpinner-DKGNN64_.js} +2 -2
  15. package/dist/{MSpinner-Btgc-0nb.js.map → MSpinner-DKGNN64_.js.map} +1 -1
  16. package/dist/{MTextField-B6M0gJEO.js → MTextField-Utsef6Kk.js} +2 -2
  17. package/dist/{MTextField-B6M0gJEO.js.map → MTextField-Utsef6Kk.js.map} +1 -1
  18. package/dist/{MTooltip-Dspji4XG.js → MTooltip-BhTFQ9Z2.js} +4 -4
  19. package/dist/{MTooltip-Dspji4XG.js.map → MTooltip-BhTFQ9Z2.js.map} +1 -1
  20. package/dist/api.json +1 -1
  21. package/dist/code-editor.js +1 -1
  22. package/dist/components/MAvailabilityCalendar.vue.d.ts +1 -1
  23. package/dist/components/MCalendarHeatmap.vue.d.ts +120 -0
  24. package/dist/components/MDatePicker.vue.d.ts +5 -0
  25. package/dist/components/MDatePickerModal.vue.d.ts +5 -0
  26. package/dist/components/MDateRangePicker.vue.d.ts +5 -0
  27. package/dist/components/MDateRangePickerModal.vue.d.ts +5 -0
  28. package/dist/components/MLightbox.vue.d.ts +1 -1
  29. package/dist/components/MListItem.vue.d.ts +10 -6
  30. package/dist/components/MMeterGroup.vue.d.ts +98 -0
  31. package/dist/components/MPhoneInput.vue.d.ts +72 -0
  32. package/dist/components/MSeekBar.vue.d.ts +1 -1
  33. package/dist/components/MTreeTable.vue.d.ts +1 -1
  34. package/dist/components/MTypewriter.vue.d.ts +57 -0
  35. package/dist/components/MVideoPlayer.vue.d.ts +1 -1
  36. package/dist/components/_MMonthYearButton.vue.d.ts +16 -0
  37. package/dist/components/_MYearGrid.vue.d.ts +19 -0
  38. package/dist/composables/_useCalendarMonth.d.ts +7 -0
  39. package/dist/composables/phoneNumber.d.ts +39 -0
  40. package/dist/composables/useLocale.d.ts +18 -0
  41. package/dist/data/phone-countries.d.ts +2 -0
  42. package/dist/draw.js +2 -2
  43. package/dist/flow.js +1 -1
  44. package/dist/gantt.js +1 -1
  45. package/dist/index.d.ts +8 -0
  46. package/dist/locales/index.js +63 -0
  47. package/dist/locales/index.js.map +1 -1
  48. package/dist/m3ui-vue.css +1 -1
  49. package/dist/m3ui.js +3662 -2028
  50. package/dist/m3ui.js.map +1 -1
  51. package/dist/map.js +5 -5
  52. package/dist/media.js +4 -4
  53. package/dist/pdf.js +4 -4
  54. package/dist/rich-text-editor.js +4 -4
  55. package/dist/styles.css +1 -1
  56. package/dist/{useLocale-DLKJIRFi.js → useLocale-B8YKWg-H.js} +10 -1
  57. package/dist/useLocale-B8YKWg-H.js.map +1 -0
  58. package/package.json +1 -1
  59. package/src/__tests__/MCalendarHeatmap.test.ts +79 -0
  60. package/src/__tests__/MListItemNestedControl.test.ts +88 -0
  61. package/src/__tests__/MMeterGroup.test.ts +43 -0
  62. package/src/__tests__/MPhoneInput.test.ts +97 -0
  63. package/src/__tests__/MTabs.test.ts +100 -2
  64. package/src/__tests__/MTypewriter.test.ts +50 -0
  65. package/src/__tests__/calendarMonth.test.ts +104 -0
  66. package/src/__tests__/ssr.test.ts +4 -0
  67. package/src/components/MCalendarHeatmap.vue +349 -0
  68. package/src/components/MDatePicker.vue +74 -34
  69. package/src/components/MDatePickerModal.vue +79 -36
  70. package/src/components/MDateRangePicker.vue +73 -33
  71. package/src/components/MDateRangePickerModal.vue +75 -35
  72. package/src/components/MListItem.vue +54 -8
  73. package/src/components/MMeterGroup.vue +196 -0
  74. package/src/components/MPhoneInput.vue +480 -0
  75. package/src/components/MTabs.vue +33 -6
  76. package/src/components/MTypewriter.vue +195 -0
  77. package/src/components/_MMonthYearButton.vue +36 -0
  78. package/src/components/_MYearGrid.vue +106 -0
  79. package/src/composables/_useCalendarMonth.ts +32 -2
  80. package/src/composables/phoneNumber.ts +137 -0
  81. package/src/composables/useLocale.ts +27 -0
  82. package/src/data/phone-countries.ts +62 -0
  83. package/src/index.ts +8 -0
  84. package/src/locales/de.ts +9 -0
  85. package/src/locales/es.ts +9 -0
  86. package/src/locales/fr.ts +9 -0
  87. package/src/locales/ja.ts +9 -0
  88. package/src/locales/ko.ts +9 -0
  89. package/src/locales/pt.ts +9 -0
  90. package/src/locales/zh.ts +9 -0
  91. package/dist/useLocale-DLKJIRFi.js.map +0 -1
@@ -0,0 +1,349 @@
1
+ <script lang="ts">
2
+ /** One day of `data` when given as an array */
3
+ export interface HeatmapDay {
4
+ /** YYYY-MM-DD */
5
+ date: string
6
+ value: number
7
+ }
8
+
9
+ /** A cell, as the tooltip slot and the select event get it */
10
+ export interface HeatmapCell {
11
+ /** YYYY-MM-DD */
12
+ date: string
13
+ value: number
14
+ /** 0 (nothing) to levels - 1 (the most) */
15
+ level: number
16
+ }
17
+ </script>
18
+
19
+ <script setup lang="ts">
20
+ /**
21
+ * MCalendarHeatmap — activity per day as a grid of weeks (a column per week, a row per weekday), each day tinted by
22
+ * how much happened: commits, workouts, sales, habits. Month and weekday names come from `Intl` in the app's
23
+ * language. Hover or focus a day for its value; arrow keys move by day (up/down) and by week (left/right), and
24
+ * Enter/Space or a click select it (v-model). Scrolls sideways when it doesn't fit, starting at the latest week.
25
+ *
26
+ * @slot tooltip Replaces the tooltip's text; receives the `HeatmapCell` (`date`, `value`, `level`)
27
+ * @event update:modelValue Emitted with the date (YYYY-MM-DD) of the day picked — powers v-model
28
+ * @event select Emitted with the `HeatmapCell` of the day picked
29
+ */
30
+ import { computed, nextTick, onMounted, ref, watch } from 'vue'
31
+ import { useLocale } from '../composables/useLocale'
32
+ import { isoDate } from '../composables/_useCalendarMonth'
33
+
34
+ const props = withDefaults(
35
+ defineProps<{
36
+ /** Values per day: `{ '2026-11-20': 4 }` or `[{ date: '2026-11-20', value: 4 }]`. Missing days are 0 */
37
+ data: Record<string, number> | HeatmapDay[]
38
+ /** Last day shown (YYYY-MM-DD or a Date); defaults to today */
39
+ end?: string | Date
40
+ /** First day shown; defaults to `weeks` weeks before `end` */
41
+ start?: string | Date
42
+ /** How many weeks to show when there's no `start` */
43
+ weeks?: number
44
+ /** First day of the week: 1 Monday (default, like the date pickers), 0 Sunday */
45
+ weekStart?: 0 | 1
46
+ /** Number of tints, the empty one included */
47
+ levels?: number
48
+ /** The value that gets the darkest tint; defaults to the largest value shown */
49
+ max?: number
50
+ /** An M3 color role (`primary`, `tertiary`, `success`...) or any CSS color */
51
+ color?: string
52
+ /** Side of a day's square, in px */
53
+ cellSize?: number
54
+ /** Space between days, in px */
55
+ gap?: number
56
+ /** Month names above the weeks */
57
+ monthLabels?: boolean
58
+ /** Weekday names (Mon, Wed, Fri) beside the rows */
59
+ dayLabels?: boolean
60
+ /** The "Less ▢▢▢ More" scale below */
61
+ legend?: boolean
62
+ /** Show the day's value on hover and focus */
63
+ tooltip?: boolean
64
+ /** The tooltip's text (and each day's accessible name); defaults to "value · date" */
65
+ formatTooltip?: (cell: HeatmapCell) => string
66
+ /** BCP-47 language for month/weekday names and numbers; defaults to the app's locale */
67
+ locale?: string
68
+ /** Selected day, YYYY-MM-DD (v-model); outlined */
69
+ modelValue?: string | null
70
+ /** Accessible name of the grid */
71
+ label?: string
72
+ }>(),
73
+ {
74
+ weeks: 53,
75
+ weekStart: 1,
76
+ levels: 5,
77
+ color: 'primary',
78
+ cellSize: 12,
79
+ gap: 3,
80
+ monthLabels: true,
81
+ dayLabels: true,
82
+ legend: true,
83
+ tooltip: true,
84
+ modelValue: null,
85
+ },
86
+ )
87
+
88
+ const emit = defineEmits<{ 'update:modelValue': [string]; select: [HeatmapCell] }>()
89
+
90
+ const t = useLocale()
91
+ const lang = computed(() => props.locale ?? t.lang)
92
+
93
+ const ROLES = new Set(['primary', 'secondary', 'tertiary', 'error', 'success', 'on-surface', 'on-surface-variant', 'outline', 'inverse-primary'])
94
+ const baseColor = computed(() => (ROLES.has(props.color) ? `var(--color-${props.color})` : props.color))
95
+
96
+ const toDate = (v: string | Date) => (v instanceof Date ? new Date(v.getFullYear(), v.getMonth(), v.getDate()) : new Date(v + 'T00:00:00'))
97
+ const iso = (d: Date) => isoDate(d.getFullYear(), d.getMonth(), d.getDate())
98
+
99
+ const values = computed(() => {
100
+ const d = props.data ?? {}
101
+ if (!Array.isArray(d)) return d
102
+ const out: Record<string, number> = {}
103
+ for (const { date, value } of d) out[date] = (out[date] ?? 0) + value
104
+ return out
105
+ })
106
+
107
+ const range = computed(() => {
108
+ const end = toDate(props.end ?? new Date())
109
+ const start = props.start ? toDate(props.start) : new Date(end.getFullYear(), end.getMonth(), end.getDate() - (props.weeks * 7 - 1))
110
+ return { start: iso(start), end: iso(end), startDate: start, endDate: end }
111
+ })
112
+
113
+ const maxValue = computed(() => {
114
+ if (props.max != null) return props.max
115
+ let m = 0
116
+ for (const [date, v] of Object.entries(values.value)) if (date >= range.value.start && date <= range.value.end && v > m) m = v
117
+ return m
118
+ })
119
+
120
+ function levelOf(value: number) {
121
+ if (value <= 0 || maxValue.value <= 0) return 0
122
+ return Math.min(props.levels - 1, Math.max(1, Math.ceil((value / maxValue.value) * (props.levels - 1))))
123
+ }
124
+
125
+ // Level 0 is the empty container; the rest mix the color into it, evenly up to the full color
126
+ function levelColor(level: number) {
127
+ if (level === 0) return 'var(--color-surface-container-highest)'
128
+ const steps = props.levels - 1
129
+ const pct = steps <= 1 ? 100 : Math.round(25 + (75 * (level - 1)) / (steps - 1))
130
+ return `color-mix(in srgb, ${baseColor.value} ${pct}%, var(--color-surface-container-highest))`
131
+ }
132
+
133
+ interface Cell extends HeatmapCell { inRange: boolean }
134
+
135
+ // Rows are weekdays, columns weeks, from the week holding `start` to the one holding `end`
136
+ const grid = computed(() => {
137
+ const { startDate, endDate, start, end } = range.value
138
+ const lead = (startDate.getDay() - props.weekStart + 7) % 7
139
+ const first = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - lead)
140
+ const span = Math.round((endDate.getTime() - first.getTime()) / 86_400_000) + 1
141
+ const cols = Math.ceil(span / 7)
142
+ const rows: Cell[][] = Array.from({ length: 7 }, () => [])
143
+ const firstOfCol: string[] = []
144
+ for (let c = 0; c < cols; c++) {
145
+ for (let r = 0; r < 7; r++) {
146
+ const date = isoDate(first.getFullYear(), first.getMonth(), first.getDate() + c * 7 + r)
147
+ const inRange = date >= start && date <= end
148
+ const value = inRange ? (values.value[date] ?? 0) : 0
149
+ rows[r]!.push({ date, value, level: inRange ? levelOf(value) : 0, inRange })
150
+ if (inRange && firstOfCol[c] === undefined) firstOfCol[c] = date
151
+ }
152
+ }
153
+ return { rows, cols, firstOfCol }
154
+ })
155
+
156
+ // A month's name over the first week that starts in it, unless too close to the previous one (or the edge) to fit
157
+ const months = computed(() => {
158
+ const f = new Intl.DateTimeFormat(lang.value, { month: 'short' })
159
+ const out: { col: number; label: string }[] = []
160
+ let prev = ''
161
+ grid.value.firstOfCol.forEach((date, col) => {
162
+ if (!date) return
163
+ const ym = date.slice(0, 7)
164
+ if (ym === prev) return
165
+ prev = ym
166
+ const last = out[out.length - 1]
167
+ if (last && col - last.col < 3) return
168
+ if (!last && grid.value.cols - col < 2) return
169
+ out.push({ col, label: f.format(new Date(date + 'T00:00:00')) })
170
+ })
171
+ // The first label is dropped when the next month starts too soon after it
172
+ if (out.length > 1 && out[1]!.col - out[0]!.col < 3) out.shift()
173
+ return out
174
+ })
175
+
176
+ // Monday, Wednesday and Friday, whatever the week starts on
177
+ const weekdayLabels = computed(() => {
178
+ const f = new Intl.DateTimeFormat(lang.value, { weekday: 'short' })
179
+ return Array.from({ length: 7 }, (_, r) => {
180
+ const day = (props.weekStart + r) % 7
181
+ return day === 1 || day === 3 || day === 5 ? f.format(new Date(2024, 0, 7 + day)) : ''
182
+ })
183
+ })
184
+
185
+ const numberFormat = computed(() => new Intl.NumberFormat(lang.value))
186
+ const dateFormat = computed(() => new Intl.DateTimeFormat(lang.value, { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' }))
187
+ function describe(cell: HeatmapCell) {
188
+ if (props.formatTooltip) return props.formatTooltip(cell)
189
+ return `${numberFormat.value.format(cell.value)} · ${dateFormat.value.format(new Date(cell.date + 'T00:00:00'))}`
190
+ }
191
+
192
+ const legendLevels = computed(() => Array.from({ length: props.levels }, (_, i) => i))
193
+
194
+ // ── Focus, selection, tooltip ─────────────────────────────────────
195
+ const root = ref<HTMLElement>()
196
+ const scroller = ref<HTMLElement>()
197
+ const focusDate = ref<string>(props.modelValue ?? range.value.end)
198
+ watch(() => props.modelValue, (v) => { if (v) focusDate.value = v })
199
+ // Keeps the Tab stop inside the range when the range changes
200
+ const tabStop = computed(() => {
201
+ const { start, end } = range.value
202
+ return focusDate.value < start ? start : focusDate.value > end ? end : focusDate.value
203
+ })
204
+
205
+ const tip = ref<{ cell: HeatmapCell; x: number; y: number } | null>(null)
206
+
207
+ function showTip(cell: Cell, e: Event) {
208
+ if (!props.tooltip || !cell.inRange || !root.value) return
209
+ const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
210
+ const o = root.value.getBoundingClientRect()
211
+ tip.value = { cell, x: r.left - o.left + r.width / 2, y: r.top - o.top }
212
+ }
213
+
214
+ function pick(cell: Cell) {
215
+ if (!cell.inRange) return
216
+ focusDate.value = cell.date
217
+ emit('update:modelValue', cell.date)
218
+ emit('select', { date: cell.date, value: cell.value, level: cell.level })
219
+ }
220
+
221
+ function cellAt(date: string) {
222
+ for (const row of grid.value.rows) for (const c of row) if (c.date === date) return c
223
+ return undefined
224
+ }
225
+
226
+ async function onKey(e: KeyboardEvent) {
227
+ const by: Record<string, number> = { ArrowUp: -1, ArrowDown: 1, ArrowLeft: -7, ArrowRight: 7 }
228
+ const { start, end } = range.value
229
+ let next: string | undefined
230
+ if (e.key in by) {
231
+ const d = new Date(tabStop.value + 'T00:00:00')
232
+ next = isoDate(d.getFullYear(), d.getMonth(), d.getDate() + by[e.key]!)
233
+ if (next < start || next > end) return e.preventDefault()
234
+ } else if (e.key === 'Home') next = start
235
+ else if (e.key === 'End') next = end
236
+ else if (e.key === 'Enter' || e.key === ' ') {
237
+ e.preventDefault()
238
+ const c = cellAt(tabStop.value)
239
+ if (c) pick(c)
240
+ return
241
+ } else return
242
+ e.preventDefault()
243
+ focusDate.value = next
244
+ await nextTick()
245
+ const el = root.value?.querySelector<HTMLElement>(`[data-date="${next}"]`)
246
+ el?.focus()
247
+ if (el) showTip(cellAt(next)!, { currentTarget: el } as unknown as Event)
248
+ }
249
+
250
+ // Starts on the latest weeks
251
+ onMounted(() => {
252
+ if (scroller.value) scroller.value.scrollLeft = scroller.value.scrollWidth
253
+ })
254
+
255
+ const step = computed(() => props.cellSize + props.gap)
256
+ </script>
257
+
258
+ <template>
259
+ <div ref="root" class="m3-heatmap relative inline-flex max-w-full flex-col gap-2 text-on-surface-variant">
260
+ <div class="flex min-w-0">
261
+ <!-- Weekday names -->
262
+ <div
263
+ v-if="dayLabels"
264
+ class="grid shrink-0 pr-2 text-label-small"
265
+ :style="{ gridTemplateRows: `repeat(7, ${cellSize}px)`, rowGap: `${gap}px`, marginTop: monthLabels ? '20px' : undefined }"
266
+ aria-hidden="true"
267
+ >
268
+ <span v-for="(d, i) in weekdayLabels" :key="i" class="flex items-center leading-none">{{ d }}</span>
269
+ </div>
270
+
271
+ <div ref="scroller" class="min-w-0 overflow-x-auto" @scroll="tip = null">
272
+ <!-- Month names -->
273
+ <div v-if="monthLabels" class="relative h-5 text-label-small" aria-hidden="true" :style="{ width: `${grid.cols * step - gap}px` }">
274
+ <span v-for="m in months" :key="m.col" class="absolute top-0 capitalize whitespace-nowrap" :style="{ left: `${m.col * step}px` }">{{ m.label }}</span>
275
+ </div>
276
+
277
+ <div
278
+ role="grid"
279
+ :aria-label="label"
280
+ class="grid"
281
+ :style="{ gridTemplateColumns: `repeat(${grid.cols}, ${cellSize}px)`, gridAutoRows: `${cellSize}px`, gap: `${gap}px` }"
282
+ @keydown="onKey"
283
+ @mouseleave="tip = null"
284
+ >
285
+ <div v-for="(row, r) in grid.rows" :key="r" role="row" class="contents">
286
+ <div
287
+ v-for="cell in row"
288
+ :key="cell.date"
289
+ :role="cell.inRange ? 'gridcell' : 'presentation'"
290
+ :data-date="cell.inRange ? cell.date : undefined"
291
+ :data-level="cell.inRange ? cell.level : undefined"
292
+ :tabindex="cell.inRange ? (cell.date === tabStop ? 0 : -1) : undefined"
293
+ :aria-label="cell.inRange ? describe(cell) : undefined"
294
+ :aria-selected="cell.inRange ? cell.date === modelValue : undefined"
295
+ class="m3-heatmap-cell rounded-[3px] outline-none"
296
+ :class="[
297
+ cell.inRange ? 'cursor-pointer' : 'invisible',
298
+ cell.date === modelValue && 'm3-heatmap-selected',
299
+ ]"
300
+ :style="{ backgroundColor: levelColor(cell.level) }"
301
+ @mouseenter="showTip(cell, $event)"
302
+ @focus="showTip(cell, $event)"
303
+ @blur="tip = null"
304
+ @click="pick(cell)"
305
+ />
306
+ </div>
307
+ </div>
308
+ </div>
309
+ </div>
310
+
311
+ <!-- Legend -->
312
+ <div v-if="legend" class="flex items-center justify-end gap-1 text-label-small" aria-hidden="true">
313
+ <span class="mr-1">{{ t.heatmapLess }}</span>
314
+ <span
315
+ v-for="l in legendLevels"
316
+ :key="l"
317
+ class="rounded-[3px]"
318
+ :style="{ width: `${cellSize}px`, height: `${cellSize}px`, backgroundColor: levelColor(l) }"
319
+ />
320
+ <span class="ml-1">{{ t.heatmapMore }}</span>
321
+ </div>
322
+
323
+ <!-- Tooltip: M3 plain tooltip (inverse surface, extra-small corners) -->
324
+ <div
325
+ v-if="tip"
326
+ class="pointer-events-none absolute z-10 -translate-x-1/2 -translate-y-full whitespace-nowrap rounded-xs bg-inverse-surface px-2 py-1 text-body-small text-inverse-on-surface shadow-elevation-1"
327
+ :style="{ left: `${tip.x}px`, top: `${tip.y - 4}px` }"
328
+ aria-hidden="true"
329
+ >
330
+ <slot name="tooltip" v-bind="tip.cell">{{ describe(tip.cell) }}</slot>
331
+ </div>
332
+ </div>
333
+ </template>
334
+
335
+ <style>
336
+ .m3-heatmap-cell {
337
+ transition: box-shadow 100ms;
338
+ }
339
+ .m3-heatmap-cell:hover {
340
+ box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--color-on-surface) 38%, transparent);
341
+ }
342
+ .m3-heatmap-cell:focus-visible {
343
+ box-shadow: 0 0 0 2px var(--color-secondary);
344
+ }
345
+ .m3-heatmap-selected,
346
+ .m3-heatmap-selected:hover {
347
+ box-shadow: 0 0 0 2px var(--color-on-surface);
348
+ }
349
+ </style>
@@ -7,6 +7,8 @@
7
7
  import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
8
8
  import MIcon from './MIcon.vue'
9
9
  import MIconButton from './MIconButton.vue'
10
+ import MMonthYearButton from './_MMonthYearButton.vue'
11
+ import MYearGrid from './_MYearGrid.vue'
10
12
  import { useFieldBg } from '../composables/useFieldBg'
11
13
  import { useLocale } from '../composables/useLocale'
12
14
  import { POPUP_MARGIN, dropdownX, fitPopup } from '../composables/_popupPosition'
@@ -23,6 +25,11 @@ const props = defineProps<{
23
25
  min?: string
24
26
  /** Maximum selectable date (YYYY-MM-DD) */
25
27
  max?: string
28
+ /**
29
+ * First and last year the year grid (opened from the month's title) offers, as `[from, to]`; the years min/max
30
+ * leave no day in are disabled. Default 1900–2100, as Compose's DatePickerDefaults.YearRange
31
+ */
32
+ yearRange?: [number, number]
26
33
  /** Disables the picker */
27
34
  disabled?: boolean
28
35
  /** Error state */
@@ -65,12 +72,25 @@ watch(() => props.modelValue, (v) => {
65
72
  })
66
73
 
67
74
  // Weekday names, the month's title and its six-week grid, shared with the other date pickers
68
- const { weekdays: WEEKDAYS, monthLabel, days: calendarDays, isOutOfRange, prevMonth, nextMonth, isToday } = useCalendarMonth(viewDate, {
75
+ const { weekdays: WEEKDAYS, monthLabel, days: calendarDays, isOutOfRange, prevMonth, nextMonth, isToday, years, showYear } = useCalendarMonth(viewDate, {
69
76
  locale: () => resolvedLocale.value,
70
77
  min: () => props.min,
71
78
  max: () => props.max,
79
+ yearRange: () => props.yearRange,
72
80
  })
73
81
 
82
+ // Compose's YearPicker: the month's title is a menu button that lays a 3-column grid of years over the days; a
83
+ // year shows that year with the same month and goes back to the days
84
+ const yearPicker = ref(false)
85
+ const yearToggle = ref<InstanceType<typeof MMonthYearButton>>()
86
+ watch(open, () => { yearPicker.value = false })
87
+
88
+ function pickYear(year: number) {
89
+ showYear(year)
90
+ yearPicker.value = false
91
+ yearToggle.value?.focus()
92
+ }
93
+
74
94
  function selectDay(day: typeof calendarDays.value[0]) {
75
95
  if (day.disabled) return
76
96
  emit('update:modelValue', day.iso)
@@ -197,42 +217,62 @@ onUnmounted(() => {
197
217
  :style="dropPos"
198
218
  >
199
219
  <!-- Header -->
200
- <div class="mb-3 flex items-center justify-between">
201
- <MIconButton icon="chevron_left" :label="prevMonthLabel ?? localeStrings.previousMonth" :size="36" @click="prevMonth" />
202
- <span class="text-title-small font-medium capitalize text-on-surface">{{ monthLabel }}</span>
203
- <MIconButton icon="chevron_right" :label="nextMonthLabel ?? localeStrings.nextMonth" :size="36" @click="nextMonth" />
220
+ <div class="mb-3 flex h-10 items-center justify-between">
221
+ <MMonthYearButton
222
+ ref="yearToggle"
223
+ :label="monthLabel"
224
+ :expanded="yearPicker"
225
+ :action="yearPicker ? localeStrings.dateSwitchToDay : localeStrings.dateSwitchToYear"
226
+ @toggle="yearPicker = !yearPicker"
227
+ />
228
+ <div v-if="!yearPicker" class="flex">
229
+ <MIconButton icon="chevron_left" :label="prevMonthLabel ?? localeStrings.previousMonth" :size="36" @click="prevMonth" />
230
+ <MIconButton icon="chevron_right" :label="nextMonthLabel ?? localeStrings.nextMonth" :size="36" @click="nextMonth" />
231
+ </div>
204
232
  </div>
205
233
 
206
- <!-- Weekday headers -->
207
- <div class="mb-1 grid grid-cols-7 gap-0.5 text-center">
208
- <span v-for="wd in WEEKDAYS" :key="wd" class="py-1 text-body-large font-medium text-on-surface">
209
- {{ wd }}
210
- </span>
211
- </div>
234
+ <div class="relative">
235
+ <!-- Weekday headers -->
236
+ <div class="mb-1 grid grid-cols-7 gap-0.5 text-center">
237
+ <span v-for="wd in WEEKDAYS" :key="wd" class="py-1 text-body-large font-medium text-on-surface">
238
+ {{ wd }}
239
+ </span>
240
+ </div>
241
+
242
+ <!-- Days grid -->
243
+ <div class="grid grid-cols-7 gap-0.5">
244
+ <button
245
+ v-for="(day, i) in calendarDays"
246
+ :key="i"
247
+ type="button"
248
+ class="flex h-10 w-full items-center justify-center rounded-full text-body-medium transition-colors duration-100"
249
+ :class="[
250
+ day.disabled
251
+ ? 'cursor-not-allowed text-on-surface/25'
252
+ : day.iso === modelValue
253
+ ? 'bg-primary text-on-primary'
254
+ : isToday(day.iso)
255
+ ? 'border border-primary text-primary cursor-pointer hover:bg-primary/8'
256
+ : day.current
257
+ ? 'cursor-pointer text-on-surface hover:bg-on-surface/8'
258
+ : 'cursor-pointer text-on-surface-variant/50 hover:bg-on-surface/4',
259
+ ]"
260
+ :disabled="day.disabled"
261
+ @click="selectDay(day)"
262
+ >
263
+ {{ day.date }}
264
+ </button>
265
+ </div>
212
266
 
213
- <!-- Days grid -->
214
- <div class="grid grid-cols-7 gap-0.5">
215
- <button
216
- v-for="(day, i) in calendarDays"
217
- :key="i"
218
- type="button"
219
- class="flex h-10 w-full items-center justify-center rounded-full text-body-medium transition-colors duration-100"
220
- :class="[
221
- day.disabled
222
- ? 'cursor-not-allowed text-on-surface/25'
223
- : day.iso === modelValue
224
- ? 'bg-primary text-on-primary'
225
- : isToday(day.iso)
226
- ? 'border border-primary text-primary cursor-pointer hover:bg-primary/8'
227
- : day.current
228
- ? 'cursor-pointer text-on-surface hover:bg-on-surface/8'
229
- : 'cursor-pointer text-on-surface-variant/50 hover:bg-on-surface/4',
230
- ]"
231
- :disabled="day.disabled"
232
- @click="selectDay(day)"
233
- >
234
- {{ day.date }}
235
- </button>
267
+ <!-- Year grid, over the days and as tall as them -->
268
+ <MYearGrid
269
+ :open="yearPicker"
270
+ :years="years"
271
+ :view-year="viewDate.getFullYear()"
272
+ :locale="resolvedLocale"
273
+ :label="localeStrings.dateSwitchToYear"
274
+ @pick="pickYear"
275
+ />
236
276
  </div>
237
277
  </div>
238
278
  </Transition>
@@ -10,7 +10,8 @@ import { computed, ref, watch } from 'vue'
10
10
  import MButton from './MButton.vue'
11
11
  import MDialog from './MDialog.vue'
12
12
  import MIconButton from './MIconButton.vue'
13
- import MIcon from './MIcon.vue'
13
+ import MMonthYearButton from './_MMonthYearButton.vue'
14
+ import MYearGrid from './_MYearGrid.vue'
14
15
  import { useLocale } from '../composables/useLocale'
15
16
  import { isoDate as fmt, useCalendarMonth } from '../composables/_useCalendarMonth'
16
17
 
@@ -30,6 +31,11 @@ const props = withDefaults(defineProps<{
30
31
  locale?: string
31
32
  /** Dialog title */
32
33
  title?: string
34
+ /**
35
+ * First and last year the year grid (opened from the month's title) offers, as `[from, to]`; the years min/max
36
+ * leave no day in are disabled. Default 1900–2100, as Compose's DatePickerDefaults.YearRange
37
+ */
38
+ yearRange?: [number, number]
33
39
  }>(), {
34
40
  modelValue: null,
35
41
  show: false,
@@ -57,18 +63,33 @@ watch(() => props.show, (open) => {
57
63
  selected.value = props.modelValue ?? null
58
64
  viewDate.value = props.modelValue ? new Date(props.modelValue + 'T00:00:00') : new Date()
59
65
  inputMode.value = 'calendar'
66
+ yearPicker.value = false
60
67
  dateInput.value = props.modelValue ? isoToDisplay(props.modelValue) : ''
61
68
  }
62
69
  })
63
70
 
64
71
  // ── Calendar logic ─────────────────────────────────────────────────
65
72
  // Weekday names, the month's title and its six-week grid, shared with the other date pickers
66
- const { weekdays: WEEKDAYS, monthLabel, days: calendarDays, isOutOfRange: isOOR, prevMonth, nextMonth } = useCalendarMonth(viewDate, {
73
+ const { weekdays: WEEKDAYS, monthLabel, days: calendarDays, isOutOfRange: isOOR, prevMonth, nextMonth, years, showYear } = useCalendarMonth(viewDate, {
67
74
  locale: () => resolvedLocale.value,
68
75
  min: () => props.min,
69
76
  max: () => props.max,
77
+ yearRange: () => props.yearRange,
70
78
  })
71
79
 
80
+ // ── Year grid ──────────────────────────────────────────────────────
81
+ // Compose's YearPicker: the month's title is a menu button that lays a 3-column grid of years over the days; a
82
+ // year shows that year with the same month and goes back to the days.
83
+ const yearPicker = ref(false)
84
+ const yearToggle = ref<InstanceType<typeof MMonthYearButton>>()
85
+ watch(inputMode, () => { yearPicker.value = false })
86
+
87
+ function pickYear(year: number) {
88
+ showYear(year)
89
+ yearPicker.value = false
90
+ yearToggle.value?.focus()
91
+ }
92
+
72
93
  const todayIso = fmt(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())
73
94
 
74
95
  function selectDay(day: typeof calendarDays.value[0]) {
@@ -167,43 +188,64 @@ function onDialog(open: boolean) {
167
188
 
168
189
  <!-- Calendar mode -->
169
190
  <div v-if="inputMode === 'calendar'" class="px-4 pt-3 pb-2">
170
- <!-- Month nav -->
171
- <div class="mb-2 flex items-center justify-between">
172
- <MIconButton icon="chevron_left" :label="locale.previousMonth" :size="36" @click="prevMonth" />
173
- <span class="text-title-small font-medium capitalize text-on-surface">{{ monthLabel }}</span>
174
- <MIconButton icon="chevron_right" :label="locale.nextMonth" :size="36" @click="nextMonth" />
191
+ <!-- Month nav: Compose's MonthsNavigation, the month/year menu button at the start and the arrows at the end
192
+ (hidden while the year grid is open) -->
193
+ <div class="mb-2 flex h-10 items-center justify-between">
194
+ <MMonthYearButton
195
+ ref="yearToggle"
196
+ :label="monthLabel"
197
+ :expanded="yearPicker"
198
+ :action="yearPicker ? locale.dateSwitchToDay : locale.dateSwitchToYear"
199
+ @toggle="yearPicker = !yearPicker"
200
+ />
201
+ <div v-if="!yearPicker" class="flex">
202
+ <MIconButton icon="chevron_left" :label="locale.previousMonth" :size="36" @click="prevMonth" />
203
+ <MIconButton icon="chevron_right" :label="locale.nextMonth" :size="36" @click="nextMonth" />
204
+ </div>
175
205
  </div>
176
206
 
177
- <!-- Weekday headers -->
178
- <div class="mb-1 grid grid-cols-7 gap-0.5 text-center">
179
- <span v-for="wd in WEEKDAYS" :key="wd" class="py-1 text-body-large font-medium text-on-surface">
180
- {{ wd }}
181
- </span>
182
- </div>
207
+ <div class="relative">
208
+ <!-- Weekday headers -->
209
+ <div class="mb-1 grid grid-cols-7 gap-0.5 text-center">
210
+ <span v-for="wd in WEEKDAYS" :key="wd" class="py-1 text-body-large font-medium text-on-surface">
211
+ {{ wd }}
212
+ </span>
213
+ </div>
214
+
215
+ <!-- Days grid -->
216
+ <div class="grid grid-cols-7 gap-0.5">
217
+ <button
218
+ v-for="(day, i) in calendarDays"
219
+ :key="i"
220
+ type="button"
221
+ class="flex h-10 w-full items-center justify-center rounded-full text-body-medium transition-colors duration-100"
222
+ :class="[
223
+ day.disabled
224
+ ? 'cursor-not-allowed text-on-surface/25'
225
+ : day.iso === selected
226
+ ? 'bg-primary text-on-primary'
227
+ : day.iso === todayIso
228
+ ? 'border border-primary text-primary cursor-pointer hover:bg-primary/8'
229
+ : day.current
230
+ ? 'cursor-pointer text-on-surface hover:bg-on-surface/8'
231
+ : 'cursor-pointer text-on-surface-variant/50 hover:bg-on-surface/4',
232
+ ]"
233
+ :disabled="day.disabled"
234
+ @click="selectDay(day)"
235
+ >
236
+ {{ day.date }}
237
+ </button>
238
+ </div>
183
239
 
184
- <!-- Days grid -->
185
- <div class="grid grid-cols-7 gap-0.5">
186
- <button
187
- v-for="(day, i) in calendarDays"
188
- :key="i"
189
- type="button"
190
- class="flex h-10 w-full items-center justify-center rounded-full text-body-medium transition-colors duration-100"
191
- :class="[
192
- day.disabled
193
- ? 'cursor-not-allowed text-on-surface/25'
194
- : day.iso === selected
195
- ? 'bg-primary text-on-primary'
196
- : day.iso === todayIso
197
- ? 'border border-primary text-primary cursor-pointer hover:bg-primary/8'
198
- : day.current
199
- ? 'cursor-pointer text-on-surface hover:bg-on-surface/8'
200
- : 'cursor-pointer text-on-surface-variant/50 hover:bg-on-surface/4',
201
- ]"
202
- :disabled="day.disabled"
203
- @click="selectDay(day)"
204
- >
205
- {{ day.date }}
206
- </button>
240
+ <!-- Year grid, over the days and as tall as them -->
241
+ <MYearGrid
242
+ :open="yearPicker"
243
+ :years="years"
244
+ :view-year="viewDate.getFullYear()"
245
+ :locale="resolvedLocale"
246
+ :label="locale.dateSwitchToYear"
247
+ @pick="pickYear"
248
+ />
207
249
  </div>
208
250
  </div>
209
251
 
@@ -228,3 +270,4 @@ function onDialog(open: boolean) {
228
270
  </div>
229
271
  </MDialog>
230
272
  </template>
273
+