@gecko-ui-kit/react 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/src/text.d.ts ADDED
@@ -0,0 +1,316 @@
1
+ import type * as React from 'react';
2
+ import type { EnhancerProps } from './core.js';
3
+
4
+ /** Props of the element a text piece renders (any prop of the element, plus `as`). */
5
+ type ElementProps = Omit<React.HTMLAttributes<HTMLElement>, 'children' | 'onChange'> & {
6
+ /** Element to render (default 'span'; 'h1', 'p'…). */
7
+ as?: React.ElementType;
8
+ };
9
+
10
+ /* ---------------- TextReveal ---------------- */
11
+
12
+ export interface TextRevealProps extends ElementProps {
13
+ /** The text (a string). A new text plays again in place (retitle). */
14
+ children?: string | number | (string | number)[];
15
+ /** The text, instead of children. */
16
+ text?: string;
17
+ /** Seconds before the first letter (0). */
18
+ delay?: number;
19
+ /** Seconds between letters (0.022). */
20
+ stagger?: number;
21
+ /** Degrees each letter starts tilted (6). */
22
+ tilt?: number;
23
+ /** Seconds one letter takes (0.85). */
24
+ duration?: number;
25
+ }
26
+ /** <TextReveal as="h1">Bikes for every trip</TextReveal> — the letters roll up out of a mask (title()). ref → the element. */
27
+ export const TextReveal: React.ForwardRefExoticComponent<TextRevealProps & React.RefAttributes<HTMLElement>>;
28
+
29
+ /* ---------------- Typewriter ---------------- */
30
+
31
+ export interface TypewriterOptions {
32
+ /** The phrases, typed in turn (default: the children's text as one phrase). A new list is typed in place. */
33
+ phrases?: string[];
34
+ /** Seconds per letter while typing (0.06). */
35
+ speed?: number;
36
+ /** 0 = even rhythm, 1 = very uneven (0.6). */
37
+ jitter?: number;
38
+ /** Seconds per letter while deleting (0.03). */
39
+ eraseSpeed?: number;
40
+ /** Delete letter by letter, or select the phrase and clear it at once. */
41
+ erase?: 'backspace' | 'select';
42
+ /** Seconds a finished phrase stays before it is deleted (1.6). */
43
+ hold?: number;
44
+ /** Seconds of empty caret before the next phrase (0.35). */
45
+ gap?: number;
46
+ /** Seconds before the first letter (0.3). */
47
+ delay?: number;
48
+ /** false: stop on the last phrase (true). */
49
+ loop?: boolean;
50
+ /** The caret's look, or false for none ('bar'). */
51
+ caret?: 'bar' | 'block' | 'underscore' | false;
52
+ /** Pause while the element is off screen (true). */
53
+ whenVisible?: boolean;
54
+ /** Language for letter splitting and the screen-reader list (default: the page's lang). */
55
+ locale?: string;
56
+ /** What screen readers hear instead of the phrase list. */
57
+ label?: string;
58
+ /** A phrase has been typed out in full. */
59
+ onPhrase?: (index: number, text: string) => void;
60
+ /** Stop typing (true) and go on (false). */
61
+ paused?: boolean;
62
+ }
63
+ export interface TypewriterHandle {
64
+ /** Index of the phrase being typed. */
65
+ readonly phrase: number;
66
+ play(): TypewriterHandle;
67
+ pause(): TypewriterHandle;
68
+ /** Start again from the first phrase. */
69
+ restart(): TypewriterHandle;
70
+ /** Delete the current phrase and type these. */
71
+ set(phrases: string[]): TypewriterHandle;
72
+ readonly el: HTMLElement;
73
+ }
74
+ export interface TypewriterProps extends ElementProps, TypewriterOptions {
75
+ /** One phrase as text, when `phrases` is not given. */
76
+ children?: string | number | (string | number)[];
77
+ }
78
+ /** <Typewriter phrases={['scooters', 'e-bikes']} /> — text typed letter by letter behind a caret. ref → the handle. */
79
+ export const Typewriter: React.ForwardRefExoticComponent<TypewriterProps & React.RefAttributes<TypewriterHandle>>;
80
+
81
+ /* ---------------- Rotator ---------------- */
82
+
83
+ export interface RotatorOptions {
84
+ /** The words, in turn. A new list starts again from the first word. */
85
+ words: string[];
86
+ /** Seconds each word stays (2.2). */
87
+ every?: number;
88
+ /** How the word changes ('roll'). */
89
+ effect?: 'roll' | 'drop' | 'slide' | 'blur' | 'letters';
90
+ /** Seconds the change takes (0.6). */
91
+ duration?: number;
92
+ /** GSAP ease of the landing ('back.out(1.6)'). */
93
+ ease?: string;
94
+ /** 'letters' only: seconds between letters (0.03). */
95
+ stagger?: number;
96
+ /** Change by itself (true); false: only through `index` or the handle. */
97
+ auto?: boolean;
98
+ /** Pause while off screen (true). */
99
+ whenVisible?: boolean;
100
+ /** Language of the screen-reader list. */
101
+ locale?: string;
102
+ /** What screen readers hear instead of the word list. */
103
+ label?: string;
104
+ /** The word changed. */
105
+ onChange?: (index: number, word: string) => void;
106
+ /** Show this word (changes to it, animated). */
107
+ index?: number;
108
+ /** Stop changing (true) and go on (false). */
109
+ paused?: boolean;
110
+ /** A soft highlight box behind the word (class rot--pill). */
111
+ pill?: boolean;
112
+ }
113
+ export interface RotatorHandle {
114
+ readonly index: number;
115
+ next(): RotatorHandle;
116
+ go(index: number): RotatorHandle;
117
+ play(): RotatorHandle;
118
+ pause(): RotatorHandle;
119
+ set(words: string[]): RotatorHandle;
120
+ readonly el: HTMLElement;
121
+ }
122
+ export interface RotatorProps extends ElementProps, RotatorOptions {}
123
+ /** <Rotator words={['scooter', 'e-bike']} pill /> — one word in a sentence keeps changing. ref → the handle. */
124
+ export const Rotator: React.ForwardRefExoticComponent<RotatorProps & React.RefAttributes<RotatorHandle>>;
125
+
126
+ /* ---------------- Shine ---------------- */
127
+
128
+ export interface ShineOptions {
129
+ /** 'gradient' (brand gradient letters, white glint), 'ink' (grey letters, ink band) or 'color' (own colour). */
130
+ look?: 'gradient' | 'ink' | 'color';
131
+ /** 'loop' forever (paused off screen), 'view' once each time it scrolls into view, 'hover' on pointer / focus. */
132
+ trigger?: 'loop' | 'view' | 'hover';
133
+ /** Seconds one sweep takes (1.4). */
134
+ duration?: number;
135
+ /** 'loop': seconds of rest between sweeps (1.8). */
136
+ repeatDelay?: number;
137
+ /** Seconds before the first sweep (0). */
138
+ delay?: number;
139
+ /** GSAP ease ('power2.inOut'). */
140
+ ease?: string;
141
+ /** Slant of the band in degrees (110). */
142
+ angle?: number;
143
+ /** Band width, percent of the sweep layer (12). */
144
+ width?: number;
145
+ /** Colour of the band (default: from the look). */
146
+ color?: string;
147
+ }
148
+ /** <Shine as="h2">Fresh roast</Shine> — a band of light sweeps across the letters. ref → the element. */
149
+ export const Shine: React.ForwardRefExoticComponent<EnhancerProps<ShineOptions> & React.RefAttributes<HTMLElement>>;
150
+
151
+ /* ---------------- Highlighter ---------------- */
152
+
153
+ export interface HighlighterOptions {
154
+ /** Which words inside get the stroke ('mark'). */
155
+ selector?: string;
156
+ /** Behind the words, or a thick stroke under them ('marker'). */
157
+ look?: 'marker' | 'underline';
158
+ /** Theme colour ('accent'). */
159
+ tone?: 'accent' | 'amber' | 'green' | 'rose' | 'sky' | 'violet';
160
+ /** Any see-through CSS colour instead of a tone. */
161
+ color?: string;
162
+ /** When it scrolls into view, straight away, or only through `active` ('view'). */
163
+ trigger?: 'view' | 'now' | 'manual';
164
+ /** 'view': how much of the element must be on screen, 0–1 (0.6). */
165
+ threshold?: number;
166
+ /** 'view': false draws again every time it comes back (true). */
167
+ once?: boolean;
168
+ /** Seconds one stroke takes (0.8). */
169
+ duration?: number;
170
+ /** Seconds between strokes (0.35). */
171
+ stagger?: number;
172
+ /** Seconds before the first stroke (0). */
173
+ delay?: number;
174
+ /** GSAP ease ('power2.inOut'). */
175
+ ease?: string;
176
+ /** Draw the strokes now (true) or clear them (false); with trigger 'manual'. */
177
+ active?: boolean;
178
+ }
179
+ /** <Highlighter>Helmets are <mark>free</mark></Highlighter> — marker strokes behind the <mark> words. ref → the element. */
180
+ export const Highlighter: React.ForwardRefExoticComponent<EnhancerProps<HighlighterOptions> & React.RefAttributes<HTMLElement>>;
181
+
182
+ /* ---------------- ScrollReveal ---------------- */
183
+
184
+ /** An element, a ref to one, or a selector. */
185
+ export type ElementLike = Element | string | React.RefObject<Element | null> | null;
186
+
187
+ export interface ScrollRevealOptions {
188
+ /** How it arrives ('rise'). */
189
+ effect?: 'fade' | 'rise' | 'slide' | 'zoom' | 'blur';
190
+ /** Split each target into words that arrive one after another. */
191
+ words?: boolean;
192
+ /** px to rise from (28). */
193
+ y?: number;
194
+ /** px to slide from; below 0 = from the left (-32). */
195
+ x?: number;
196
+ /** 'zoom' start size (0.94). */
197
+ scale?: number;
198
+ /** 'blur' start blur in px (10). */
199
+ blur?: number;
200
+ /** Seconds per block (0.8) or per word (0.7). */
201
+ duration?: number;
202
+ /** Seconds between blocks that enter together (0.08). */
203
+ stagger?: number;
204
+ /** Seconds between words (0.04). */
205
+ wordStagger?: number;
206
+ /** Seconds before it starts (0). */
207
+ delay?: number;
208
+ /** GSAP ease ('expo.out' for blocks, 'power3.out' for words). */
209
+ ease?: string;
210
+ /** false: hide again on leaving, play every time (true). */
211
+ once?: boolean;
212
+ /** How much of a block must be visible before it plays, 0–1 (0.2). */
213
+ threshold?: number;
214
+ /** IntersectionObserver rootMargin ('0px 0px -6% 0px'). */
215
+ rootMargin?: string;
216
+ /** The scrolling box (default the page). */
217
+ root?: ElementLike;
218
+ /** A block was revealed. */
219
+ onEnter?: (el: Element) => void;
220
+ /** A block was hidden again (once: false). */
221
+ onLeave?: (el: Element) => void;
222
+ }
223
+ export interface ScrollRevealProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onChange'>, ScrollRevealOptions {
224
+ /** Element to render ('div'). */
225
+ as?: React.ElementType;
226
+ /** A selector inside: reveal each match instead of the element itself (':scope > article'). */
227
+ items?: string;
228
+ children?: React.ReactNode;
229
+ }
230
+ /** <ScrollReveal effect="rise">…</ScrollReveal> — arrives when it scrolls into view. ref → the element. */
231
+ export const ScrollReveal: React.ForwardRefExoticComponent<ScrollRevealProps & React.RefAttributes<HTMLElement>>;
232
+
233
+ /* ---------------- BlurIn ---------------- */
234
+
235
+ export interface BlurInProps extends React.HTMLAttributes<HTMLElement> {
236
+ /** Element to render ('p'). */
237
+ as?: React.ElementType;
238
+ /** Start blur in px (10). */
239
+ blur?: number;
240
+ /** Start offset of each word, percent of a line (25). */
241
+ y?: number;
242
+ /** Seconds per word (0.7). */
243
+ duration?: number;
244
+ /** Seconds between words (0.04). */
245
+ stagger?: number;
246
+ /** Seconds before the first word (0). */
247
+ delay?: number;
248
+ /** GSAP ease ('power3.out'). */
249
+ ease?: string;
250
+ children?: React.ReactNode;
251
+ }
252
+ /** <BlurIn>Slow mornings…</BlurIn> — the words come into focus one after another, right away. ref → the element. */
253
+ export const BlurIn: React.ForwardRefExoticComponent<BlurInProps & React.RefAttributes<HTMLElement>>;
254
+
255
+ /* ---------------- ScrollProgress ---------------- */
256
+
257
+ export interface ScrollProgressProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'children'> {
258
+ /** The scrolling box (default the page). */
259
+ root?: ElementLike;
260
+ /** How far it has been read, 0…1. */
261
+ onChange?: (progress: number) => void;
262
+ }
263
+ /** <ScrollProgress /> — a thin line that fills as the page (or root) is read. ref → the element. */
264
+ export const ScrollProgress: React.ForwardRefExoticComponent<ScrollProgressProps & React.RefAttributes<HTMLDivElement>>;
265
+
266
+ /* ---------------- Odometer ---------------- */
267
+
268
+ export interface OdometerProps extends ElementProps {
269
+ /** The number (or a ready-made string). A new value rolls in place. */
270
+ value: number | string;
271
+ /** Intl.NumberFormat options, or a function that returns the text (default: grouped digits). */
272
+ format?: Intl.NumberFormatOptions | ((value: number) => string);
273
+ /** Locale for Intl (default: the browser's). */
274
+ locale?: string;
275
+ /** Seconds one column takes to roll (0.9). */
276
+ duration?: number;
277
+ /** Seconds between columns, from the right (0.06). */
278
+ stagger?: number;
279
+ /** 'auto': up when the number grows, down when it shrinks. */
280
+ direction?: 'auto' | 'up' | 'down';
281
+ /** GSAP ease ('back.out(1.15)'). */
282
+ ease?: string;
283
+ /** On mount, roll in from this value instead of just showing. */
284
+ from?: number | string;
285
+ }
286
+ /** <Odometer value={1990} format={{ style: 'currency', currency: 'THB' }} /> — digits roll like a meter. ref → the element. */
287
+ export const Odometer: React.ForwardRefExoticComponent<OdometerProps & React.RefAttributes<HTMLElement>>;
288
+
289
+ /* ---------------- CountUp ---------------- */
290
+
291
+ export interface CountUpProps extends ElementProps {
292
+ /** The number. A new value counts from the number on screen. */
293
+ value: number;
294
+ /** Count from this on mount (0). */
295
+ from?: number;
296
+ /** Seconds the count takes (1.2). */
297
+ duration?: number;
298
+ /** Seconds before it starts on mount (0). */
299
+ delay?: number;
300
+ /** Text before the number ('฿'). */
301
+ prefix?: string;
302
+ /** Text after the number (' / 5'). */
303
+ suffix?: string;
304
+ /** Digits after the point (0). */
305
+ decimals?: number;
306
+ /** Locale for the grouping ('en-US'). */
307
+ locale?: string;
308
+ /** Your own text for a value (instead of prefix / suffix / decimals). */
309
+ format?: (value: number) => string;
310
+ /** Tint green going up, red going down, when the value changes. */
311
+ tick?: boolean;
312
+ }
313
+ /** <CountUp value={48250} prefix="฿" /> — the number counts up (num() + count()). ref → the element. */
314
+ export const CountUp: React.ForwardRefExoticComponent<CountUpProps & React.RefAttributes<HTMLElement>>;
315
+
316
+ export {};
package/src/text.js ADDED
@@ -0,0 +1,354 @@
1
+ 'use client';
2
+ /* Text motion from @gecko-ui-kit/motion as React components: TextReveal, Typewriter, Rotator, Shine, Highlighter,
3
+ ScrollReveal, BlurIn, ScrollProgress, Odometer, CountUp.
4
+
5
+ Pieces that rewrite their own text (letters, typed text, rolling words and digits) draw their first state on the
6
+ server, then hand the inside of their element to the vanilla code: React never touches that inside again, and new
7
+ props reach the piece through its own handle (retitle, set, go, odometer(el, value)…), so the two never fight.
8
+ Pieces that split React children into words (ScrollReveal words, BlurIn) get a fresh element when the text
9
+ changes. Classes the vanilla code adds to the element (is-typing, is-armed…) survive a className change. */
10
+ import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
11
+ import * as M from '@gecko-ui-kit/motion';
12
+ import { h, cx, same, stop, changedOnly, useLatest, useIsoLayoutEffect, enhancer } from './core.js';
13
+
14
+ /* ---------------- small helpers ---------------- */
15
+
16
+ /** Options with every function swapped for a stable caller of the latest prop. */
17
+ const live = (o, latest) => {
18
+ const out = {};
19
+ for (const k in o) { const v = o[k]; out[k] = typeof v === 'function' ? (...a) => latest.current[k]?.(...a) : v; }
20
+ return out;
21
+ };
22
+ /** An element from a ref, an element or a selector. */
23
+ const resolve = (x) => (x && typeof x === 'object' && 'current' in x ? x.current : x) ?? null;
24
+ /** The text of children made of strings and numbers ('Hello', ['Hi ', name]). */
25
+ const textOf = (c) => (c == null || typeof c === 'boolean' ? '' : Array.isArray(c) ? c.map(textOf).join('') : typeof c === 'object' ? textOf(c.props?.children) : String(c));
26
+ /** A fingerprint of children (tags, text and simple props), to know when split words must be drawn again. */
27
+ const keyOf = (c) => {
28
+ if (c == null || typeof c === 'boolean') return '';
29
+ if (typeof c === 'string' || typeof c === 'number') return String(c);
30
+ if (Array.isArray(c)) return c.map(keyOf).join('\u0001');
31
+ if (typeof c === 'object' && c.props) {
32
+ const { children, ...p } = c.props;
33
+ const attrs = Object.keys(p).filter((k) => ['string', 'number', 'boolean'].includes(typeof p[k])).map((k) => `${k}=${p[k]}`).join(' ');
34
+ return `<${typeof c.type === 'string' ? c.type : c.type?.displayName || c.type?.name || 'C'} ${attrs}>${keyOf(children)}</>`;
35
+ }
36
+ return '?';
37
+ };
38
+ /** "a, b and c" (what screen readers hear for a list). The vanilla code writes its own on mount. */
39
+ const listText = (items, locale) => {
40
+ if (items.length < 2) return items[0] ?? '';
41
+ try { return new Intl.ListFormat(locale, { type: 'conjunction' }).format(items); } catch { return items.join(', '); }
42
+ };
43
+ /** Split props into [options, element props]; options left undefined keep the vanilla default. */
44
+ const part = (props, keys, defaults = {}) => {
45
+ const o = { ...defaults }, d = {};
46
+ for (const k in props) { if (keys.includes(k)) { if (props[k] !== undefined) o[k] = props[k]; } else d[k] = props[k]; }
47
+ return [o, d];
48
+ };
49
+
50
+ /** Classes the vanilla code added to the element (not ours), so a className change in React keeps them. */
51
+ function useKept(el, own, key = '') {
52
+ const mine = useRef({ own, key });
53
+ useIsoLayoutEffect(() => { mine.current = { own, key }; });
54
+ const node = el.current;
55
+ if (!node || mine.current.key !== key) return '';
56
+ const ours = new Set(String(mine.current.own || '').split(/\s+/));
57
+ return [...node.classList].filter((c) => !ours.has(c)).join(' ');
58
+ }
59
+
60
+ /** ref → the vanilla handle's methods and values plus `el` (asHandle), or → the element. */
61
+ function useRefTo(ref, el, cur, asHandle) {
62
+ const proxy = useRef(null);
63
+ if (asHandle && !proxy.current) {
64
+ proxy.current = new Proxy({}, {
65
+ get(_, k) {
66
+ if (k === 'el') return el.current;
67
+ const x = cur.current?.handle; if (x == null || typeof x !== 'object') return undefined;
68
+ const v = x[k]; return typeof v === 'function' ? v.bind(x) : v;
69
+ },
70
+ has(_, k) { return k === 'el' || (cur.current?.handle != null && k in Object(cur.current.handle)); },
71
+ });
72
+ }
73
+ useImperativeHandle(ref, () => (asHandle ? proxy.current : el.current), []);
74
+ }
75
+
76
+ /**
77
+ * A component whose vanilla motion owns the inside of its element.
78
+ * keys option prop names (the rest goes on the element)
79
+ * cls(o) classes the element always has (so the server HTML already looks right)
80
+ * kids(o) the first content, drawn on the server and kept as it is (React never redraws it)
81
+ * run(el, o) → handle · end(handle, el) clean up (default: stop) · update(handle, next, prev, el) → true when applied
82
+ * children(o, children) read options from children (e.g. the text)
83
+ */
84
+ function owned(displayName, spec) {
85
+ const { tag = 'span', keys, defaults, cls, kids, run, end = stop, update, handle: asHandle = false, children: fromKids } = spec;
86
+ const C = forwardRef(function GeckoText({ as, className, children, ...props }, ref) {
87
+ const [opts, dom] = part(props, keys, defaults);
88
+ fromKids?.(opts, children);
89
+ const latest = useLatest(opts);
90
+ const el = useRef(null), cur = useRef(null);
91
+ const own = cx(cls?.(opts), className);
92
+ const kept = useKept(el, own);
93
+ const [first] = useState(() => kids(opts));
94
+ useRefTo(ref, el, cur, asHandle);
95
+
96
+ const start = () => { const o = latest.current, node = el.current; cur.current = { el: node, opts: o, handle: run(node, live(o, latest)) ?? null }; };
97
+ const finish = () => { const c = cur.current; cur.current = null; if (c) end(c.handle, c.el); };
98
+ useIsoLayoutEffect(() => { start(); return finish; }, []);
99
+ useIsoLayoutEffect(() => {
100
+ const c = cur.current; if (!c) return;
101
+ const next = latest.current, prev = c.opts;
102
+ if (same(next, prev)) return;
103
+ if (update && update(c.handle, next, prev, c.el) === true) { c.opts = next; return; }
104
+ finish(); start();
105
+ });
106
+ return h(as || tag, { ref: el, ...dom, className: cx(own, kept) || undefined, suppressHydrationWarning: true }, first);
107
+ });
108
+ C.displayName = displayName;
109
+ return C;
110
+ }
111
+
112
+ /* ---------------- TextReveal ---------------- */
113
+
114
+ /**
115
+ * <TextReveal as="h1" stagger={0.022} tilt={6}>Bikes for every trip</TextReveal>
116
+ * Same as title(el, { delay, stagger, tilt, duration }): the letters roll up out of a mask. Children are the text
117
+ * (a string); a new text plays again in place (retitle), new options replay it. Screen readers hear the whole text.
118
+ */
119
+ export const TextReveal = owned('TextReveal', {
120
+ keys: ['text', 'delay', 'stagger', 'tilt', 'duration'],
121
+ children: (o, c) => { if (o.text === undefined) o.text = textOf(c); },
122
+ kids: (o) => o.text,
123
+ run: (el, o) => ({ tw: M.title(el, o) }),
124
+ end: (x) => x?.tw?.kill(),
125
+ update(x, next, prev, el) {
126
+ x.tw?.kill();
127
+ x.tw = next.text !== prev.text ? M.retitle(el, next.text, next) : M.title(el, next);
128
+ return true;
129
+ },
130
+ });
131
+
132
+ /* ---------------- Typewriter ---------------- */
133
+
134
+ const CARETS = ['bar', 'block', 'underscore'];
135
+ const caretOf = (c) => (c === undefined ? 'bar' : CARETS.includes(c) ? c : 'none');
136
+
137
+ /**
138
+ * <Typewriter phrases={['scooters', 'city bikes', 'e-bikes']} caret="bar" erase="select" /> or <Typewriter loop={false}>Booked!</Typewriter>
139
+ * Same as typewriter(el, options). New `phrases` delete the current one and type the new list (tw.set).
140
+ * paused stops and resumes it. ref → { phrase, play(), pause(), restart(), set(list), el }.
141
+ */
142
+ export const Typewriter = owned('Typewriter', {
143
+ keys: ['phrases', 'speed', 'jitter', 'eraseSpeed', 'erase', 'hold', 'gap', 'delay', 'loop', 'caret', 'whenVisible', 'locale', 'label', 'onPhrase', 'paused'],
144
+ handle: true,
145
+ children: (o, c) => { if (!o.phrases) { const t = textOf(c); o.phrases = t ? [t] : []; } },
146
+ cls: (o) => cx('tw', 'tw--' + caretOf(o.caret)),
147
+ kids: (o) => [
148
+ h('span', { key: 's', className: 'tw__sr', suppressHydrationWarning: true }, o.label ?? listText(o.phrases, o.locale)),
149
+ h('span', { key: 'v', className: 'tw__vis', 'aria-hidden': true }, h('span', { className: 'tw__text' }), h('i', { className: 'tw__caret' })),
150
+ ],
151
+ run: (el, { paused, ...o }) => { const t = M.typewriter(el, o); if (paused) t?.pause(); return t; },
152
+ update(t, next, prev) {
153
+ if (!t || !changedOnly(next, prev, ['phrases', 'paused'])) return false;
154
+ if (!same(next.phrases, prev.phrases)) t.set(next.phrases);
155
+ if (!!next.paused !== !!prev.paused) (next.paused ? t.pause() : t.play());
156
+ return true;
157
+ },
158
+ });
159
+
160
+ /* ---------------- Rotator ---------------- */
161
+
162
+ /**
163
+ * <h2>Rent a <Rotator words={['scooter', 'city bike', 'e-bike']} effect="roll" pill /> by the hour</h2>
164
+ * Same as rotator(el, options): one word keeps changing while its slot glides to the new width.
165
+ * New `words` start again from the first (r.set); `index` shows that word (r.go); paused stops it.
166
+ * onChange(i, word). ref → { index, next(), go(i), play(), pause(), set(words), el }.
167
+ */
168
+ export const Rotator = owned('Rotator', {
169
+ keys: ['words', 'every', 'effect', 'duration', 'ease', 'stagger', 'auto', 'whenVisible', 'locale', 'label', 'onChange', 'index', 'paused', 'pill'],
170
+ defaults: { words: [] },
171
+ handle: true,
172
+ cls: (o) => cx('rot', o.pill && 'rot--pill'),
173
+ kids: (o) => {
174
+ const w = o.words[o.index ?? 0] ?? o.words[0] ?? '';
175
+ return [
176
+ h('span', { key: 's', className: 'rot__sr', suppressHydrationWarning: true }, o.label ?? listText(o.words, o.locale)),
177
+ h('span', { key: 'b', className: 'rot__box', 'aria-hidden': true }, h('span', { className: 'rot__sizer' }, w), h('span', { className: 'rot__w' }, w)),
178
+ ];
179
+ },
180
+ run: (el, { index, paused, pill, ...o }) => {
181
+ const r = M.rotator(el, o);
182
+ if (r && index) r.go(index);
183
+ if (r && paused) r.pause();
184
+ return r;
185
+ },
186
+ update(r, next, prev) {
187
+ if (!r || !changedOnly(next, prev, ['words', 'index', 'paused', 'pill'])) return false;
188
+ if (!same(next.words, prev.words)) r.set(next.words);
189
+ if (next.index != null && next.index !== r.index) r.go(next.index);
190
+ if (!!next.paused !== !!prev.paused) (next.paused ? r.pause() : r.play());
191
+ return true;
192
+ },
193
+ });
194
+
195
+ /* ---------------- Shine ---------------- */
196
+
197
+ const SHINE_LOOKS = ['gradient', 'ink', 'color'];
198
+ const ShineMotion = enhancer('Shine', (el, o) => M.shine(el, o), {
199
+ tag: 'span',
200
+ options: ['look', 'trigger', 'duration', 'repeatDelay', 'delay', 'ease', 'angle', 'width', 'color'],
201
+ });
202
+ /**
203
+ * <Shine as="h2" look="gradient" trigger="loop">Ride the river path</Shine>
204
+ * Same as shine(el, options): a slanted band of light sweeps across the letters. Children are any text or nodes.
205
+ */
206
+ export const Shine = forwardRef(function Shine({ className, ...props }, ref) {
207
+ const look = SHINE_LOOKS.includes(props.look) ? props.look : 'gradient';
208
+ return h(ShineMotion, { ...props, ref, className: cx('shine', 'shine--' + look, className) });
209
+ });
210
+ Shine.displayName = 'Shine';
211
+
212
+ /* ---------------- Highlighter ---------------- */
213
+
214
+ const HL_KEYS = ['selector', 'look', 'tone', 'color', 'trigger', 'threshold', 'once', 'duration', 'stagger', 'delay', 'ease', 'active'];
215
+ /**
216
+ * <Highlighter as="p" tone="amber">Helmets are <mark>free with every rental</mark>.</Highlighter>
217
+ * Same as highlighter(el, options): the <mark> words inside get a marker stroke when they scroll into view.
218
+ * With trigger="manual", `active` draws the strokes (true) or clears them (false).
219
+ */
220
+ export const Highlighter = enhancer('Highlighter', (el, { active, ...o }) => {
221
+ const hl = M.highlighter(el, o);
222
+ if (hl && active && o.trigger === 'manual') hl.play();
223
+ return hl;
224
+ }, {
225
+ tag: 'p', options: HL_KEYS, rerunOnChildren: true,
226
+ update(hl, next, prev) {
227
+ if (!hl || !changedOnly(next, prev, ['active'])) return false;
228
+ if (next.active) hl.play(); else hl.reset();
229
+ return true;
230
+ },
231
+ });
232
+
233
+ /* ---------------- ScrollReveal ---------------- */
234
+
235
+ const SR_KEYS = ['effect', 'words', 'y', 'x', 'scale', 'blur', 'duration', 'stagger', 'wordStagger', 'delay', 'ease', 'once', 'threshold', 'rootMargin', 'root', 'onEnter', 'onLeave'];
236
+ /**
237
+ * <ScrollReveal effect="rise">…one block…</ScrollReveal>
238
+ * <ScrollReveal items=":scope > article" effect="slide" x={-40} once={false}>{steps}</ScrollReveal>
239
+ * <ScrollReveal as="p" words effect="blur">Slow mornings and a bike at the door.</ScrollReveal>
240
+ * Same as scrollReveal(targets, options). Without `items` the element itself is revealed (hidden until then, also
241
+ * in the server HTML); with `items` (a selector inside) each match is, top to bottom. New items are revealed as
242
+ * they arrive. With `words` the text is split into words, and a new text gets a fresh element.
243
+ * root: the scrolling box (element, ref or selector; default the page).
244
+ */
245
+ export const ScrollReveal = forwardRef(function ScrollReveal({ as, items, className, children, ...props }, ref) {
246
+ const [opts, dom] = part(props, SR_KEYS);
247
+ const latest = useLatest(opts);
248
+ const key = opts.words ? keyOf(children) : '';
249
+ const el = useRef(null), st = useRef(null);
250
+ const kept = useKept(el, className, key);
251
+ useImperativeHandle(ref, () => el.current, [key]);
252
+
253
+ const arm = () => {
254
+ const s = st.current, node = el.current; if (!s || !node) return;
255
+ const targets = items ? [...node.querySelectorAll(items)] : [node];
256
+ const fresh = targets.filter((t) => !s.armed.has(t)); if (!fresh.length) return;
257
+ fresh.forEach((t) => s.armed.add(t));
258
+ const o = live(latest.current, latest);
259
+ s.handles.push(M.scrollReveal(fresh, { ...o, root: resolve(o.root) }));
260
+ };
261
+ const reset = () => { st.current?.handles.forEach((x) => x.destroy()); st.current = { handles: [], armed: new WeakSet(), opts: latest.current }; };
262
+ useEffect(() => { reset(); arm(); return () => { st.current?.handles.forEach((x) => x.destroy()); st.current = null; }; }, [key]);
263
+ useEffect(() => {
264
+ const s = st.current; if (!s) return;
265
+ if (!same(latest.current, s.opts)) { reset(); arm(); } else arm(); // new options: arm again · new items: arm them
266
+ });
267
+ return h(as || 'div', { key, ref: el, ...(items ? null : { 'data-reveal': '' }), ...dom, className: cx(className, kept) || undefined }, children);
268
+ });
269
+
270
+ /* ---------------- BlurIn ---------------- */
271
+
272
+ /**
273
+ * <BlurIn as="p" blur={12} stagger={0.05}>Slow mornings, fresh coffee and a bike waiting at the door.</BlurIn>
274
+ * Same as blurIn(el, options): the words come into focus one after another, right away. A new text (or new options)
275
+ * plays again. Hidden in the server HTML until it plays.
276
+ */
277
+ export const BlurIn = forwardRef(function BlurIn({ as, blur, y, duration, stagger, delay, ease, children, ...rest }, ref) {
278
+ const opts = { blur, y, duration, stagger, delay, ease };
279
+ const key = keyOf(children);
280
+ const el = useRef(null), stable = useRef(opts);
281
+ if (!same(stable.current, opts)) stable.current = opts;
282
+ useImperativeHandle(ref, () => el.current, [key]);
283
+ useIsoLayoutEffect(() => {
284
+ const node = el.current;
285
+ node.removeAttribute('data-reveal'); // it was hiding the text until now
286
+ const o = {}; for (const k in stable.current) if (stable.current[k] !== undefined) o[k] = stable.current[k];
287
+ const tw = M.blurIn(node, o);
288
+ return () => { tw.kill(); M.blurIn.clear(node); };
289
+ }, [key, stable.current]);
290
+ return h(as || 'p', { key, ref: el, 'data-reveal': '', ...rest }, children);
291
+ });
292
+
293
+ /* ---------------- ScrollProgress ---------------- */
294
+
295
+ /**
296
+ * <ScrollProgress /> or <ScrollProgress root={panelRef} onChange={(p) => setRead(p)} />
297
+ * Same as scrollProgress(bar, { root, onChange }): a thin line on .scroll-progress that fills as the page (or the
298
+ * scrolling box root) is read. onChange(progress 0…1).
299
+ */
300
+ export const ScrollProgress = forwardRef(function ScrollProgress({ root, onChange, className, ...rest }, ref) {
301
+ const el = useRef(null), cb = useLatest(onChange);
302
+ useImperativeHandle(ref, () => el.current, []);
303
+ useEffect(() => {
304
+ const p = M.scrollProgress(el.current, { root: resolve(root), onChange: (v) => cb.current?.(v) });
305
+ return () => p.destroy();
306
+ }, [root]);
307
+ return h('div', { ref: el, 'aria-hidden': true, ...rest, className: cx('scroll-progress', className) });
308
+ });
309
+
310
+ /* ---------------- Odometer ---------------- */
311
+
312
+ const odoText = (v, o) => {
313
+ if (typeof v === 'string') return v;
314
+ if (typeof o.format === 'function') return String(o.format(v));
315
+ try { return new Intl.NumberFormat(o.locale, o.format || undefined).format(v); } catch { return String(v); }
316
+ };
317
+ const ODO_KEYS = ['value', 'format', 'locale', 'duration', 'stagger', 'direction', 'ease', 'from'];
318
+ /**
319
+ * <Odometer value={price} format={{ style: 'currency', currency: 'THB', maximumFractionDigits: 0 }} locale="th-TH" />
320
+ * Same as odometer(el, value, options): when `value` changes the digits roll in place, one reel each.
321
+ * from: roll in from this value on mount. value may also be a ready-made string.
322
+ */
323
+ export const Odometer = owned('Odometer', {
324
+ keys: ODO_KEYS,
325
+ defaults: { value: 0 },
326
+ cls: () => 'odo',
327
+ kids: (o) => odoText(o.from ?? o.value, o),
328
+ run: (el, { value, ...o }) => { M.odometer(el, value, o); return null; },
329
+ end: (_, el) => M.odometer.clear(el),
330
+ update: (_, { value, ...o }, prev, el) => { M.odometer(el, value, o); return true; },
331
+ });
332
+
333
+ /* ---------------- CountUp ---------------- */
334
+
335
+ const countFormat = (o) => (typeof o.format === 'function' ? (v) => o.format(v)
336
+ : (v) => (o.prefix ?? '') + Number(v).toLocaleString(o.locale ?? 'en-US', { minimumFractionDigits: o.decimals ?? 0, maximumFractionDigits: o.decimals ?? 0 }) + (o.suffix ?? ''));
337
+ const COUNT_KEYS = ['value', 'from', 'duration', 'delay', 'prefix', 'suffix', 'decimals', 'locale', 'format', 'tick'];
338
+ /**
339
+ * <CountUp value={48250} prefix="฿" /> <CountUp value={4.8} decimals={1} suffix=" / 5" tick />
340
+ * Same as num() + count(): the number counts up from `from` (0) on mount, and from the number on screen to the
341
+ * new one when `value` changes (tick: tints it green going up, red going down).
342
+ */
343
+ export const CountUp = owned('CountUp', {
344
+ keys: COUNT_KEYS,
345
+ defaults: { value: 0 },
346
+ kids: (o) => countFormat(o)(o.from ?? 0),
347
+ run: (el, o) => ({ tw: M.count(el, o.value, countFormat(o), { duration: o.duration, delay: o.delay, from: o.from ?? 0 }) }),
348
+ end: (x) => x?.tw?.kill(),
349
+ update(x, next, prev, el) {
350
+ x.tw = M.count(el, next.value, countFormat(next), { duration: next.duration });
351
+ if (next.tick && next.value !== prev.value) M.tick(el, next.value > prev.value);
352
+ return true;
353
+ },
354
+ });