@momoi-labs/kiso-react 0.13.0 → 0.14.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.
@@ -0,0 +1,28 @@
1
+ import * as React from "react";
2
+ export type PaneGridLayout = {
3
+ rows: string[][];
4
+ sizes: Record<string, number>;
5
+ };
6
+ export type PaneGridOverflow = "wrap" | "scroll";
7
+ export type PaneGridProps = Omit<React.ComponentProps<"section">, "title"> & {
8
+ title?: React.ReactNode;
9
+ layout?: PaneGridLayout;
10
+ defaultLayout?: PaneGridLayout;
11
+ onLayoutChange?: (layout: PaneGridLayout) => void;
12
+ flow?: "rows" | "masonry";
13
+ overflow?: PaneGridOverflow;
14
+ fill?: boolean;
15
+ pack?: boolean;
16
+ scrollPages?: number;
17
+ debug?: boolean;
18
+ };
19
+ export declare function PaneGrid({ title, layout: controlled, defaultLayout, onLayoutChange, overflow, flow, fill, pack, scrollPages, debug, className, children, ...props }: PaneGridProps): React.JSX.Element;
20
+ export type GridPaneProps = Omit<React.ComponentProps<"article">, "title" | "id"> & {
21
+ id: string;
22
+ title: string;
23
+ min?: number;
24
+ size?: number;
25
+ newRow?: boolean;
26
+ actions?: React.ReactNode;
27
+ };
28
+ export declare function GridPane({ id, title, min: _min, size: _size, newRow: _newRow, actions, className, style, children, ...props }: GridPaneProps): React.JSX.Element;
@@ -0,0 +1,488 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { clsx as cn } from "clsx";
5
+ import { useIsomorphicLayoutEffect } from "./use-isomorphic-layout-effect.js";
6
+ const COLUMNS = 12;
7
+ /* A drag ends on release, on cancellation, or when capture is lost. */
8
+ const RELEASE = ["pointerup", "pointercancel", "lostpointercapture"];
9
+ /* DashboardGrid's breakpoints, measured on the grid's own width so a grid
10
+ beside a sidebar or panel gets the columns its space allows: above 1024px
11
+ twelve columns, above 640px six, then one. Unmeasured grids use twelve. */
12
+ const pickColumns = (width) => !width ? COLUMNS : width <= 640 ? 1 : width <= 1024 ? 6 : COLUMNS;
13
+ const PaneContext = React.createContext(null);
14
+ const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
15
+ /* Every pane appears exactly once, with a size at or above its minimum.
16
+ Panes missing from the layout go to the end, or to a row of their own. */
17
+ function normalize(layout, specs) {
18
+ const known = new Map(specs.map((spec) => [spec.id, spec]));
19
+ const seen = new Set();
20
+ const rows = (layout?.rows ?? [])
21
+ .map((row) => row.filter((id) => known.has(id) && !seen.has(id) && seen.add(id)))
22
+ .filter((row) => row.length);
23
+ for (const spec of specs) {
24
+ if (seen.has(spec.id))
25
+ continue;
26
+ if (spec.newRow || !rows.length)
27
+ rows.push([spec.id]);
28
+ else
29
+ rows[rows.length - 1].push(spec.id);
30
+ }
31
+ const sizes = {};
32
+ for (const spec of specs) {
33
+ sizes[spec.id] = clamp(Math.round(layout?.sizes[spec.id] ?? spec.size), spec.min, COLUMNS);
34
+ }
35
+ return { rows, sizes };
36
+ }
37
+ function sameLayout(a, b) {
38
+ return (a.rows.length === b.rows.length &&
39
+ a.rows.every((row, index) => row.length === b.rows[index].length && row.every((id, at) => id === b.rows[index][at])) &&
40
+ Object.keys(a.sizes).length === Object.keys(b.sizes).length &&
41
+ Object.entries(a.sizes).every(([id, size]) => b.sizes[id] === size));
42
+ }
43
+ /* Lay a row's panes out on lines. In order by default, so the person's
44
+ arrangement holds; with `pack`, big panes first and small ones fill the
45
+ holes left behind. With `scroll`, one line however wide. `fill` hands a
46
+ line's leftover columns to its panes, for display only. */
47
+ function layRow(ids, specs, sizes, columns, options) {
48
+ const scale = (value) => clamp(Math.ceil((value * columns) / COLUMNS), 1, columns);
49
+ let items = ids.map((id) => ({ id, size: scale(sizes[id]), min: scale(specs.get(id).min), col: 1, span: 0 }));
50
+ if (options.pack && !options.scroll)
51
+ items = [...items].sort((a, b) => b.size - a.size);
52
+ const lines = [];
53
+ for (const item of items) {
54
+ let line = options.pack && !options.scroll ? lines.find((line) => line.used + item.size <= columns) : lines[lines.length - 1];
55
+ if (!line || (!options.scroll && line.used + item.size > columns)) {
56
+ line = { used: 0, free: 0, scroll: false, items: [] };
57
+ lines.push(line);
58
+ }
59
+ line.items.push(item);
60
+ line.used += item.size;
61
+ }
62
+ for (const line of lines) {
63
+ line.scroll = options.scroll && line.used > columns;
64
+ line.free = Math.max(0, columns - line.used);
65
+ const extra = line.items.map(() => 0);
66
+ if (options.fill && !line.scroll)
67
+ for (let left = line.free, at = 0; left > 0; left--, at++)
68
+ extra[at % extra.length]++;
69
+ let col = 1;
70
+ line.items.forEach((item, index) => {
71
+ item.col = col;
72
+ item.span = item.size + extra[index];
73
+ col += item.span;
74
+ });
75
+ }
76
+ return lines;
77
+ }
78
+ export function PaneGrid({ title, layout: controlled, defaultLayout, onLayoutChange, overflow = "wrap", flow = "rows", fill = false, pack = false, scrollPages = 2, debug = false, className, children, ...props }) {
79
+ const elements = new Map();
80
+ const specs = [];
81
+ for (const child of React.Children.toArray(children)) {
82
+ if (!React.isValidElement(child) || typeof child.props.id !== "string")
83
+ continue;
84
+ elements.set(child.props.id, child);
85
+ specs.push({ id: child.props.id, min: child.props.min ?? 1, size: child.props.size ?? 6, newRow: !!child.props.newRow });
86
+ }
87
+ const specMap = new Map(specs.map((spec) => [spec.id, spec]));
88
+ const [internal, setInternal] = React.useState(() => normalize(defaultLayout, specs));
89
+ const layout = normalize(controlled ?? internal, specs);
90
+ const update = (next) => {
91
+ if (sameLayout(next, layout))
92
+ return;
93
+ if (!controlled)
94
+ setInternal(next);
95
+ onLayoutChange?.(next);
96
+ };
97
+ const bodyRef = React.useRef(null);
98
+ const dropRef = React.useRef(null);
99
+ const [width, setWidth] = React.useState(0);
100
+ useIsomorphicLayoutEffect(() => {
101
+ const body = bodyRef.current;
102
+ if (!body || typeof ResizeObserver === "undefined")
103
+ return;
104
+ const measure = () => setWidth(body.clientWidth);
105
+ measure();
106
+ const observer = new ResizeObserver(measure);
107
+ observer.observe(body);
108
+ return () => observer.disconnect();
109
+ }, []);
110
+ const columns = pickColumns(width);
111
+ const compact = columns !== COLUMNS;
112
+ const stacked = columns === 1;
113
+ const scrolling = overflow === "scroll" && !stacked;
114
+ const masonry = flow === "masonry" && overflow === "wrap";
115
+ const scrollMax = columns * Math.max(1, scrollPages);
116
+ let gridRow = 0;
117
+ const rows = layout.rows.map((ids) => {
118
+ const lines = layRow(ids, specMap, layout.sizes, columns, { fill: fill && !masonry, pack, scroll: scrolling }).map((line) => ({ ...line, row: ++gridRow }));
119
+ return { ids, lines };
120
+ });
121
+ // Pixel tracks keep natural heights while sharing the same column grid.
122
+ // Observe direct children only: nested PaneGrids own their measurements.
123
+ useIsomorphicLayoutEffect(() => {
124
+ const body = bodyRef.current;
125
+ if (!body || !masonry)
126
+ return;
127
+ const panes = Array.from(body.children).filter((node) => node instanceof HTMLElement && node.hasAttribute("data-pane-id"));
128
+ const byId = new Map(panes.map(pane => [pane.dataset.paneId, pane]));
129
+ const measure = () => {
130
+ const gap = parseFloat(getComputedStyle(body).columnGap) || 0;
131
+ const heights = new Map(panes.map(pane => [pane.dataset.paneId, pane.getBoundingClientRect().height]));
132
+ let base = 0;
133
+ rows.forEach((row, index) => {
134
+ const bottoms = Array(columns).fill(base);
135
+ const items = row.lines.flatMap(line => line.items);
136
+ if (pack)
137
+ items.sort((a, b) => b.size - a.size);
138
+ for (const item of items) {
139
+ const pane = byId.get(item.id);
140
+ if (!pane)
141
+ continue;
142
+ let col = 0;
143
+ let top = Infinity;
144
+ for (let start = 0; start <= columns - item.span; start++) {
145
+ const candidate = Math.max(...bottoms.slice(start, start + item.span));
146
+ if (candidate < top) {
147
+ col = start;
148
+ top = candidate;
149
+ }
150
+ }
151
+ const height = Math.ceil(heights.get(item.id) ?? 0);
152
+ pane.style.setProperty("--masonry-r", String(Math.ceil(top) + 1));
153
+ pane.style.setProperty("--masonry-c", String(col + 1));
154
+ pane.style.setProperty("--masonry-h", String(Math.max(1, height)));
155
+ for (let at = col; at < col + item.span; at++)
156
+ bottoms[at] = top + height + gap;
157
+ }
158
+ const rule = body.querySelector(`:scope > [data-pane-row="${index}"]`);
159
+ rule?.style.setProperty("--masonry-r", String(Math.ceil(base) + 1));
160
+ base = Math.max(...bottoms);
161
+ });
162
+ };
163
+ measure();
164
+ if (typeof ResizeObserver === "undefined")
165
+ return;
166
+ let frame = 0;
167
+ const observer = new ResizeObserver(() => {
168
+ cancelAnimationFrame(frame);
169
+ frame = requestAnimationFrame(measure);
170
+ });
171
+ observer.observe(body);
172
+ panes.forEach(pane => observer.observe(pane));
173
+ return () => { observer.disconnect(); cancelAnimationFrame(frame); };
174
+ });
175
+ /* Handlers read the latest render through this ref, so a drag that spans
176
+ several renders never acts on a stale layout. */
177
+ const state = React.useRef({ layout, rows, columns, compact, stacked, scrolling, masonry, scrollMax, update });
178
+ state.current = { layout, rows, columns, compact, stacked, scrolling, masonry, scrollMax, update };
179
+ const specsRef = React.useRef(specMap);
180
+ specsRef.current = specMap;
181
+ const api = React.useMemo(() => {
182
+ const paneElement = (id) => bodyRef.current?.querySelector(`:scope > [data-pane-id="${CSS.escape(id)}"], :scope > .pane-grid-scroll > [data-pane-id="${CSS.escape(id)}"]`);
183
+ const lineOf = (id) => {
184
+ for (const row of state.current.rows)
185
+ for (const line of row.lines)
186
+ if (line.items.some((item) => item.id === id))
187
+ return line;
188
+ return undefined;
189
+ };
190
+ const gaps = () => {
191
+ const style = bodyRef.current ? getComputedStyle(bodyRef.current) : null;
192
+ return { x: parseFloat(style?.columnGap ?? "0") || 0, y: parseFloat((state.current.masonry ? style?.columnGap : style?.rowGap) ?? "0") || 0 };
193
+ };
194
+ const maxOf = (id) => {
195
+ const { layout, columns, scrolling, masonry, scrollMax } = state.current;
196
+ const line = lineOf(id);
197
+ const size = layout.sizes[id];
198
+ if (masonry)
199
+ return columns;
200
+ if (!line)
201
+ return size;
202
+ return scrolling ? Math.min(columns, size + scrollMax - line.used) : size + line.free;
203
+ };
204
+ const resize = (id, next) => {
205
+ const { layout, compact, update } = state.current;
206
+ if (compact)
207
+ return;
208
+ const spec = specsRef.current.get(id);
209
+ if (!spec)
210
+ return;
211
+ const size = clamp(Math.round(next), spec.min, maxOf(id));
212
+ if (size === layout.sizes[id])
213
+ return;
214
+ update({ rows: layout.rows, sizes: { ...layout.sizes, [id]: size } });
215
+ };
216
+ const startResize = (id, event) => {
217
+ const { layout, columns, compact } = state.current;
218
+ const body = bodyRef.current;
219
+ const pane = paneElement(id);
220
+ if (compact || !body || !pane)
221
+ return;
222
+ event.preventDefault();
223
+ const handle = event.currentTarget;
224
+ handle.setPointerCapture(event.pointerId);
225
+ const startX = event.clientX;
226
+ const startSize = layout.sizes[id];
227
+ const columnWidth = (body.clientWidth - gaps().x * (columns - 1)) / columns + gaps().x;
228
+ handle.classList.add("dragging");
229
+ pane.classList.add("resizing");
230
+ const move = (moved) => resize(id, startSize + Math.round((moved.clientX - startX) / columnWidth));
231
+ const stop = () => {
232
+ handle.classList.remove("dragging");
233
+ pane.classList.remove("resizing");
234
+ handle.removeEventListener("pointermove", move);
235
+ for (const type of RELEASE)
236
+ handle.removeEventListener(type, stop);
237
+ };
238
+ handle.addEventListener("pointermove", move);
239
+ for (const type of RELEASE)
240
+ handle.addEventListener(type, stop);
241
+ };
242
+ /* Where a dragged pane lands: before or after a pane on one of a row's
243
+ lines, in the whole gap above a row as a row of its own, or after the
244
+ last row. */
245
+ const hitTest = (x, y, self) => {
246
+ const { rows, stacked, masonry } = state.current;
247
+ const rect = (id) => paneElement(id)?.getBoundingClientRect();
248
+ for (const row of rows) {
249
+ const bands = row.lines.map((line) => {
250
+ const rects = line.items.map((item) => rect(item.id)).filter((box) => !!box);
251
+ return { line, top: Math.min(...rects.map((box) => box.top)), bottom: Math.max(...rects.map((box) => box.bottom)) };
252
+ });
253
+ if (!bands.length)
254
+ continue;
255
+ const top = Math.min(...bands.map(band => band.top));
256
+ const bottom = Math.max(...bands.map(band => band.bottom));
257
+ if (y < top)
258
+ return { ref: row.ids[0], where: "newrow-before" };
259
+ if (y > bottom)
260
+ continue;
261
+ if (masonry) {
262
+ const candidates = row.ids.filter(id => id !== self).flatMap(id => {
263
+ const box = rect(id);
264
+ if (!box)
265
+ return [];
266
+ const dx = Math.max(box.left - x, 0, x - box.right);
267
+ const dy = Math.max(box.top - y, 0, y - box.bottom);
268
+ return [{ id, box, distance: Math.hypot(dx, dy) }];
269
+ }).sort((a, b) => a.distance - b.distance);
270
+ const nearest = candidates[0];
271
+ if (!nearest)
272
+ return null;
273
+ const before = stacked ? y < nearest.box.top + nearest.box.height / 2 : x < nearest.box.left + nearest.box.width / 2;
274
+ return { ref: nearest.id, where: before ? "before" : "after" };
275
+ }
276
+ const band = bands.reduce((near, next) => Math.abs((next.top + next.bottom) / 2 - y) < Math.abs((near.top + near.bottom) / 2 - y) ? next : near);
277
+ const items = band.line.items.filter((item) => item.id !== self);
278
+ if (!items.length) {
279
+ const others = row.ids.filter((id) => id !== self);
280
+ return others.length ? { ref: others[others.length - 1], where: "after" } : null;
281
+ }
282
+ for (const item of items) {
283
+ const box = rect(item.id);
284
+ if (!box)
285
+ continue;
286
+ const before = stacked ? y < box.top + box.height / 2 : x < box.left + box.width / 2;
287
+ if (before)
288
+ return { ref: item.id, where: "before" };
289
+ }
290
+ return { ref: items[items.length - 1].id, where: "after" };
291
+ }
292
+ return { ref: null, where: "newrow-end" };
293
+ };
294
+ const paint = (target) => {
295
+ const drop = dropRef.current;
296
+ const body = bodyRef.current;
297
+ if (!drop || !body)
298
+ return;
299
+ if (!target) {
300
+ drop.hidden = true;
301
+ return;
302
+ }
303
+ const gap = gaps().y;
304
+ const box = body.getBoundingClientRect();
305
+ drop.hidden = false;
306
+ drop.dataset.invalid = String(!!target.invalid);
307
+ const ref = target.ref ? paneElement(target.ref)?.getBoundingClientRect() : null;
308
+ const across = (top) => {
309
+ drop.dataset.axis = "y";
310
+ Object.assign(drop.style, { left: "0px", width: `${box.width}px`, height: "", top: `${top}px` });
311
+ };
312
+ if (target.where === "newrow-end")
313
+ across(box.height + gap / 2);
314
+ else if (!ref)
315
+ drop.hidden = true;
316
+ else if (target.where === "newrow-before") {
317
+ const row = state.current.rows.find(row => row.ids.includes(target.ref));
318
+ const top = row ? Math.min(...row.ids.map(id => paneElement(id)?.getBoundingClientRect().top ?? ref.top)) : ref.top;
319
+ across(top - box.top - gap / 2 - 1.5);
320
+ }
321
+ else if (state.current.stacked)
322
+ across((target.where === "before" ? ref.top - gap / 2 : ref.bottom + gap / 2) - box.top - 1.5);
323
+ else {
324
+ drop.dataset.axis = "x";
325
+ const left = (target.where === "before" ? ref.left - gap / 2 : ref.right + gap / 2) - box.left - 1.5;
326
+ Object.assign(drop.style, { top: `${ref.top - box.top}px`, height: `${ref.height}px`, left: `${left}px`, width: "" });
327
+ }
328
+ };
329
+ const plan = (id, target) => {
330
+ const rows = state.current.layout.rows.map((row) => row.filter((other) => other !== id));
331
+ if (target.where === "before" || target.where === "after") {
332
+ const row = rows.find((row) => target.ref !== null && row.includes(target.ref));
333
+ if (!row)
334
+ return null;
335
+ row.splice(row.indexOf(target.ref) + (target.where === "after" ? 1 : 0), 0, id);
336
+ }
337
+ else if (target.where === "newrow-before") {
338
+ const at = rows.findIndex((row) => target.ref !== null && row.includes(target.ref));
339
+ if (at < 0)
340
+ return null;
341
+ rows.splice(at, 0, [id]);
342
+ }
343
+ else
344
+ rows.push([id]);
345
+ return rows.filter((row) => row.length);
346
+ };
347
+ const exceeds = (id, target) => {
348
+ const { layout, scrolling, scrollMax, columns } = state.current;
349
+ if (!scrolling || (target.where !== "before" && target.where !== "after"))
350
+ return false;
351
+ const row = layout.rows.find((row) => target.ref !== null && row.includes(target.ref));
352
+ if (!row)
353
+ return false;
354
+ const scale = (value) => clamp(Math.ceil((value * columns) / COLUMNS), 1, columns);
355
+ return row.filter((other) => other !== id).reduce((sum, other) => sum + scale(layout.sizes[other]), 0) + scale(layout.sizes[id]) > scrollMax;
356
+ };
357
+ const startMove = (id, event) => {
358
+ const pane = paneElement(id);
359
+ if (!pane)
360
+ return;
361
+ if (event.target.closest("button, a, input, select, textarea, [role='button']:not(.grid-pane-head)"))
362
+ return;
363
+ event.preventDefault();
364
+ const handle = event.currentTarget;
365
+ handle.setPointerCapture(event.pointerId);
366
+ const start = { x: event.clientX, y: event.clientY };
367
+ let active = false;
368
+ let target = null;
369
+ const move = (moved) => {
370
+ if (!active) {
371
+ if (Math.hypot(moved.clientX - start.x, moved.clientY - start.y) < 6)
372
+ return;
373
+ active = true;
374
+ pane.classList.add("dragging");
375
+ if (bodyRef.current)
376
+ bodyRef.current.dataset.dragging = "true";
377
+ }
378
+ target = hitTest(moved.clientX, moved.clientY, id);
379
+ if (target)
380
+ target.invalid = exceeds(id, target);
381
+ paint(target);
382
+ };
383
+ const stop = () => {
384
+ handle.removeEventListener("pointermove", move);
385
+ for (const type of RELEASE)
386
+ handle.removeEventListener(type, stop);
387
+ pane.classList.remove("dragging");
388
+ delete bodyRef.current?.dataset.dragging;
389
+ paint(null);
390
+ if (!active || !target || target.invalid)
391
+ return;
392
+ const rows = plan(id, target);
393
+ if (rows)
394
+ state.current.update({ rows, sizes: state.current.layout.sizes });
395
+ };
396
+ handle.addEventListener("pointermove", move);
397
+ for (const type of RELEASE)
398
+ handle.addEventListener(type, stop);
399
+ };
400
+ const moveBy = (id, direction) => {
401
+ const { layout, update } = state.current;
402
+ const order = layout.rows.flat();
403
+ const at = order.indexOf(id);
404
+ const to = at + direction;
405
+ if (at < 0 || to < 0 || to >= order.length)
406
+ return;
407
+ const other = order[to];
408
+ const rows = layout.rows.map((row) => row.map((entry) => (entry === id ? other : entry === other ? id : entry)));
409
+ update({ rows, sizes: layout.sizes });
410
+ };
411
+ /* A pane at the start of a row rejoins the row above; anywhere else it
412
+ starts a row of its own with the panes after it. */
413
+ const toggleRow = (id) => {
414
+ const { layout, update } = state.current;
415
+ const rows = layout.rows.map((row) => [...row]);
416
+ const at = rows.findIndex((row) => row.includes(id));
417
+ if (at < 0)
418
+ return;
419
+ const index = rows[at].indexOf(id);
420
+ if (index > 0)
421
+ rows.splice(at + 1, 0, rows[at].splice(index));
422
+ else if (at > 0)
423
+ rows[at - 1].push(...rows.splice(at, 1)[0]);
424
+ else
425
+ return;
426
+ update({ rows, sizes: layout.sizes });
427
+ };
428
+ return { resize, startResize, startMove, moveBy, toggleRow };
429
+ }, []);
430
+ const placements = new Map();
431
+ for (const row of rows) {
432
+ for (const line of row.lines) {
433
+ for (const item of line.items) {
434
+ const max = masonry ? columns : scrolling ? Math.min(columns, item.size + scrollMax - line.used) : item.size + line.free;
435
+ placements.set(item.id, { id: item.id, row: line.scroll ? 1 : line.row, col: item.col, span: item.span, size: item.size, min: item.min, max, compact, stacked, debug });
436
+ }
437
+ }
438
+ }
439
+ const renderPane = (id) => {
440
+ const element = elements.get(id);
441
+ const placement = placements.get(id);
442
+ if (!element || !placement)
443
+ return null;
444
+ return (_jsx(PaneContext.Provider, { value: { placement, api }, children: element }, id));
445
+ };
446
+ return (_jsxs("section", { "data-slot": "pane-grid", className: cn("pane-grid", className), ...props, children: [title != null && _jsx("h2", { "data-slot": "pane-grid-title", className: "pane-grid-title t-h2", children: title }), _jsxs("div", { ref: bodyRef, "data-slot": "pane-grid-body", className: "pane-grid-body", "data-columns": columns, "data-compact": compact ? "true" : undefined, "data-overflow": overflow, "data-flow": masonry ? "masonry" : "rows", "data-debug": debug ? "true" : undefined, style: { "--pane-cols": columns, "--pane-grid-width": `${width}px` }, children: [rows.flatMap((row, index) => [
447
+ (index > 0 || debug) && (_jsx("div", { className: "pane-grid-rule", "data-pane-row": index, "aria-hidden": "true", style: { "--r": row.lines[0].row }, children: debug && `row ${index + 1} · ${row.lines.map((line) => `${line.used}/${columns}${line.scroll ? " ⇆ scroll" : ""}`).join(" + ")}` }, `rule-${index}`)),
448
+ ...(masonry ? row.ids.map(renderPane) : row.lines.flatMap((line) => line.scroll
449
+ ? [
450
+ _jsx("div", { className: "pane-grid-scroll", style: { "--r": line.row, "--n": line.used }, children: line.items.map((item) => renderPane(item.id)) }, `scroll-${line.items[0].id}`),
451
+ ]
452
+ : [
453
+ ...line.items.map((item) => renderPane(item.id)),
454
+ debug && !masonry && line.free > 0 && !fill && (_jsxs("div", { className: "pane-grid-free", "aria-hidden": "true", style: { "--r": line.row, "--c": line.used + 1, "--s": line.free }, children: ["free ", line.free] }, `free-${line.row}`)),
455
+ ])),
456
+ ]), _jsx("div", { ref: dropRef, className: "pane-grid-drop", "aria-hidden": "true", hidden: true })] })] }));
457
+ }
458
+ export function GridPane({ id, title, min: _min, size: _size, newRow: _newRow, actions, className, style, children, ...props }) {
459
+ const context = React.useContext(PaneContext);
460
+ if (!context)
461
+ throw new Error("GridPane must be a child of PaneGrid.");
462
+ const { placement, api } = context;
463
+ return (_jsxs("article", { "data-slot": "grid-pane", "data-pane-id": id, className: cn("grid-pane card", className), style: { "--r": placement.row, "--c": placement.col, "--s": placement.span, ...style }, ...props, children: [_jsxs("div", { "data-slot": "grid-pane-head", className: "grid-pane-head", role: "button", tabIndex: 0, "aria-label": `Move ${title}`, onPointerDown: (event) => api.startMove(id, event), onKeyDown: (event) => {
464
+ if (event.target !== event.currentTarget)
465
+ return;
466
+ if (event.key === "ArrowLeft" || (placement.stacked && event.key === "ArrowUp"))
467
+ api.moveBy(id, -1);
468
+ else if (event.key === "ArrowRight" || (placement.stacked && event.key === "ArrowDown"))
469
+ api.moveBy(id, 1);
470
+ else if (event.key === "Enter" || event.key === " ")
471
+ api.toggleRow(id);
472
+ else
473
+ return;
474
+ event.preventDefault();
475
+ }, children: [_jsx("h3", { "data-slot": "grid-pane-title", className: "grid-pane-title t-h3", children: title }), placement.debug && _jsxs("span", { className: "grid-pane-meta", children: ["size=", placement.size, " \u00B7 min=", placement.min, " \u00B7 max=", placement.max] }), actions != null && _jsx("div", { "data-slot": "grid-pane-actions", className: "grid-pane-actions", children: actions })] }), _jsx("div", { "data-slot": "grid-pane-body", className: "grid-pane-body", children: children }), !placement.compact && (_jsx("div", { "data-slot": "grid-pane-resizer", className: "grid-pane-resizer", role: "separator", tabIndex: 0, "aria-orientation": "vertical", "aria-label": `Resize ${title}`, "aria-valuenow": placement.size, "aria-valuemin": placement.min, "aria-valuemax": placement.max, "aria-disabled": placement.min === placement.max || undefined, onPointerDown: (event) => api.startResize(id, event), onKeyDown: (event) => {
476
+ if (event.key === "ArrowRight")
477
+ api.resize(id, placement.size + 1);
478
+ else if (event.key === "ArrowLeft")
479
+ api.resize(id, placement.size - 1);
480
+ else if (event.key === "Home")
481
+ api.resize(id, placement.min);
482
+ else if (event.key === "End")
483
+ api.resize(id, placement.max);
484
+ else
485
+ return;
486
+ event.preventDefault();
487
+ } }))] }));
488
+ }
@@ -0,0 +1,15 @@
1
+ import { type ComponentProps } from "react";
2
+ import { Input } from "./input.js";
3
+ declare const defaultLabels: {
4
+ show: string;
5
+ hide: string;
6
+ showName: string;
7
+ hideName: string;
8
+ shown: string;
9
+ hidden: string;
10
+ };
11
+ export type PasswordInputProps = Omit<ComponentProps<typeof Input>, "type"> & {
12
+ labels?: Partial<typeof defaultLabels>;
13
+ };
14
+ export declare function PasswordInput({ labels, id: providedId, disabled, ...props }: PasswordInputProps): import("react").JSX.Element;
15
+ export {};
@@ -0,0 +1,31 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useEffect, useId, useState } from "react";
4
+ import { Button } from "./button.js";
5
+ import { Input } from "./input.js";
6
+ const defaultLabels = {
7
+ show: "Show", hide: "Hide",
8
+ showName: "Show password", hideName: "Hide password",
9
+ shown: "Your password is visible", hidden: "Your password is hidden",
10
+ };
11
+ export function PasswordInput({ labels, id: providedId, disabled, ...props }) {
12
+ const generatedId = useId();
13
+ const id = providedId ?? generatedId;
14
+ const text = { ...defaultLabels, ...labels };
15
+ const [revealed, setRevealed] = useState(false);
16
+ const [status, setStatus] = useState("");
17
+ // Mask again on submit so the browser never saves the value as plain text.
18
+ useEffect(() => {
19
+ const form = document.getElementById(id)?.form;
20
+ if (!revealed || !form)
21
+ return;
22
+ const hide = () => { setRevealed(false); setStatus(text.hidden); };
23
+ form.addEventListener("submit", hide);
24
+ return () => form.removeEventListener("submit", hide);
25
+ }, [id, revealed, text.hidden]);
26
+ function toggle() {
27
+ setRevealed(!revealed);
28
+ setStatus(revealed ? text.hidden : text.shown);
29
+ }
30
+ return (_jsxs("div", { className: "field-control password-input", "data-slot": "password-input", "data-revealed": revealed, children: [_jsx(Input, { autoComplete: "current-password", autoCapitalize: "none", autoCorrect: "off", spellCheck: false, ...props, id: id, disabled: disabled, type: revealed ? "text" : "password" }), _jsxs(Button, { size: "sm", variant: "ghost", className: "password-input-toggle", "aria-controls": id, "aria-label": revealed ? text.hideName : text.showName, disabled: disabled, onClick: toggle, children: [_jsx("span", { "aria-hidden": revealed, children: text.show }), _jsx("span", { "aria-hidden": !revealed, children: text.hide })] }), _jsx("span", { className: "password-input-status", "aria-live": "polite", children: status })] }));
31
+ }
package/dist/search.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { clsx as cn } from "clsx";
3
+ import { Search as SearchIcon } from "lucide-react";
3
4
  import { Input } from "./input.js";
