@m3ui-vue/m3ui-vue 1.4.0 → 1.4.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.
- package/dist/API.md +45 -3
- package/dist/api.json +1 -1
- package/dist/components/MCountdown.vue.d.ts +80 -0
- package/dist/index.d.ts +2 -0
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3318 -3167
- package/dist/m3ui.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/src/__tests__/MCountdown.test.ts +75 -0
- package/src/__tests__/clearToNull.test.ts +30 -0
- package/src/components/MAutocomplete.vue +2 -2
- package/src/components/MCountdown.vue +200 -0
- package/src/components/MSelect.vue +2 -2
- package/src/index.ts +2 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
export type CountdownUnit = 'days' | 'hours' | 'minutes' | 'seconds'
|
|
3
|
+
|
|
4
|
+
/** What the default slot receives: the time left, split into the units shown */
|
|
5
|
+
export interface CountdownParts {
|
|
6
|
+
days: number
|
|
7
|
+
hours: number
|
|
8
|
+
minutes: number
|
|
9
|
+
seconds: number
|
|
10
|
+
/** Milliseconds left (0 once finished) */
|
|
11
|
+
total: number
|
|
12
|
+
finished: boolean
|
|
13
|
+
}
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<script setup lang="ts">
|
|
17
|
+
/**
|
|
18
|
+
* MCountdown — a countdown to a date: days, hours, minutes and seconds with rolling digits, in boxes or inline.
|
|
19
|
+
* Unit names come from `Intl` in the app's language. Units left out of `units` fold into the next larger one shown
|
|
20
|
+
* (no `days` → 50 hours). Ticks on the second boundary; every tick costs one timer, not one per digit.
|
|
21
|
+
*
|
|
22
|
+
* @slot default Replaces the whole rendering; receives `CountdownParts` (`days`, `hours`, `minutes`, `seconds`, `total`, `finished`)
|
|
23
|
+
* @slot finished Shown instead of the countdown once it reaches zero
|
|
24
|
+
* @event finished Emitted once when the countdown reaches zero (not on mount if `to` is already past)
|
|
25
|
+
* @event tick Emitted every second with the `CountdownParts`
|
|
26
|
+
*/
|
|
27
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
28
|
+
import MAnimatedNumber from './MAnimatedNumber.vue'
|
|
29
|
+
import { useLocale } from '../composables/useLocale'
|
|
30
|
+
|
|
31
|
+
const props = withDefaults(
|
|
32
|
+
defineProps<{
|
|
33
|
+
/** The moment it counts down to: a `Date`, a timestamp in ms, or a date string */
|
|
34
|
+
to: Date | number | string
|
|
35
|
+
/** Units shown, largest first; leaving one out folds it into the next larger one */
|
|
36
|
+
units?: CountdownUnit[]
|
|
37
|
+
/** `boxes`: each unit in its own container with its name below; `inline`: one line of text (3d 04h 12m) */
|
|
38
|
+
variant?: 'boxes' | 'inline'
|
|
39
|
+
/** Digit size */
|
|
40
|
+
size?: 'sm' | 'md' | 'lg'
|
|
41
|
+
/** Show unit names */
|
|
42
|
+
labels?: boolean
|
|
43
|
+
/** Hide leading units while they're zero (no "0 days" a few hours before) */
|
|
44
|
+
hideEmpty?: boolean
|
|
45
|
+
/** Roll the digits; false (or the system's reduced motion) swaps them */
|
|
46
|
+
animate?: boolean
|
|
47
|
+
/** BCP-47 language for the unit names; defaults to the app's locale */
|
|
48
|
+
locale?: string
|
|
49
|
+
}>(),
|
|
50
|
+
{
|
|
51
|
+
units: () => ['days', 'hours', 'minutes', 'seconds'],
|
|
52
|
+
variant: 'boxes',
|
|
53
|
+
size: 'md',
|
|
54
|
+
labels: true,
|
|
55
|
+
hideEmpty: false,
|
|
56
|
+
animate: true,
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const emit = defineEmits<{ finished: []; tick: [CountdownParts] }>()
|
|
61
|
+
|
|
62
|
+
const t = useLocale()
|
|
63
|
+
const lang = computed(() => props.locale ?? t.lang)
|
|
64
|
+
|
|
65
|
+
const target = computed(() => {
|
|
66
|
+
const v = props.to
|
|
67
|
+
return v instanceof Date ? v.getTime() : typeof v === 'number' ? v : new Date(v).getTime()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const now = ref(Date.now())
|
|
71
|
+
const total = computed(() => (Number.isNaN(target.value) ? 0 : Math.max(0, target.value - now.value)))
|
|
72
|
+
|
|
73
|
+
const ORDER: CountdownUnit[] = ['days', 'hours', 'minutes', 'seconds']
|
|
74
|
+
const MS: Record<CountdownUnit, number> = { days: 86_400_000, hours: 3_600_000, minutes: 60_000, seconds: 1000 }
|
|
75
|
+
|
|
76
|
+
const shown = computed(() => ORDER.filter((u) => props.units.includes(u)))
|
|
77
|
+
|
|
78
|
+
const parts = computed<CountdownParts>(() => {
|
|
79
|
+
// Round up to the smallest unit shown: 0.4s left reads 1s (30s left reads 1 min without seconds), and 0 only
|
|
80
|
+
// appears when it's really over.
|
|
81
|
+
const step = MS[shown.value[shown.value.length - 1] ?? 'seconds']
|
|
82
|
+
let rest = Math.ceil(total.value / step) * step
|
|
83
|
+
const out: CountdownParts = { days: 0, hours: 0, minutes: 0, seconds: 0, total: total.value, finished: total.value <= 0 }
|
|
84
|
+
for (const u of shown.value) {
|
|
85
|
+
out[u] = Math.floor(rest / MS[u])
|
|
86
|
+
rest -= out[u] * MS[u]
|
|
87
|
+
}
|
|
88
|
+
return out
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const visible = computed(() => {
|
|
92
|
+
if (!props.hideEmpty) return shown.value
|
|
93
|
+
const first = shown.value.findIndex((u) => parts.value[u] > 0)
|
|
94
|
+
// Always keep the smallest unit, so the finished state still reads 00.
|
|
95
|
+
return shown.value.slice(first === -1 ? shown.value.length - 1 : Math.min(first, shown.value.length - 1))
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const INTL_UNIT: Record<CountdownUnit, string> = { days: 'day', hours: 'hour', minutes: 'minute', seconds: 'second' }
|
|
99
|
+
|
|
100
|
+
/** The language's own word for the unit, plural-aware ("1 día", "3 días"), or its narrow form for inline */
|
|
101
|
+
function unitLabel(u: CountdownUnit, n: number, display: 'long' | 'narrow') {
|
|
102
|
+
try {
|
|
103
|
+
const fmt = new Intl.NumberFormat(lang.value, { style: 'unit', unit: INTL_UNIT[u], unitDisplay: display })
|
|
104
|
+
return fmt.formatToParts(n).filter((p) => p.type === 'unit' || (display === 'narrow' && p.type === 'literal'))
|
|
105
|
+
.map((p) => p.value).join('').trim()
|
|
106
|
+
} catch {
|
|
107
|
+
return u
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const accessibleText = computed(() =>
|
|
112
|
+
shown.value
|
|
113
|
+
.map((u) => {
|
|
114
|
+
try {
|
|
115
|
+
return new Intl.NumberFormat(lang.value, { style: 'unit', unit: INTL_UNIT[u], unitDisplay: 'long' }).format(parts.value[u])
|
|
116
|
+
} catch {
|
|
117
|
+
return `${parts.value[u]} ${u}`
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
.join(', '),
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
// Leading unit unpadded (3 days, 120 hours), the rest always two digits (04:09).
|
|
124
|
+
const digitFormat = (i: number) => ({ minimumIntegerDigits: i === 0 ? 1 : 2, useGrouping: false })
|
|
125
|
+
|
|
126
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
127
|
+
let done = false
|
|
128
|
+
|
|
129
|
+
function schedule() {
|
|
130
|
+
clearTimeout(timer)
|
|
131
|
+
if (total.value <= 0) return
|
|
132
|
+
// Wake when the remaining time crosses the next whole second, so displayed seconds flip on time
|
|
133
|
+
// (a late timer is harmless, it just reschedules from the new remainder).
|
|
134
|
+
const delay = total.value % 1000 || 1000
|
|
135
|
+
timer = setTimeout(step, delay)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function step() {
|
|
139
|
+
now.value = Date.now()
|
|
140
|
+
emit('tick', parts.value)
|
|
141
|
+
if (total.value <= 0 && !done) {
|
|
142
|
+
done = true
|
|
143
|
+
emit('finished')
|
|
144
|
+
}
|
|
145
|
+
schedule()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
watch(target, () => {
|
|
149
|
+
now.value = Date.now()
|
|
150
|
+
done = total.value <= 0
|
|
151
|
+
schedule()
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
onMounted(() => {
|
|
155
|
+
now.value = Date.now()
|
|
156
|
+
done = total.value <= 0
|
|
157
|
+
schedule()
|
|
158
|
+
})
|
|
159
|
+
onBeforeUnmount(() => clearTimeout(timer))
|
|
160
|
+
|
|
161
|
+
const sizeClass = computed(
|
|
162
|
+
() => ({ sm: 'text-title-large', md: 'text-display-small', lg: 'text-display-medium' })[props.size],
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
defineExpose({ parts })
|
|
166
|
+
</script>
|
|
167
|
+
|
|
168
|
+
<template>
|
|
169
|
+
<div class="m3-countdown" :data-variant="variant" role="timer" :aria-label="accessibleText">
|
|
170
|
+
<slot v-if="parts.finished && $slots.finished" name="finished" />
|
|
171
|
+
<slot v-else-if="$slots.default" v-bind="parts" />
|
|
172
|
+
<div v-else-if="variant === 'boxes'" class="flex items-start gap-2" aria-hidden="true">
|
|
173
|
+
<div v-for="(u, i) in visible" :key="u" class="flex flex-col items-center gap-1">
|
|
174
|
+
<div
|
|
175
|
+
class="flex min-w-[2ch] items-center justify-center rounded-md bg-surface-container-high px-3 py-2 tabular-nums text-on-surface"
|
|
176
|
+
:class="sizeClass"
|
|
177
|
+
>
|
|
178
|
+
<MAnimatedNumber :value="parts[u]" :format="digitFormat(i)" :animate="animate" />
|
|
179
|
+
</div>
|
|
180
|
+
<span v-if="labels" class="text-label-medium text-on-surface-variant">{{ unitLabel(u, parts[u], 'long') }}</span>
|
|
181
|
+
</div>
|
|
182
|
+
</div>
|
|
183
|
+
<span v-else class="inline-flex items-baseline gap-1.5 tabular-nums text-on-surface" :class="sizeClass" aria-hidden="true">
|
|
184
|
+
<span v-for="(u, i) in visible" :key="u" class="inline-flex items-baseline">
|
|
185
|
+
<MAnimatedNumber :value="parts[u]" :format="digitFormat(i)" :animate="animate" />
|
|
186
|
+
<span v-if="labels" class="text-on-surface-variant">{{ unitLabel(u, parts[u], 'narrow') }}</span>
|
|
187
|
+
<span v-else-if="i < visible.length - 1" class="text-on-surface-variant">:</span>
|
|
188
|
+
</span>
|
|
189
|
+
</span>
|
|
190
|
+
</div>
|
|
191
|
+
</template>
|
|
192
|
+
|
|
193
|
+
<style>
|
|
194
|
+
/* Defaults in the base layer so the consumer's classes on the root win (see MShape). */
|
|
195
|
+
@layer base {
|
|
196
|
+
.m3-countdown {
|
|
197
|
+
display: inline-block;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
</style>
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* A single-value dropdown select, docked or as a modal dialog on smaller screens.
|
|
4
4
|
*
|
|
5
|
-
* @event update:modelValue Emitted with the selected option's value.
|
|
5
|
+
* @event update:modelValue Emitted with the selected option's value (`null` when the clear button empties it).
|
|
6
6
|
*/
|
|
7
7
|
import { computed, ref, useId, onMounted, onUnmounted, watch } from 'vue'
|
|
8
8
|
import MIcon from './MIcon.vue'
|
|
@@ -288,7 +288,7 @@ const labelClasses = computed(() => {
|
|
|
288
288
|
v-if="clearable && hasValue && !disabled"
|
|
289
289
|
type="button"
|
|
290
290
|
class="absolute right-9 top-1/2 -translate-y-1/2 flex h-6 w-6 cursor-pointer items-center justify-center rounded-full text-on-surface-variant transition-colors hover:bg-on-surface/8 hover:text-on-surface"
|
|
291
|
-
@click.stop="emit('update:modelValue',
|
|
291
|
+
@click.stop="emit('update:modelValue', null); open = false"
|
|
292
292
|
>
|
|
293
293
|
<MIcon name="close" :size="18" />
|
|
294
294
|
</button>
|
package/src/index.ts
CHANGED
|
@@ -73,6 +73,8 @@ export type { SparkleOptions, SparkleValue } from './directives/vSparkle'
|
|
|
73
73
|
export { default as MAbsolute } from './components/MAbsolute.vue'
|
|
74
74
|
export { default as MAlert } from './components/MAlert.vue'
|
|
75
75
|
export { default as MAnimatedNumber } from './components/MAnimatedNumber.vue'
|
|
76
|
+
export { default as MCountdown } from './components/MCountdown.vue'
|
|
77
|
+
export type { CountdownUnit, CountdownParts } from './components/MCountdown.vue'
|
|
76
78
|
export { default as MAutocomplete } from './components/MAutocomplete.vue'
|
|
77
79
|
export { default as MAppBar } from './components/MAppBar.vue'
|
|
78
80
|
export { default as MAppBarWithSearch } from './components/MAppBarWithSearch.vue'
|