@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/src/motion.js ADDED
@@ -0,0 +1,216 @@
1
+ /* Motions from @gecko-ui-kit/motion that wrap elements you already have: Rise (rise / slide / deal / reveal
2
+ entrances), Magnetic, Lift, Tilt, Spotlight and Marquee. Any other helper (flash, pop, shake, swap…) runs on
3
+ your own element with useMotion(fn, options) from the core. */
4
+ import { defineComponent, ref, unref, isVNode, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, normalizeClass, Fragment, Comment, Text } from 'vue';
5
+ import * as M from '@gecko-ui-kit/motion';
6
+ import { h, B, ANY, anyProps, enhancer, same, changedOnly, vnodeKey } from './core.js';
7
+
8
+ /* ---------------- Rise ---------------- */
9
+
10
+ const KINDS = ['rise', 'slide', 'deal', 'reveal'];
11
+ /**
12
+ * <Rise class="gk-grid"><BikeCard v-for="b in bikes" :key="b.id" :bike="b" /></Rise>
13
+ * <Rise kind="slide" :x="-24" :stagger="0.04">…rows…</Rise> <Rise kind="deal" :tilt="4">…cards…</Rise>
14
+ * Same as rise / slide / deal(els, options): the children arrive one after another when it mounts, and children
15
+ * added later arrive the same way (animate-new, on by default). kind="reveal" is the small fade-down of
16
+ * reveal(el, y) on the element itself (an accordion body). items: a selector inside instead of the direct children.
17
+ * A template ref on it is the component; its element is `.$el`.
18
+ */
19
+ export const Rise = defineComponent({
20
+ name: 'Rise',
21
+ props: {
22
+ as: String, kind: { type: String, default: 'rise' }, items: String,
23
+ ...anyProps('y', 'x', 'stagger', 'delay', 'duration', 'tilt'),
24
+ animateNew: { type: Boolean, default: true },
25
+ },
26
+ setup(props, { slots }) {
27
+ const el = ref(null);
28
+ let seen = null, first = [];
29
+ const kind = () => (KINDS.includes(props.kind) ? props.kind : 'rise');
30
+ const targets = (node) => {
31
+ if (!node) return [];
32
+ if (props.items) return [...node.querySelectorAll(props.items)];
33
+ return kind() === 'reveal' ? [node] : [...node.children];
34
+ };
35
+ const play = (els, isFirst) => {
36
+ const { y, x, stagger, delay, duration, tilt } = props;
37
+ const k = kind(), d = isFirst ? delay : 0;
38
+ if (k === 'slide') M.slide(els, { x, y, stagger, delay: d, duration });
39
+ else if (k === 'deal') M.deal(els, { stagger, delay: d, tilt });
40
+ else if (k === 'reveal') els.forEach((e) => M.reveal(e, y));
41
+ else M.rise(els, { y, stagger, delay: d, duration });
42
+ };
43
+ onMounted(() => {
44
+ first = targets(el.value);
45
+ seen = new WeakSet(first);
46
+ play(first, true);
47
+ });
48
+ // children added later (a new card in the list) arrive the same way; the ones already here stay put
49
+ onUpdated(() => {
50
+ if (!seen || !props.animateNew) return;
51
+ const fresh = targets(el.value).filter((t) => !seen.has(t));
52
+ fresh.forEach((t) => seen.add(t));
53
+ if (fresh.length) play(fresh, false);
54
+ });
55
+ onBeforeUnmount(() => {
56
+ seen = null;
57
+ const now = [...new Set([...first, ...targets(el.value)])];
58
+ first = [];
59
+ if (now.length) { M.g.killTweensOf(now); M.g.set(now, { clearProps: 'transform,opacity' }); }
60
+ });
61
+ return () => h(props.as || 'div', { ref: el }, slots.default?.());
62
+ },
63
+ });
64
+
65
+ /* ---------------- Magnetic & Lift ---------------- */
66
+
67
+ const MagneticMotion = enhancer('MagneticMotion', (el, o) => M.magnetic(el, o), { tag: 'span', props: anyProps('strength', 'strengthY'), class: 'gk-magnetic' });
68
+ /**
69
+ * <Magnetic><Button variant="primary" icon-right="arrowR">Book a bike</Button></Magnetic>
70
+ * Same as magnetic(el, { strength, strengthY }): the element leans toward the pointer and springs back
71
+ * (mouse / trackpad only). magnet multiplies the pull (data-magnet). as="a" makes the element itself magnetic.
72
+ */
73
+ export const Magnetic = defineComponent({
74
+ name: 'Magnetic',
75
+ props: { ...anyProps('strength', 'strengthY', 'magnet'), as: String },
76
+ setup(props, { slots }) {
77
+ return () => h(MagneticMotion, {
78
+ strength: props.strength, strengthY: props.strengthY, as: props.as, 'data-magnet': props.magnet,
79
+ style: props.as ? undefined : { display: 'inline-block' },
80
+ }, slots);
81
+ },
82
+ });
83
+
84
+ const LiftMotion = enhancer('LiftMotion', (el, o) => M.lift(el, o.y), { props: anyProps('y') });
85
+ /**
86
+ * <Lift as="article" :y="-6" class="gk-card">…</Lift>
87
+ * Same as lift(el, y): the element rises a few pixels while the pointer is over it (mouse / trackpad only).
88
+ */
89
+ export const Lift = defineComponent({
90
+ name: 'Lift',
91
+ props: { y: ANY, as: String },
92
+ setup(props, { slots }) {
93
+ return () => h(LiftMotion, { y: props.y, as: props.as, 'data-lift': props.y != null ? String(props.y) : '' }, slots);
94
+ },
95
+ });
96
+
97
+ /* ---------------- Tilt ---------------- */
98
+ /**
99
+ * <Tilt :max="12" glare class="gk-card"><img data-depth="40" src="lamp.png" alt="" /><h3 data-depth="20">Lamp</h3></Tilt>
100
+ * Same as tilt(el, options). Children with data-depth float above the card. as="article" changes the element.
101
+ */
102
+ export const Tilt = enhancer('Tilt', (el, o) => M.tilt(el, o), {
103
+ props: { ...anyProps('max', 'scale', 'perspective', 'parallax', 'follow', 'spring'), glare: B },
104
+ class: 'gk-tilt', rerunOnChildren: true,
105
+ });
106
+
107
+ /* ---------------- Spotlight ---------------- */
108
+
109
+ const SpotlightMotion = enhancer('SpotlightMotion', (el, o) => M.spotlight(el, o), {
110
+ props: { ...anyProps('items', 'size', 'follow'), border: B, fill: B, focus: B }, rerunOnChildren: true,
111
+ });
112
+ /**
113
+ * <Spotlight class="plans" :size="300"><div v-for="p in plans" :key="p.id" class="gk-card">…</div></Spotlight>
114
+ * Same as spotlight(group, options): a soft light follows the pointer across the cards (its children, or `items`)
115
+ * and lights up their edges; a card with keyboard focus pulls the light onto itself.
116
+ */
117
+ export const Spotlight = defineComponent({
118
+ name: 'Spotlight',
119
+ props: { ...anyProps('items', 'size', 'follow'), border: B, fill: B, focus: B, as: String },
120
+ setup(props, { slots }) {
121
+ return () => h(SpotlightMotion, {
122
+ items: props.items, size: props.size, follow: props.follow, border: props.border, fill: props.fill, focus: props.focus, as: props.as,
123
+ class: ['gk-spotlight', props.border === false && 'gk-spotlight--no-ring', props.fill === false && 'gk-spotlight--no-fill'],
124
+ }, slots);
125
+ },
126
+ });
127
+
128
+ /* ---------------- Marquee ---------------- */
129
+
130
+ const MQ_SET = ['speed', 'direction', 'gap', 'fade'];
131
+ const MQ_KEYS = [...MQ_SET, 'pauseOnHover', 'button', 'words', 'paused'];
132
+ /** An element from an element, a template ref, a component (its $el) or a selector (kept as text). */
133
+ const resolve = (x) => { x = unref(x); return (x && typeof x === 'object' && '$el' in x ? x.$el : x) ?? null; };
134
+ /** Slot content as a flat list of items: fragments (v-for) opened, comments and blank text left out. */
135
+ const flat = (nodes) => (nodes == null ? [] : (Array.isArray(nodes) ? nodes : [nodes])).flatMap((n) => (
136
+ !isVNode(n) ? (n == null || typeof n === 'boolean' || n === '' ? [] : [n])
137
+ : n.type === Fragment ? flat(n.children)
138
+ : n.type === Comment ? []
139
+ : n.type === Text && !String(n.children).trim() ? [] : [n]));
140
+ /* A fingerprint of the items, so the copies are measured again when they change. */
141
+ const keyOf = (items) => items.map((c) => (isVNode(c) ? `${String(c.key ?? '')}<${vnodeKey(c)}>` : String(c))).join('\u0001');
142
+
143
+ /**
144
+ * <Marquee aria-label="Our partners" :speed="50" :gap="48"><img v-for="l in logos" :key="l.id" :src="l.src" :alt="l.name" /></Marquee>
145
+ * Same as marquee(el, options): an endless strip; each child becomes one item (an <li>). Hover or focus slows it
146
+ * to a stop; `paused` stops it; button (element, template ref or selector) gets play/pause wiring. speed,
147
+ * direction, gap and fade change in place; new items are measured again.
148
+ * Template ref → { play(), pause(), toggle(), paused, refresh(), el }.
149
+ */
150
+ export const Marquee = defineComponent({
151
+ name: 'Marquee',
152
+ props: {
153
+ as: String, listAs: { type: String, default: 'ul' },
154
+ ...anyProps('speed', 'direction', 'gap', 'button', 'words'), pauseOnHover: B, fade: B, paused: B,
155
+ },
156
+ setup(props, { slots, attrs, expose }) {
157
+ const el = ref(null);
158
+ let cur = null, items = '', lastClass = '', kept = null;
159
+ const read = () => {
160
+ const o = {};
161
+ for (const k of MQ_KEYS) { const v = k === 'button' ? resolve(props.button) : props[k]; if (v !== undefined && v !== null) o[k] = v; }
162
+ return o;
163
+ };
164
+ expose(new Proxy({}, {
165
+ get(t, k) {
166
+ if (k in t) return t[k]; // Vue's own marks (__v_skip)
167
+ if (k === 'el') return el.value;
168
+ const m = cur?.m; if (!m) return undefined;
169
+ const v = m[k]; return typeof v === 'function' ? v.bind(m) : v;
170
+ },
171
+ has(t, k) { return k in t || k === 'el' || (!!cur?.m && k in cur.m); },
172
+ }));
173
+
174
+ const start = () => {
175
+ const o = read();
176
+ const { paused, ...rest } = o;
177
+ const m = M.marquee(el.value, rest);
178
+ if (paused) m.pause();
179
+ cur = { m, o, items };
180
+ };
181
+ const end = () => { const c = cur; cur = null; c?.m.destroy(); };
182
+
183
+ onMounted(start);
184
+ // Vue rewrites the whole class attribute when it changes, which would drop classes marquee() added (is-static)
185
+ onBeforeUpdate(() => {
186
+ const node = el.value; if (!node) return;
187
+ const was = new Set(lastClass.split(/\s+/));
188
+ kept = [...node.classList].filter((c) => !was.has(c));
189
+ });
190
+ onUpdated(() => {
191
+ if (kept?.length) el.value?.classList.add(...kept);
192
+ kept = null;
193
+ const c = cur; if (!c) return;
194
+ const next = read(), prev = c.o;
195
+ if (!same(next, prev)) {
196
+ if (!changedOnly(next, prev, [...MQ_SET, 'paused'])) { end(); start(); return; }
197
+ if (MQ_SET.some((k) => !same(next[k], prev[k]))) c.m.set({ speed: 50, direction: 'left', fade: true, ...Object.fromEntries(MQ_SET.filter((k) => next[k] !== undefined).map((k) => [k, next[k]])) });
198
+ if (!!next.paused !== !!prev.paused) (next.paused ? c.m.pause() : c.m.play());
199
+ c.o = next;
200
+ }
201
+ if (c.items !== items) { c.items = items; c.m.refresh(); }
202
+ });
203
+ onBeforeUnmount(end);
204
+
205
+ return () => {
206
+ const list = flat(slots.default?.());
207
+ items = keyOf(list);
208
+ const own = ['gk-marquee', props.fade !== false && 'gk-marquee--fade'];
209
+ lastClass = String(normalizeClass([own, attrs.class]) || '');
210
+ const gap = props.gap != null ? { '--gk-marquee-gap': typeof props.gap === 'number' ? `${props.gap}px` : props.gap } : null;
211
+ const item = props.listAs === 'ul' || props.listAs === 'ol' ? 'li' : 'div';
212
+ return h(props.as || 'div', { ref: el, class: own, style: gap },
213
+ [h(props.listAs || 'ul', { class: 'gk-marquee__list' }, list.map((c, i) => h(item, { key: isVNode(c) && c.key != null ? c.key : '_' + i }, [c])))]);
214
+ };
215
+ },
216
+ });