4
5
  // Filters a collection that is already on screen. Global commands are
5
6
  // CommandPalette; a single field is Input.
6
7
  function Search({ className, containerClassName, ...props }) {
7
- return (_jsxs("div", { "data-slot": "search", className: cn("input-group", containerClassName), children: [_jsxs("svg", { className: "icon", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "aria-hidden": "true", children: [_jsx("circle", { cx: "7", cy: "7", r: "4.5" }), _jsx("path", { d: "m10.5 10.5 3 3" })] }), _jsx(Input, { type: "search", className: className, ...props })] }));
8
+ return (_jsxs("div", { "data-slot": "search", className: cn("input-group", containerClassName), children: [_jsx(SearchIcon, { className: "icon", "aria-hidden": "true" }), _jsx(Input, { type: "search", className: className, ...props })] }));
8
9
  }
9
10
  export { Search };
package/dist/select.js CHANGED
@@ -2,6 +2,7 @@
2
2
  "use client";
3
3
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
4
4
  import { clsx as cn } from "clsx";
5
+ import { Check } from "lucide-react";
5
6
  import { Select as SelectPrimitive } from "radix-ui";
6
7
  function Select({ ...props }) {
7
8
  return _jsx(SelectPrimitive.Root, { "data-slot": "select", ...props });
@@ -22,7 +23,7 @@ function SelectLabel({ className, ...props }) {
22
23
  return (_jsx(SelectPrimitive.Label, { "data-slot": "select-label", className: cn("menu-label t-caps", className), ...props }));
23
24
  }
24
25
  function SelectItem({ className, children, ...props }) {
25
- return (_jsxs(SelectPrimitive.Item, { "data-slot": "select-item", className: cn("menu-item", className), ...props, children: [_jsx(SelectPrimitive.ItemText, { children: children }), _jsx(SelectPrimitive.ItemIndicator, { "data-slot": "select-item-indicator", "aria-hidden": "true", children: _jsx("svg", { className: "icon icon-sm", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", children: _jsx("path", { d: "m3 8 3.5 3.5L13 5" }) }) })] }));
26
+ return (_jsxs(SelectPrimitive.Item, { "data-slot": "select-item", className: cn("menu-item", className), ...props, children: [_jsx(SelectPrimitive.ItemText, { children: children }), _jsx(SelectPrimitive.ItemIndicator, { "data-slot": "select-item-indicator", "aria-hidden": "true", children: _jsx(Check, { className: "icon icon-sm" }) })] }));
26
27
  }
27
28
  function SelectSeparator({ className, ...props }) {
28
29
  return (_jsx(SelectPrimitive.Separator, { "data-slot": "select-separator", className: cn("menu-separator", className), ...props }));
@@ -1,4 +1,5 @@
1
1
  import * as React from "react";
2
+ import { type ChartStyle } from "./chart-style.js";
2
3
  export type SparklineProps = Omit<React.ComponentProps<"div">, "ref"> & {
3
4
  values: readonly (number | null)[];
4
5
  height?: number;
@@ -8,6 +9,7 @@ export type SparklineProps = Omit<React.ComponentProps<"div">, "ref"> & {
8
9
  max?: number;
9
10
  label?: string;
10
11
  ref?: React.Ref<HTMLDivElement>;
12
+ chartStyle?: ChartStyle;
11
13
  };
12
- declare function Sparkline({ values, height, tone, fill, min, max, label, className, style, ref, ...props }: SparklineProps): React.JSX.Element | null;
14
+ declare function Sparkline({ values, height, tone, fill, min, max, label, className, style, ref, chartStyle, ...props }: SparklineProps): React.JSX.Element | null;
13
15
  export { Sparkline };
package/dist/sparkline.js CHANGED
@@ -1,11 +1,19 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ // Sparkline: one metric series at cell size, no axis, grid, or tooltip.
4
+ // Contract: kiso/docs/components/sparkline.md. Recharts generates the path;
5
+ // the `.sparkline` frame in ui.css picks the color tokens.
6
+ import * as React from "react";
3
7
  import { Area, AreaChart, ResponsiveContainer, YAxis } from "recharts";
4
8
  import { clsx as cn } from "clsx";
5
- function Sparkline({ values, height = 24, tone = "neutral", fill = false, min, max, label, className, style, ref, ...props }) {
9
+ import { ChartPattern, pixelCurve, useChartStyle } from "./chart-style.js";
10
+ function Sparkline({ values, height = 24, tone = "neutral", fill = false, min, max, label, className, style, ref, chartStyle, ...props }) {
11
+ const id = React.useId();
12
+ const frame = React.useRef(null);
6
13
  // Below two samples there is no shape to show, and a flat rule across a
7
14
  // cell reads as a border rather than as a measurement.
8
15
  const known = values.filter((value) => value !== null && Number.isFinite(value));
16
+ const treatment = useChartStyle(frame, chartStyle, known.length >= 2);
9
17
  if (known.length < 2)
10
18
  return null;
11
19
  // Default domain is the data's own extent; pass min and max to make
@@ -15,6 +23,12 @@ function Sparkline({ values, height = 24, tone = "neutral", fill = false, min, m
15
23
  const pad = lo === hi ? Math.max(1, Math.abs(lo) * 0.1) : 0;
16
24
  lo -= pad;
17
25
  hi += pad;
18
- return (_jsx("div", { ref: ref, "data-slot": "sparkline", "data-tone": tone, role: label ? "img" : undefined, "aria-label": label, "aria-hidden": label ? undefined : true, className: cn("sparkline", tone === "primary" && "primary", className), style: { height, ...style }, ...props, children: _jsx(ResponsiveContainer, { width: "100%", height: "100%", children: _jsxs(AreaChart, { data: values.map((value) => ({ value: value !== null && Number.isFinite(value) ? value : null })), margin: { top: 2, right: 0, bottom: 2, left: 0 }, children: [_jsx(YAxis, { hide: true, domain: [lo, hi] }), _jsx(Area, { type: "monotone", connectNulls: false, dataKey: "value", stroke: "currentColor", strokeWidth: 1.5, fill: fill ? "currentColor" : "none", fillOpacity: fill ? 0.12 : 0, isAnimationActive: false, dot: false, activeDot: false })] }) }) }));
26
+ return (_jsx("div", { ref: element => {
27
+ frame.current = element;
28
+ if (typeof ref === "function")
29
+ return ref(element);
30
+ if (ref)
31
+ ref.current = element;
32
+ }, "data-slot": "sparkline", "data-chart-treatment": treatment, "data-tone": tone, role: label ? "img" : undefined, "aria-label": label, "aria-hidden": label ? undefined : true, className: cn("sparkline", tone === "primary" && "primary", className), style: { height, ...style }, ...props, children: _jsx(ResponsiveContainer, { width: "100%", height: "100%", children: _jsxs(AreaChart, { data: values.map((value) => ({ value: value !== null && Number.isFinite(value) ? value : null })), margin: { top: 2, right: 0, bottom: 2, left: 0 }, children: [_jsx("defs", { children: _jsx(ChartPattern, { id: `${id}-halftone`, color: "currentColor" }) }), _jsx(YAxis, { hide: true, domain: [lo, hi] }), _jsx(Area, { type: treatment === "pixel" ? pixelCurve : treatment === "rounded" ? "monotone" : "linear", connectNulls: false, dataKey: "value", stroke: "currentColor", strokeWidth: treatment === "pixel" || treatment === "rounded" ? 2 : 1.5, strokeLinecap: treatment === "rounded" ? "round" : "butt", strokeLinejoin: treatment === "rounded" ? "round" : "miter", fill: treatment === "halftone" ? `url(#${id}-halftone)` : fill ? "currentColor" : "none", fillOpacity: treatment === "halftone" ? 0.3 : fill ? 0.12 : 0, isAnimationActive: false, dot: false, activeDot: false })] }) }) }));
19
33
  }
20
34
  export { Sparkline };