@m3ui-vue/m3ui-vue 1.3.7 → 1.4.0

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 (130) hide show
  1. package/dist/API.md +1097 -26
  2. package/dist/{MButton-D5eJiDza.js → MButton-DQMTIlaV.js} +2 -2
  3. package/dist/{MButton-D5eJiDza.js.map → MButton-DQMTIlaV.js.map} +1 -1
  4. package/dist/{MDialog-WqB0jewp.js → MDialog-cdgapGfZ.js} +2 -2
  5. package/dist/{MDialog-WqB0jewp.js.map → MDialog-cdgapGfZ.js.map} +1 -1
  6. package/dist/{MListItem-Cf-C2Ru-.js → MListItem-ZkSGIIrb.js} +2 -2
  7. package/dist/{MListItem-Cf-C2Ru-.js.map → MListItem-ZkSGIIrb.js.map} +1 -1
  8. package/dist/{MLoadingIndicator-D4GzEZoH.js → MLoadingIndicator-U6_oGx8z.js} +2 -2
  9. package/dist/{MLoadingIndicator-D4GzEZoH.js.map → MLoadingIndicator-U6_oGx8z.js.map} +1 -1
  10. package/dist/{MSeekBar-B19xoWE3.js → MSeekBar-DTZVizZg.js} +2 -2
  11. package/dist/{MSeekBar-B19xoWE3.js.map → MSeekBar-DTZVizZg.js.map} +1 -1
  12. package/dist/{MSlider-9RoiuebS.js → MSlider-CqJkEYRZ.js} +2 -2
  13. package/dist/{MSlider-9RoiuebS.js.map → MSlider-CqJkEYRZ.js.map} +1 -1
  14. package/dist/{MSpinner-BNF8MN-L.js → MSpinner-Btgc-0nb.js} +2 -2
  15. package/dist/{MSpinner-BNF8MN-L.js.map → MSpinner-Btgc-0nb.js.map} +1 -1
  16. package/dist/{MTextField-CuG0T3qh.js → MTextField-B6M0gJEO.js} +2 -2
  17. package/dist/{MTextField-CuG0T3qh.js.map → MTextField-B6M0gJEO.js.map} +1 -1
  18. package/dist/{MTooltip-Bv7yxngk.js → MTooltip-Dspji4XG.js} +4 -4
  19. package/dist/{MTooltip-Bv7yxngk.js.map → MTooltip-Dspji4XG.js.map} +1 -1
  20. package/dist/api.json +1 -1
  21. package/dist/code-editor.js +1 -1
  22. package/dist/components/MAudioPlayer.vue.d.ts +2 -2
  23. package/dist/components/MAvailabilityCalendar.vue.d.ts +83 -0
  24. package/dist/components/MBadge.vue.d.ts +1 -1
  25. package/dist/components/MBeforeAfter.vue.d.ts +61 -0
  26. package/dist/components/MBodyMap.vue.d.ts +70 -0
  27. package/dist/components/MCandlestickChart.vue.d.ts +82 -0
  28. package/dist/components/MCertificate.vue.d.ts +76 -0
  29. package/dist/components/MCreditCardInput.vue.d.ts +75 -0
  30. package/dist/components/MFlow.vue.d.ts +13 -0
  31. package/dist/components/MMortgageCalculator.vue.d.ts +83 -0
  32. package/dist/components/MOdontogram.vue.d.ts +86 -0
  33. package/dist/components/MOrderBook.vue.d.ts +89 -0
  34. package/dist/components/MOrderTicket.vue.d.ts +91 -0
  35. package/dist/components/MPosKeypad.vue.d.ts +84 -0
  36. package/dist/components/MProductConfigurator.vue.d.ts +136 -0
  37. package/dist/components/MQuiz.vue.d.ts +102 -0
  38. package/dist/components/MReceipt.vue.d.ts +93 -0
  39. package/dist/components/MSeatMap.vue.d.ts +131 -0
  40. package/dist/components/MSeekBar.vue.d.ts +1 -1
  41. package/dist/components/MSlider.vue.d.ts +2 -2
  42. package/dist/components/MSplitter.vue.d.ts +1 -1
  43. package/dist/components/MTicket.vue.d.ts +83 -0
  44. package/dist/components/MTour.vue.d.ts +52 -2
  45. package/dist/components/MVideoPlayer.vue.d.ts +1 -1
  46. package/dist/components/MVitalsChart.vue.d.ts +73 -0
  47. package/dist/composables/_printElement.d.ts +9 -0
  48. package/dist/composables/cardBrand.d.ts +25 -0
  49. package/dist/composables/flowPaths.d.ts +11 -3
  50. package/dist/composables/mortgage.d.ts +37 -0
  51. package/dist/composables/useFlow.d.ts +4 -0
  52. package/dist/composables/useLocale.d.ts +131 -0
  53. package/dist/draw.js +2 -2
  54. package/dist/flow.d.ts +1 -1
  55. package/dist/flow.js +526 -434
  56. package/dist/flow.js.map +1 -1
  57. package/dist/{flowPaths-DzW-aiET.js → flowPaths-CcWOKY4Z.js} +5 -5
  58. package/dist/flowPaths-CcWOKY4Z.js.map +1 -0
  59. package/dist/gantt.js +2 -2
  60. package/dist/index.d.ts +35 -0
  61. package/dist/locales/index.js +924 -7
  62. package/dist/locales/index.js.map +1 -1
  63. package/dist/m3ui-vue.css +1 -1
  64. package/dist/m3ui.js +8393 -3613
  65. package/dist/m3ui.js.map +1 -1
  66. package/dist/map.js +5 -5
  67. package/dist/media.js +4 -4
  68. package/dist/pdf.js +4 -4
  69. package/dist/rich-text-editor.js +4 -4
  70. package/dist/styles.css +1 -1
  71. package/dist/{useLocale-D2ucaY-l.js → useLocale-DLKJIRFi.js} +133 -2
  72. package/dist/useLocale-DLKJIRFi.js.map +1 -0
  73. package/package.json +1 -1
  74. package/src/__tests__/MAvailabilityCalendar.test.ts +85 -0
  75. package/src/__tests__/MBeforeAfter.test.ts +79 -0
  76. package/src/__tests__/MBodyMap.test.ts +60 -0
  77. package/src/__tests__/MCandlestickChart.test.ts +66 -0
  78. package/src/__tests__/MCertificate.test.ts +32 -0
  79. package/src/__tests__/MCreditCardInput.test.ts +122 -0
  80. package/src/__tests__/MFlow.test.ts +69 -0
  81. package/src/__tests__/MMortgageCalculator.test.ts +49 -0
  82. package/src/__tests__/MOdontogram.test.ts +93 -0
  83. package/src/__tests__/MOrderBook.test.ts +66 -0
  84. package/src/__tests__/MOrderTicket.test.ts +68 -0
  85. package/src/__tests__/MPosKeypad.test.ts +116 -0
  86. package/src/__tests__/MProductConfigurator.test.ts +73 -0
  87. package/src/__tests__/MQuiz.test.ts +104 -0
  88. package/src/__tests__/MReceipt.test.ts +52 -0
  89. package/src/__tests__/MSeatMap.test.ts +76 -0
  90. package/src/__tests__/MTicket.test.ts +38 -0
  91. package/src/__tests__/MTour.test.ts +102 -0
  92. package/src/__tests__/MVitalsChart.test.ts +49 -0
  93. package/src/__tests__/mortgage.test.ts +25 -0
  94. package/src/__tests__/ssr.test.ts +11 -0
  95. package/src/components/MAvailabilityCalendar.vue +460 -0
  96. package/src/components/MBeforeAfter.vue +248 -0
  97. package/src/components/MBodyMap.vue +266 -0
  98. package/src/components/MCandlestickChart.vue +479 -0
  99. package/src/components/MCertificate.vue +351 -0
  100. package/src/components/MCreditCardInput.vue +351 -0
  101. package/src/components/MFlow.vue +117 -17
  102. package/src/components/MMortgageCalculator.vue +258 -0
  103. package/src/components/MOdontogram.vue +462 -0
  104. package/src/components/MOrderBook.vue +287 -0
  105. package/src/components/MOrderTicket.vue +217 -0
  106. package/src/components/MPosKeypad.vue +406 -0
  107. package/src/components/MProductConfigurator.vue +412 -0
  108. package/src/components/MQuiz.vue +531 -0
  109. package/src/components/MReceipt.vue +249 -0
  110. package/src/components/MSeatMap.vue +431 -0
  111. package/src/components/MTicket.vue +209 -0
  112. package/src/components/MTour.vue +145 -27
  113. package/src/components/MVitalsChart.vue +303 -0
  114. package/src/composables/_printElement.ts +36 -0
  115. package/src/composables/cardBrand.ts +104 -0
  116. package/src/composables/flowPaths.ts +16 -7
  117. package/src/composables/mortgage.ts +70 -0
  118. package/src/composables/useFlow.ts +4 -0
  119. package/src/composables/useLocale.ts +277 -0
  120. package/src/flow.ts +1 -1
  121. package/src/index.ts +35 -0
  122. package/src/locales/de.ts +131 -0
  123. package/src/locales/es.ts +131 -0
  124. package/src/locales/fr.ts +131 -0
  125. package/src/locales/ja.ts +131 -0
  126. package/src/locales/ko.ts +131 -0
  127. package/src/locales/pt.ts +131 -0
  128. package/src/locales/zh.ts +131 -0
  129. package/dist/flowPaths-DzW-aiET.js.map +0 -1
  130. package/dist/useLocale-D2ucaY-l.js.map +0 -1
