@gecko-ui-kit/motion 0.0.0-stage → 0.8.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 +73 -2
- package/index.js +25 -0
- package/motion.css +137 -0
- package/package.json +38 -4
- package/src/bin.js +96 -0
- package/src/burger.js +123 -0
- package/src/charts.js +37 -0
- package/src/confetti.js +166 -0
- package/src/core.js +26 -0
- package/src/count.js +21 -0
- package/src/drag.js +71 -0
- package/src/edge.js +127 -0
- package/src/enter.js +24 -0
- package/src/feedback.js +25 -0
- package/src/fly.js +88 -0
- package/src/hold.js +169 -0
- package/src/hover.js +26 -0
- package/src/indicator.js +57 -0
- package/src/liquid.js +130 -0
- package/src/odometer.js +169 -0
- package/src/overlay.js +12 -0
- package/src/search.js +134 -0
- package/src/swap.js +24 -0
- package/src/text.js +28 -0
- package/src/theme.js +29 -0
package/src/search.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/* Search that opens out of its own icon. A round magnifier button stretches into a field with a
|
|
2
|
+
little overshoot, the magnifier swings like it is looking around, the placeholder types itself
|
|
3
|
+
in and a close × draws its two strokes. Closing runs it backwards and the lens gives a nod.
|
|
4
|
+
Escape clears, then closes; clicking away from an empty field closes it. */
|
|
5
|
+
import { g, RM, $$, esc } from './core.js';
|
|
6
|
+
|
|
7
|
+
const GLASS = `<svg class="xs__ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><g class="xs__glass"><circle cx="10.5" cy="10.5" r="6"/><path d="M15 15l4.5 4.5"/></g></svg>`;
|
|
8
|
+
const CROSS = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path class="xs__x" d="M7 7l10 10" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1"/><path class="xs__x" d="M17 7 7 17" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1"/></svg>`;
|
|
9
|
+
const LENS = '10.5 10.5';
|
|
10
|
+
|
|
11
|
+
let uid = 0;
|
|
12
|
+
/**
|
|
13
|
+
* searchField({ placeholder: 'Search…', label: 'Search', name: 'q', look: 'outline'|'quiet', size: 'md'|'sm' }) → HTML
|
|
14
|
+
* The markup expandSearch() opens and closes: a <form role="search"> with the icon button, the field and a close button.
|
|
15
|
+
*/
|
|
16
|
+
export const searchField = ({ placeholder = 'Search…', label = 'Search', name = 'q', look = 'outline', size = 'md' } = {}) => {
|
|
17
|
+
const id = `xs-${++uid}`;
|
|
18
|
+
const cls = ['xs', look === 'quiet' ? 'xs--quiet' : '', size === 'sm' ? 'xs--sm' : ''].filter(Boolean).join(' ');
|
|
19
|
+
return `<form class="${cls}" role="search" data-placeholder="${esc(placeholder)}"><button type="button" class="xs__btn" aria-label="${esc(label)}" aria-expanded="false" aria-controls="${id}">${GLASS}</button><input class="xs__input" id="${id}" type="search" name="${esc(name)}" aria-label="${esc(label)}" autocomplete="off" spellcheck="false" enterkeyhint="search" tabindex="-1"><button type="button" class="xs__close" aria-label="Close search" tabindex="-1">${CROSS}</button></form>`;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const editable = (t) => t && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* const s = expandSearch(el, { width: 260, placeholder, typing: true, autoClose: true, key: '/', onOpen, onClose, onInput, onSearch })
|
|
26
|
+
* el: a .xs form from searchField(), or any box (one is built inside it).
|
|
27
|
+
* width: open width in px, or any CSS width such as '100%'.
|
|
28
|
+
* typing: the placeholder types itself in. autoClose: close when focus leaves an empty field.
|
|
29
|
+
* key: a key that opens it from anywhere on the page (not while typing in another field). It listens on
|
|
30
|
+
* document and stops the key there, so it wins over a shortcut for the same key on window.
|
|
31
|
+
* onInput(value) runs on every keystroke, onSearch(value) on Enter or a click on the magnifier.
|
|
32
|
+
* Returns { el, input, opts, isOpen, open(), close(), toggle(), clear(), destroy() }.
|
|
33
|
+
*/
|
|
34
|
+
export const expandSearch = (el, opts = {}) => {
|
|
35
|
+
el = $$(el)[0];
|
|
36
|
+
if (!el) return null;
|
|
37
|
+
let box = el.matches('.xs') ? el : el.querySelector('.xs');
|
|
38
|
+
if (!box) { el.insertAdjacentHTML('beforeend', searchField(opts)); box = el.lastElementChild; }
|
|
39
|
+
if (box.__xs) { Object.assign(box.__xs.opts, opts); return box.__xs; }
|
|
40
|
+
const o = { width: 260, placeholder: box.dataset.placeholder || 'Search…', typing: true, autoClose: true, key: '', ...opts };
|
|
41
|
+
|
|
42
|
+
const btn = box.querySelector('.xs__btn'), input = box.querySelector('.xs__input'), shut = box.querySelector('.xs__close');
|
|
43
|
+
const glass = box.querySelector('.xs__glass'), xs = [...box.querySelectorAll('.xs__x')];
|
|
44
|
+
let isOpen = false, tl = null, typer = null, w0 = 0;
|
|
45
|
+
const target = () => (typeof o.width === 'number' ? `${o.width}px` : o.width);
|
|
46
|
+
const stop = () => { tl?.kill(); typer?.kill(); tl = typer = null; };
|
|
47
|
+
|
|
48
|
+
const typeIn = () => {
|
|
49
|
+
const ph = o.placeholder;
|
|
50
|
+
if (!o.typing || RM()) { input.placeholder = ph; return; }
|
|
51
|
+
const p = { n: 0 }; input.placeholder = '';
|
|
52
|
+
typer = g.to(p, { n: ph.length, duration: Math.min(1, ph.length * 0.045), ease: 'none', delay: 0.2, onUpdate: () => { input.placeholder = ph.slice(0, Math.round(p.n)); } });
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const api = {
|
|
56
|
+
el: box, input, opts: o,
|
|
57
|
+
get isOpen() { return isOpen; },
|
|
58
|
+
/** Stretch into a field and put the cursor in it. */
|
|
59
|
+
open() {
|
|
60
|
+
if (isOpen) { input.focus(); return g.timeline(); }
|
|
61
|
+
stop(); isOpen = true;
|
|
62
|
+
if (!box.classList.contains('is-closing')) w0 = box.offsetWidth;
|
|
63
|
+
box.classList.remove('is-closing'); box.classList.add('is-open');
|
|
64
|
+
btn.setAttribute('aria-expanded', 'true'); input.tabIndex = 0; shut.tabIndex = 0;
|
|
65
|
+
input.focus({ preventScroll: true }); // straight away, so phones show the keyboard
|
|
66
|
+
typeIn(); o.onOpen?.(api);
|
|
67
|
+
if (RM()) { g.set(box, { width: target() }); g.set(xs, { attr: { 'stroke-dashoffset': 0 } }); return g.timeline(); }
|
|
68
|
+
tl = g.timeline()
|
|
69
|
+
.to(box, { scale: 0.94, duration: 0.09, ease: 'power2.out' })
|
|
70
|
+
.to(box, { width: target(), scale: 1, duration: 0.6, ease: 'back.out(1.5)' })
|
|
71
|
+
.to(glass, { keyframes: { rotation: [0, -30, 14, -5, 0] }, svgOrigin: LENS, duration: 0.8, ease: 'power1.inOut' }, 0.05)
|
|
72
|
+
.fromTo(input, { opacity: 0, x: -10 }, { opacity: 1, x: 0, duration: 0.35, ease: 'power2.out', clearProps: 'opacity,transform' }, 0.2)
|
|
73
|
+
.to(xs, { attr: { 'stroke-dashoffset': 0 }, duration: 0.24, stagger: 0.09, ease: 'power2.out' }, 0.38);
|
|
74
|
+
return tl;
|
|
75
|
+
},
|
|
76
|
+
/** Shrink back to the round button. */
|
|
77
|
+
close() {
|
|
78
|
+
if (!isOpen) return g.timeline();
|
|
79
|
+
stop(); isOpen = false;
|
|
80
|
+
const hadFocus = box.contains(document.activeElement);
|
|
81
|
+
btn.setAttribute('aria-expanded', 'false'); input.tabIndex = -1; shut.tabIndex = -1;
|
|
82
|
+
box.classList.add('is-closing');
|
|
83
|
+
const end = () => {
|
|
84
|
+
box.classList.remove('is-open', 'is-closing'); input.placeholder = '';
|
|
85
|
+
g.set(box, { clearProps: 'width,transform' }); g.set(input, { clearProps: 'opacity,transform' });
|
|
86
|
+
if (hadFocus && !isOpen) btn.focus({ preventScroll: true });
|
|
87
|
+
o.onClose?.(api);
|
|
88
|
+
};
|
|
89
|
+
if (RM()) { g.set(xs, { attr: { 'stroke-dashoffset': 1 } }); end(); return g.timeline(); }
|
|
90
|
+
tl = g.timeline({ onComplete: end })
|
|
91
|
+
.to(xs, { attr: { 'stroke-dashoffset': 1 }, duration: 0.16, stagger: 0.05, ease: 'power2.in' })
|
|
92
|
+
.to(input, { opacity: 0, x: -8, duration: 0.18, ease: 'power2.in' }, 0)
|
|
93
|
+
.to(box, { width: w0, duration: 0.45, ease: 'power3.inOut' }, 0.08)
|
|
94
|
+
.to(glass, { keyframes: { scale: [1, 0.78, 1.1, 1] }, svgOrigin: LENS, duration: 0.45, ease: 'power2.out' }, 0.38);
|
|
95
|
+
return tl;
|
|
96
|
+
},
|
|
97
|
+
toggle() { return isOpen ? api.close() : api.open(); },
|
|
98
|
+
/** Empty the field (the text slides away) and tell onInput. */
|
|
99
|
+
clear() {
|
|
100
|
+
if (!input.value) return api;
|
|
101
|
+
const done = () => { input.value = ''; o.onInput?.('', api); };
|
|
102
|
+
if (RM()) done(); else g.to(input, { opacity: 0, x: 10, duration: 0.15, ease: 'power2.in', onComplete: () => { done(); g.fromTo(input, { opacity: 0, x: -6 }, { opacity: 1, x: 0, duration: 0.2, clearProps: 'opacity,transform' }); } });
|
|
103
|
+
return api;
|
|
104
|
+
},
|
|
105
|
+
destroy() { stop(); ac.abort(); delete box.__xs; },
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const search = () => {
|
|
109
|
+
o.onSearch?.(input.value, api);
|
|
110
|
+
if (!RM()) g.fromTo(glass, { scale: 1 }, { keyframes: { scale: [1, 1.25, 1] }, svgOrigin: LENS, duration: 0.35, ease: 'power2.out' });
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const ac = new AbortController(), on = { signal: ac.signal };
|
|
114
|
+
btn.addEventListener('click', () => { if (!isOpen) api.open(); else if (input.value.trim()) search(); else api.close(); }, on);
|
|
115
|
+
shut.addEventListener('click', () => { if (input.value) { input.value = ''; o.onInput?.('', api); } api.close(); }, on);
|
|
116
|
+
box.addEventListener('submit', (e) => { e.preventDefault(); search(); }, on);
|
|
117
|
+
input.addEventListener('input', () => o.onInput?.(input.value, api), on);
|
|
118
|
+
box.addEventListener('keydown', (e) => {
|
|
119
|
+
if (e.key !== 'Escape' || !isOpen) return;
|
|
120
|
+
e.preventDefault(); e.stopPropagation();
|
|
121
|
+
if (input.value) api.clear(); else api.close();
|
|
122
|
+
}, on);
|
|
123
|
+
box.addEventListener('focusout', (e) => {
|
|
124
|
+
if (o.autoClose && isOpen && !input.value && !box.contains(e.relatedTarget)) api.close();
|
|
125
|
+
}, on);
|
|
126
|
+
document.addEventListener('keydown', (e) => {
|
|
127
|
+
if (!o.key || e.key !== o.key || e.metaKey || e.ctrlKey || e.altKey || editable(e.target) || !box.isConnected) return;
|
|
128
|
+
e.preventDefault(); e.stopPropagation(); // the key is ours: don't let a page-wide shortcut on window take it too
|
|
129
|
+
api.open();
|
|
130
|
+
}, on);
|
|
131
|
+
|
|
132
|
+
box.__xs = api;
|
|
133
|
+
return api;
|
|
134
|
+
};
|
package/src/swap.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* Page swap: the old page leaves (cards fade + drop, header lifts, column nudges back),
|
|
2
|
+
the new page slides in from the direction of travel. dir = 1 forward, -1 back. */
|
|
3
|
+
import { g, RM, $$ } from './core.js';
|
|
4
|
+
|
|
5
|
+
export const swapTiming = { out: 0.18, in: 0.7, shift: 16, enter: 26 };
|
|
6
|
+
|
|
7
|
+
/** await swap(view, 1, () => { view.innerHTML = next }) */
|
|
8
|
+
export const swap = (view, dir, render, { leave = '.card, .kpi, .lane, [data-swap]', head = '.phead, .tabs, .toolbar, .chips' } = {}) => {
|
|
9
|
+
const T = swapTiming;
|
|
10
|
+
if (RM() || !view.children.length) { render(); return Promise.resolve(); }
|
|
11
|
+
if (view.__swapping) { view.__pending = render; return view.__swapping; }
|
|
12
|
+
const vh = innerHeight;
|
|
13
|
+
const cards = $$(leave, view).filter((e) => { const r = e.getBoundingClientRect(); return r.bottom > 0 && r.top < vh; });
|
|
14
|
+
g.to(cards, { opacity: 0, y: 8, scale: 0.985, duration: T.out, ease: 'power2.in', stagger: 0.008, overwrite: true });
|
|
15
|
+
g.to($$(head, view), { opacity: 0, y: -6, duration: T.out * 0.8, ease: 'power1.in' });
|
|
16
|
+
view.__swapping = new Promise((res) => {
|
|
17
|
+
g.to(view, { x: -dir * T.shift, duration: T.out, ease: 'power2.in', overwrite: true, onComplete: () => {
|
|
18
|
+
const r = view.__pending || render; view.__pending = null; r();
|
|
19
|
+
g.fromTo(view, { x: dir * T.enter }, { x: 0, duration: T.in, ease: 'expo.out', overwrite: true, clearProps: 'transform' });
|
|
20
|
+
view.__swapping = null; res();
|
|
21
|
+
} });
|
|
22
|
+
});
|
|
23
|
+
return view.__swapping;
|
|
24
|
+
};
|
package/src/text.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* Text reveal: split a heading into characters, roll them up with a slight tilt */
|
|
2
|
+
import { g, D, $$, esc } from './core.js';
|
|
3
|
+
|
|
4
|
+
export const split = (el) => {
|
|
5
|
+
const t = el.dataset.text ?? el.textContent ?? '';
|
|
6
|
+
if (el.dataset.split === t) return $$('.ch', el);
|
|
7
|
+
el.setAttribute('aria-label', t);
|
|
8
|
+
el.innerHTML = t.trim().split(' ').map((w) =>
|
|
9
|
+
`<span class="word" aria-hidden="true">${[...w].map((c) => `<span class="ch">${esc(c)}</span>`).join('')}</span>`).join(' ');
|
|
10
|
+
el.dataset.split = t;
|
|
11
|
+
return $$('.ch', el);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** title(h1, { delay, stagger, tilt }) */
|
|
15
|
+
export const title = (el, { delay = 0, stagger = 0.022, tilt = 6, duration = 0.85 } = {}) => {
|
|
16
|
+
if (!el) return g.timeline();
|
|
17
|
+
const chars = split(el);
|
|
18
|
+
return g.fromTo(chars, { yPercent: 115, rotation: tilt },
|
|
19
|
+
{ yPercent: 0, rotation: 0, duration: D(duration), ease: 'power4.out', stagger: D(stagger), delay: D(delay), clearProps: 'transform' });
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Change the text and play the reveal again. */
|
|
23
|
+
export const retitle = (el, text, opts) => {
|
|
24
|
+
if (!el) return g.timeline();
|
|
25
|
+
el.dataset.text = text;
|
|
26
|
+
delete el.dataset.split; // force a fresh split, even when the text is unchanged
|
|
27
|
+
return title(el, opts);
|
|
28
|
+
};
|
package/src/theme.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* Theme switch with a circular reveal that grows from the button (View Transitions API).
|
|
2
|
+
Falls back to an instant switch where the API is missing or motion is reduced. */
|
|
3
|
+
import { RM } from './core.js';
|
|
4
|
+
|
|
5
|
+
export const getTheme = () => (document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light');
|
|
6
|
+
|
|
7
|
+
/** theme('dark', buttonEl, { storageKey: 'theme', duration: 450 }) */
|
|
8
|
+
export const theme = (next, from, { storageKey = 'tandem:theme', duration = 450, easing = 'cubic-bezier(.65,0,.35,1)' } = {}) => {
|
|
9
|
+
const root = document.documentElement;
|
|
10
|
+
if (getTheme() === next) return Promise.resolve();
|
|
11
|
+
const apply = () => {
|
|
12
|
+
root.dataset.theme = next;
|
|
13
|
+
try { if (storageKey) localStorage.setItem(storageKey, next); } catch {}
|
|
14
|
+
window.dispatchEvent(new CustomEvent('themechange', { detail: next }));
|
|
15
|
+
};
|
|
16
|
+
if (!document.startViewTransition || RM()) { apply(); return Promise.resolve(); }
|
|
17
|
+
const r = from?.getBoundingClientRect();
|
|
18
|
+
const x = r ? r.left + r.width / 2 : innerWidth - 40, y = r ? r.top + r.height / 2 : 40;
|
|
19
|
+
const rad = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
|
|
20
|
+
const vt = document.startViewTransition(apply);
|
|
21
|
+
vt.ready.then(() => {
|
|
22
|
+
root.animate({ clipPath: [`circle(0px at ${x}px ${y}px)`, `circle(${rad}px at ${x}px ${y}px)`] },
|
|
23
|
+
{ duration, easing, pseudoElement: '::view-transition-new(root)' });
|
|
24
|
+
}).catch(() => {});
|
|
25
|
+
return vt.finished.catch(() => {});
|
|
26
|
+
};
|
|
27
|
+
export const toggleTheme = (from, opts) => theme(getTheme() === 'dark' ? 'light' : 'dark', from, opts);
|
|
28
|
+
/** Apply the saved theme on load (call early to avoid a flash). */
|
|
29
|
+
export const restoreTheme = (storageKey = 'tandem:theme') => { try { const t = localStorage.getItem(storageKey); if (t) document.documentElement.dataset.theme = t; } catch {} };
|