@gecko-ui-kit/vue 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +2016 -2
- package/index.d.ts +13 -0
- package/index.js +16 -0
- package/package.json +48 -4
- package/src/actions.d.ts +186 -0
- package/src/actions.js +329 -0
- package/src/basics.d.ts +315 -0
- package/src/basics.js +475 -0
- package/src/charts.d.ts +219 -0
- package/src/charts.js +93 -0
- package/src/controls.d.ts +149 -0
- package/src/controls.js +354 -0
- package/src/core.d.ts +109 -0
- package/src/core.js +414 -0
- package/src/data.d.ts +754 -0
- package/src/data.js +718 -0
- package/src/effects.d.ts +407 -0
- package/src/effects.js +699 -0
- package/src/forms.d.ts +757 -0
- package/src/forms.js +481 -0
- package/src/motion.d.ts +127 -0
- package/src/motion.js +216 -0
- package/src/overlays.d.ts +463 -0
- package/src/overlays.js +707 -0
- package/src/plugin.d.ts +6 -0
- package/src/plugin.js +27 -0
- package/src/shell.d.ts +210 -0
- package/src/shell.js +716 -0
- package/src/text.d.ts +309 -0
- package/src/text.js +383 -0
package/src/basics.js
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/* Markup pieces from @gecko-ui-kit/components as Vue components: they draw the same HTML as the
|
|
2
|
+
vanilla builders (btn(), tag(), card()…), so the same CSS styles them. Progress bits (Meter, Ring, Steps,
|
|
3
|
+
Bars) animate with the motion helpers from @gecko-ui-kit/motion when they appear and when the value changes. */
|
|
4
|
+
import { defineComponent, ref, computed, watch, onMounted, onUpdated, onBeforeUnmount, withDirectives, vModelText, normalizeClass } from 'vue';
|
|
5
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
6
|
+
import * as C from '@gecko-ui-kit/components';
|
|
7
|
+
import { h, B, ANY, iconNode, useMotion, useModel, modelProps, slotOr, hasContent } from './core.js';
|
|
8
|
+
|
|
9
|
+
const has = (x) => x != null && x !== false && x !== '';
|
|
10
|
+
|
|
11
|
+
/* ---------------- spinner ---------------- */
|
|
12
|
+
const SPIN_TONES = ['accent', 'ink', 'muted', 'good', 'warn', 'bad', 'current'];
|
|
13
|
+
/**
|
|
14
|
+
* <Spinner :size="28" tone="accent" label="Loading bookings…" stack />
|
|
15
|
+
* Same markup as spinner({ size, tone, speed, track, label, stack, words, decorative }): an orbit loader.
|
|
16
|
+
*/
|
|
17
|
+
export const Spinner = defineComponent({
|
|
18
|
+
name: 'Spinner',
|
|
19
|
+
props: { size: ANY, tone: { type: String, default: 'accent' }, speed: { type: [Number, String], default: 1 }, track: { type: Boolean, default: true }, label: ANY, stack: B, words: ANY, decorative: B },
|
|
20
|
+
setup(props) {
|
|
21
|
+
// reduceMotion(true) forced from code: the CSS media query covers the OS setting, this covers the rest (after mount, so the server HTML matches)
|
|
22
|
+
const still = ref(false);
|
|
23
|
+
onMounted(() => { if (M.RM()) still.value = true; });
|
|
24
|
+
return () => {
|
|
25
|
+
const t = SPIN_TONES.includes(props.tone) ? props.tone : 'accent';
|
|
26
|
+
const st = {};
|
|
27
|
+
if (has(props.size)) st.fontSize = typeof props.size === 'number' ? props.size + 'px' : props.size;
|
|
28
|
+
if (+props.speed && +props.speed !== 1) st['--orb-speed'] = +props.speed + 's';
|
|
29
|
+
const text = has(props.label) ? h('span', { class: 'orb__label' }, props.label)
|
|
30
|
+
: props.decorative ? null : h('span', { class: 'orb__sr' }, props.words?.loading ?? 'Loading…');
|
|
31
|
+
return h('span', {
|
|
32
|
+
...(props.decorative ? { 'aria-hidden': 'true' } : { role: 'status' }), style: st,
|
|
33
|
+
class: ['orb', 'orb--' + t, props.track === false && 'orb--bare', props.stack && has(props.label) && 'orb--stack', still.value && 'orb--still'],
|
|
34
|
+
}, [h('span', { class: 'orb__dial', 'aria-hidden': 'true' }, [h('span', { class: 'orb__spin' })]), text]);
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/* ---------------- buttons ---------------- */
|
|
40
|
+
/**
|
|
41
|
+
* <Button variant="primary" size="sm" icon="check" @click="save">Save</Button>
|
|
42
|
+
* Same as btn('Save', { variant, size, icon, iconRight, block, magnetic }). Any other attribute goes on the <button>.
|
|
43
|
+
* busy: locks the button (disabled + aria-busy), swaps the icon for a spinner and the label for busyText.
|
|
44
|
+
*/
|
|
45
|
+
export const Button = defineComponent({
|
|
46
|
+
name: 'Button',
|
|
47
|
+
props: { variant: String, size: String, icon: ANY, iconRight: ANY, block: B, magnetic: B, busy: B, busyText: String, type: { type: String, default: 'button' }, disabled: B },
|
|
48
|
+
setup(props, { slots }) {
|
|
49
|
+
const el = useMotion((n) => (props.magnetic ? M.magnetic(n) : undefined), () => !!props.magnetic);
|
|
50
|
+
return () => {
|
|
51
|
+
const busy = !!props.busy;
|
|
52
|
+
const label = busy && has(props.busyText) ? props.busyText : slots.default?.();
|
|
53
|
+
const spin = busy ? h(Spinner, { size: '1.15em', tone: 'current', decorative: true }) : null;
|
|
54
|
+
return h('button', {
|
|
55
|
+
ref: el, type: props.type, disabled: props.disabled || busy || undefined, 'aria-busy': busy ? 'true' : undefined,
|
|
56
|
+
class: ['btn', props.variant && 'btn--' + props.variant, props.size && 'btn--' + props.size, props.block && 'btn--block', props.magnetic && 'magnetic'],
|
|
57
|
+
}, [
|
|
58
|
+
busy ? (props.icon || !props.iconRight ? spin : null) : iconNode(props.icon),
|
|
59
|
+
label != null && label !== false ? h('span', null, label) : null,
|
|
60
|
+
busy && !props.icon && props.iconRight ? spin : iconNode(props.iconRight),
|
|
61
|
+
]);
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* <IconButton icon="bell" label="Notifications" size="sm" @click="openInbox" />
|
|
68
|
+
* <IconButton icon="heart" label="Favourite bike" v-model:pressed="fav" /> (or default-pressed)
|
|
69
|
+
* Same as iconBtn(icon, label, { size }). With v-model:pressed (or default-pressed) it is an on/off button
|
|
70
|
+
* (aria-pressed, pops when pressed): @change(pressed, event). Slot content goes after the icon.
|
|
71
|
+
*/
|
|
72
|
+
export const IconButton = defineComponent({
|
|
73
|
+
name: 'IconButton',
|
|
74
|
+
props: { icon: ANY, label: String, size: String, pressed: B, defaultPressed: B, 'onUpdate:pressed': Function, onChange: Function, onClick: Function, type: { type: String, default: 'button' } },
|
|
75
|
+
setup(props, { slots }) {
|
|
76
|
+
const [on, set] = useModel(props, { prop: 'pressed', def: 'defaultPressed', initial: false });
|
|
77
|
+
const toggle = () => props.pressed !== undefined || props.defaultPressed !== undefined;
|
|
78
|
+
return () => h('button', {
|
|
79
|
+
type: props.type, 'aria-label': props.label, 'aria-pressed': toggle() ? String(!!on.value) : undefined,
|
|
80
|
+
class: ['iconbtn', props.size && 'iconbtn--' + props.size],
|
|
81
|
+
onClick: (e) => {
|
|
82
|
+
props.onClick?.(e);
|
|
83
|
+
if (!toggle() || e.defaultPrevented) return;
|
|
84
|
+
M.pop(e.currentTarget); set(!on.value, e);
|
|
85
|
+
},
|
|
86
|
+
}, [iconNode(props.icon), slots.default?.()]);
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
/* ---------------- labels ---------------- */
|
|
91
|
+
/** <Tag tone="good">Paid</Tag> — same as tag('Paid', 'good', dot). tone: good | warn | bad | accent | plain */
|
|
92
|
+
export const Tag = defineComponent({
|
|
93
|
+
name: 'Tag',
|
|
94
|
+
props: { tone: { type: String, default: 'plain' }, dot: { type: Boolean, default: true }, icon: ANY },
|
|
95
|
+
setup(props, { slots }) {
|
|
96
|
+
return () => h('span', { class: ['tag', 'tag--' + (props.tone || 'plain')] },
|
|
97
|
+
[props.dot && !props.icon ? h('i') : null, iconNode(props.icon), slots.default?.()]);
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/** <Badge tone="accent">3</Badge> — same as badge(3, 'accent'). */
|
|
102
|
+
export const Badge = defineComponent({
|
|
103
|
+
name: 'Badge',
|
|
104
|
+
props: { tone: String },
|
|
105
|
+
setup(props, { slots }) {
|
|
106
|
+
return () => h('span', { class: ['badge', props.tone && 'badge--' + props.tone] }, slots.default?.());
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/* ---------------- people ---------------- */
|
|
111
|
+
/**
|
|
112
|
+
* <Avatar name="Pim Wong" tone="teal" size="sm" online />
|
|
113
|
+
* Same as av({ name, ini, tone }, size, online). No name and no ini: an empty "open slot" circle with a plus.
|
|
114
|
+
*/
|
|
115
|
+
export const Avatar = defineComponent({
|
|
116
|
+
name: 'Avatar',
|
|
117
|
+
props: { name: String, ini: String, tone: String, size: String, online: B },
|
|
118
|
+
setup(props) {
|
|
119
|
+
return () => {
|
|
120
|
+
if (!has(props.name) && !has(props.ini)) return h('span', { class: ['av', 'av--open', props.size && 'av--' + props.size] }, [iconNode('plus')]);
|
|
121
|
+
return h('span', { title: props.name, class: ['av', 'av--' + (props.tone || 'indigo'), props.size && 'av--' + props.size] },
|
|
122
|
+
[props.ini ?? C.initials(props.name || ''), props.online ? h('i', { class: 'av__on' }) : null]);
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/** <AvatarGroup :people="team" :max="4" /> — same as avs(people, max): small overlapping avatars, then "+N". */
|
|
128
|
+
export const AvatarGroup = defineComponent({
|
|
129
|
+
name: 'AvatarGroup',
|
|
130
|
+
props: { people: { type: Array, default: () => [] }, max: { type: Number, default: 4 } },
|
|
131
|
+
setup(props) {
|
|
132
|
+
return () => {
|
|
133
|
+
const { people, max } = props;
|
|
134
|
+
return h('span', { class: 'avs' }, [
|
|
135
|
+
...people.slice(0, max).map((p, i) => h(Avatar, { key: p.id ?? p.name ?? i, name: p.name, ini: p.ini, tone: p.tone, size: 'sm' })),
|
|
136
|
+
people.length > max ? h('span', { class: 'av av--sm av--more' }, '+' + (people.length - max)) : null,
|
|
137
|
+
]);
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
/** <Cell name="Pim Wong" tone="teal" sub="Mechanic" online /> — same as cell(person, sub): avatar + name + small line (or a #sub slot). */
|
|
143
|
+
export const Cell = defineComponent({
|
|
144
|
+
name: 'Cell',
|
|
145
|
+
props: { name: String, ini: String, tone: String, sub: ANY, online: B },
|
|
146
|
+
setup(props, { slots }) {
|
|
147
|
+
return () => h('span', { class: 'cell' }, [
|
|
148
|
+
h(Avatar, { name: props.name, ini: props.ini, tone: props.tone, online: props.online }),
|
|
149
|
+
h('span', { class: 'cell__text' }, [h('b', null, props.name ?? ''), slots.sub || props.sub != null ? h('small', null, slotOr(slots.sub, props.sub)) : null]),
|
|
150
|
+
]);
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
/* ---------------- cards ---------------- */
|
|
155
|
+
const headParts = (props, slots) => [
|
|
156
|
+
h('div', { class: 'chead__text' }, [h('h2', null, slotOr(slots.title, props.title)), slots.sub || has(props.sub) ? h('p', null, slotOr(slots.sub, props.sub)) : null]),
|
|
157
|
+
slots.right || has(props.right) ? h('div', { class: 'chead__right' }, slotOr(slots.right, props.right)) : null,
|
|
158
|
+
];
|
|
159
|
+
/**
|
|
160
|
+
* <CardHead title="Today" sub="12 bookings"><template #right><Button size="sm">See all</Button></template></CardHead>
|
|
161
|
+
* Same as chead(title, sub, right). title / sub / right: text props or #title / #sub / #right slots.
|
|
162
|
+
*/
|
|
163
|
+
export const CardHead = defineComponent({
|
|
164
|
+
name: 'CardHead',
|
|
165
|
+
props: { title: ANY, sub: ANY, right: ANY },
|
|
166
|
+
setup(props, { slots }) {
|
|
167
|
+
return () => h('div', { class: 'chead' }, headParts(props, slots));
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* <Card title="Today" sub="12 bookings" lift flush>…body…<template #right>…</template></Card>
|
|
173
|
+
* Same as card({ title, sub, right, body, flush, lift }): a <section class="card">; the head shows when there is a
|
|
174
|
+
* title (prop or #title slot). The default slot is the body.
|
|
175
|
+
*/
|
|
176
|
+
export const Card = defineComponent({
|
|
177
|
+
name: 'Card',
|
|
178
|
+
props: { title: ANY, sub: ANY, right: ANY, flush: B, lift: B },
|
|
179
|
+
setup(props, { slots }) {
|
|
180
|
+
const el = useMotion((n) => (props.lift ? M.lift(n) : undefined), () => !!props.lift);
|
|
181
|
+
return () => h('section', { ref: el, 'data-lift': props.lift ? '' : undefined, class: ['card', props.flush && 'card--flush'] }, [
|
|
182
|
+
slots.title || has(props.title) ? h('div', { class: 'chead' }, headParts(props, slots)) : null,
|
|
183
|
+
slots.default?.(),
|
|
184
|
+
]);
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
/** <Kpi label="Revenue" value="฿48,250" sub="This month" tone="good" /> — same as kpi(label, value, sub, cls). Each text also has a slot (#label, #value, #sub). */
|
|
189
|
+
export const Kpi = defineComponent({
|
|
190
|
+
name: 'Kpi',
|
|
191
|
+
props: { label: ANY, value: ANY, sub: ANY, tone: String },
|
|
192
|
+
setup(props, { slots }) {
|
|
193
|
+
return () => h('div', { class: ['kpi', props.tone && 'kpi--' + props.tone] }, [
|
|
194
|
+
h('small', null, slotOr(slots.label, props.label)), h('b', null, slotOr(slots.value, props.value)),
|
|
195
|
+
slots.sub || has(props.sub) ? h('span', { class: 'kpi__sub' }, slotOr(slots.sub, props.sub)) : null,
|
|
196
|
+
]);
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* <Empty icon="inbox" title="No bookings yet" body="New ones show up here."><template #action><Button>New booking</Button></template></Empty>
|
|
202
|
+
* Same as empty(icon, title, body, action). title / body: text props or slots; #action: a button under the text;
|
|
203
|
+
* the default slot goes last.
|
|
204
|
+
*/
|
|
205
|
+
export const Empty = defineComponent({
|
|
206
|
+
name: 'Empty',
|
|
207
|
+
props: { icon: { type: null, default: 'inbox' }, title: ANY, body: ANY, action: ANY },
|
|
208
|
+
setup(props, { slots }) {
|
|
209
|
+
return () => h('div', { class: 'empty' }, [
|
|
210
|
+
h('span', { class: 'empty__icon' }, [iconNode(props.icon)]), h('b', null, slotOr(slots.title, props.title)),
|
|
211
|
+
slots.body || has(props.body) ? h('p', null, slotOr(slots.body, props.body)) : null,
|
|
212
|
+
slotOr(slots.action, props.action ?? null), slots.default?.(),
|
|
213
|
+
]);
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
/* ---------------- table ---------------- */
|
|
218
|
+
/**
|
|
219
|
+
* A table styled by .table, inside .table-wrap (scrolls sideways on small screens).
|
|
220
|
+
* Data: <Table :columns="[{ key: 'bike', title: 'Bike' }, { key: 'days', title: 'Days', align: 'right' }]" :rows="bookings" row-key="id" />
|
|
221
|
+
* cells: #cell-<key>="{ row, column, value, index }" for one column, #cell="{ … }" for any column (when it
|
|
222
|
+
* renders nothing the plain value shows); heads: #head-<key> / #head="{ column }"; #foot: <tr>s for a <tfoot>.
|
|
223
|
+
* Your own: <Table><thead>…</thead><tbody>…</tbody></Table>
|
|
224
|
+
* slide-rows: rows slide in when they first appear (also rows added later). Attributes go on the <table>.
|
|
225
|
+
*/
|
|
226
|
+
export const Table = defineComponent({
|
|
227
|
+
name: 'Table',
|
|
228
|
+
inheritAttrs: false,
|
|
229
|
+
props: { columns: Array, rows: Array, rowKey: { type: [String, Function], default: 'id' }, wrap: { type: Boolean, default: true }, slideRows: B },
|
|
230
|
+
setup(props, { slots, attrs }) {
|
|
231
|
+
const el = ref(null);
|
|
232
|
+
let seen = null, tweens = [];
|
|
233
|
+
const slide = () => {
|
|
234
|
+
if (!props.slideRows || !el.value) return;
|
|
235
|
+
seen ||= new WeakSet();
|
|
236
|
+
const fresh = [...el.value.querySelectorAll('tbody > tr')].filter((r) => !seen.has(r));
|
|
237
|
+
fresh.forEach((r) => seen.add(r));
|
|
238
|
+
if (fresh.length) tweens = [...tweens.filter((t) => t.isActive()), M.slide(fresh, { y: 10, stagger: 0.04 })];
|
|
239
|
+
};
|
|
240
|
+
onMounted(slide);
|
|
241
|
+
onUpdated(slide);
|
|
242
|
+
onBeforeUnmount(() => { tweens.forEach((t) => t.progress(1).kill()); tweens = []; seen = null; });
|
|
243
|
+
const key = (r, i) => (typeof props.rowKey === 'function' ? props.rowKey(r, i) : r?.[props.rowKey] ?? i);
|
|
244
|
+
const cls = (c) => { const k = normalizeClass([c.align === 'right' && 'r', c.class]); return k ? { key: c.key, class: k } : { key: c.key }; };
|
|
245
|
+
const fill = (slot, args, fallback) => { if (slot) { const out = slot(args); if (hasContent(out)) return out; } return fallback(); };
|
|
246
|
+
return () => {
|
|
247
|
+
const cols = props.columns;
|
|
248
|
+
const body = cols ? [
|
|
249
|
+
h('thead', null, [h('tr', null, cols.map((c) => h('th', cls(c), fill(slots['head-' + c.key] || slots.head, { column: c }, () => c.title ?? c.key))))]),
|
|
250
|
+
h('tbody', null, (props.rows || []).map((r, i) => h('tr', { key: key(r, i) }, cols.map((c) => h('td', cls(c),
|
|
251
|
+
fill(slots['cell-' + c.key] || slots.cell, { row: r, column: c, value: r?.[c.key], index: i }, () => (c.render ? c.render(r, i) : r?.[c.key]))))))),
|
|
252
|
+
slots.foot ? h('tfoot', null, slots.foot()) : null,
|
|
253
|
+
] : slots.default?.();
|
|
254
|
+
const table = h('table', { ...attrs, ref: el, class: ['table', attrs.class] }, body);
|
|
255
|
+
return props.wrap ? h('div', { class: 'table-wrap' }, [table]) : table;
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
/* ---------------- progress ---------------- */
|
|
261
|
+
const clamp01 = (f) => Math.max(0, Math.min(1, +f || 0));
|
|
262
|
+
/* Elements GSAP animates keep the SAME style object on every render: Vue re-applies every key of a new style
|
|
263
|
+
object, which would put the bar back to its start. */
|
|
264
|
+
const ZERO_X = { transform: 'scaleX(0)' };
|
|
265
|
+
|
|
266
|
+
/** <Meter :value="0.7" tone="good" /> — same as meter(0.7, 'good'): fills from 0 when it appears, glides when value changes. */
|
|
267
|
+
export const Meter = defineComponent({
|
|
268
|
+
name: 'Meter',
|
|
269
|
+
props: { value: { type: [Number, String], default: 0 }, tone: String, delay: { type: Number, default: 0.15 } },
|
|
270
|
+
setup(props) {
|
|
271
|
+
const bar = ref(null);
|
|
272
|
+
const f = computed(() => clamp01(props.value));
|
|
273
|
+
let tw = null, first = true;
|
|
274
|
+
const done = () => { tw?.progress(1); tw?.kill(); tw = null; };
|
|
275
|
+
onMounted(() => {
|
|
276
|
+
watch(f, (v) => {
|
|
277
|
+
done();
|
|
278
|
+
tw = first ? M.meter(bar.value, v, props.delay)
|
|
279
|
+
: M.g.to(bar.value, { scaleX: v, duration: M.D(0.75), ease: 'expo.out', transformOrigin: '0 50%', overwrite: 'auto' });
|
|
280
|
+
first = false;
|
|
281
|
+
}, { immediate: true, flush: 'post' });
|
|
282
|
+
});
|
|
283
|
+
onBeforeUnmount(done);
|
|
284
|
+
return () => h('span', { class: ['meter', props.tone && 'is-' + props.tone] }, [h('i', { ref: bar, 'data-f': f.value, style: ZERO_X })]);
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* <Ring :value="0.72" :size="56" :stroke="6" label="72%" /> — same as ring(0.72, 56, 6, label):
|
|
290
|
+
* the circle sweeps on from 0, and from the last value when value changes. #label slot for richer content.
|
|
291
|
+
*/
|
|
292
|
+
export const Ring = defineComponent({
|
|
293
|
+
name: 'Ring',
|
|
294
|
+
props: { value: { type: [Number, String], default: 0 }, size: { type: Number, default: 56 }, stroke: { type: Number, default: 6 }, label: ANY },
|
|
295
|
+
setup(props, { slots }) {
|
|
296
|
+
const fg = ref(null);
|
|
297
|
+
const f = computed(() => clamp01(props.value));
|
|
298
|
+
const len = computed(() => 2 * Math.PI * ((props.size - props.stroke) / 2));
|
|
299
|
+
let tw = null, prev = null;
|
|
300
|
+
const done = () => { tw?.progress(1); tw?.kill(); tw = null; };
|
|
301
|
+
onMounted(() => {
|
|
302
|
+
watch([f, len], ([v]) => {
|
|
303
|
+
done();
|
|
304
|
+
const from = prev ?? 0, first = prev === null; prev = v;
|
|
305
|
+
tw = M.ring(fg.value, v, { from, delay: first ? 0.2 : 0 });
|
|
306
|
+
}, { immediate: true, flush: 'post' });
|
|
307
|
+
});
|
|
308
|
+
onBeforeUnmount(done);
|
|
309
|
+
return () => {
|
|
310
|
+
const { size, stroke } = props, r = (size - stroke) / 2, c = size / 2, l = len.value;
|
|
311
|
+
return h('span', { class: 'ring', style: { width: size + 'px', height: size + 'px' } }, [
|
|
312
|
+
h('svg', { viewBox: `0 0 ${size} ${size}` }, [
|
|
313
|
+
h('circle', { cx: c, cy: c, r, 'stroke-width': stroke, class: 'ring__bg' }),
|
|
314
|
+
h('circle', { ref: fg, cx: c, cy: c, r, 'stroke-width': stroke, class: 'ring__fg', 'data-len': l, 'data-f': f.value, 'stroke-dasharray': l, 'stroke-dashoffset': l }),
|
|
315
|
+
]),
|
|
316
|
+
h('b', null, slotOr(slots.label, props.label)),
|
|
317
|
+
]);
|
|
318
|
+
};
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* <Steps :labels="['Bike', 'Dates', 'Pay']" :now="1" /> — same as steps(labels, now): done steps get a tick,
|
|
324
|
+
* the bars fill up to `now`, and the new dot pops when now changes. #label="{ label, index }" draws a label yourself.
|
|
325
|
+
*/
|
|
326
|
+
export const Steps = defineComponent({
|
|
327
|
+
name: 'Steps',
|
|
328
|
+
props: { labels: { type: Array, default: () => [] }, now: { type: Number, default: 0 } },
|
|
329
|
+
setup(props, { slots }) {
|
|
330
|
+
const el = ref(null);
|
|
331
|
+
let prev = null, parts = [];
|
|
332
|
+
const done = () => { M.g.getTweensOf(parts).forEach((t) => t.progress(1).kill()); parts = []; };
|
|
333
|
+
onMounted(() => {
|
|
334
|
+
watch([() => props.now, () => props.labels.length], ([now]) => {
|
|
335
|
+
done();
|
|
336
|
+
const p = prev === null ? now : prev; prev = now;
|
|
337
|
+
M.steps(el.value, now, p);
|
|
338
|
+
parts = [...el.value.querySelectorAll('.steps__fill, .steps__dot')];
|
|
339
|
+
}, { immediate: true, flush: 'post' });
|
|
340
|
+
});
|
|
341
|
+
onBeforeUnmount(done);
|
|
342
|
+
return () => {
|
|
343
|
+
const { labels, now } = props;
|
|
344
|
+
return h('ol', { ref: el, class: 'steps' }, labels.map((l, i) => h('li', { key: i, ...(i < now ? { class: 'is-done' } : i === now ? { class: 'is-now', 'aria-current': 'step' } : null) }, [
|
|
345
|
+
h('span', { class: 'steps__dot' }, [iconNode('check')]),
|
|
346
|
+
i < labels.length - 1 ? h('span', { class: 'steps__bar' }, [h('i', { class: 'steps__fill', style: ZERO_X })]) : null,
|
|
347
|
+
h('small', null, slotOr(slots.label, l, { label: l, index: i })),
|
|
348
|
+
])));
|
|
349
|
+
};
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
/* Bars has no stylesheet of its own in the library yet, so its look is written inline (same look as the
|
|
354
|
+
playground's "Bars, meter & ring" chart). The class names are there for your own CSS. The bar colour comes in
|
|
355
|
+
through --bar-c on its column, so the bar itself keeps one style object (see ZERO_X). */
|
|
356
|
+
const BAR_S = {
|
|
357
|
+
root: { display: 'flex', gap: '8px', width: '100%' },
|
|
358
|
+
col: { flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '6px' },
|
|
359
|
+
track: { position: 'relative', flex: 1, width: '100%' },
|
|
360
|
+
bar: { position: 'absolute', inset: 0, borderRadius: '7px 7px 3px 3px', transform: 'scaleY(0)', transformOrigin: '50% 100%', transition: 'background .4s', background: 'var(--bar-c)' },
|
|
361
|
+
v: { position: 'absolute', left: 0, right: 0, paddingBottom: '4px', textAlign: 'center', fontSize: '11px', fontWeight: 700, color: 'var(--ink-2)', fontVariantNumeric: 'tabular-nums', transition: 'bottom .75s var(--ease-in-out, ease)' },
|
|
362
|
+
label: { color: 'var(--muted)', fontSize: '11px', fontWeight: 600, maxWidth: '100%', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* <Bars :values="[12, 30, 22]" :labels="['Mon', 'Tue', 'Wed']" :max="40" show-values />
|
|
366
|
+
* A small column chart on bars(els, values, max): the bars grow up from the baseline, then morph when values change.
|
|
367
|
+
*/
|
|
368
|
+
export const Bars = defineComponent({
|
|
369
|
+
name: 'Bars',
|
|
370
|
+
props: { values: { type: Array, default: () => [] }, max: Number, labels: Array, showValues: B, format: { type: Function, default: (v) => v }, highlightTop: { type: Boolean, default: true }, height: { type: [Number, String], default: 210 }, label: String },
|
|
371
|
+
setup(props) {
|
|
372
|
+
const el = ref(null);
|
|
373
|
+
const top = computed(() => Math.max(0, ...props.values.map((v) => +v || 0)));
|
|
374
|
+
const m = computed(() => props.max ?? (top.value || 1));
|
|
375
|
+
let first = true, bars = [];
|
|
376
|
+
const done = () => { M.g.getTweensOf(bars).forEach((t) => t.progress(1).kill()); };
|
|
377
|
+
onMounted(() => {
|
|
378
|
+
watch(() => props.values.join(',') + '|' + m.value, () => {
|
|
379
|
+
done();
|
|
380
|
+
bars = [...el.value.querySelectorAll('.bars__bar')];
|
|
381
|
+
M.bars(bars, props.values, m.value, { first }); first = false;
|
|
382
|
+
}, { immediate: true, flush: 'post' });
|
|
383
|
+
});
|
|
384
|
+
onBeforeUnmount(done);
|
|
385
|
+
return () => {
|
|
386
|
+
const { values, labels, highlightTop, height } = props;
|
|
387
|
+
return h('div', {
|
|
388
|
+
ref: el, role: 'img', 'aria-label': props.label ?? values.map((v, i) => (labels?.[i] != null ? `${labels[i]}: ${v}` : v)).join(', '),
|
|
389
|
+
class: 'bars', style: { ...BAR_S.root, height: typeof height === 'number' ? height + 'px' : height },
|
|
390
|
+
}, values.map((v, i) => {
|
|
391
|
+
const isTop = highlightTop && +v === top.value && top.value > 0;
|
|
392
|
+
return h('div', { key: i, class: ['bars__col', isTop && 'is-top'], style: { ...BAR_S.col, '--bar-c': isTop || !highlightTop ? 'var(--accent)' : 'color-mix(in oklab, var(--accent) 35%, var(--card))' } }, [
|
|
393
|
+
h('span', { class: 'bars__track', style: BAR_S.track }, [
|
|
394
|
+
h('i', { class: 'bars__bar', style: BAR_S.bar }),
|
|
395
|
+
props.showValues ? h('b', { class: 'bars__v', 'aria-hidden': 'true', style: { ...BAR_S.v, bottom: Math.min(100, ((+v || 0) / m.value) * 100) + '%' } }, props.format(v, i)) : null,
|
|
396
|
+
]),
|
|
397
|
+
labels?.[i] != null ? h('small', { class: 'bars__label', style: BAR_S.label }, labels[i]) : null,
|
|
398
|
+
]);
|
|
399
|
+
}));
|
|
400
|
+
};
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
/* ---------------- form fields ---------------- */
|
|
405
|
+
/**
|
|
406
|
+
* <Field label="Email" hint="We only use it for reminders."><Input v-model="email" type="email" /></Field>
|
|
407
|
+
* Same as field(label, control, hint). label / hint: text props or #label / #hint slots. invalid: red border + red hint.
|
|
408
|
+
*/
|
|
409
|
+
export const Field = defineComponent({
|
|
410
|
+
name: 'Field',
|
|
411
|
+
props: { label: ANY, hint: ANY, invalid: B },
|
|
412
|
+
setup(props, { slots }) {
|
|
413
|
+
return () => h('label', { class: ['field', props.invalid && 'is-bad'] }, [
|
|
414
|
+
h('span', null, slotOr(slots.label, props.label)), slots.default?.(),
|
|
415
|
+
slots.hint || has(props.hint) ? h('small', { class: 'field__hint' }, slotOr(slots.hint, props.hint)) : null,
|
|
416
|
+
]);
|
|
417
|
+
},
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
/* v-model on a text box works exactly like v-model on a native one (Vue's own vModelText: updates on input, waits
|
|
421
|
+
for IME composition to end, .lazy / .trim / .number). Without v-model / default-value the box is left alone. */
|
|
422
|
+
const textModel = (props, node, v) => {
|
|
423
|
+
const bound = props.modelValue !== undefined || props.defaultValue !== undefined || !!props['onUpdate:modelValue'];
|
|
424
|
+
return bound ? withDirectives(node, [[vModelText, v, undefined, props.modelModifiers || {}]]) : node;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* <Input v-model="email" type="email" placeholder="you@shop.com" size="sm" />
|
|
429
|
+
* Same as input({ type, value, placeholder }): an <input class="input">. v-model works like on a native input
|
|
430
|
+
* (also .lazy / .trim / .number); every other attribute and listener (@input, @change, @blur…) goes on the <input>.
|
|
431
|
+
*/
|
|
432
|
+
export const Input = defineComponent({
|
|
433
|
+
name: 'Input',
|
|
434
|
+
props: { ...modelProps(), modelModifiers: Object, size: String, type: { type: String, default: 'text' } },
|
|
435
|
+
setup(props) {
|
|
436
|
+
const [v, set] = useModel(props, { event: null });
|
|
437
|
+
return () => textModel(props, h('input', {
|
|
438
|
+
type: props.type, class: ['input', props.size && 'input--' + props.size], 'onUpdate:modelValue': (x) => set(x),
|
|
439
|
+
}), v.value);
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* <Textarea v-model="note" :rows="3" :auto-grow="6" /> — a <textarea class="input">. auto-grow: the box gets taller
|
|
445
|
+
* with each line, up to that many lines (true = 6), then scrolls. v-model works like on a native <textarea>.
|
|
446
|
+
*/
|
|
447
|
+
export const Textarea = defineComponent({
|
|
448
|
+
name: 'Textarea',
|
|
449
|
+
props: { ...modelProps(), modelModifiers: Object, autoGrow: { type: [Boolean, Number, String], default: false }, size: String },
|
|
450
|
+
setup(props) {
|
|
451
|
+
const [v, set] = useModel(props, { event: null });
|
|
452
|
+
const el = ref(null);
|
|
453
|
+
const lines = () => (props.autoGrow === true || props.autoGrow === '' ? 6 : +props.autoGrow || 0);
|
|
454
|
+
let grown = false;
|
|
455
|
+
const fit = () => {
|
|
456
|
+
const ta = el.value; if (!ta) return;
|
|
457
|
+
const n = lines();
|
|
458
|
+
if (!n) { if (grown) { ta.style.height = ''; ta.style.overflowY = ''; grown = false; } return; }
|
|
459
|
+
grown = true;
|
|
460
|
+
const s = getComputedStyle(ta), line = parseFloat(s.lineHeight) || 20;
|
|
461
|
+
const border = ta.offsetHeight - ta.clientHeight;
|
|
462
|
+
const max = line * n + parseFloat(s.paddingTop) + parseFloat(s.paddingBottom) + border;
|
|
463
|
+
ta.style.height = 'auto';
|
|
464
|
+
const full = ta.scrollHeight + border;
|
|
465
|
+
ta.style.height = Math.min(full, max) + 'px';
|
|
466
|
+
ta.style.overflowY = full > max ? 'auto' : 'hidden';
|
|
467
|
+
};
|
|
468
|
+
onMounted(() => { watch([lines, () => v.value], fit, { immediate: true, flush: 'post' }); });
|
|
469
|
+
return () => textModel(props, h('textarea', {
|
|
470
|
+
ref: el, ...(lines() ? { style: NO_RESIZE } : null), onInput: fit,
|
|
471
|
+
class: ['input', props.size && 'input--' + props.size], 'onUpdate:modelValue': (x) => set(x),
|
|
472
|
+
}), v.value);
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
const NO_RESIZE = { resize: 'none' };
|