@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/src/basics.d.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { IconLike } from './core.js';
|
|
3
|
+
|
|
4
|
+
type Tone = 'good' | 'warn' | 'bad' | 'accent' | 'plain' | (string & {});
|
|
5
|
+
type AvatarTone = 'indigo' | 'teal' | 'rose' | 'amber' | 'violet' | 'green' | 'slate' | 'sky' | (string & {});
|
|
6
|
+
type SpanAttrs = React.HTMLAttributes<HTMLSpanElement>;
|
|
7
|
+
|
|
8
|
+
export interface SpinnerProps extends SpanAttrs {
|
|
9
|
+
/** Size in px, or any CSS length ('1.2em' matches the text around it). The ring is 1em across. */
|
|
10
|
+
size?: number | string;
|
|
11
|
+
/** Colour; 'current' takes the text colour. Default 'accent'. */
|
|
12
|
+
tone?: 'accent' | 'ink' | 'muted' | 'good' | 'warn' | 'bad' | 'current';
|
|
13
|
+
/** Seconds per lap (default 1). */
|
|
14
|
+
speed?: number;
|
|
15
|
+
/** The faint full ring behind the comet (default true). */
|
|
16
|
+
track?: boolean;
|
|
17
|
+
/** Text shown next to it; without it a hidden "Loading…" is read by screen readers. */
|
|
18
|
+
label?: React.ReactNode;
|
|
19
|
+
/** The label under the ring instead of beside it. */
|
|
20
|
+
stack?: boolean;
|
|
21
|
+
/** Text for other languages: { loading }. */
|
|
22
|
+
words?: { loading?: string };
|
|
23
|
+
/** Hidden from screen readers (no role, no text), e.g. inside a button that already says "Saving…". */
|
|
24
|
+
decorative?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** <Spinner size={36} label="Loading bookings…" stack /> — the orbit loader. */
|
|
27
|
+
export const Spinner: React.ForwardRefExoticComponent<SpinnerProps & React.RefAttributes<HTMLSpanElement>>;
|
|
28
|
+
|
|
29
|
+
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
30
|
+
variant?: 'primary' | 'dark' | 'ghost' | 'danger' | (string & {});
|
|
31
|
+
size?: 'sm' | 'lg' | (string & {});
|
|
32
|
+
/** Icon before the label: a name ('check') or any node. */
|
|
33
|
+
icon?: IconLike;
|
|
34
|
+
iconRight?: IconLike;
|
|
35
|
+
/** Full width. */
|
|
36
|
+
block?: boolean;
|
|
37
|
+
/** Leans toward the pointer (mouse only). */
|
|
38
|
+
magnetic?: boolean;
|
|
39
|
+
/** Working: locks the button (disabled, aria-busy) and shows a spinner in place of the icon. */
|
|
40
|
+
busy?: boolean;
|
|
41
|
+
/** Label while busy, e.g. "Saving…" (default: the normal label). */
|
|
42
|
+
busyText?: React.ReactNode;
|
|
43
|
+
}
|
|
44
|
+
/** <Button variant="primary" icon="check" busy={saving} busyText="Saving…">Save</Button> */
|
|
45
|
+
export const Button: React.ForwardRefExoticComponent<ButtonProps & React.RefAttributes<HTMLButtonElement>>;
|
|
46
|
+
|
|
47
|
+
export interface IconButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onChange'> {
|
|
48
|
+
/** The icon: a name ('bell') or any node. */
|
|
49
|
+
icon: IconLike;
|
|
50
|
+
/** What it does, read by screen readers (aria-label). */
|
|
51
|
+
label: string;
|
|
52
|
+
size?: 'sm' | (string & {});
|
|
53
|
+
/** On/off button (aria-pressed), controlled. */
|
|
54
|
+
pressed?: boolean;
|
|
55
|
+
/** On/off button (aria-pressed), uncontrolled. */
|
|
56
|
+
defaultPressed?: boolean;
|
|
57
|
+
/** Runs when an on/off button is pressed. */
|
|
58
|
+
onChange?: (pressed: boolean, event: React.MouseEvent<HTMLButtonElement>) => void;
|
|
59
|
+
}
|
|
60
|
+
/** <IconButton icon="bell" label="Notifications" /> */
|
|
61
|
+
export const IconButton: React.ForwardRefExoticComponent<IconButtonProps & React.RefAttributes<HTMLButtonElement>>;
|
|
62
|
+
|
|
63
|
+
export interface TagProps extends SpanAttrs {
|
|
64
|
+
/** Colour: good | warn | bad | accent | plain (default). */
|
|
65
|
+
tone?: Tone;
|
|
66
|
+
/** The small dot before the text (default true). */
|
|
67
|
+
dot?: boolean;
|
|
68
|
+
/** An icon instead of the dot. */
|
|
69
|
+
icon?: IconLike;
|
|
70
|
+
}
|
|
71
|
+
/** <Tag tone="good">Paid</Tag> */
|
|
72
|
+
export const Tag: React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLSpanElement>>;
|
|
73
|
+
|
|
74
|
+
export interface BadgeProps extends SpanAttrs {
|
|
75
|
+
/** 'accent' fills it with the accent colour. */
|
|
76
|
+
tone?: 'accent' | (string & {});
|
|
77
|
+
}
|
|
78
|
+
/** <Badge tone="accent">3</Badge> */
|
|
79
|
+
export const Badge: React.ForwardRefExoticComponent<BadgeProps & React.RefAttributes<HTMLSpanElement>>;
|
|
80
|
+
|
|
81
|
+
/** A person for Avatar, AvatarGroup and Cell. */
|
|
82
|
+
export interface Person {
|
|
83
|
+
/** Full name (the tooltip; initials come from it). */
|
|
84
|
+
name?: string;
|
|
85
|
+
/** Initials to show instead of the ones from name. */
|
|
86
|
+
ini?: string;
|
|
87
|
+
/** Colour: indigo teal rose amber violet green slate sky. */
|
|
88
|
+
tone?: AvatarTone;
|
|
89
|
+
}
|
|
90
|
+
export interface AvatarProps extends Omit<SpanAttrs, 'title'>, Person {
|
|
91
|
+
size?: 'sm' | 'lg' | (string & {});
|
|
92
|
+
/** A green "online" dot. */
|
|
93
|
+
online?: boolean;
|
|
94
|
+
title?: string;
|
|
95
|
+
}
|
|
96
|
+
/** <Avatar name="Pim Wong" tone="teal" /> — no name and no ini: an empty "open slot". */
|
|
97
|
+
export const Avatar: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
|
|
98
|
+
|
|
99
|
+
export interface AvatarGroupProps extends SpanAttrs {
|
|
100
|
+
/** The people; an id (or the name) keys each one. */
|
|
101
|
+
people: (Person & { id?: string | number })[];
|
|
102
|
+
/** How many to show before "+N" (default 4). */
|
|
103
|
+
max?: number;
|
|
104
|
+
}
|
|
105
|
+
/** <AvatarGroup people={team} max={4} /> */
|
|
106
|
+
export const AvatarGroup: React.ForwardRefExoticComponent<AvatarGroupProps & React.RefAttributes<HTMLSpanElement>>;
|
|
107
|
+
|
|
108
|
+
export interface CellProps extends SpanAttrs, Person {
|
|
109
|
+
/** The small line under the name (role, email…). */
|
|
110
|
+
sub?: React.ReactNode;
|
|
111
|
+
online?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/** <Cell name="Pim Wong" tone="teal" sub="Mechanic" /> */
|
|
114
|
+
export const Cell: React.ForwardRefExoticComponent<CellProps & React.RefAttributes<HTMLSpanElement>>;
|
|
115
|
+
|
|
116
|
+
export interface CardHeadProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
|
|
117
|
+
title: React.ReactNode;
|
|
118
|
+
sub?: React.ReactNode;
|
|
119
|
+
/** Things on the right: buttons, chips… */
|
|
120
|
+
right?: React.ReactNode;
|
|
121
|
+
}
|
|
122
|
+
/** <CardHead title="Today" sub="12 bookings" right={…} /> */
|
|
123
|
+
export const CardHead: React.ForwardRefExoticComponent<CardHeadProps & React.RefAttributes<HTMLDivElement>>;
|
|
124
|
+
|
|
125
|
+
export interface CardProps extends Omit<React.HTMLAttributes<HTMLElement>, 'title'> {
|
|
126
|
+
/** Heading; the head row shows only when it is set. */
|
|
127
|
+
title?: React.ReactNode;
|
|
128
|
+
/** Small line under the title. */
|
|
129
|
+
sub?: React.ReactNode;
|
|
130
|
+
/** Things on the right of the head: buttons, chips… */
|
|
131
|
+
right?: React.ReactNode;
|
|
132
|
+
/** No padding, so a table or list reaches the edges. */
|
|
133
|
+
flush?: boolean;
|
|
134
|
+
/** Rises a little when the mouse is over it. */
|
|
135
|
+
lift?: boolean;
|
|
136
|
+
}
|
|
137
|
+
/** <Card title="Today" sub="12 bookings" lift>…</Card> */
|
|
138
|
+
export const Card: React.ForwardRefExoticComponent<CardProps & React.RefAttributes<HTMLElement>>;
|
|
139
|
+
|
|
140
|
+
export interface KpiProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
141
|
+
/** Small caption on top. */
|
|
142
|
+
label: React.ReactNode;
|
|
143
|
+
/** The big number. */
|
|
144
|
+
value: React.ReactNode;
|
|
145
|
+
/** Small line under it. */
|
|
146
|
+
sub?: React.ReactNode;
|
|
147
|
+
/** accent: tinted tile; good / bad: coloured number. */
|
|
148
|
+
tone?: 'accent' | 'good' | 'bad' | (string & {});
|
|
149
|
+
}
|
|
150
|
+
/** <Kpi label="Revenue" value="฿48,250" sub="This month" /> */
|
|
151
|
+
export const Kpi: React.ForwardRefExoticComponent<KpiProps & React.RefAttributes<HTMLDivElement>>;
|
|
152
|
+
|
|
153
|
+
export interface EmptyProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
|
|
154
|
+
/** Icon name or node (default 'inbox'). */
|
|
155
|
+
icon?: IconLike;
|
|
156
|
+
title: React.ReactNode;
|
|
157
|
+
body?: React.ReactNode;
|
|
158
|
+
/** A button or link under the text. */
|
|
159
|
+
action?: React.ReactNode;
|
|
160
|
+
}
|
|
161
|
+
/** <Empty icon="search" title="No bikes match" body="Try a shorter word." action={…} /> */
|
|
162
|
+
export const Empty: React.ForwardRefExoticComponent<EmptyProps & React.RefAttributes<HTMLDivElement>>;
|
|
163
|
+
|
|
164
|
+
export interface TableColumn<R = any> {
|
|
165
|
+
/** Field of the row to show (and the React key of the column). */
|
|
166
|
+
key: string;
|
|
167
|
+
/** Column title (default: key). */
|
|
168
|
+
title?: React.ReactNode;
|
|
169
|
+
/** 'right' for numbers. */
|
|
170
|
+
align?: 'left' | 'right';
|
|
171
|
+
/** Draw the cell yourself. */
|
|
172
|
+
render?: (row: R, index: number) => React.ReactNode;
|
|
173
|
+
className?: string;
|
|
174
|
+
}
|
|
175
|
+
export interface TableProps<R = any> extends React.TableHTMLAttributes<HTMLTableElement> {
|
|
176
|
+
/** Columns; with rows, the table draws its own head and body. Leave out to write <thead>/<tbody> as children. */
|
|
177
|
+
columns?: TableColumn<R>[];
|
|
178
|
+
rows?: R[];
|
|
179
|
+
/** A field name or function giving each row a stable key (default 'id'). */
|
|
180
|
+
rowKey?: string | ((row: R, index: number) => React.Key);
|
|
181
|
+
/** Rows for a <tfoot> (e.g. a total row). */
|
|
182
|
+
foot?: React.ReactNode;
|
|
183
|
+
/** Wrap in .gk-table-wrap so it scrolls sideways on small screens (default true). */
|
|
184
|
+
wrap?: boolean;
|
|
185
|
+
/** Rows slide in when they first appear (also rows added later). */
|
|
186
|
+
slideRows?: boolean;
|
|
187
|
+
}
|
|
188
|
+
/** <Table columns={[{ key: 'bike', title: 'Bike' }]} rows={bookings} /> */
|
|
189
|
+
export const Table: React.ForwardRefExoticComponent<TableProps & React.RefAttributes<HTMLTableElement>>;
|
|
190
|
+
|
|
191
|
+
export interface MeterProps extends SpanAttrs {
|
|
192
|
+
/** How full, 0 to 1. */
|
|
193
|
+
value: number;
|
|
194
|
+
tone?: 'good' | 'warn' | 'bad' | (string & {});
|
|
195
|
+
/** Seconds before it starts filling the first time (default 0.15). */
|
|
196
|
+
delay?: number;
|
|
197
|
+
}
|
|
198
|
+
/** <Meter value={0.7} tone="good" /> */
|
|
199
|
+
export const Meter: React.ForwardRefExoticComponent<MeterProps & React.RefAttributes<HTMLSpanElement>>;
|
|
200
|
+
|
|
201
|
+
export interface RingProps extends SpanAttrs {
|
|
202
|
+
/** How full, 0 to 1. */
|
|
203
|
+
value: number;
|
|
204
|
+
/** Width and height in px (default 56). */
|
|
205
|
+
size?: number;
|
|
206
|
+
/** Line thickness in px (default 6). */
|
|
207
|
+
stroke?: number;
|
|
208
|
+
/** In the middle, e.g. "72%". */
|
|
209
|
+
label?: React.ReactNode;
|
|
210
|
+
}
|
|
211
|
+
/** <Ring value={0.72} label="72%" /> */
|
|
212
|
+
export const Ring: React.ForwardRefExoticComponent<RingProps & React.RefAttributes<HTMLSpanElement>>;
|
|
213
|
+
|
|
214
|
+
export interface StepsProps extends React.OlHTMLAttributes<HTMLOListElement> {
|
|
215
|
+
labels: React.ReactNode[];
|
|
216
|
+
/** The current step, from 0; steps before it are done. */
|
|
217
|
+
now: number;
|
|
218
|
+
}
|
|
219
|
+
/** <Steps labels={['Bike', 'Dates', 'Pay']} now={1} /> */
|
|
220
|
+
export const Steps: React.ForwardRefExoticComponent<StepsProps & React.RefAttributes<HTMLOListElement>>;
|
|
221
|
+
|
|
222
|
+
export interface BarsProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
223
|
+
values: number[];
|
|
224
|
+
/** The value of a full-height bar (default: the biggest value). */
|
|
225
|
+
max?: number;
|
|
226
|
+
/** A caption under each bar. */
|
|
227
|
+
labels?: React.ReactNode[];
|
|
228
|
+
/** The number above each bar. */
|
|
229
|
+
showValues?: boolean;
|
|
230
|
+
/** How a shown value is written (default: as is). */
|
|
231
|
+
format?: (value: number, index: number) => React.ReactNode;
|
|
232
|
+
/** The tallest bar in full accent, the others lighter (default true). */
|
|
233
|
+
highlightTop?: boolean;
|
|
234
|
+
/** Chart height (default 210 px). */
|
|
235
|
+
height?: number | string;
|
|
236
|
+
/** What screen readers hear (default: the labels and values). */
|
|
237
|
+
label?: string;
|
|
238
|
+
}
|
|
239
|
+
/** <Bars values={[12, 30, 22]} labels={['Mon', 'Tue', 'Wed']} showValues /> */
|
|
240
|
+
export const Bars: React.ForwardRefExoticComponent<BarsProps & React.RefAttributes<HTMLDivElement>>;
|
|
241
|
+
|
|
242
|
+
export interface FieldProps extends React.LabelHTMLAttributes<HTMLLabelElement> {
|
|
243
|
+
/** The title above the control. */
|
|
244
|
+
label: React.ReactNode;
|
|
245
|
+
/** A small line under the control. */
|
|
246
|
+
hint?: React.ReactNode;
|
|
247
|
+
/** Red border and red hint. */
|
|
248
|
+
invalid?: boolean;
|
|
249
|
+
}
|
|
250
|
+
/** <Field label="Email" hint="…"><Input type="email" /></Field> */
|
|
251
|
+
export const Field: React.ForwardRefExoticComponent<FieldProps & React.RefAttributes<HTMLLabelElement>>;
|
|
252
|
+
|
|
253
|
+
export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'> {
|
|
254
|
+
/** 'sm': 34px tall. */
|
|
255
|
+
size?: 'sm' | (string & {});
|
|
256
|
+
}
|
|
257
|
+
/** <Input type="email" placeholder="you@shop.com" /> */
|
|
258
|
+
export const Input: React.ForwardRefExoticComponent<InputProps & React.RefAttributes<HTMLInputElement>>;
|
|
259
|
+
|
|
260
|
+
export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
|
261
|
+
/** Grows with each line up to this many lines (true = 6), then scrolls. */
|
|
262
|
+
autoGrow?: boolean | number;
|
|
263
|
+
size?: 'sm' | (string & {});
|
|
264
|
+
}
|
|
265
|
+
/** <Textarea rows={2} autoGrow={6} /> */
|
|
266
|
+
export const Textarea: React.ForwardRefExoticComponent<TextareaProps & React.RefAttributes<HTMLTextAreaElement>>;
|
|
267
|
+
|
|
268
|
+
export {};
|
package/src/basics.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* Markup pieces from @gecko-ui-kit/components as React components: they draw the same HTML as the
|
|
3
|
+
vanilla builders (btn(), tag(), card()…), so the same CSS styles them. Progress bits (Meter, Ring, Steps,
|
|
4
|
+
Bars) animate with the motion helpers from @gecko-ui-kit/motion when they appear and when the value changes. */
|
|
5
|
+
import { forwardRef, useRef, useState } from 'react';
|
|
6
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
7
|
+
import * as C from '@gecko-ui-kit/components';
|
|
8
|
+
import { h, cx, iconNode, useMotion, useIsoLayoutEffect } from './core.js';
|
|
9
|
+
|
|
10
|
+
const assignRef = (ref, el) => { if (typeof ref === 'function') ref(el); else if (ref) ref.current = el; };
|
|
11
|
+
const has = (x) => x != null && x !== false && x !== '';
|
|
12
|
+
|
|
13
|
+
/* ---------------- spinner ---------------- */
|
|
14
|
+
const SPIN_TONES = ['accent', 'ink', 'muted', 'good', 'warn', 'bad', 'current'];
|
|
15
|
+
/**
|
|
16
|
+
* <Spinner size={28} tone="accent" label="Loading bookings…" stack />
|
|
17
|
+
* Same markup as spinner({ size, tone, speed, track, label, stack, words, decorative }): an orbit loader.
|
|
18
|
+
*/
|
|
19
|
+
export const Spinner = forwardRef(function Spinner({ size, tone = 'accent', speed = 1, track = true, label, stack = false, words, decorative = false, className, style, ...rest }, ref) {
|
|
20
|
+
// reduceMotion(true) forced from code: the CSS media query covers the OS setting, this covers the rest (after mount, so the server HTML matches)
|
|
21
|
+
const [still, setStill] = useState(false);
|
|
22
|
+
useIsoLayoutEffect(() => { if (M.RM()) setStill(true); }, []);
|
|
23
|
+
const t = SPIN_TONES.includes(tone) ? tone : 'accent';
|
|
24
|
+
const st = { ...style };
|
|
25
|
+
if (has(size)) st.fontSize = typeof size === 'number' ? size + 'px' : size;
|
|
26
|
+
if (+speed && +speed !== 1) st['--gk-orb-speed'] = +speed + 's';
|
|
27
|
+
const text = has(label) ? h('span', { className: 'gk-orb__label' }, label)
|
|
28
|
+
: decorative ? null : h('span', { className: 'gk-orb__sr' }, words?.loading ?? 'Loading…');
|
|
29
|
+
return h('span', {
|
|
30
|
+
ref, ...(decorative ? { 'aria-hidden': true } : { role: 'status' }), ...rest, style: st,
|
|
31
|
+
className: cx('gk-orb', 'gk-orb--' + t, track === false && 'gk-orb--bare', stack && has(label) && 'gk-orb--stack', still && 'gk-orb--still', className),
|
|
32
|
+
}, h('span', { className: 'gk-orb__dial', 'aria-hidden': true }, h('span', { className: 'gk-orb__spin' })), text);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/* ---------------- buttons ---------------- */
|
|
36
|
+
/**
|
|
37
|
+
* <Button variant="primary" size="sm" icon="check" onClick={save}>Save</Button>
|
|
38
|
+
* Same as btn('Save', { variant, size, icon, iconRight, block, magnetic }). Any other prop goes on the <button>.
|
|
39
|
+
* busy: locks the button (disabled + aria-busy), swaps the icon for a spinner and the label for busyText.
|
|
40
|
+
*/
|
|
41
|
+
export const Button = forwardRef(function Button({ variant, size, icon, iconRight, block, magnetic, busy = false, busyText, type = 'button', className, children, disabled, ...rest }, ref) {
|
|
42
|
+
const mag = useMotion((el) => (magnetic ? M.magnetic(el) : undefined), [magnetic]);
|
|
43
|
+
const setRef = (el) => { mag.current = el; assignRef(ref, el); };
|
|
44
|
+
const label = busy && has(busyText) ? busyText : children;
|
|
45
|
+
const spin = busy ? h(Spinner, { size: '1.15em', tone: 'current', decorative: true }) : null;
|
|
46
|
+
return h('button', {
|
|
47
|
+
ref: setRef, type, ...rest, disabled: disabled || busy || undefined, 'aria-busy': busy || undefined,
|
|
48
|
+
className: cx('gk-btn', variant && 'gk-btn--' + variant, size && 'gk-btn--' + size, block && 'gk-btn--block', magnetic && 'gk-magnetic', className),
|
|
49
|
+
}, busy ? (icon || !iconRight ? spin : null) : iconNode(icon),
|
|
50
|
+
label != null && label !== false ? h('span', null, label) : null,
|
|
51
|
+
busy && !icon && iconRight ? spin : iconNode(iconRight));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* <IconButton icon="bell" label="Notifications" size="sm" />
|
|
56
|
+
* Same as iconBtn(icon, label, { size }). Pass pressed / defaultPressed to make it an on/off button
|
|
57
|
+
* (aria-pressed, pops when pressed): onChange(pressed, event).
|
|
58
|
+
*/
|
|
59
|
+
export const IconButton = forwardRef(function IconButton({ icon, label, size, pressed, defaultPressed, onChange, onClick, type = 'button', className, children, ...rest }, ref) {
|
|
60
|
+
const toggle = pressed !== undefined || defaultPressed !== undefined;
|
|
61
|
+
const [inner, setInner] = useState(!!defaultPressed);
|
|
62
|
+
const on = pressed !== undefined ? !!pressed : inner;
|
|
63
|
+
return h('button', {
|
|
64
|
+
ref, type, 'aria-label': label, ...(toggle ? { 'aria-pressed': on } : null), ...rest,
|
|
65
|
+
className: cx('gk-iconbtn', size && 'gk-iconbtn--' + size, className),
|
|
66
|
+
onClick: (e) => {
|
|
67
|
+
onClick?.(e);
|
|
68
|
+
if (!toggle || e.defaultPrevented) return;
|
|
69
|
+
if (pressed === undefined) setInner(!on);
|
|
70
|
+
M.pop(e.currentTarget); onChange?.(!on, e);
|
|
71
|
+
},
|
|
72
|
+
}, iconNode(icon), children);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/* ---------------- labels ---------------- */
|
|
76
|
+
/** <Tag tone="good">Paid</Tag> — same as tag('Paid', 'good', dot). tone: good | warn | bad | accent | plain */
|
|
77
|
+
export const Tag = forwardRef(function Tag({ tone = 'plain', dot = true, icon, className, children, ...rest }, ref) {
|
|
78
|
+
return h('span', { ref, ...rest, className: cx('gk-tag', 'gk-tag--' + (tone || 'plain'), className) }, dot && !icon ? h('i') : null, iconNode(icon), children);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** <Badge tone="accent">3</Badge> — same as badge(3, 'accent'). */
|
|
82
|
+
export const Badge = forwardRef(function Badge({ tone, className, children, ...rest }, ref) {
|
|
83
|
+
return h('span', { ref, ...rest, className: cx('gk-badge', tone && 'gk-badge--' + tone, className) }, children);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/* ---------------- people ---------------- */
|
|
87
|
+
/**
|
|
88
|
+
* <Avatar name="Pim Wong" tone="teal" size="sm" online />
|
|
89
|
+
* Same as av({ name, ini, tone }, size, online). No name and no ini: an empty "open slot" circle with a plus.
|
|
90
|
+
*/
|
|
91
|
+
export const Avatar = forwardRef(function Avatar({ name, ini, tone, size, online = false, className, ...rest }, ref) {
|
|
92
|
+
if (!has(name) && !has(ini)) return h('span', { ref, ...rest, className: cx('gk-av', 'gk-av--open', size && 'gk-av--' + size, className) }, iconNode('plus'));
|
|
93
|
+
return h('span', { ref, title: name, ...rest, className: cx('gk-av', 'gk-av--' + (tone || 'indigo'), size && 'gk-av--' + size, className) },
|
|
94
|
+
ini ?? C.initials(name || ''), online ? h('i', { className: 'gk-av__on' }) : null);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/** <AvatarGroup people={team} max={4} /> — same as avs(people, max): small overlapping avatars, then "+N". */
|
|
98
|
+
export const AvatarGroup = forwardRef(function AvatarGroup({ people = [], max = 4, className, ...rest }, ref) {
|
|
99
|
+
return h('span', { ref, ...rest, className: cx('gk-avs', className) },
|
|
100
|
+
people.slice(0, max).map((p, i) => h(Avatar, { key: p.id ?? p.name ?? i, name: p.name, ini: p.ini, tone: p.tone, size: 'sm' })),
|
|
101
|
+
people.length > max ? h('span', { className: 'gk-av gk-av--sm gk-av--more' }, '+' + (people.length - max)) : null);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/** <Cell name="Pim Wong" tone="teal" sub="Mechanic" online /> — same as cell(person, sub): avatar + name + small line. */
|
|
105
|
+
export const Cell = forwardRef(function Cell({ name, ini, tone, sub, online = false, className, ...rest }, ref) {
|
|
106
|
+
return h('span', { ref, ...rest, className: cx('gk-cell', className) },
|
|
107
|
+
h(Avatar, { name, ini, tone, online }),
|
|
108
|
+
h('span', { className: 'gk-cell__text' }, h('b', null, name ?? ''), sub !== undefined && sub !== null ? h('small', null, sub) : null));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/* ---------------- cards ---------------- */
|
|
112
|
+
/** <CardHead title="Today" sub="12 bookings" right={<Button size="sm">See all</Button>} /> — same as chead(title, sub, right). */
|
|
113
|
+
export const CardHead = forwardRef(function CardHead({ title, sub, right, className, ...rest }, ref) {
|
|
114
|
+
return h('div', { ref, ...rest, className: cx('gk-chead', className) },
|
|
115
|
+
h('div', { className: 'gk-chead__text' }, h('h2', null, title), has(sub) ? h('p', null, sub) : null),
|
|
116
|
+
has(right) ? h('div', { className: 'gk-chead__right' }, right) : null);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* <Card title="Today" sub="12 bookings" right={…} lift flush>…body…</Card>
|
|
121
|
+
* Same as card({ title, sub, right, body, flush, lift }): a <section class="gk-card">; the head shows when title is set.
|
|
122
|
+
*/
|
|
123
|
+
export const Card = forwardRef(function Card({ title, sub, right, flush = false, lift = false, className, children, ...rest }, ref) {
|
|
124
|
+
const el = useMotion((n) => (lift ? M.lift(n) : undefined), [lift]);
|
|
125
|
+
return h('section', {
|
|
126
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, ...(lift ? { 'data-lift': '' } : null), ...rest,
|
|
127
|
+
className: cx('gk-card', flush && 'gk-card--flush', className),
|
|
128
|
+
}, has(title) ? h(CardHead, { title, sub, right }) : null, children);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/** <Kpi label="Revenue" value="฿48,250" sub="This month" tone="good" /> — same as kpi(label, value, sub, cls). */
|
|
132
|
+
export const Kpi = forwardRef(function Kpi({ label, value, sub, tone, className, ...rest }, ref) {
|
|
133
|
+
return h('div', { ref, ...rest, className: cx('gk-kpi', tone && 'gk-kpi--' + tone, className) },
|
|
134
|
+
h('small', null, label), h('b', null, value), has(sub) ? h('span', { className: 'gk-kpi__sub' }, sub) : null);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/** <Empty icon="inbox" title="No bookings yet" body="New ones show up here." action={<Button>New booking</Button>} /> — same as empty(icon, title, body, action). */
|
|
138
|
+
export const Empty = forwardRef(function Empty({ icon = 'inbox', title, body, action, className, children, ...rest }, ref) {
|
|
139
|
+
return h('div', { ref, ...rest, className: cx('gk-empty', className) },
|
|
140
|
+
h('span', { className: 'gk-empty__icon' }, iconNode(icon)), h('b', null, title), has(body) ? h('p', null, body) : null, action ?? null, children);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/* ---------------- table ---------------- */
|
|
144
|
+
/**
|
|
145
|
+
* A table styled by .gk-table, inside .gk-table-wrap (scrolls sideways on small screens).
|
|
146
|
+
* Data: <Table columns={[{ key: 'bike', title: 'Bike' }, { key: 'days', title: 'Days', align: 'right' }]} rows={bookings} rowKey="id" />
|
|
147
|
+
* Your own: <Table><thead>…</thead><tbody>…</tbody></Table>
|
|
148
|
+
* slideRows: rows slide in when they first appear (also rows added later).
|
|
149
|
+
*/
|
|
150
|
+
export const Table = forwardRef(function Table({ columns, rows, rowKey = 'id', foot, wrap = true, slideRows = false, className, children, ...rest }, ref) {
|
|
151
|
+
const el = useRef(null), seen = useRef(null), tweens = useRef([]);
|
|
152
|
+
useIsoLayoutEffect(() => {
|
|
153
|
+
if (!slideRows || !el.current) return;
|
|
154
|
+
seen.current ||= new WeakSet();
|
|
155
|
+
const fresh = [...el.current.querySelectorAll('tbody > tr')].filter((r) => !seen.current.has(r));
|
|
156
|
+
fresh.forEach((r) => seen.current.add(r));
|
|
157
|
+
if (fresh.length) tweens.current = [...tweens.current.filter((t) => t.isActive()), M.slide(fresh, { y: 10, stagger: 0.04 })];
|
|
158
|
+
});
|
|
159
|
+
useIsoLayoutEffect(() => () => { tweens.current.forEach((t) => t.progress(1).kill()); tweens.current = []; seen.current = null; }, []);
|
|
160
|
+
const key = (r, i) => (typeof rowKey === 'function' ? rowKey(r, i) : r?.[rowKey] ?? i);
|
|
161
|
+
const al = (c) => (c.align === 'right' ? 'gk-r' : undefined);
|
|
162
|
+
const body = columns ? [
|
|
163
|
+
h('thead', { key: 'h' }, h('tr', null, columns.map((c) => h('th', { key: c.key, className: cx(al(c), c.className) || undefined }, c.title ?? c.key)))),
|
|
164
|
+
h('tbody', { key: 'b' }, (rows || []).map((r, i) => h('tr', { key: key(r, i) },
|
|
165
|
+
columns.map((c) => h('td', { key: c.key, className: cx(al(c), c.className) || undefined }, c.render ? c.render(r, i) : r?.[c.key]))))),
|
|
166
|
+
foot ? h('tfoot', { key: 'f' }, foot) : null,
|
|
167
|
+
] : children;
|
|
168
|
+
const table = h('table', { ref: (n) => { el.current = n; assignRef(ref, n); }, ...rest, className: cx('gk-table', className) }, body);
|
|
169
|
+
return wrap ? h('div', { className: 'gk-table-wrap' }, table) : table;
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/* ---------------- progress ---------------- */
|
|
173
|
+
const clamp01 = (f) => Math.max(0, Math.min(1, +f || 0));
|
|
174
|
+
|
|
175
|
+
/** <Meter value={0.7} tone="good" /> — same as meter(0.7, 'good'): fills from 0 when it appears, glides when value changes. */
|
|
176
|
+
export const Meter = forwardRef(function Meter({ value = 0, tone, delay = 0.15, className, ...rest }, ref) {
|
|
177
|
+
const bar = useRef(null), prev = useRef(null);
|
|
178
|
+
const f = clamp01(value);
|
|
179
|
+
useIsoLayoutEffect(() => {
|
|
180
|
+
const first = prev.current === null; prev.current = f;
|
|
181
|
+
const tw = first ? M.meter(bar.current, f, delay)
|
|
182
|
+
: M.g.to(bar.current, { scaleX: f, duration: M.D(0.75), ease: 'expo.out', transformOrigin: '0 50%', overwrite: 'auto' });
|
|
183
|
+
return () => { tw?.progress(1); tw?.kill(); };
|
|
184
|
+
}, [f]);
|
|
185
|
+
useIsoLayoutEffect(() => () => { prev.current = null; }, []);
|
|
186
|
+
return h('span', { ref, ...rest, className: cx('gk-meter', tone && 'gk-is-' + tone, className) },
|
|
187
|
+
h('i', { ref: bar, 'data-f': f, style: { transform: 'scaleX(0)' } }));
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* <Ring value={0.72} size={56} stroke={6} label="72%" /> — same as ring(0.72, 56, 6, label):
|
|
192
|
+
* the circle sweeps on from 0, and from the last value when value changes.
|
|
193
|
+
*/
|
|
194
|
+
export const Ring = forwardRef(function Ring({ value = 0, size = 56, stroke = 6, label, className, style, ...rest }, ref) {
|
|
195
|
+
const fg = useRef(null), prev = useRef(null);
|
|
196
|
+
const f = clamp01(value), r = (size - stroke) / 2, len = 2 * Math.PI * r, c = size / 2;
|
|
197
|
+
useIsoLayoutEffect(() => {
|
|
198
|
+
const from = prev.current ?? 0, first = prev.current === null; prev.current = f;
|
|
199
|
+
const tw = M.ring(fg.current, f, { from, delay: first ? 0.2 : 0 });
|
|
200
|
+
return () => { tw?.progress(1); tw?.kill(); };
|
|
201
|
+
}, [f, len]);
|
|
202
|
+
useIsoLayoutEffect(() => () => { prev.current = null; }, []);
|
|
203
|
+
return h('span', { ref, ...rest, className: cx('gk-ring', className), style: { width: size, height: size, ...style } },
|
|
204
|
+
h('svg', { viewBox: `0 0 ${size} ${size}` },
|
|
205
|
+
h('circle', { cx: c, cy: c, r, strokeWidth: stroke, className: 'gk-ring__bg' }),
|
|
206
|
+
h('circle', { ref: fg, cx: c, cy: c, r, strokeWidth: stroke, className: 'gk-ring__fg', 'data-len': len, 'data-f': f, strokeDasharray: len, strokeDashoffset: len })),
|
|
207
|
+
h('b', null, label));
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* <Steps labels={['Bike', 'Dates', 'Pay']} now={1} /> — same as steps(labels, now): done steps get a tick,
|
|
212
|
+
* the bars fill up to `now`, and the new dot pops when now changes.
|
|
213
|
+
*/
|
|
214
|
+
export const Steps = forwardRef(function Steps({ labels = [], now = 0, className, ...rest }, ref) {
|
|
215
|
+
const el = useRef(null), prev = useRef(null);
|
|
216
|
+
useIsoLayoutEffect(() => {
|
|
217
|
+
const p = prev.current === null ? now : prev.current; prev.current = now;
|
|
218
|
+
M.steps(el.current, now, p);
|
|
219
|
+
const parts = el.current ? [...el.current.querySelectorAll('.gk-steps__fill, .gk-steps__dot')] : [];
|
|
220
|
+
return () => { M.g.getTweensOf(parts).forEach((t) => t.progress(1).kill()); };
|
|
221
|
+
}, [now, labels.length]);
|
|
222
|
+
useIsoLayoutEffect(() => () => { prev.current = null; }, []);
|
|
223
|
+
return h('ol', { ref: (n) => { el.current = n; assignRef(ref, n); }, ...rest, className: cx('gk-steps', className) },
|
|
224
|
+
labels.map((l, i) => h('li', { key: i, className: i < now ? 'gk-is-done' : i === now ? 'gk-is-now' : undefined, 'aria-current': i === now ? 'step' : undefined },
|
|
225
|
+
h('span', { className: 'gk-steps__dot' }, iconNode('check')),
|
|
226
|
+
i < labels.length - 1 ? h('span', { className: 'gk-steps__bar' }, h('i', { className: 'gk-steps__fill', style: { transform: 'scaleX(0)' } })) : null,
|
|
227
|
+
h('small', null, l))));
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
/* Bars has no stylesheet of its own in the library yet, so its look is written inline (same look as the
|
|
231
|
+
playground's "Bars, meter & ring" chart). The class names are there for your own CSS. */
|
|
232
|
+
const BAR_S = {
|
|
233
|
+
root: { display: 'flex', gap: 8, width: '100%' },
|
|
234
|
+
col: { flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 },
|
|
235
|
+
track: { position: 'relative', flex: 1, width: '100%' },
|
|
236
|
+
bar: { position: 'absolute', inset: 0, borderRadius: '7px 7px 3px 3px', transform: 'scaleY(0)', transformOrigin: '50% 100%', transition: 'background .4s' },
|
|
237
|
+
v: { position: 'absolute', left: 0, right: 0, paddingBottom: 4, textAlign: 'center', fontSize: 11, fontWeight: 'var(--gk-w-bold)', color: 'var(--gk-ink-2)', fontVariantNumeric: 'tabular-nums', transition: 'bottom .75s var(--gk-ease-in-out, ease)' },
|
|
238
|
+
label: { color: 'var(--gk-muted)', fontSize: 11, fontWeight: 'var(--gk-w-semibold)', maxWidth: '100%', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* <Bars values={[12, 30, 22]} labels={['Mon', 'Tue', 'Wed']} max={40} showValues />
|
|
242
|
+
* A small column chart on bars(els, values, max): the bars grow up from the baseline, then morph when values change.
|
|
243
|
+
*/
|
|
244
|
+
export const Bars = forwardRef(function Bars({ values = [], max, labels, showValues = false, format = (v) => v, highlightTop = true, height = 210, label, className, style, ...rest }, ref) {
|
|
245
|
+
const el = useRef(null), first = useRef(true);
|
|
246
|
+
const top = Math.max(0, ...values.map((v) => +v || 0));
|
|
247
|
+
const m = max ?? (top || 1);
|
|
248
|
+
const key = values.join(',') + '|' + m;
|
|
249
|
+
useIsoLayoutEffect(() => {
|
|
250
|
+
const bars = el.current ? [...el.current.querySelectorAll('.gk-bars__bar')] : [];
|
|
251
|
+
M.bars(bars, values, m, { first: first.current }); first.current = false;
|
|
252
|
+
return () => { M.g.getTweensOf(bars).forEach((t) => t.progress(1).kill()); };
|
|
253
|
+
}, [key]);
|
|
254
|
+
useIsoLayoutEffect(() => () => { first.current = true; }, []);
|
|
255
|
+
return h('div', {
|
|
256
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, role: 'img', 'aria-label': label ?? values.map((v, i) => (labels?.[i] != null ? `${labels[i]}: ${v}` : v)).join(', '), ...rest,
|
|
257
|
+
className: cx('gk-bars', className), style: { ...BAR_S.root, height, ...style },
|
|
258
|
+
}, values.map((v, i) => {
|
|
259
|
+
const isTop = highlightTop && +v === top && top > 0;
|
|
260
|
+
return h('div', { key: i, className: cx('gk-bars__col', isTop && 'gk-is-top'), style: BAR_S.col },
|
|
261
|
+
h('span', { className: 'gk-bars__track', style: BAR_S.track },
|
|
262
|
+
h('i', { className: 'gk-bars__bar', style: { ...BAR_S.bar, background: isTop || !highlightTop ? 'var(--gk-accent)' : 'color-mix(in oklab, var(--gk-accent) 35%, var(--gk-card))' } }),
|
|
263
|
+
showValues ? h('b', { className: 'gk-bars__v', 'aria-hidden': true, style: { ...BAR_S.v, bottom: Math.min(100, ((+v || 0) / m) * 100) + '%' } }, format(v, i)) : null),
|
|
264
|
+
labels?.[i] != null ? h('small', { className: 'gk-bars__label', style: BAR_S.label }, labels[i]) : null);
|
|
265
|
+
}));
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
/* ---------------- form fields ---------------- */
|
|
269
|
+
/** <Field label="Email" hint="We only use it for reminders."><Input type="email" /></Field> — same as field(label, control, hint). invalid: red border + red hint. */
|
|
270
|
+
export const Field = forwardRef(function Field({ label, hint, invalid = false, className, children, ...rest }, ref) {
|
|
271
|
+
return h('label', { ref, ...rest, className: cx('gk-field', invalid && 'gk-is-bad', className) },
|
|
272
|
+
h('span', null, label), children, has(hint) ? h('small', { className: 'gk-field__hint' }, hint) : null);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
/** <Input type="email" placeholder="you@shop.com" size="sm" /> — same as input({ type, value, placeholder }): an <input class="gk-input">. Every <input> prop works. */
|
|
276
|
+
export const Input = forwardRef(function Input({ size, type = 'text', className, ...rest }, ref) {
|
|
277
|
+
return h('input', { ref, type, ...rest, className: cx('gk-input', size && 'gk-input--' + size, className) });
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* <Textarea rows={3} autoGrow={6} /> — a <textarea class="gk-input">. autoGrow: the box gets taller with each line,
|
|
282
|
+
* up to that many lines (true = 6), then scrolls.
|
|
283
|
+
*/
|
|
284
|
+
export const Textarea = forwardRef(function Textarea({ autoGrow = false, size, className, style, onInput, ...rest }, ref) {
|
|
285
|
+
const el = useRef(null);
|
|
286
|
+
const maxLines = autoGrow === true ? 6 : +autoGrow || 0;
|
|
287
|
+
const fit = () => {
|
|
288
|
+
const ta = el.current; if (!ta || !maxLines) return;
|
|
289
|
+
const s = getComputedStyle(ta), line = parseFloat(s.lineHeight) || 20;
|
|
290
|
+
const border = ta.offsetHeight - ta.clientHeight;
|
|
291
|
+
const max = line * maxLines + parseFloat(s.paddingTop) + parseFloat(s.paddingBottom) + border;
|
|
292
|
+
ta.style.height = 'auto';
|
|
293
|
+
const full = ta.scrollHeight + border;
|
|
294
|
+
ta.style.height = Math.min(full, max) + 'px';
|
|
295
|
+
ta.style.overflowY = full > max ? 'auto' : 'hidden';
|
|
296
|
+
};
|
|
297
|
+
useIsoLayoutEffect(() => {
|
|
298
|
+
fit();
|
|
299
|
+
const ta = el.current;
|
|
300
|
+
return () => { if (ta && maxLines) { ta.style.height = ''; ta.style.overflowY = ''; } };
|
|
301
|
+
}, [maxLines, rest.value]);
|
|
302
|
+
return h('textarea', {
|
|
303
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, ...rest,
|
|
304
|
+
style: maxLines ? { resize: 'none', ...style } : style,
|
|
305
|
+
onInput: (e) => { onInput?.(e); fit(); },
|
|
306
|
+
className: cx('gk-input', size && 'gk-input--' + size, className),
|
|
307
|
+
});
|
|
308
|
+
});
|