@@ -0,0 +1,351 @@
1
+ <script lang="ts">
2
+ /** A signature line of an `MCertificate`. */
3
+ export interface CertificateSignature {
4
+ name: string
5
+ /** e.g. "Course director" */
6
+ role?: string
7
+ /** Image of the signature (a URL or a data URL, e.g. from MSignaturePad), drawn over the line */
8
+ image?: string
9
+ }
10
+ </script>
11
+
12
+ <script setup lang="ts">
13
+ /**
14
+ * A printable certificate or diploma: a course completion, an award, a training, a membership. It keeps an A4 shape
15
+ * at any width and scales its type with it, so the same component works as a thumbnail, on screen and on paper;
16
+ * `print()` prints just the certificate on an A4 page. Three looks: `classic` (double frame and serif type),
17
+ * `modern` (a colored band) and `minimal`.
18
+ *
19
+ * @slot logo The issuer's logo, at the top
20
+ * @slot seal Replaces the seal (a rosette with an icon)
21
+ * @slot default Extra content under the description (grades, hours, a QR to verify)
22
+ */
23
+ import { computed, ref } from 'vue'
24
+ import MIcon from './MIcon.vue'
25
+ import { useLocale } from '../composables/useLocale'
26
+ import { printElement } from '../composables/_printElement'
27
+
28
+ const locale = useLocale()
29
+
30
+ const ROLES = ['primary', 'secondary', 'tertiary', 'error', 'success']
31
+
32
+ const props = withDefaults(
33
+ defineProps<{
34
+ /** Who receives it */
35
+ recipient: string
36
+ /** Big title. Defaults to the locale's "Certificate of completion" */
37
+ title?: string
38
+ /** Line before the name. Defaults to the locale's "This certifies that" */
39
+ presentedTo?: string
40
+ /** What it is for, e.g. "has completed the course Vue 3 Fundamentals (24 hours)" */
41
+ description?: string
42
+ /** Name of the issuing organization */
43
+ issuer?: string
44
+ /** Date it was awarded (Date, ISO string or epoch ms) */
45
+ date?: Date | string | number
46
+ /** Certificate number or verification code */
47
+ code?: string
48
+ signatures?: CertificateSignature[]
49
+ /** Look of the certificate */
50
+ variant?: 'classic' | 'modern' | 'minimal'
51
+ /** Accent color: a role (primary, secondary, tertiary, error, success) or any CSS color */
52
+ color?: string
53
+ /** Material Symbol in the seal; `false` hides the seal */
54
+ seal?: string | false
55
+ orientation?: 'landscape' | 'portrait'
56
+ }>(),
57
+ {
58
+ signatures: () => [],
59
+ variant: 'classic',
60
+ color: 'primary',
61
+ seal: 'workspace_premium',
62
+ orientation: 'landscape',
63
+ },
64
+ )
65
+
66
+ const accent = computed(() => (ROLES.includes(props.color) ? `var(--color-${props.color})` : props.color))
67
+
68
+ const dateText = computed(() => {
69
+ if (props.date === undefined) return ''
70
+ const d = props.date instanceof Date ? props.date : new Date(props.date)
71
+ return new Intl.DateTimeFormat(locale.lang, { dateStyle: 'long' }).format(d)
72
+ })
73
+
74
+ const rootEl = ref<HTMLElement | null>(null)
75
+
76
+ /** Prints just the certificate, filling an A4 page */
77
+ function print() {
78
+ if (!rootEl.value) return
79
+ printElement(rootEl.value, {
80
+ page: `A4 ${props.orientation}`,
81
+ css: '.m3-certificate{width:100vw!important;max-width:none!important;box-shadow:none!important}',
82
+ })
83
+ }
84
+
85
+ defineExpose({ print })
86
+ </script>
87
+
88
+ <template>
89
+ <div
90
+ ref="rootEl"
91
+ class="m3-certificate"
92
+ :class="[`m3-certificate--${variant}`]"
93
+ :style="{ '--m3-cert-accent': accent, aspectRatio: orientation === 'landscape' ? '297 / 210' : '210 / 297' }"
94
+ :data-variant="variant"
95
+ >
96
+ <div class="m3-certificate__frame">
97
+ <div v-if="$slots.logo || issuer" class="m3-certificate__issuer">
98
+ <slot name="logo" />
99
+ <span v-if="issuer" data-issuer>{{ issuer }}</span>
100
+ </div>
101
+
102
+ <h2 class="m3-certificate__title" data-title>{{ title ?? locale.certTitle }}</h2>
103
+ <p class="m3-certificate__lead">{{ presentedTo ?? locale.certPresentedTo }}</p>
104
+ <p class="m3-certificate__name" data-recipient>{{ recipient }}</p>
105
+ <p v-if="description" class="m3-certificate__desc">{{ description }}</p>
106
+ <div v-if="$slots.default" class="m3-certificate__extra"><slot /></div>
107
+
108
+ <div class="m3-certificate__footer">
109
+ <div v-if="dateText" class="m3-certificate__sign">
110
+ <span class="m3-certificate__sign-value">{{ dateText }}</span>
111
+ <span class="m3-certificate__sign-line" />
112
+ <span class="m3-certificate__sign-role">{{ locale.certDate }}</span>
113
+ </div>
114
+
115
+ <div v-if="seal !== false || $slots.seal" class="m3-certificate__seal" aria-hidden="true">
116
+ <slot name="seal">
117
+ <svg viewBox="0 0 100 100" class="m3-certificate__rosette">
118
+ <polygon
119
+ :points="Array.from({ length: 24 }, (_, i) => {
120
+ const a = (i * Math.PI) / 12
121
+ const r = i % 2 ? 44 : 50
122
+ return `${50 + r * Math.cos(a)},${50 + r * Math.sin(a)}`
123
+ }).join(' ')"
124
+ />
125
+ <circle cx="50" cy="50" r="36" fill="none" stroke="currentColor" stroke-width="1.5" stroke-dasharray="2 2.5" />
126
+ </svg>
127
+ <MIcon :name="seal || 'workspace_premium'" class="m3-certificate__seal-icon" />
128
+ </slot>
129
+ </div>
130
+
131
+ <div v-for="s in signatures" :key="s.name" class="m3-certificate__sign" data-signature>
132
+ <img v-if="s.image" :src="s.image" alt="" class="m3-certificate__sign-img" />
133
+ <span v-else class="m3-certificate__sign-value">&nbsp;</span>
134
+ <span class="m3-certificate__sign-line" />
135
+ <span class="m3-certificate__sign-name">{{ s.name }}</span>
136
+ <span v-if="s.role" class="m3-certificate__sign-role">{{ s.role }}</span>
137
+ </div>
138
+ </div>
139
+
140
+ <p v-if="code" class="m3-certificate__code" data-code>{{ locale.certId }}: {{ code }}</p>
141
+ </div>
142
+ </div>
143
+ </template>
144
+
145
+ <style>
146
+ @layer base {
147
+ .m3-certificate {
148
+ width: 100%;
149
+ position: relative;
150
+ overflow: hidden;
151
+ background: #fffdf8;
152
+ color: #1d1b20;
153
+ box-shadow: var(--shadow-elevation-2);
154
+ }
155
+ }
156
+ .m3-certificate {
157
+ container-type: inline-size;
158
+ /* The paper is always light: in the dark theme the accent role is a light tone, so it's darkened into ink */
159
+ --m3-cert-ink: var(--m3-cert-accent);
160
+ }
161
+ :where(.dark) .m3-certificate {
162
+ --m3-cert-ink: color-mix(in oklab, var(--m3-cert-accent) 55%, #000);
163
+ }
164
+ .m3-certificate__frame {
165
+ position: absolute;
166
+ inset: 3.2cqw;
167
+ display: flex;
168
+ flex-direction: column;
169
+ align-items: center;
170
+ justify-content: center;
171
+ text-align: center;
172
+ padding: 3cqw 6cqw;
173
+ gap: 1cqw;
174
+ }
175
+ .m3-certificate__issuer {
176
+ display: flex;
177
+ align-items: center;
178
+ gap: 1.2cqw;
179
+ font-size: 1.7cqw;
180
+ font-weight: 600;
181
+ letter-spacing: 0.25em;
182
+ text-transform: uppercase;
183
+ color: var(--m3-cert-ink);
184
+ }
185
+ .m3-certificate__issuer img,
186
+ .m3-certificate__issuer svg {
187
+ height: 4.5cqw;
188
+ width: auto;
189
+ }
190
+ .m3-certificate__title {
191
+ font-size: 4.6cqw;
192
+ line-height: 1.1;
193
+ font-weight: 700;
194
+ margin-top: 1cqw;
195
+ }
196
+ .m3-certificate__lead {
197
+ font-size: 1.8cqw;
198
+ color: #49454f;
199
+ margin-top: 1.4cqw;
200
+ }
201
+ .m3-certificate__name {
202
+ font-size: 5.4cqw;
203
+ line-height: 1.15;
204
+ color: var(--m3-cert-ink);
205
+ padding: 0 4cqw 0.6cqw;
206
+ border-bottom: 0.15cqw solid color-mix(in srgb, var(--m3-cert-ink) 50%, transparent);
207
+ }
208
+ .m3-certificate__desc {
209
+ font-size: 1.9cqw;
210
+ line-height: 1.45;
211
+ max-width: 72cqw;
212
+ color: #49454f;
213
+ margin-top: 0.6cqw;
214
+ }
215
+ .m3-certificate__extra {
216
+ font-size: 1.6cqw;
217
+ }
218
+ .m3-certificate__footer {
219
+ display: flex;
220
+ align-items: flex-end;
221
+ justify-content: center;
222
+ gap: 6cqw;
223
+ width: 100%;
224
+ margin-top: auto;
225
+ }
226
+ .m3-certificate__sign {
227
+ display: flex;
228
+ flex-direction: column;
229
+ align-items: center;
230
+ min-width: 18cqw;
231
+ }
232
+ .m3-certificate__sign-value {
233
+ font-size: 1.7cqw;
234
+ padding-bottom: 0.4cqw;
235
+ }
236
+ .m3-certificate__sign-img {
237
+ height: 5.5cqw;
238
+ max-width: 22cqw;
239
+ object-fit: contain;
240
+ margin-bottom: -0.8cqw;
241
+ }
242
+ .m3-certificate__sign-line {
243
+ align-self: stretch;
244
+ border-top: 0.12cqw solid #1d1b20;
245
+ }
246
+ .m3-certificate__sign-name {
247
+ font-size: 1.6cqw;
248
+ font-weight: 600;
249
+ margin-top: 0.5cqw;
250
+ }
251
+ .m3-certificate__sign-role {
252
+ font-size: 1.35cqw;
253
+ color: #49454f;
254
+ margin-top: 0.3cqw;
255
+ }
256
+ .m3-certificate__seal {
257
+ position: relative;
258
+ width: 11cqw;
259
+ height: 11cqw;
260
+ flex-shrink: 0;
261
+ color: #fff;
262
+ }
263
+ .m3-certificate__rosette {
264
+ width: 100%;
265
+ height: 100%;
266
+ fill: var(--m3-cert-ink);
267
+ }
268
+ .m3-certificate__seal-icon {
269
+ position: absolute;
270
+ inset: 0;
271
+ margin: auto;
272
+ font-size: 5cqw !important;
273
+ width: 5cqw !important;
274
+ height: 5cqw !important;
275
+ display: flex;
276
+ align-items: center;
277
+ justify-content: center;
278
+ }
279
+ .m3-certificate__code {
280
+ position: absolute;
281
+ bottom: 1.2cqw;
282
+ right: 2cqw;
283
+ font-size: 1.1cqw;
284
+ color: #79747e;
285
+ font-family: var(--font-mono, ui-monospace, monospace);
286
+ }
287
+
288
+ /* classic: double frame, corner marks, serif type */
289
+ .m3-certificate--classic {
290
+ font-family: var(--font-serif, Georgia, 'Times New Roman', serif);
291
+ }
292
+ .m3-certificate--classic .m3-certificate__frame {
293
+ border: 0.5cqw double var(--m3-cert-ink);
294
+ outline: 0.15cqw solid color-mix(in srgb, var(--m3-cert-ink) 45%, transparent);
295
+ outline-offset: 0.8cqw;
296
+ }
297
+ .m3-certificate--classic .m3-certificate__title {
298
+ font-weight: 400;
299
+ letter-spacing: 0.06em;
300
+ text-transform: uppercase;
301
+ }
302
+ .m3-certificate--classic .m3-certificate__name {
303
+ font-style: italic;
304
+ }
305
+
306
+ /* modern: a band of the accent color on the left */
307
+ .m3-certificate--modern {
308
+ font-family: var(--font-sans, system-ui, sans-serif);
309
+ }
310
+ .m3-certificate--modern::before {
311
+ content: '';
312
+ position: absolute;
313
+ inset: 0 auto 0 0;
314
+ width: 2.4cqw;
315
+ background: var(--m3-cert-ink);
316
+ }
317
+ .m3-certificate--modern::after {
318
+ content: '';
319
+ position: absolute;
320
+ top: -12cqw;
321
+ right: -12cqw;
322
+ width: 34cqw;
323
+ height: 34cqw;
324
+ border-radius: 50%;
325
+ background: color-mix(in srgb, var(--m3-cert-ink) 10%, transparent);
326
+ }
327
+ .m3-certificate--modern .m3-certificate__frame {
328
+ align-items: flex-start;
329
+ text-align: start;
330
+ inset: 3.2cqw 3.2cqw 3.2cqw 6cqw;
331
+ z-index: 1;
332
+ }
333
+ .m3-certificate--modern .m3-certificate__name {
334
+ padding-inline: 0 4cqw;
335
+ font-weight: 600;
336
+ }
337
+ .m3-certificate--modern .m3-certificate__footer {
338
+ justify-content: flex-start;
339
+ }
340
+
341
+ /* minimal: a hairline frame */
342
+ .m3-certificate--minimal {
343
+ font-family: var(--font-sans, system-ui, sans-serif);
344
+ }
345
+ .m3-certificate--minimal .m3-certificate__frame {
346
+ border: 0.12cqw solid #cac4d0;
347
+ }
348
+ .m3-certificate--minimal .m3-certificate__title {
349
+ font-weight: 300;
350
+ }
351
+ </style>
@@ -0,0 +1,351 @@
1
+ <script lang="ts">
2
+ /** Value of `MCreditCardInput` (v-model). `number` holds digits only; `expiry` is `MM/YY`. */
3
+ export interface CreditCardValue {
4
+ number: string
5
+ expiry: string
6
+ cvc: string
7
+ name?: string
8
+ }
9
+
10
+ export type CreditCardField = 'number' | 'expiry' | 'cvc' | 'name'
11
+ </script>
12
+
13
+ <script setup lang="ts">
14
+ /**
15
+ * Card payment fields: number, expiry, security code and optionally the cardholder's name, with a live card
16
+ * preview that turns over while the security code is typed. The number is grouped as its network prints it
17
+ * (4-6-5 for American Express), the network is detected from the first digits, and every field is checked
18
+ * (length, Luhn checksum, a future expiry, the code's length for that network).
19
+ *
20
+ * It only collects and checks what is typed: it doesn't tokenize or send anything. Hand the value to your
21
+ * payment provider's SDK.
22
+ *
23
+ * @slot brand Replaces the network wordmark on the preview. Slot props: `{ brand, name }`
24
+ * @event update:modelValue Emitted on every change — powers v-model
25
+ * @event complete Emitted when every field becomes valid, with the value
26
+ * @event brand Emitted when the detected network changes
27
+ */
28
+ import { computed, nextTick, onMounted, reactive, ref, watch } from 'vue'
29
+ import MTextField from './MTextField.vue'
30
+ import { useLocale } from '../composables/useLocale'
31
+ import {
32
+ detectCardBrand,
33
+ formatCardNumber,
34
+ isValidCardNumber,
35
+ isValidExpiry,
36
+ type CardBrand,
37
+ } from '../composables/cardBrand'
38
+
39
+ const locale = useLocale()
40
+
41
+ const props = withDefaults(
42
+ defineProps<{
43
+ /** Card value (v-model) */
44
+ modelValue: CreditCardValue
45
+ /** Shows the card preview above the fields */
46
+ preview?: boolean
47
+ /** Adds the "Name on card" field */
48
+ showName?: boolean
49
+ /** Visual style of the fields */
50
+ variant?: 'filled' | 'outlined'
51
+ /** Moves focus to the next field once one is complete and valid */
52
+ autoAdvance?: boolean
53
+ /** Error messages to show regardless of the built-in checks, e.g. a decline from the server */
54
+ errors?: Partial<Record<CreditCardField, string>>
55
+ /** Disables every field */
56
+ disabled?: boolean
57
+ }>(),
58
+ {
59
+ preview: true,
60
+ showName: false,
61
+ variant: 'filled',
62
+ autoAdvance: true,
63
+ disabled: false,
64
+ },
65
+ )
66
+
67
+ const emit = defineEmits<{
68
+ 'update:modelValue': [CreditCardValue]
69
+ complete: [CreditCardValue]
70
+ brand: [CardBrand]
71
+ }>()
72
+
73
+ const numberField = ref<InstanceType<typeof MTextField> | null>(null)
74
+ const expiryField = ref<InstanceType<typeof MTextField> | null>(null)
75
+ const cvcField = ref<InstanceType<typeof MTextField> | null>(null)
76
+ const nameField = ref<InstanceType<typeof MTextField> | null>(null)
77
+
78
+ const brandInfo = computed(() => detectCardBrand(props.modelValue.number ?? ''))
79
+ const maxDigits = computed(() => Math.max(...brandInfo.value.lengths))
80
+
81
+ const formattedNumber = computed(() => formatCardNumber(props.modelValue.number ?? ''))
82
+
83
+ const validity = computed(() => ({
84
+ number: isValidCardNumber(props.modelValue.number ?? ''),
85
+ expiry: isValidExpiry(props.modelValue.expiry ?? ''),
86
+ cvc: new RegExp(`^\\d{${brandInfo.value.cvcLength}}$`).test(props.modelValue.cvc ?? ''),
87
+ name: !props.showName || !!props.modelValue.name?.trim(),
88
+ }))
89
+ const valid = computed(() => Object.values(validity.value).every(Boolean))
90
+
91
+ watch(valid, (v) => { if (v) emit('complete', { ...props.modelValue }) })
92
+ watch(() => brandInfo.value.brand, (b) => emit('brand', b))
93
+
94
+ // A field shows its error once it has been left (or validate() was called)
95
+ const touched = reactive<Record<CreditCardField, boolean>>({ number: false, expiry: false, cvc: false, name: false })
96
+
97
+ const messages: Record<CreditCardField, () => string> = {
98
+ number: () => locale.cardInvalidNumber,
99
+ expiry: () => locale.cardInvalidExpiry,
100
+ cvc: () => locale.cardInvalidCvc,
101
+ name: () => '',
102
+ }
103
+
104
+ function errorFor(field: CreditCardField): string | undefined {
105
+ if (props.errors?.[field]) return props.errors[field]
106
+ if (!touched[field] || validity.value[field]) return undefined
107
+ return messages[field]() || undefined
108
+ }
109
+
110
+ function update(patch: Partial<CreditCardValue>) {
111
+ emit('update:modelValue', { ...props.modelValue, ...patch })
112
+ }
113
+
114
+ // Writes the reformatted text into the input (Vue skips the patch when the bound value didn't change, e.g. a
115
+ // letter or a digit past the maximum was typed) and puts the caret back after the same number of digits
116
+ function restoreCaret(field: InstanceType<typeof MTextField> | null, text: string, digitsBefore: number) {
117
+ nextTick(() => {
118
+ const el = field?.inputEl as HTMLInputElement | null | undefined
119
+ if (!el) return
120
+ if (el.value !== text) el.value = text
121
+ if (document.activeElement !== el) return
122
+ let pos = 0
123
+ let seen = 0
124
+ while (pos < el.value.length && seen < digitsBefore) {
125
+ if (/\d/.test(el.value[pos]!)) seen++
126
+ pos++
127
+ }
128
+ el.setSelectionRange(pos, pos)
129
+ })
130
+ }
131
+
132
+ function digitsBeforeCaret(field: InstanceType<typeof MTextField> | null, text: string) {
133
+ const el = field?.inputEl as HTMLInputElement | null | undefined
134
+ const caret = el?.selectionStart ?? text.length
135
+ return text.slice(0, caret).replace(/\D/g, '').length
136
+ }
137
+
138
+ function onNumber(text: string) {
139
+ const before = digitsBeforeCaret(numberField.value, text)
140
+ const digits = text.replace(/\D/g, '').slice(0, Math.max(...detectCardBrand(text).lengths))
141
+ update({ number: digits })
142
+ restoreCaret(numberField.value, formatCardNumber(digits), before)
143
+ if (props.autoAdvance && digits.length === Math.max(...detectCardBrand(digits).lengths) && isValidCardNumber(digits)) {
144
+ nextTick(() => (props.showName ? nameField.value : expiryField.value)?.focus())
145
+ }
146
+ }
147
+
148
+ function formatExpiry(digits: string) {
149
+ // A first digit above 1 can only be a single-digit month
150
+ if (digits.length === 1 && Number(digits) > 1) digits = '0' + digits
151
+ return digits.length > 2 ? `${digits.slice(0, 2)}/${digits.slice(2, 4)}` : digits
152
+ }
153
+
154
+ function onExpiry(text: string) {
155
+ const before = digitsBeforeCaret(expiryField.value, text)
156
+ const digits = text.replace(/\D/g, '').slice(0, 4)
157
+ const expiry = formatExpiry(digits)
158
+ update({ expiry })
159
+ restoreCaret(expiryField.value, expiry, before + (digits.length === 1 && expiry.length === 2 ? 1 : 0))
160
+ if (props.autoAdvance && expiry.length === 5 && isValidExpiry(expiry)) nextTick(() => cvcField.value?.focus())
161
+ }
162
+
163
+ function onCvc(text: string) {
164
+ const cvc = text.replace(/\D/g, '').slice(0, brandInfo.value.cvcLength)
165
+ update({ cvc })
166
+ restoreCaret(cvcField.value, cvc, digitsBeforeCaret(cvcField.value, text))
167
+ }
168
+
169
+ function onName(text: string) {
170
+ update({ name: text })
171
+ }
172
+
173
+ // Re-trim the security code when the network changes to one with a shorter code
174
+ watch(() => brandInfo.value.cvcLength, (len) => {
175
+ if ((props.modelValue.cvc ?? '').length > len) update({ cvc: props.modelValue.cvc.slice(0, len) })
176
+ })
177
+
178
+ const cvcFocused = ref(false)
179
+ const focusedField = ref<CreditCardField | null>(null)
180
+
181
+ onMounted(() => {
182
+ // MTextField forwards attrs to its wrapper, so input hints are set on the inputs here
183
+ const set = (f: InstanceType<typeof MTextField> | null, attrs: Record<string, string>) => {
184
+ const el = f?.inputEl as HTMLInputElement | null | undefined
185
+ if (el) for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v)
186
+ }
187
+ set(numberField.value, { inputmode: 'numeric', name: 'cc-number' })
188
+ set(expiryField.value, { inputmode: 'numeric', name: 'cc-exp', maxlength: '5' })
189
+ set(cvcField.value, { inputmode: 'numeric', name: 'cc-csc', maxlength: '4' })
190
+ set(nameField.value, { name: 'cc-name', autocapitalize: 'characters' })
191
+ })
192
+
193
+ function onFocusIn(field: CreditCardField) {
194
+ focusedField.value = field
195
+ cvcFocused.value = field === 'cvc'
196
+ }
197
+ function onFocusOut(field: CreditCardField) {
198
+ touched[field] = true
199
+ if (focusedField.value === field) focusedField.value = null
200
+ if (field === 'cvc') cvcFocused.value = false
201
+ }
202
+
203
+ // Preview: placeholder bullets where digits haven't been typed yet
204
+ const previewNumber = computed(() => {
205
+ const digits = props.modelValue.number ?? ''
206
+ const len = Math.max(digits.length, brandInfo.value.brand === 'amex' ? 15 : brandInfo.value.brand === 'diners' && digits.length <= 14 ? 14 : 16)
207
+ const { gaps } = brandInfo.value
208
+ let out = ''
209
+ for (let i = 0; i < len; i++) {
210
+ if (i > 0 && gaps.includes(i)) out += ' '
211
+ out += digits[i] ?? '•'
212
+ }
213
+ return out
214
+ })
215
+ const previewExpiry = computed(() => props.modelValue.expiry || 'MM/YY')
216
+ const previewCvc = computed(() => (props.modelValue.cvc ?? '').padEnd(brandInfo.value.cvcLength, '•'))
217
+
218
+ defineExpose({
219
+ /** Detected card network */
220
+ brand: computed(() => brandInfo.value.brand),
221
+ /** Whether every field is valid */
222
+ valid,
223
+ /** Shows the errors of every field (as if each had been visited) and returns whether all are valid */
224
+ validate: () => {
225
+ touched.number = touched.expiry = touched.cvc = touched.name = true
226
+ return valid.value
227
+ },
228
+ /** Focuses a field (the number by default) */
229
+ focus: (field: CreditCardField = 'number') =>
230
+ ({ number: numberField, expiry: expiryField, cvc: cvcField, name: nameField })[field].value?.focus(),
231
+ })
232
+ </script>
233
+
234
+ <template>
235
+ <div class="flex flex-col gap-4">
236
+ <div v-if="preview" class="m3-cc-preview mx-auto w-full max-w-[340px]" aria-hidden="true">
237
+ <div class="m3-cc-preview__inner relative aspect-[1.586]" :class="{ 'is-flipped': cvcFocused }">
238
+ <!-- Front -->
239
+ <div class="m3-cc-preview__face absolute inset-0 flex flex-col justify-between overflow-hidden rounded-xl bg-linear-135 from-primary to-tertiary p-5 text-on-primary shadow-elevation-2">
240
+ <div class="flex items-start justify-between">
241
+ <div class="h-8 w-11 rounded-md bg-on-primary/25 ring-1 ring-on-primary/30" />
242
+ <slot name="brand" :brand="brandInfo.brand" :name="brandInfo.name">
243
+ <span class="text-title-medium font-bold italic tracking-wide" :class="brandInfo.name ? 'opacity-100' : 'opacity-0'">{{ brandInfo.name }}</span>
244
+ </slot>
245
+ </div>
246
+ <div
247
+ class="m3-cc-mono text-title-large tracking-[0.12em] tabular-nums whitespace-nowrap"
248
+ :class="focusedField === 'number' ? 'opacity-100' : 'opacity-90'"
249
+ >{{ previewNumber }}</div>
250
+ <div class="flex items-end justify-between gap-3 text-label-large uppercase">
251
+ <span class="min-w-0 truncate" :class="{ 'opacity-60': !modelValue.name }">{{ modelValue.name || (showName ? locale.cardName : '') }}</span>
252
+ <span class="m3-cc-mono shrink-0 tabular-nums" :class="{ 'opacity-60': !modelValue.expiry }">{{ previewExpiry }}</span>
253
+ </div>
254
+ </div>
255
+ <!-- Back -->
256
+ <div class="m3-cc-preview__face m3-cc-preview__back absolute inset-0 overflow-hidden rounded-xl bg-linear-135 from-tertiary to-primary text-on-primary shadow-elevation-2">
257
+ <div class="mt-6 h-10 bg-on-surface/80" />
258
+ <div class="mx-5 mt-5 flex h-9 items-center justify-end rounded-sm bg-surface-container-highest px-3 text-on-surface">
259
+ <span class="m3-cc-mono text-title-medium italic tracking-widest">{{ previewCvc }}</span>
260
+ </div>
261
+ </div>
262
+ </div>
263
+ </div>
264
+
265
+ <div class="flex flex-col gap-3">
266
+ <div @focusin="onFocusIn('number')" @focusout="onFocusOut('number')">
267
+ <MTextField
268
+ ref="numberField"
269
+ :model-value="formattedNumber"
270
+ :label="locale.cardNumber"
271
+ :variant="variant"
272
+ :disabled="disabled"
273
+ autocomplete="cc-number"
274
+ leading-icon="credit_card"
275
+ :error="!!errorFor('number')"
276
+ :error-label="errorFor('number')"
277
+ @update:model-value="onNumber"
278
+ />
279
+ </div>
280
+ <div v-if="showName" @focusin="onFocusIn('name')" @focusout="onFocusOut('name')">
281
+ <MTextField
282
+ ref="nameField"
283
+ :model-value="modelValue.name ?? ''"
284
+ :label="locale.cardName"
285
+ :variant="variant"
286
+ :disabled="disabled"
287
+ autocomplete="cc-name"
288
+ :error="!!errors?.name"
289
+ :error-label="errors?.name"
290
+ @update:model-value="onName"
291
+ />
292
+ </div>
293
+ <div class="grid grid-cols-2 gap-3">
294
+ <div @focusin="onFocusIn('expiry')" @focusout="onFocusOut('expiry')">
295
+ <MTextField
296
+ ref="expiryField"
297
+ :model-value="modelValue.expiry ?? ''"
298
+ :label="locale.cardExpiry"
299
+ :variant="variant"
300
+ :disabled="disabled"
301
+ autocomplete="cc-exp"
302
+ :error="!!errorFor('expiry')"
303
+ :error-label="errorFor('expiry')"
304
+ @update:model-value="onExpiry"
305
+ />
306
+ </div>
307
+ <div @focusin="onFocusIn('cvc')" @focusout="onFocusOut('cvc')">
308
+ <MTextField
309
+ ref="cvcField"
310
+ :model-value="modelValue.cvc ?? ''"
311
+ :label="locale.cardCvc"
312
+ :variant="variant"
313
+ :disabled="disabled"
314
+ autocomplete="cc-csc"
315
+ :error="!!errorFor('cvc')"
316
+ :error-label="errorFor('cvc')"
317
+ @update:model-value="onCvc"
318
+ />
319
+ </div>
320
+ </div>
321
+ </div>
322
+ </div>
323
+ </template>
324
+
325
+ <style>
326
+ .m3-cc-preview {
327
+ perspective: 1200px;
328
+ }
329
+ .m3-cc-preview__inner {
330
+ transform-style: preserve-3d;
331
+ transition: transform 500ms cubic-bezier(0.2, 0, 0, 1);
332
+ }
333
+ .m3-cc-preview__inner.is-flipped {
334
+ transform: rotateY(180deg);
335
+ }
336
+ .m3-cc-preview__face {
337
+ backface-visibility: hidden;
338
+ -webkit-backface-visibility: hidden;
339
+ }
340
+ .m3-cc-preview__back {
341
+ transform: rotateY(180deg);
342
+ }
343
+ .m3-cc-mono {
344
+ font-family: var(--font-mono, ui-monospace, monospace);
345
+ }
346
+ @media (prefers-reduced-motion: reduce) {
347
+ .m3-cc-preview__inner {
348
+ transition: none;
349
+ }
350
+ }
351
+ </style>