@gecko-ui-kit/vue 0.0.0-stage → 0.3.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 +2052 -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 +467 -0
- package/src/overlays.js +710 -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/controls.js
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/* Controls from @gecko-ui-kit/components (segmented, switch, checkbox, tabs, chips, slider, colour, search,
|
|
2
|
+
dropdown, code field) as Vue components.
|
|
3
|
+
Each one works with v-model or on its own (defaultValue). Vue owns the state; the vanilla motion helpers
|
|
4
|
+
only animate (thumb, knob, pop). */
|
|
5
|
+
import { defineComponent, ref, computed, watch, nextTick, mergeProps, onMounted, onUpdated, onBeforeUnmount } from 'vue';
|
|
6
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
7
|
+
import * as C from '@gecko-ui-kit/components';
|
|
8
|
+
import * as O from '@gecko-ui-kit/overlays';
|
|
9
|
+
import { h, B, iconNode, useModel, modelProps } from './core.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* <Switch v-model="on" label="Wi-Fi" /> (or default-value)
|
|
13
|
+
* Same markup as sw(on, label): a button with role="switch"; the knob springs across.
|
|
14
|
+
* @change(checked, event)
|
|
15
|
+
*/
|
|
16
|
+
export const Switch = defineComponent({
|
|
17
|
+
name: 'Switch',
|
|
18
|
+
props: { ...modelProps(), label: String, onChange: Function, onClick: Function },
|
|
19
|
+
setup(props) {
|
|
20
|
+
const [on, set] = useModel(props, { initial: false });
|
|
21
|
+
const el = ref(null);
|
|
22
|
+
onMounted(() => {
|
|
23
|
+
let first = true;
|
|
24
|
+
watch(() => !!on.value, (v) => { M.switchKnob(el.value, v, !first); first = false; }, { immediate: true, flush: 'post' });
|
|
25
|
+
});
|
|
26
|
+
return () => h('button', {
|
|
27
|
+
ref: el, type: 'button', role: 'switch', 'aria-checked': String(!!on.value), 'aria-label': props.label, class: 'gk-switch',
|
|
28
|
+
onClick: (e) => { props.onClick?.(e); if (!e.defaultPrevented) set(!on.value, e); },
|
|
29
|
+
}, [h('i')]);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** [id, label, icon] | { value, label, icon, disabled } | 'id' → { value, label, icon, disabled } */
|
|
34
|
+
export const normOption = (o) => Array.isArray(o) ? { value: o[0], label: o[1] ?? o[0], icon: o[2], count: o[3] }
|
|
35
|
+
: o && typeof o === 'object' ? { label: o.value, ...o } : { value: o, label: o };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* <Segmented v-model="view" :options="[['list', 'List', 'list'], ['grid', 'Grid', 'cards']]" />
|
|
39
|
+
* Same as seg(name, options, value, { size }): the thumb stretches to the pressed button.
|
|
40
|
+
* options: [id, label, icon?, count?] arrays, { value, label, icon, count } objects or plain strings. @change(value, event)
|
|
41
|
+
*/
|
|
42
|
+
export const Segmented = defineComponent({
|
|
43
|
+
name: 'Segmented',
|
|
44
|
+
props: { ...modelProps(), options: { type: Array, default: () => [] }, size: String, name: String, onChange: Function },
|
|
45
|
+
setup(props) {
|
|
46
|
+
const list = computed(() => props.options.map(normOption));
|
|
47
|
+
const [v, set] = useModel(props, { initial: list.value[0]?.value });
|
|
48
|
+
const root = ref(null), thumb = ref(null);
|
|
49
|
+
const current = () => root.value?.querySelector('button[aria-pressed="true"]');
|
|
50
|
+
let untrack = null;
|
|
51
|
+
onMounted(() => {
|
|
52
|
+
let first = true;
|
|
53
|
+
watch([v, () => list.value.length], () => { M.indicator(thumb.value, current(), { animate: !first, radius: 'auto' }); first = false; }, { immediate: true, flush: 'post' });
|
|
54
|
+
untrack = M.trackIndicator(thumb.value, current, { radius: 'auto' });
|
|
55
|
+
});
|
|
56
|
+
onBeforeUnmount(() => { untrack?.(); untrack = null; });
|
|
57
|
+
return () => h('div', { ref: root, role: 'group', 'data-seg': props.name, class: ['gk-seg', props.size && 'gk-seg--' + props.size] }, [
|
|
58
|
+
h('span', { class: 'gk-seg__thumb', ref: thumb }),
|
|
59
|
+
...list.value.map((o) => h('button', {
|
|
60
|
+
key: o.value, type: 'button', 'data-v': o.value, 'aria-pressed': String(o.value === v.value), disabled: o.disabled || undefined,
|
|
61
|
+
onClick: (e) => { if (o.value === v.value) return; M.pop(e.currentTarget); set(o.value, e); },
|
|
62
|
+
}, [iconNode(o.icon), o.label, o.count != null && o.count !== '' ? h('span', { class: 'gk-seg__count' }, String(o.count)) : null])),
|
|
63
|
+
]);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* <Checkbox v-model="done" label="Helmet returned" /> (or default-value)
|
|
69
|
+
* Same markup as chk(on, label): a button with role="checkbox" and a tick; it pops when ticked.
|
|
70
|
+
* @change(checked, event)
|
|
71
|
+
*/
|
|
72
|
+
export const Checkbox = defineComponent({
|
|
73
|
+
name: 'Checkbox',
|
|
74
|
+
props: { ...modelProps(), label: String, onChange: Function, onClick: Function },
|
|
75
|
+
setup(props) {
|
|
76
|
+
const [on, set] = useModel(props, { initial: false });
|
|
77
|
+
return () => h('button', {
|
|
78
|
+
type: 'button', role: 'checkbox', 'aria-checked': String(!!on.value), 'aria-label': props.label, class: 'gk-check',
|
|
79
|
+
onClick: (e) => { props.onClick?.(e); if (e.defaultPrevented) return; M.pop(e.currentTarget); set(!on.value, e); },
|
|
80
|
+
}, [iconNode('check')]);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/** [id, label, count] | { value, label, count, disabled } | 'id' */
|
|
85
|
+
const normTab = (o) => Array.isArray(o) ? { value: o[0], label: o[1] ?? o[0], count: o[2] }
|
|
86
|
+
: o && typeof o === 'object' ? { label: o.value, ...o } : { value: o, label: o };
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* <Tabs v-model="tab" :items="[['all', 'All bookings'], ['late', 'Late', 3]]" />
|
|
90
|
+
* Same as tabs(items, value, { name }): tab buttons with a line that stretches to the selected one; a count shows as a badge.
|
|
91
|
+
* @change(value, event)
|
|
92
|
+
*/
|
|
93
|
+
export const Tabs = defineComponent({
|
|
94
|
+
name: 'Tabs',
|
|
95
|
+
props: { ...modelProps(), items: { type: Array, default: () => [] }, name: { type: String, default: 'tabs' }, onChange: Function },
|
|
96
|
+
setup(props) {
|
|
97
|
+
const list = computed(() => props.items.map(normTab));
|
|
98
|
+
const [v, set] = useModel(props, { initial: list.value[0]?.value });
|
|
99
|
+
const root = ref(null), line = ref(null);
|
|
100
|
+
const current = () => root.value?.querySelector('[aria-selected="true"]');
|
|
101
|
+
let untrack = null;
|
|
102
|
+
onMounted(() => {
|
|
103
|
+
let first = true;
|
|
104
|
+
watch([v, () => list.value.length], () => { M.indicator(line.value, current(), { animate: !first, radius: 2, line: true }); first = false; }, { immediate: true, flush: 'post' });
|
|
105
|
+
untrack = M.trackIndicator(line.value, current, { radius: 2, line: true });
|
|
106
|
+
});
|
|
107
|
+
onBeforeUnmount(() => { untrack?.(); untrack = null; });
|
|
108
|
+
return () => h('div', { ref: root, role: 'tablist', 'data-tabs': props.name, class: 'gk-tabs' }, [
|
|
109
|
+
h('span', { class: 'gk-tabs__line', ref: line }),
|
|
110
|
+
...list.value.map((o) => h('button', {
|
|
111
|
+
key: o.value, type: 'button', role: 'tab', 'data-v': o.value, 'aria-selected': String(o.value === v.value), disabled: o.disabled || undefined,
|
|
112
|
+
onClick: (e) => { if (o.value !== v.value) set(o.value, e); },
|
|
113
|
+
}, [o.label, o.count ? h('span', { class: 'gk-badge' }, o.count) : null])),
|
|
114
|
+
]);
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* <Chips v-model="kind" :options="[['all', 'All'], ['scooter', 'Scooters', 'moto']]" />
|
|
120
|
+
* <Chips v-model="picked" multi :options="…" /> (an array of values)
|
|
121
|
+
* Same as chips(items, value, { name, multi }): pill buttons; one picked, or several with multi. @change(value | values, event)
|
|
122
|
+
*/
|
|
123
|
+
export const Chips = defineComponent({
|
|
124
|
+
name: 'Chips',
|
|
125
|
+
props: { ...modelProps(), options: { type: Array, default: () => [] }, multi: B, name: { type: String, default: 'chips' }, onChange: Function },
|
|
126
|
+
setup(props) {
|
|
127
|
+
const list = computed(() => props.options.map(normOption));
|
|
128
|
+
const [v, set] = useModel(props, { initial: props.multi ? [] : list.value[0]?.value });
|
|
129
|
+
const isOn = (id) => (props.multi ? (v.value || []).includes(id) : id === v.value);
|
|
130
|
+
return () => h('div', { 'data-chips': props.name, 'data-multi': props.multi ? '' : undefined, class: 'gk-chips' }, list.value.map((o) => h('button', {
|
|
131
|
+
key: o.value, type: 'button', class: 'gk-chip', 'data-v': o.value, 'aria-pressed': String(isOn(o.value)), disabled: o.disabled || undefined,
|
|
132
|
+
onClick: (e) => {
|
|
133
|
+
if (props.multi) {
|
|
134
|
+
const cur = v.value || []; M.pop(e.currentTarget);
|
|
135
|
+
set(isOn(o.value) ? cur.filter((x) => x !== o.value) : list.value.map((x) => x.value).filter((x) => x === o.value || cur.includes(x)), e);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (o.value === v.value) return; M.pop(e.currentTarget); set(o.value, e);
|
|
139
|
+
},
|
|
140
|
+
}, [iconNode(o.icon), o.label])));
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* <Range v-model="days" :min="1" :max="14" />
|
|
146
|
+
* Same as range({ min, max, step, value }): a slider with a filled track (it fills up from zero when first shown)
|
|
147
|
+
* and a springy thumb. v-model is a number. @change(value, event) runs while it moves.
|
|
148
|
+
*/
|
|
149
|
+
export const Range = defineComponent({
|
|
150
|
+
name: 'Range',
|
|
151
|
+
props: { ...modelProps(), min: { type: [Number, String], default: 0 }, max: { type: [Number, String], default: 100 }, step: { type: [Number, String], default: 1 }, onChange: Function },
|
|
152
|
+
setup(props) {
|
|
153
|
+
const [v, set] = useModel(props, { initial: Math.round((+props.min + +props.max) / 2) });
|
|
154
|
+
const el = ref(null);
|
|
155
|
+
const p = computed(() => ((+v.value - +props.min) / ((+props.max - +props.min) || 1)) * 100 + '%');
|
|
156
|
+
let tw = null;
|
|
157
|
+
onMounted(() => {
|
|
158
|
+
const r = el.value;
|
|
159
|
+
if (!M.RM()) tw = M.g.fromTo(r, { '--gk-p': '0%' }, { '--gk-p': p.value, duration: 0.9, ease: 'expo.out' });
|
|
160
|
+
watch(p, (x) => { M.g.killTweensOf(r); tw = null; r.style.setProperty('--gk-p', x); }, { flush: 'post' });
|
|
161
|
+
});
|
|
162
|
+
onBeforeUnmount(() => { tw?.kill(); tw = null; });
|
|
163
|
+
return () => h('input', {
|
|
164
|
+
ref: el, type: 'range', min: props.min, max: props.max, step: props.step, value: v.value,
|
|
165
|
+
class: 'gk-range', style: { '--gk-p': p.value },
|
|
166
|
+
onInput: (e) => { M.g.killTweensOf(e.currentTarget); set(+e.currentTarget.value, e); },
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* <Swatch v-model="colour" aria-label="Label colour" />
|
|
173
|
+
* Same as swatch(value): a colour picker drawn as a round swatch; it pops when a colour is picked.
|
|
174
|
+
* @change(value: '#rrggbb', event) runs while the colour changes.
|
|
175
|
+
*/
|
|
176
|
+
export const Swatch = defineComponent({
|
|
177
|
+
name: 'Swatch',
|
|
178
|
+
props: { ...modelProps(), onChange: Function },
|
|
179
|
+
setup(props) {
|
|
180
|
+
const [v, set] = useModel(props, { initial: '#5b4cf0' });
|
|
181
|
+
return () => h('input', {
|
|
182
|
+
type: 'color', value: v.value, class: 'gk-swatch',
|
|
183
|
+
onInput: (e) => set(e.currentTarget.value, e),
|
|
184
|
+
onChange: (e) => M.pop(e.currentTarget), // the native "picked" event
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/* Typing with an IME (Thai, Japanese…) sends input events mid-word: wait until the word is done, like v-model does. */
|
|
190
|
+
const composing = {
|
|
191
|
+
onCompositionstart: (e) => { e.target.composing = true; },
|
|
192
|
+
onCompositionend: (e) => { if (!e.target.composing) return; e.target.composing = false; e.target.dispatchEvent(new Event('input', { bubbles: true })); },
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* <SearchBox v-model="q" placeholder="Search bikes…" @search="run" />
|
|
197
|
+
* Same as searchbox(placeholder): a field with a magnifying glass. @search(text, event) runs on Enter
|
|
198
|
+
* (an empty box shakes instead); @change(value, event) as it is typed. class / style go on the <label>;
|
|
199
|
+
* every other attribute and listener goes on the <input>.
|
|
200
|
+
*/
|
|
201
|
+
export const SearchBox = defineComponent({
|
|
202
|
+
name: 'SearchBox',
|
|
203
|
+
inheritAttrs: false,
|
|
204
|
+
props: { ...modelProps(), placeholder: { type: String, default: 'Search…' }, size: String, onChange: Function, onSearch: Function },
|
|
205
|
+
setup(props, { attrs }) {
|
|
206
|
+
const [v, set] = useModel(props, { initial: '' });
|
|
207
|
+
const box = ref(null);
|
|
208
|
+
return () => {
|
|
209
|
+
const { class: cls, style, ...rest } = attrs;
|
|
210
|
+
return h('label', { ref: box, class: ['gk-searchbox', cls], style }, [
|
|
211
|
+
iconNode('search'),
|
|
212
|
+
h('input', mergeProps({ 'aria-label': props.placeholder }, rest, {
|
|
213
|
+
placeholder: props.placeholder, value: v.value ?? '', class: ['gk-input', props.size && 'gk-input--' + props.size], ...composing,
|
|
214
|
+
onInput: (e) => { if (!e.target.composing) set(e.currentTarget.value, e); },
|
|
215
|
+
onKeydown: (e) => {
|
|
216
|
+
if (e.defaultPrevented || e.key !== 'Enter' || !props.onSearch || e.isComposing) return;
|
|
217
|
+
const text = e.currentTarget.value.trim();
|
|
218
|
+
if (text) props.onSearch(text, e); else M.shake(box.value);
|
|
219
|
+
},
|
|
220
|
+
})),
|
|
221
|
+
]);
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
/* ---------------- dropdown (Select) ---------------- */
|
|
227
|
+
const normSelect = (o) => (o && typeof o === 'object' && !Array.isArray(o) && Array.isArray(o.options) ? { group: o.group ?? o.label, options: o.options.map(normOption) } : normOption(o));
|
|
228
|
+
const firstValue = (list) => { for (const o of list) { if (o.options) { const f = o.options.find((x) => !x.disabled); if (f) return f.value; } else if (!o.disabled) return o.value; } return ''; };
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* <Select v-model="bike" :options="[['scooter', 'Scooter 125', 'moto'], ['ebike', 'E-bike', 'bike']]" />
|
|
232
|
+
* A real <select class="gk-input"> (forms, FormData and labels keep working) that dropdown() from
|
|
233
|
+
* @gecko-ui-kit/overlays turns into the animated dropdown: the list pops out of the button, a highlight follows
|
|
234
|
+
* the mouse and ↑ ↓, and the chosen text rolls in. Options: [value, label, icon?], { value, label, icon, disabled },
|
|
235
|
+
* 'value', or a group { group: 'Bikes', options: [...] }. @change(value, event). Attributes (name, disabled,
|
|
236
|
+
* aria-label…) go on the <select>; the template ref's $el holds it ($el.querySelector('select')).
|
|
237
|
+
*/
|
|
238
|
+
export const Select = defineComponent({
|
|
239
|
+
name: 'Select',
|
|
240
|
+
inheritAttrs: false,
|
|
241
|
+
props: { ...modelProps(), options: { type: Array, default: () => [] }, size: String, onChange: Function },
|
|
242
|
+
setup(props, { attrs }) {
|
|
243
|
+
const list = computed(() => props.options.map(normSelect));
|
|
244
|
+
const [v, set] = useModel(props, { initial: firstValue(list.value) });
|
|
245
|
+
const sel = ref(null), up = ref(false);
|
|
246
|
+
let dd = null;
|
|
247
|
+
// the <select> shows the v-model value, also when the app did not take a pick
|
|
248
|
+
const show = () => {
|
|
249
|
+
const s = sel.value; if (!s) return;
|
|
250
|
+
const want = String(v.value ?? '');
|
|
251
|
+
if (s.value !== want) {
|
|
252
|
+
s.value = want;
|
|
253
|
+
// the pick is already rolling in: drop it, so the kept value snaps back instead of rolling over it
|
|
254
|
+
const val = dd?.button.querySelector('.gk-dd__val');
|
|
255
|
+
if (val) { M.g.killTweensOf([...val.children]); val.replaceChildren(); delete val.dataset.t; }
|
|
256
|
+
}
|
|
257
|
+
dd?.sync();
|
|
258
|
+
};
|
|
259
|
+
onMounted(() => { dd = O.dropdown(sel.value, { size: props.size }); up.value = !!dd; });
|
|
260
|
+
onUpdated(show);
|
|
261
|
+
onBeforeUnmount(() => { dd?.close(); dd = null; });
|
|
262
|
+
const opt = (o) => h('option', { key: o.value, value: o.value, selected: String(o.value) === String(v.value ?? ''), disabled: o.disabled || undefined, 'data-icon': typeof o.icon === 'string' ? o.icon : undefined }, o.label);
|
|
263
|
+
return () => {
|
|
264
|
+
const { class: cls, tabindex, ...rest } = attrs;
|
|
265
|
+
return h('span', { class: 'gk-dd-host', style: HOST }, [
|
|
266
|
+
h('select', {
|
|
267
|
+
...rest, ref: sel, tabindex: up.value ? -1 : tabindex, 'aria-hidden': up.value ? 'true' : undefined,
|
|
268
|
+
class: ['gk-input', props.size && 'gk-input--' + props.size, up.value && 'gk-dd__native', cls],
|
|
269
|
+
onChange: (e) => { set(e.currentTarget.value, e); nextTick(show); },
|
|
270
|
+
}, list.value.map((o, i) => (o.options ? h('optgroup', { key: 'g' + i, label: o.group }, o.options.map(opt)) : opt(o)))),
|
|
271
|
+
]);
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
const HOST = { display: 'contents' };
|
|
276
|
+
|
|
277
|
+
/* ---------------- code field (Otp) ---------------- */
|
|
278
|
+
const cleanCode = (s, letters) => (letters ? String(s ?? '').toUpperCase().replace(/[^A-Z0-9]/g, '') : String(s ?? '').replace(/\D/g, ''));
|
|
279
|
+
const ANSWER = { true: 'good', good: 'good', false: 'bad', bad: 'bad' };
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* <Otp v-model="code" :length="6" split @complete="verify" />
|
|
283
|
+
* Same as otp({ length, split, mask, letters, label, name }) wired by bindOtp: one box per character, typing moves on,
|
|
284
|
+
* Backspace goes back, paste / the phone's code autofill fills every box. @complete(code, el) runs when the last box
|
|
285
|
+
* is filled; return (or resolve) true / 'good' to play the green wave (and gather into a tick), false / 'bad' to shake
|
|
286
|
+
* and clear. v-model / @change(code) follow the code as it is typed; a new v-model value is written into the boxes.
|
|
287
|
+
* The template ref's $el is the .gk-otp element (otpState / otpClear from @gecko-ui-kit/components work on it).
|
|
288
|
+
*/
|
|
289
|
+
export const Otp = defineComponent({
|
|
290
|
+
name: 'Otp',
|
|
291
|
+
props: {
|
|
292
|
+
...modelProps(), length: { type: Number, default: 6 }, split: B, mask: B, letters: B, label: { type: String, default: 'Verification code' }, name: { type: String, default: '' },
|
|
293
|
+
onChange: Function, onComplete: Function, gather: { type: Boolean, default: true }, clear: { type: Boolean, default: true }, disabled: B, autofocus: B,
|
|
294
|
+
},
|
|
295
|
+
setup(props) {
|
|
296
|
+
const el = ref(null);
|
|
297
|
+
let last = '', alive = false, tl = null, done = null;
|
|
298
|
+
const evs = ['input', 'paste', 'keydown'];
|
|
299
|
+
const write = (code) => {
|
|
300
|
+
const box = el.value; if (!box) return;
|
|
301
|
+
const chars = [...cleanCode(code, props.letters)];
|
|
302
|
+
box.querySelectorAll('.gk-otp__box').forEach((b, i) => { b.value = chars[i] ?? ''; b.classList.toggle('gk-is-filled', !!b.value); });
|
|
303
|
+
last = C.otpValue(box);
|
|
304
|
+
};
|
|
305
|
+
const report = () => {
|
|
306
|
+
if (!alive) return;
|
|
307
|
+
const code = C.otpValue(el.value);
|
|
308
|
+
if (code !== last) { last = code; props['onUpdate:modelValue']?.(code); props.onChange?.(code); }
|
|
309
|
+
};
|
|
310
|
+
onMounted(() => {
|
|
311
|
+
const box = el.value;
|
|
312
|
+
alive = true;
|
|
313
|
+
const first = props.modelValue !== undefined ? props.modelValue : props.defaultValue;
|
|
314
|
+
if (first) write(first);
|
|
315
|
+
done = (code, node) => {
|
|
316
|
+
report();
|
|
317
|
+
Promise.resolve(props.onComplete?.(code, node)).then((res) => {
|
|
318
|
+
const state = ANSWER[String(res)]; if (!alive || !state) return;
|
|
319
|
+
tl = C.otpState(node, state, { gather: props.gather, clear: props.clear });
|
|
320
|
+
tl.eventCallback('onComplete', report);
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
C.bindOtp(box, done);
|
|
324
|
+
evs.forEach((t) => box.addEventListener(t, report));
|
|
325
|
+
if (props.autofocus) box.querySelector('.gk-otp__box')?.focus();
|
|
326
|
+
watch(() => props.modelValue, (v) => { if (v !== undefined && cleanCode(v, props.letters) !== C.otpValue(box)) write(v); }, { flush: 'post' });
|
|
327
|
+
});
|
|
328
|
+
onBeforeUnmount(() => {
|
|
329
|
+
const box = el.value;
|
|
330
|
+
alive = false; tl?.kill(); tl = null;
|
|
331
|
+
const i = box?.__otpDone?.indexOf(done); if (i > -1) box.__otpDone.splice(i, 1);
|
|
332
|
+
evs.forEach((t) => box?.removeEventListener(t, report));
|
|
333
|
+
});
|
|
334
|
+
return () => {
|
|
335
|
+
const { length, split, mask, letters, label } = props;
|
|
336
|
+
const half = split ? Math.ceil(length / 2) : -1;
|
|
337
|
+
const boxes = [];
|
|
338
|
+
for (let i = 0; i < length; i++) {
|
|
339
|
+
if (i === half) boxes.push(h('span', { key: 'dash', class: 'gk-otp__dash', 'aria-hidden': 'true' }));
|
|
340
|
+
boxes.push(h('input', {
|
|
341
|
+
key: i, class: 'gk-otp__box', type: mask ? 'password' : 'text', inputmode: letters ? 'text' : 'numeric', pattern: letters ? undefined : '[0-9]*',
|
|
342
|
+
autocomplete: i === 0 ? 'one-time-code' : 'off', autocapitalize: 'characters', spellcheck: 'false', disabled: props.disabled || undefined,
|
|
343
|
+
'aria-label': `${label}, ${i + 1} of ${length}`,
|
|
344
|
+
}));
|
|
345
|
+
}
|
|
346
|
+
return h('div', {
|
|
347
|
+
ref: el, role: 'group', 'aria-label': label, 'data-otp': props.name || 'code', 'data-len': length,
|
|
348
|
+
'data-letters': letters ? '' : undefined, class: ['gk-otp', letters && 'gk-otp--letters'],
|
|
349
|
+
}, [...boxes, h('span', { class: 'gk-otp__done', 'aria-hidden': 'true' }, [
|
|
350
|
+
h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': 2.6, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, [h('path', { d: 'M5.5 12.5l4 4L18.5 7.5' })]),
|
|
351
|
+
])]);
|
|
352
|
+
};
|
|
353
|
+
},
|
|
354
|
+
});
|
package/src/core.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { DefineSetupFnComponent, SlotsType, VNode, VNodeChild, Component, ShallowRef, MaybeRefOrGetter, PropType } from 'vue';
|
|
2
|
+
|
|
3
|
+
/** Join class names, skipping empty ones. */
|
|
4
|
+
export function cx(...names: unknown[]): string;
|
|
5
|
+
export const h: typeof import('vue').h;
|
|
6
|
+
|
|
7
|
+
/** A Boolean prop that stays undefined when not given (so the vanilla default applies). */
|
|
8
|
+
export const B: { type: BooleanConstructor; default: undefined };
|
|
9
|
+
/** A prop of any type. */
|
|
10
|
+
export const ANY: { type: null; default: undefined };
|
|
11
|
+
export function anyProps<K extends string>(...names: K[]): Record<K, { type: null; default: undefined }>;
|
|
12
|
+
|
|
13
|
+
/** Same data by content (functions always count as the same). */
|
|
14
|
+
export function same(a: unknown, b: unknown): boolean;
|
|
15
|
+
/** A plain, non-reactive copy of arrays and plain objects (refs read, everything else kept). */
|
|
16
|
+
export function snap<T>(value: T): T;
|
|
17
|
+
export function changedOnly(next: object, prev: object, keys: string[]): boolean;
|
|
18
|
+
/** Stop what a vanilla call returned: destroy(), revert(), kill(), stop() or a stop function. */
|
|
19
|
+
export function stop(handle: unknown): void;
|
|
20
|
+
/** false on the server and during hydration, true once mounted. */
|
|
21
|
+
export function useClient(): Readonly<ShallowRef<boolean>>;
|
|
22
|
+
/** The main value of a markup piece with or without v-model: [value, set(next, ...extra)]. */
|
|
23
|
+
export function useModel<T = any>(props: Record<string, any>, opts?: { prop?: string; def?: string; event?: string | null; initial?: T }): [Readonly<{ value: T }>, (next: T, ...extra: any[]) => void];
|
|
24
|
+
export function modelProps(prop?: string): Record<string, unknown>;
|
|
25
|
+
|
|
26
|
+
/** What a template ref on a mounted component gives you: the vanilla handle's methods and values, plus `el`. */
|
|
27
|
+
export type Mounted<H> = H & { readonly el: HTMLElement };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A Gecko component's type: P = its props (callbacks are onX props, so `@x` works), S = its slots.
|
|
31
|
+
* Every component also takes class, style and any other attribute (they go on its element).
|
|
32
|
+
*/
|
|
33
|
+
export type GeckoComponent<P extends Record<string, any>, S extends Record<string, any> = {}> = DefineSetupFnComponent<P & Omit<HostAttrs, keyof P>, {}, SlotsType<S>>;
|
|
34
|
+
/** Attributes every component accepts besides its own props: they go on its element. */
|
|
35
|
+
export interface HostAttrs {
|
|
36
|
+
id?: string;
|
|
37
|
+
title?: string;
|
|
38
|
+
role?: string;
|
|
39
|
+
tabindex?: number | string;
|
|
40
|
+
hidden?: boolean;
|
|
41
|
+
lang?: string;
|
|
42
|
+
dir?: string;
|
|
43
|
+
onClick?: (event: MouseEvent) => void;
|
|
44
|
+
onKeydown?: (event: KeyboardEvent) => void;
|
|
45
|
+
onFocus?: (event: FocusEvent) => void;
|
|
46
|
+
onBlur?: (event: FocusEvent) => void;
|
|
47
|
+
onPointerenter?: (event: PointerEvent) => void;
|
|
48
|
+
onPointerleave?: (event: PointerEvent) => void;
|
|
49
|
+
[attr: `aria-${string}`]: string | number | boolean | undefined;
|
|
50
|
+
[attr: `data-${string}`]: string | number | boolean | undefined;
|
|
51
|
+
}
|
|
52
|
+
/** v-model props of a piece whose value is T. */
|
|
53
|
+
export interface ModelProps<T> {
|
|
54
|
+
/** The value (v-model). Without v-model the piece keeps its own value. */
|
|
55
|
+
modelValue?: T;
|
|
56
|
+
/** The first value when there is no v-model. */
|
|
57
|
+
defaultValue?: T;
|
|
58
|
+
'onUpdate:modelValue'?: (value: T) => void;
|
|
59
|
+
}
|
|
60
|
+
/** Slots that take any content. */
|
|
61
|
+
export type DefaultSlot = { default?: () => VNodeChild };
|
|
62
|
+
/** Props of an enhancer: its options, plus `as` (the element to render, default div). */
|
|
63
|
+
export type EnhancerProps<O> = O & { as?: string };
|
|
64
|
+
|
|
65
|
+
export interface MountedSpec<O = any> {
|
|
66
|
+
tag?: string;
|
|
67
|
+
props?: Record<string, unknown>;
|
|
68
|
+
model?: { key?: string; event?: string; arg?: number | ((...args: any[]) => unknown) };
|
|
69
|
+
options?: (opts: any, props: any) => O;
|
|
70
|
+
prepare?: (el: HTMLElement, options: O) => void;
|
|
71
|
+
update?: (handle: any, next: O, prev: O, el: HTMLElement) => boolean;
|
|
72
|
+
}
|
|
73
|
+
/** Turn a vanilla mount(el, options) → handle into a component (see core.js). */
|
|
74
|
+
export function mounted<O = any>(name: string, mount: (el: HTMLElement, options: O) => unknown, spec?: MountedSpec<O>): GeckoComponent<any, any>;
|
|
75
|
+
export interface EnhancerSpec { tag?: string; props?: Record<string, unknown>; update?: (handle: any, next: any, prev: any, el: HTMLElement) => boolean; rerunOnChildren?: boolean; defaults?: Record<string, unknown>; class?: string }
|
|
76
|
+
/** Turn a motion fn(el, options) into a wrapper component around slot content. */
|
|
77
|
+
export function enhancer(name: string, fn: (el: HTMLElement, options: any) => unknown, spec?: EnhancerSpec): GeckoComponent<any, DefaultSlot>;
|
|
78
|
+
/** An update for mounted(): apply changed options through the handle's methods. */
|
|
79
|
+
export function inPlace(map: Record<string, null | ((handle: any, value: any, next: any, prev: any, el: HTMLElement) => false | void)>): (handle: any, next: any, prev: any, el: HTMLElement) => boolean;
|
|
80
|
+
export function withDefault<O>(mount: (el: HTMLElement, o: O) => unknown, key?: string, from?: string): (el: HTMLElement, o: O) => unknown;
|
|
81
|
+
export function vnodeKey(nodes: unknown): string;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Run a motion helper on an element you render. Returns a template ref to put on it.
|
|
85
|
+
* const card = useMotion(tilt, { max: 12 }) → <div ref="card">
|
|
86
|
+
* options: a value, a ref or a getter (it runs again when they change).
|
|
87
|
+
*/
|
|
88
|
+
export function useMotion<E extends Element = HTMLElement, O = any>(fn: (el: E, options: O) => unknown, options?: MaybeRefOrGetter<O>): ShallowRef<E | null>;
|
|
89
|
+
|
|
90
|
+
export interface IconProps {
|
|
91
|
+
/** An icon name from @gecko-ui-kit/icons ('check', 'search'…). */
|
|
92
|
+
name: string;
|
|
93
|
+
/** px (number) or any CSS length. */
|
|
94
|
+
size?: number | string;
|
|
95
|
+
/** Stroke width (1.7). */
|
|
96
|
+
stroke?: number | string;
|
|
97
|
+
}
|
|
98
|
+
/** <Icon name="search" /> — the kit's stroke icon as a real <svg class="gk-ico">. */
|
|
99
|
+
export const Icon: GeckoComponent<IconProps>;
|
|
100
|
+
/** An icon: a name ('check'), a component or a VNode. */
|
|
101
|
+
export type IconLike = string | Component | VNode;
|
|
102
|
+
export function iconNode(icon: IconLike | null | undefined | false, props?: Partial<IconProps>): VNode | null;
|
|
103
|
+
export function slotOr<T>(slot: ((...a: any[]) => T) | undefined, fallback: unknown, ...args: any[]): T | unknown;
|
|
104
|
+
export function hasContent(nodes: unknown): boolean;
|
|
105
|
+
|
|
106
|
+
/** A date as a Date or an ISO string ('2026-12-04' or '2026-12-04T10:00'). */
|
|
107
|
+
export type DateLike = Date | string;
|
|
108
|
+
|
|
109
|
+
export {};
|