@gecko-ui-kit/react 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 +1859 -2
- package/index.d.ts +12 -0
- package/index.js +15 -0
- package/package.json +49 -4
- package/src/actions.d.ts +192 -0
- package/src/actions.js +238 -0
- package/src/basics.d.ts +268 -0
- package/src/basics.js +308 -0
- package/src/charts.d.ts +207 -0
- package/src/charts.js +79 -0
- package/src/controls.d.ts +159 -0
- package/src/controls.js +261 -0
- package/src/core.d.ts +78 -0
- package/src/core.js +284 -0
- package/src/data.d.ts +762 -0
- package/src/data.js +608 -0
- package/src/effects.d.ts +410 -0
- package/src/effects.js +580 -0
- package/src/forms.d.ts +724 -0
- package/src/forms.js +403 -0
- package/src/motion.d.ts +130 -0
- package/src/motion.js +176 -0
- package/src/overlays.d.ts +454 -0
- package/src/overlays.js +583 -0
- package/src/shell.d.ts +206 -0
- package/src/shell.js +602 -0
- package/src/text.d.ts +316 -0
- package/src/text.js +354 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './src/core.js';
|
|
2
|
+
export * from './src/basics.js';
|
|
3
|
+
export * from './src/controls.js';
|
|
4
|
+
export * from './src/forms.js';
|
|
5
|
+
export * from './src/data.js';
|
|
6
|
+
export * from './src/actions.js';
|
|
7
|
+
export * from './src/charts.js';
|
|
8
|
+
export * from './src/text.js';
|
|
9
|
+
export * from './src/motion.js';
|
|
10
|
+
export * from './src/effects.js';
|
|
11
|
+
export * from './src/overlays.js';
|
|
12
|
+
export * from './src/shell.js';
|
package/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* @gecko-ui-kit/react — every Gecko UI Kit piece as a React component (works in Next.js: each file is a client
|
|
3
|
+
component and renders on the server). Import the CSS once in your root layout, see README. */
|
|
4
|
+
export * from './src/core.js';
|
|
5
|
+
export * from './src/basics.js';
|
|
6
|
+
export * from './src/controls.js';
|
|
7
|
+
export * from './src/forms.js';
|
|
8
|
+
export * from './src/data.js';
|
|
9
|
+
export * from './src/actions.js';
|
|
10
|
+
export * from './src/charts.js';
|
|
11
|
+
export * from './src/text.js';
|
|
12
|
+
export * from './src/motion.js';
|
|
13
|
+
export * from './src/effects.js';
|
|
14
|
+
export * from './src/overlays.js';
|
|
15
|
+
export * from './src/shell.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gecko-ui-kit/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Every Gecko UI Kit piece as a React component (Next.js ready): buttons, forms, pickers, data display, charts, motion, overlays and the app shell.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"nextjs",
|
|
8
|
+
"ui",
|
|
9
|
+
"components",
|
|
10
|
+
"gsap"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://woraphondev.github.io/UI-Package/playground/",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/WoraphonDev/UI-Package.git",
|
|
16
|
+
"directory": "packages/react"
|
|
17
|
+
},
|
|
18
|
+
"bugs": "https://github.com/WoraphonDev/UI-Package/issues",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"default": "./index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts",
|
|
32
|
+
"src/",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@gecko-ui-kit/tokens": "^0.3.0",
|
|
37
|
+
"@gecko-ui-kit/icons": "^0.4.0",
|
|
38
|
+
"@gecko-ui-kit/motion": "^0.10.0",
|
|
39
|
+
"@gecko-ui-kit/components": "^0.23.0",
|
|
40
|
+
"@gecko-ui-kit/overlays": "^0.8.0",
|
|
41
|
+
"@gecko-ui-kit/shell": "^0.4.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"gsap": "^3.12.0",
|
|
45
|
+
"react": ">=18",
|
|
46
|
+
"react-dom": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/actions.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { HostProps, Mounted, IconLike } from './core.js';
|
|
3
|
+
|
|
4
|
+
type ActionButtonAttrs = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onChange' | 'value' | 'defaultValue' | 'type'>;
|
|
5
|
+
/** 'accent' | 'good' | 'warn' | 'bad', a palette name ('rose', 'teal', 'amber'…) or any CSS colour. */
|
|
6
|
+
export type ActionTone = 'accent' | 'good' | 'warn' | 'bad' | 'indigo' | 'teal' | 'rose' | 'amber' | 'violet' | 'green' | 'slate' | 'sky' | (string & {});
|
|
7
|
+
|
|
8
|
+
/* ---------- LikeButton ---------- */
|
|
9
|
+
export interface LikeHandle {
|
|
10
|
+
readonly el: HTMLButtonElement;
|
|
11
|
+
readonly pressed: boolean;
|
|
12
|
+
/** The number shown (null when there is no count). */
|
|
13
|
+
readonly count: number | null;
|
|
14
|
+
/** Turn it on or off (counts ±1 like a click). silent (default true) skips onChange. */
|
|
15
|
+
set(pressed: boolean, options?: { silent?: boolean; animate?: boolean }): LikeHandle;
|
|
16
|
+
/** As if clicked: calls onChange. */
|
|
17
|
+
toggle(): LikeHandle;
|
|
18
|
+
/** Roll the number to n (for example from the server). */
|
|
19
|
+
setCount(n: number, animate?: boolean): LikeHandle;
|
|
20
|
+
}
|
|
21
|
+
export interface LikeButtonProps extends ActionButtonAttrs {
|
|
22
|
+
/** 'heart' (default) | 'star' | 'bookmark'. */
|
|
23
|
+
kind?: 'heart' | 'star' | 'bookmark';
|
|
24
|
+
/** Controlled: on or off. Keep it in step from onChange. */
|
|
25
|
+
pressed?: boolean;
|
|
26
|
+
/** Uncontrolled: on at the start. */
|
|
27
|
+
defaultPressed?: boolean;
|
|
28
|
+
/** A number shows a rolling count (leave it out for none). A new value rolls to it. */
|
|
29
|
+
count?: number | null;
|
|
30
|
+
/** The text (default per kind: Like / Star / Save). With iconOnly it is the aria-label. */
|
|
31
|
+
label?: string;
|
|
32
|
+
/** The text while on (bookmark: Saved). */
|
|
33
|
+
onLabel?: string;
|
|
34
|
+
/** A round icon button; the label (and count) become its aria-label. */
|
|
35
|
+
iconOnly?: boolean;
|
|
36
|
+
size?: '' | 'sm' | 'lg';
|
|
37
|
+
/** The "on" colour. */
|
|
38
|
+
tone?: ActionTone;
|
|
39
|
+
/** Writes the count (default toLocaleString). */
|
|
40
|
+
format?: (n: number) => string;
|
|
41
|
+
words?: { like?: string; star?: string; save?: string; saved?: string; withCount?: (label: string, count: string) => string };
|
|
42
|
+
/** It was clicked. Return a promise: if it fails, the button goes back and shakes (controlled: set your state back too). */
|
|
43
|
+
onChange?: (pressed: boolean, count: number | null, handle: Mounted<LikeHandle>) => unknown;
|
|
44
|
+
}
|
|
45
|
+
/** <LikeButton kind="heart" count={128} onChange={(on) => api.like(id, on)} /> */
|
|
46
|
+
export const LikeButton: React.ForwardRefExoticComponent<LikeButtonProps & React.RefAttributes<Mounted<LikeHandle>>>;
|
|
47
|
+
|
|
48
|
+
/* ---------- BellSwitch ---------- */
|
|
49
|
+
export interface BellHandle {
|
|
50
|
+
readonly el: HTMLButtonElement;
|
|
51
|
+
readonly on: boolean;
|
|
52
|
+
/** Switch it on or off. silent (default true) skips onChange. */
|
|
53
|
+
set(on: boolean, options?: { silent?: boolean; animate?: boolean }): BellHandle;
|
|
54
|
+
/** As if clicked: calls onChange. */
|
|
55
|
+
toggle(): BellHandle;
|
|
56
|
+
/** Ring the bell again (something new came in). */
|
|
57
|
+
ring(): unknown;
|
|
58
|
+
}
|
|
59
|
+
export interface BellSwitchProps extends ActionButtonAttrs {
|
|
60
|
+
/** Controlled: on or off. */
|
|
61
|
+
on?: boolean;
|
|
62
|
+
/** Uncontrolled: on at the start. */
|
|
63
|
+
defaultOn?: boolean;
|
|
64
|
+
/** The visible name (default words.label, "Notifications"). A string when iconOnly (it becomes the aria-label). */
|
|
65
|
+
label?: React.ReactNode;
|
|
66
|
+
/** Only the round bell. */
|
|
67
|
+
iconOnly?: boolean;
|
|
68
|
+
/** The "on" colour. */
|
|
69
|
+
tone?: ActionTone;
|
|
70
|
+
words?: { label?: string; on?: string; off?: string };
|
|
71
|
+
/** It was switched. Return a promise: if it fails, it switches back and shakes. */
|
|
72
|
+
onChange?: (on: boolean, handle: Mounted<BellHandle>) => unknown;
|
|
73
|
+
}
|
|
74
|
+
/** <BellSwitch label="Booking alerts" defaultOn onChange={save} /> */
|
|
75
|
+
export const BellSwitch: React.ForwardRefExoticComponent<BellSwitchProps & React.RefAttributes<Mounted<BellHandle>>>;
|
|
76
|
+
/** The same component as BellSwitch. */
|
|
77
|
+
export const BellButton: typeof BellSwitch;
|
|
78
|
+
|
|
79
|
+
/* ---------- PayButton ---------- */
|
|
80
|
+
export interface PayHandle {
|
|
81
|
+
readonly el: HTMLButtonElement;
|
|
82
|
+
readonly state: 'idle' | 'busy' | 'done' | 'failed';
|
|
83
|
+
/** Start paying, as if pressed. Resolves true (paid) or false (failed). */
|
|
84
|
+
pay(): Promise<boolean>;
|
|
85
|
+
/** Back to the start. */
|
|
86
|
+
reset(): PayHandle;
|
|
87
|
+
}
|
|
88
|
+
export interface PayButtonProps extends ActionButtonAttrs {
|
|
89
|
+
/** The idle text, any React node (default words.label, "Pay now"). */
|
|
90
|
+
label?: React.ReactNode;
|
|
91
|
+
/** 'dark' (default) | 'primary'. */
|
|
92
|
+
variant?: 'dark' | 'primary' | (string & {});
|
|
93
|
+
/** 'lg' (default) | '' | 'sm'. */
|
|
94
|
+
size?: 'lg' | '' | 'sm';
|
|
95
|
+
/** Fill the row. */
|
|
96
|
+
block?: boolean;
|
|
97
|
+
/** Before the label: an icon name or a node ('card' by default, '' = none). */
|
|
98
|
+
icon?: IconLike;
|
|
99
|
+
/** The card swiping through the slot (default true). */
|
|
100
|
+
card?: boolean;
|
|
101
|
+
/** Seconds "Processing" shows at least (default 1.4). */
|
|
102
|
+
minBusy?: number;
|
|
103
|
+
/** Seconds before it returns to the start after paying (default 3; 0 = stays "Paid"). */
|
|
104
|
+
back?: number;
|
|
105
|
+
words?: { label?: string; busy?: string; done?: string; retry?: string; sayBusy?: string; sayDone?: string; sayFail?: string | ((error: unknown) => string) };
|
|
106
|
+
/** Runs while busy: resolve = paid; throw, reject or return false = failed. */
|
|
107
|
+
onPay?: (handle: Mounted<PayHandle>) => unknown;
|
|
108
|
+
/** Paid: gets what onPay resolved with. */
|
|
109
|
+
onDone?: (result: unknown, handle: Mounted<PayHandle>) => void;
|
|
110
|
+
/** Failed: gets the error. */
|
|
111
|
+
onFail?: (error: unknown, handle: Mounted<PayHandle>) => void;
|
|
112
|
+
}
|
|
113
|
+
/** <PayButton label="Pay ฿700" block onPay={() => checkout(cart)} /> */
|
|
114
|
+
export const PayButton: React.ForwardRefExoticComponent<PayButtonProps & React.RefAttributes<Mounted<PayHandle>>>;
|
|
115
|
+
|
|
116
|
+
/* ---------- SpeedDial ---------- */
|
|
117
|
+
export interface SpeedDialAction {
|
|
118
|
+
id?: string;
|
|
119
|
+
label: string;
|
|
120
|
+
/** An icon name or an <svg> string. */
|
|
121
|
+
icon?: string;
|
|
122
|
+
/** Called when this action is picked (before onSelect). */
|
|
123
|
+
onClick?: (action: SpeedDialAction, dial: SpeedDialHandle) => void;
|
|
124
|
+
[key: string]: unknown;
|
|
125
|
+
}
|
|
126
|
+
export interface SpeedDialHandle {
|
|
127
|
+
readonly isOpen: boolean;
|
|
128
|
+
readonly actions: SpeedDialAction[];
|
|
129
|
+
/** Fan the actions out. focus: move the focus to the nearest action. */
|
|
130
|
+
open(options?: { focus?: boolean }): SpeedDialHandle;
|
|
131
|
+
/** Fold them away. focus: give the focus back to the button. */
|
|
132
|
+
close(options?: { focus?: boolean }): SpeedDialHandle;
|
|
133
|
+
toggle(): SpeedDialHandle;
|
|
134
|
+
/** New actions (closes first). */
|
|
135
|
+
setActions(actions: SpeedDialAction[]): SpeedDialHandle;
|
|
136
|
+
destroy(): void;
|
|
137
|
+
}
|
|
138
|
+
export interface SpeedDialProps extends HostProps {
|
|
139
|
+
actions: SpeedDialAction[];
|
|
140
|
+
/** Where the actions fan out (default 'up'). */
|
|
141
|
+
direction?: 'up' | 'down';
|
|
142
|
+
/** Which edge the button sits on (default 'right'); labels point inwards. */
|
|
143
|
+
side?: 'right' | 'left';
|
|
144
|
+
/** false = icons only (default true). */
|
|
145
|
+
labels?: boolean;
|
|
146
|
+
/** Pinned to the corner of the screen (--gk-sd-x / --gk-sd-y set the gap). */
|
|
147
|
+
fixed?: boolean;
|
|
148
|
+
/** The main button's icon (default 'plus'; it turns into an ×). */
|
|
149
|
+
icon?: string;
|
|
150
|
+
/** label names the main button; actions names the list. */
|
|
151
|
+
words?: { label?: string; actions?: string };
|
|
152
|
+
/** An action was picked. */
|
|
153
|
+
onSelect?: (action: SpeedDialAction, dial: SpeedDialHandle) => void;
|
|
154
|
+
/** It opened or closed. */
|
|
155
|
+
onToggle?: (open: boolean, dial: SpeedDialHandle) => void;
|
|
156
|
+
}
|
|
157
|
+
/** <SpeedDial actions={[{ id: 'booking', label: 'New booking', icon: 'calendar' }]} onSelect={(a) => …} /> */
|
|
158
|
+
export const SpeedDial: React.ForwardRefExoticComponent<SpeedDialProps & React.RefAttributes<Mounted<SpeedDialHandle>>>;
|
|
159
|
+
|
|
160
|
+
/* ---------- Typing ---------- */
|
|
161
|
+
export interface TypingHandle {
|
|
162
|
+
readonly visible: boolean;
|
|
163
|
+
/** The text it says now ("Pim is typing…"). */
|
|
164
|
+
readonly text: string;
|
|
165
|
+
readonly root: HTMLElement;
|
|
166
|
+
show(names?: string | string[]): TypingHandle;
|
|
167
|
+
hide(): TypingHandle;
|
|
168
|
+
toggle(on?: boolean): TypingHandle;
|
|
169
|
+
/** Change who is typing. */
|
|
170
|
+
set(names: string | string[]): TypingHandle;
|
|
171
|
+
destroy(): void;
|
|
172
|
+
}
|
|
173
|
+
export interface TypingProps extends HostProps {
|
|
174
|
+
/** Shown or hidden (default false); changes animate in place. */
|
|
175
|
+
visible?: boolean;
|
|
176
|
+
/** Who is typing: 'Pim' or ['Pim', 'Mali'] (empty = no name). */
|
|
177
|
+
names?: string | string[];
|
|
178
|
+
/** The text beside the bubble (default true; screen readers hear it either way). */
|
|
179
|
+
text?: boolean;
|
|
180
|
+
/** false: only the dots, for inside a list row or a button. */
|
|
181
|
+
bubble?: boolean;
|
|
182
|
+
size?: '' | 'sm' | 'lg';
|
|
183
|
+
/** Colour of the dots (default 'muted'). */
|
|
184
|
+
tone?: 'muted' | 'accent' | 'ink';
|
|
185
|
+
/** The row closes up when hidden (default true). */
|
|
186
|
+
collapse?: boolean;
|
|
187
|
+
words?: { anon?: string; one?: (name: string) => string; two?: (a: string, b: string) => string; many?: (names: string[]) => string };
|
|
188
|
+
}
|
|
189
|
+
/** <Typing visible={typing} names={['Pim']} /> */
|
|
190
|
+
export const Typing: React.ForwardRefExoticComponent<TypingProps & React.RefAttributes<Mounted<TypingHandle>>>;
|
|
191
|
+
|
|
192
|
+
export {};
|
package/src/actions.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* Action buttons and status: LikeButton, BellSwitch (BellButton), PayButton, SpeedDial, Typing.
|
|
3
|
+
|
|
4
|
+
LikeButton, BellSwitch and PayButton draw the same HTML as the vanilla builders (likeBtn(), bellBtn(),
|
|
5
|
+
payBtn()) in React, so they render on the server, then the vanilla code wires that very button for the
|
|
6
|
+
clicks and the little shows. The parts the vanilla code changes later (aria-pressed, the count, the
|
|
7
|
+
"On / Off" line…) are drawn once from the first value and never redrawn by React, so the two never fight;
|
|
8
|
+
value changes go through the handle (set(), setCount()). SpeedDial and Typing are mounted pieces. */
|
|
9
|
+
import { forwardRef, useId, useImperativeHandle, useReducer, useRef } from 'react';
|
|
10
|
+
import * as C from '@gecko-ui-kit/components';
|
|
11
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
12
|
+
import { h, cx, iconNode, Icon, mounted, changedOnly, same, useLatest, useIsoLayoutEffect } from './core.js';
|
|
13
|
+
|
|
14
|
+
/* A ref value that stays the same object and forwards to the live vanilla handle (its methods and `el`). */
|
|
15
|
+
function useHandleRef(ref, cur) {
|
|
16
|
+
const proxy = useRef(null);
|
|
17
|
+
if (!proxy.current) {
|
|
18
|
+
proxy.current = new Proxy({}, {
|
|
19
|
+
get(_, k) { const x = cur.current; if (!x) return undefined; const v = x[k]; return typeof v === 'function' ? v.bind(x) : v; },
|
|
20
|
+
has(_, k) { const x = cur.current; return !!x && k in x; },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
useImperativeHandle(ref, () => proxy.current, []);
|
|
24
|
+
return proxy.current;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const TONES = { accent: 'var(--gk-accent)', good: 'var(--gk-good)', warn: 'var(--gk-warn)', bad: 'var(--gk-bad)' };
|
|
28
|
+
const HUES = ['indigo', 'teal', 'rose', 'amber', 'violet', 'green', 'slate', 'sky'];
|
|
29
|
+
const toColor = (t) => (!t ? '' : TONES[t] || (HUES.includes(t) ? `var(--gk-hue-${t})` : String(t)));
|
|
30
|
+
const withVar = (name, c, style) => (c ? { [name]: c, ...style } : style);
|
|
31
|
+
const wordsKey = (w) => JSON.stringify(w || {}); // the text words (functions are read live or ignored)
|
|
32
|
+
|
|
33
|
+
/* ------------------------------------------------------------------ LikeButton */
|
|
34
|
+
const LK_WORDS = { like: 'Like', star: 'Star', save: 'Save', saved: 'Saved', withCount: (label, count) => `${label}, ${count}` };
|
|
35
|
+
const LK_KINDS = ['heart', 'star', 'bookmark'];
|
|
36
|
+
const lkPath = {}; // the outline of each kind, read from likeBtn() so it never drifts from the vanilla one
|
|
37
|
+
const lkShape = (kind) => (lkPath[kind] ??= /<path d="([^"]+)"/.exec(C.likeBtn({ kind, iconOnly: true }))?.[1] ?? '');
|
|
38
|
+
const lkSvg = (kind) => h('svg', { viewBox: '0 0 24 24', 'aria-hidden': true, focusable: 'false' }, h('path', { d: lkShape(kind) }));
|
|
39
|
+
const lkCount = (n) => Math.max(0, Math.round(+n || 0));
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* <LikeButton kind="heart" pressed={liked} count={likes} onChange={(on, n) => { setLiked(on); setLikes(n); return api.react(id, on); }} />
|
|
43
|
+
* Same markup as likeBtn(options), wired by likeButton(): the heart / star pops with a burst, the bookmark fills,
|
|
44
|
+
* the count rolls. pressed (controlled) or defaultPressed. onChange(pressed, count, handle): return a promise and
|
|
45
|
+
* the button goes back (with a shake) if it fails. A new `count` rolls to it. ref → { pressed, count, set, toggle, setCount, el }.
|
|
46
|
+
*/
|
|
47
|
+
export const LikeButton = forwardRef(function LikeButton({ kind, pressed, defaultPressed = false, count, label, onLabel, iconOnly = false, size = '',
|
|
48
|
+
tone, format, words, onChange, className, style, ...rest }, ref) {
|
|
49
|
+
const k = LK_KINDS.includes(kind) ? kind : 'heart';
|
|
50
|
+
const W = { ...LK_WORDS, ...words };
|
|
51
|
+
const L = label ?? (k === 'bookmark' ? W.save : k === 'star' ? W.star : W.like);
|
|
52
|
+
const OL = onLabel ?? (k === 'bookmark' ? W.saved : '');
|
|
53
|
+
const hasCount = count != null && count !== '';
|
|
54
|
+
const controlled = pressed !== undefined;
|
|
55
|
+
const fmt = (n) => (format ? String(format(n)) : n.toLocaleString());
|
|
56
|
+
const swap = !iconOnly && !!OL && OL !== L;
|
|
57
|
+
const shape = JSON.stringify([k, L, OL, !!iconOnly, hasCount, wordsKey(words)]);
|
|
58
|
+
|
|
59
|
+
const btn = useRef(null), cur = useRef(null), prevCount = useRef(count);
|
|
60
|
+
const latest = useLatest({ onChange, format, controlled });
|
|
61
|
+
const [, force] = useReducer((x) => x + 1, 0);
|
|
62
|
+
const proxy = useHandleRef(ref, cur);
|
|
63
|
+
|
|
64
|
+
/* what React draws for the parts likeButton() changes later: the first value, kept until the shape changes */
|
|
65
|
+
const snap = useRef(null);
|
|
66
|
+
if (!snap.current || snap.current.shape !== shape) {
|
|
67
|
+
const x = cur.current;
|
|
68
|
+
const on = controlled ? !!pressed : x ? x.pressed : !!defaultPressed;
|
|
69
|
+
const n = hasCount ? (x?.count ?? lkCount(count)) : null;
|
|
70
|
+
snap.current = { shape, on, n, num: n == null ? '' : fmt(n), aria: n == null ? L : W.withCount(L, fmt(n)) };
|
|
71
|
+
}
|
|
72
|
+
const s = snap.current;
|
|
73
|
+
|
|
74
|
+
useIsoLayoutEffect(() => {
|
|
75
|
+
const x = C.likeButton(btn.current, {
|
|
76
|
+
words,
|
|
77
|
+
format: (n) => { const f = latest.current.format; return f ? f(n) : n.toLocaleString(); },
|
|
78
|
+
onChange: (on, n) => { const r = latest.current.onChange?.(on, n, proxy); if (latest.current.controlled) force(); return r; },
|
|
79
|
+
});
|
|
80
|
+
cur.current = x; prevCount.current = count;
|
|
81
|
+
return () => { if (cur.current === x) cur.current = null; x.destroy(); };
|
|
82
|
+
}, [shape]);
|
|
83
|
+
useIsoLayoutEffect(() => { // keep the vanilla state in step with the props
|
|
84
|
+
const x = cur.current; if (!x) return;
|
|
85
|
+
if (controlled && x.pressed !== !!pressed) x.set(!!pressed, { silent: true });
|
|
86
|
+
if (hasCount && !same(count, prevCount.current)) { prevCount.current = count; if (x.count !== lkCount(count)) x.setCount(lkCount(count)); }
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return h('button', {
|
|
90
|
+
key: shape, ref: btn, type: 'button', ...rest,
|
|
91
|
+
className: cx('gk-lk', 'gk-lk--' + k, iconOnly && 'gk-lk--icon', size && 'gk-lk--' + size, className),
|
|
92
|
+
'aria-pressed': s.on, 'data-kind': k, 'data-label': L, 'data-on-label': OL || undefined, 'data-count': hasCount ? s.n : undefined,
|
|
93
|
+
style: withVar('--gk-lk-c', toColor(tone), style),
|
|
94
|
+
...(iconOnly ? { 'aria-label': s.aria } : {}),
|
|
95
|
+
},
|
|
96
|
+
h('span', { className: 'gk-lk__icon', 'aria-hidden': true },
|
|
97
|
+
k === 'bookmark' ? null : h('span', { className: 'gk-lk__ring' }),
|
|
98
|
+
h('span', { className: 'gk-lk__bits' }, Array.from({ length: k === 'bookmark' ? 4 : 8 }, (_, i) => h('i', { key: i }))),
|
|
99
|
+
h('span', { className: 'gk-lk__glyph' }, h('span', { className: 'gk-lk__line' }, lkSvg(k)), h('span', { className: 'gk-lk__solid' }, lkSvg(k)))),
|
|
100
|
+
iconOnly ? null : [
|
|
101
|
+
swap ? h('span', { key: 'sr', className: 'gk-lk__sr' }, L) : null,
|
|
102
|
+
h('span', { key: 'l', className: 'gk-lk__label', 'aria-hidden': swap ? true : undefined }, s.on && swap ? OL : L),
|
|
103
|
+
hasCount ? h('span', { key: 'c', className: 'gk-lk__count' }, h('span', { className: 'gk-lk__num' }, s.num)) : null,
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
/* ------------------------------------------------------------------ BellSwitch */
|
|
108
|
+
const NB_WORDS = { label: 'Notifications', on: 'On', off: 'Off' };
|
|
109
|
+
const nbSvg = {}; // the bell drawing from bellBtn(), on and off (library geometry, no app data)
|
|
110
|
+
const bellSvg = (on) => (nbSvg[on] ??= /<svg[\s\S]*<\/svg>/.exec(C.bellBtn({ on, iconOnly: true }))?.[0] ?? '');
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* <BellSwitch label="Booking alerts" on={alerts} onChange={(on) => { setAlerts(on); return save(on); }} />
|
|
114
|
+
* Same markup as bellBtn(options), wired by bellSwitch(): on rings the bell, off draws a slash across it.
|
|
115
|
+
* on (controlled) or defaultOn. onChange(on, handle): return a promise and it switches back (with a shake) if it fails.
|
|
116
|
+
* ref → { on, set, toggle, ring, el }: ring() when something new comes in. BellButton is the same component.
|
|
117
|
+
*/
|
|
118
|
+
export const BellSwitch = forwardRef(function BellSwitch({ on, defaultOn = false, label, iconOnly = false, tone, words, onChange, className, style, ...rest }, ref) {
|
|
119
|
+
const W = { ...NB_WORDS, ...words };
|
|
120
|
+
const id = useId();
|
|
121
|
+
const controlled = on !== undefined;
|
|
122
|
+
const shape = JSON.stringify([!!iconOnly, W.on, W.off]);
|
|
123
|
+
const btn = useRef(null), cur = useRef(null);
|
|
124
|
+
const latest = useLatest({ onChange, controlled });
|
|
125
|
+
const [, force] = useReducer((x) => x + 1, 0);
|
|
126
|
+
const proxy = useHandleRef(ref, cur);
|
|
127
|
+
|
|
128
|
+
const snap = useRef(null);
|
|
129
|
+
if (!snap.current || snap.current.shape !== shape) {
|
|
130
|
+
const v = controlled ? !!on : cur.current ? cur.current.on : !!defaultOn;
|
|
131
|
+
snap.current = { shape, on: v };
|
|
132
|
+
}
|
|
133
|
+
const s = snap.current;
|
|
134
|
+
|
|
135
|
+
useIsoLayoutEffect(() => {
|
|
136
|
+
const x = C.bellSwitch(btn.current, {
|
|
137
|
+
words,
|
|
138
|
+
onChange: (v) => { const r = latest.current.onChange?.(v, proxy); if (latest.current.controlled) force(); return r; },
|
|
139
|
+
});
|
|
140
|
+
cur.current = x;
|
|
141
|
+
return () => { if (cur.current === x) cur.current = null; x.destroy(); };
|
|
142
|
+
}, [shape]);
|
|
143
|
+
useIsoLayoutEffect(() => {
|
|
144
|
+
const x = cur.current;
|
|
145
|
+
if (x && controlled && x.on !== !!on) x.set(!!on, { silent: true });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const name = label ?? W.label;
|
|
149
|
+
return h('button', {
|
|
150
|
+
key: shape, ref: btn, type: 'button', role: 'switch', ...rest,
|
|
151
|
+
className: cx('gk-nbell', iconOnly && 'gk-nbell--icon', className),
|
|
152
|
+
'aria-checked': s.on,
|
|
153
|
+
...(iconOnly ? { 'aria-label': typeof name === 'string' ? name : W.label } : { 'aria-labelledby': id }),
|
|
154
|
+
'data-on-text': W.on, 'data-off-text': W.off,
|
|
155
|
+
style: withVar('--gk-nb-c', toColor(tone), style),
|
|
156
|
+
},
|
|
157
|
+
h('span', { className: 'gk-nbell__icon', dangerouslySetInnerHTML: { __html: bellSvg(s.on) } }),
|
|
158
|
+
iconOnly ? null : h('span', { className: 'gk-nbell__text' },
|
|
159
|
+
h('b', { className: 'gk-nbell__label', id }, name),
|
|
160
|
+
h('small', { className: 'gk-nbell__state', 'aria-hidden': true }, h('span', null, s.on ? W.on : W.off))));
|
|
161
|
+
});
|
|
162
|
+
/** The same component as BellSwitch, under the name of the vanilla builder bellBtn(). */
|
|
163
|
+
export const BellButton = BellSwitch;
|
|
164
|
+
|
|
165
|
+
/* ------------------------------------------------------------------ PayButton */
|
|
166
|
+
const PAY_WORDS = { label: 'Pay now', busy: 'Processing', done: 'Paid', retry: 'Try again' };
|
|
167
|
+
const PAY_STATES = ['gk-is-busy', 'gk-is-done', 'gk-is-failed'];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* <PayButton label="Pay ฿700" block onPay={() => checkout(cart)} onDone={(receipt) => show(receipt)} onFail={(e) => …} />
|
|
171
|
+
* Same markup as payBtn(options), wired by payButton(): press it and a card swipes through, "Processing", then
|
|
172
|
+
* "Paid" with a tick (onPay resolved) or red "Try again" (onPay threw, rejected or returned false).
|
|
173
|
+
* label can be any React node. ref → { state, pay(), reset(), el }.
|
|
174
|
+
*/
|
|
175
|
+
export const PayButton = forwardRef(function PayButton({ label, variant = 'dark', size = 'lg', block = false, icon = 'card', card = true, minBusy = 1.4, back = 3,
|
|
176
|
+
words, onPay, onDone, onFail, className, ...rest }, ref) {
|
|
177
|
+
const W = { ...PAY_WORDS, ...words };
|
|
178
|
+
const btn = useRef(null), cur = useRef(null);
|
|
179
|
+
const latest = useLatest({ onPay, onDone, onFail });
|
|
180
|
+
const proxy = useHandleRef(ref, cur);
|
|
181
|
+
const cls = cx('gk-btn', variant && 'gk-btn--' + variant, size && 'gk-btn--' + size, block && 'gk-btn--block', 'gk-pay', className);
|
|
182
|
+
|
|
183
|
+
useIsoLayoutEffect(() => {
|
|
184
|
+
const x = C.payButton(btn.current, {
|
|
185
|
+
words, card, minBusy, back,
|
|
186
|
+
onPay: () => latest.current.onPay?.(proxy),
|
|
187
|
+
onDone: (r) => latest.current.onDone?.(r, proxy),
|
|
188
|
+
onFail: (e) => latest.current.onFail?.(e, proxy),
|
|
189
|
+
});
|
|
190
|
+
cur.current = x;
|
|
191
|
+
return () => { if (cur.current === x) cur.current = null; x.destroy(); };
|
|
192
|
+
}, [wordsKey(words)]);
|
|
193
|
+
useIsoLayoutEffect(() => { const x = cur.current; if (x) Object.assign(x.opts, { card, minBusy, back }); });
|
|
194
|
+
useIsoLayoutEffect(() => { // React wrote a new class list: put the state classes back
|
|
195
|
+
const x = cur.current, b = btn.current; if (!x || !b) return;
|
|
196
|
+
PAY_STATES.forEach((c) => b.classList.toggle(c, c === 'gk-is-' + (x.state === 'failed' ? 'failed' : x.state)));
|
|
197
|
+
b.classList.toggle('gk-is-still', x.state === 'busy' && M.RM());
|
|
198
|
+
}, [cls]);
|
|
199
|
+
|
|
200
|
+
return h('button', { ref: btn, type: 'button', ...rest, className: cls },
|
|
201
|
+
h('span', { className: 'gk-pay__face gk-pay__idle' }, iconNode(icon || null), h('span', { className: 'gk-pay__label' }, label ?? W.label)),
|
|
202
|
+
h('span', { className: 'gk-pay__face gk-pay__busy', 'aria-hidden': true }, h('span', null, W.busy), h('span', { className: 'gk-pay__dots' }, h('i'), h('i'), h('i'))),
|
|
203
|
+
h('span', { className: 'gk-pay__face gk-pay__done', 'aria-hidden': true },
|
|
204
|
+
h('svg', { className: 'gk-pay__tick', viewBox: '0 0 24 24', 'aria-hidden': true, focusable: 'false' },
|
|
205
|
+
h('path', { d: 'm5 12.5 4.5 4.5L19 7.5', pathLength: 1, strokeDasharray: '1 2', strokeDashoffset: 1 })),
|
|
206
|
+
h('span', null, W.done)),
|
|
207
|
+
h('span', { className: 'gk-pay__face gk-pay__fail', 'aria-hidden': true }, h(Icon, { name: 'alert' }), h('span', null, W.retry)),
|
|
208
|
+
h('span', { className: 'gk-pay__slot', 'aria-hidden': true }, h('i')),
|
|
209
|
+
h('span', { className: 'gk-pay__card', 'aria-hidden': true }));
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/* ------------------------------------------------------------------ SpeedDial */
|
|
213
|
+
/**
|
|
214
|
+
* <SpeedDial actions={[{ id: 'booking', label: 'New booking', icon: 'calendar' }, …]} onSelect={(a) => go(a.id)} />
|
|
215
|
+
* Same as speedDial(el, options): the + turns into an × and the actions spring out. New `actions` are swapped in
|
|
216
|
+
* place (setActions). ref → { isOpen, open(), close(), toggle(), setActions(list), el }.
|
|
217
|
+
*/
|
|
218
|
+
export const SpeedDial = mounted('SpeedDial', C.speedDial, {
|
|
219
|
+
update(d, next, prev) {
|
|
220
|
+
if (changedOnly(next, prev, ['actions'])) { d.setActions(next.actions || []); return true; }
|
|
221
|
+
return false;
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/* ------------------------------------------------------------------ Typing */
|
|
226
|
+
/**
|
|
227
|
+
* <Typing visible={othersTyping} names={['Pim']} />
|
|
228
|
+
* Same as typing(el, options): three bouncing dots in a bubble with "Pim is typing…". `visible` shows / hides it
|
|
229
|
+
* (the row opens and closes), `names` changes who, both in place. ref → { visible, text, show(), hide(), set(), el }.
|
|
230
|
+
*/
|
|
231
|
+
export const Typing = mounted('Typing', C.typing, {
|
|
232
|
+
update(t, next, prev) {
|
|
233
|
+
if (!changedOnly(next, prev, ['visible', 'names'])) return false;
|
|
234
|
+
if (!same(next.names, prev.names)) t.set(next.names ?? []);
|
|
235
|
+
if (!!next.visible !== t.visible) (next.visible ? t.show() : t.hide());
|
|
236
|
+
return true;
|
|
237
|
+
},
|
|
238
|
+
});
|