devnonla-ui 0.5.1 → 0.7.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/README.md +25 -1
- package/package.json +1 -1
- package/src/desktop/DesktopWindow.tsx +98 -9
- package/src/index.ts +4 -0
- package/src/input/EditableInput.tsx +408 -0
- package/src/lib/sizes.ts +5 -2
- package/src/sidebar/Sidebar.tsx +198 -0
- package/src/table/Table.tsx +140 -16
package/README.md
CHANGED
|
@@ -115,7 +115,31 @@ import { DesktopStage, DesktopHeader, MeadowDesktop, DesktopWindow, MeadowShell,
|
|
|
115
115
|
</DesktopStage>
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
-
`MeadowShell` is the login/setup backdrop. `DesktopWindow` chrome is inline: traffic lights, `left`, a flex middle (title — double-click to expand), then `right`. A child can fill the same slots with `WindowHeader`. `Menu` is the glass picker (trigger + items + hover action) used by AgentsMenu. Wallpaper defaults to the bundled meadow; pass `src` on `MeadowWallpaper` to swap.
|
|
118
|
+
`MeadowShell` is the login/setup backdrop. `DesktopWindow` chrome is inline: traffic lights, `left`, a flex middle (title — double-click to expand), then `right`. A child can fill the same slots with `WindowHeader`. Last collapsed position and size are restored on reopen (`persistKey`, or `false` to disable). Resize handles stay when expanded; dragging an edge leaves expand and keeps the new size. `Menu` is the glass picker (trigger + items + hover action) used by AgentsMenu. Wallpaper defaults to the bundled meadow; pass `src` on `MeadowWallpaper` to swap.
|
|
119
|
+
|
|
120
|
+
# Sidebar
|
|
121
|
+
|
|
122
|
+
Grouped side nav. Pass JSON groups + items; the app owns routing.
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
import { Sidebar, type SidebarItemType } from "devnonla-ui";
|
|
126
|
+
|
|
127
|
+
const items: SidebarItemType[] = [
|
|
128
|
+
{
|
|
129
|
+
type: "group",
|
|
130
|
+
key: "chat",
|
|
131
|
+
label: "Chat",
|
|
132
|
+
children: [
|
|
133
|
+
{ key: "inbox", label: "Inbox", icon: "chat-24" },
|
|
134
|
+
{ key: "archive", label: "Archive", icon: "library-24" },
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
<Sidebar items={items} selectedKey={active} searchable onSelect={({ key }) => setActive(key)} />
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`icon` may be a Fluent name string (JSON-friendly) or a node. Set `href` to render `<a>`; left-click still goes through `onSelect` so a SPA can `navigate`. Cmd/Ctrl-click keeps the native new-tab. Parent sets width; `Sidebar` fills height.
|
|
119
143
|
|
|
120
144
|
# Chat
|
|
121
145
|
|
package/package.json
CHANGED
|
@@ -139,6 +139,69 @@ function offsetFromRect(view: Size, rect: Rect): Point {
|
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
type Geometry = Point & Size;
|
|
143
|
+
|
|
144
|
+
const GEOMETRY_PREFIX = "nonla-desktop-window:";
|
|
145
|
+
const geometryMemory = new Map<string, Geometry>();
|
|
146
|
+
|
|
147
|
+
function isGeometry(value: unknown): value is Geometry {
|
|
148
|
+
if (!value || typeof value !== "object") return false;
|
|
149
|
+
const { x, y, w, h } = value as Record<string, unknown>;
|
|
150
|
+
return [x, y, w, h].every((n) => typeof n === "number" && Number.isFinite(n));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function geometryStorageKey(persistKey: string) {
|
|
154
|
+
return `${GEOMETRY_PREFIX}${persistKey}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readGeometry(persistKey: string | false): Geometry | null {
|
|
158
|
+
if (!persistKey) return null;
|
|
159
|
+
const cached = geometryMemory.get(persistKey);
|
|
160
|
+
if (cached) return cached;
|
|
161
|
+
try {
|
|
162
|
+
const raw = localStorage.getItem(geometryStorageKey(persistKey));
|
|
163
|
+
if (!raw) return null;
|
|
164
|
+
const parsed: unknown = JSON.parse(raw);
|
|
165
|
+
if (!isGeometry(parsed)) return null;
|
|
166
|
+
geometryMemory.set(persistKey, parsed);
|
|
167
|
+
return parsed;
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function writeGeometry(persistKey: string | false, geometry: Geometry) {
|
|
174
|
+
if (!persistKey) return;
|
|
175
|
+
geometryMemory.set(persistKey, geometry);
|
|
176
|
+
try {
|
|
177
|
+
localStorage.setItem(geometryStorageKey(persistKey), JSON.stringify(geometry));
|
|
178
|
+
} catch {
|
|
179
|
+
/* private mode / quota */
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function initialView(): Size {
|
|
184
|
+
return { w: window.innerWidth, h: Math.max(0, window.innerHeight - 42) };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function restoreGeometry(persistKey: string | false, view: Size): { offset: Point; size: Size | null } {
|
|
188
|
+
const stored = readGeometry(persistKey);
|
|
189
|
+
if (!stored) return { offset: { x: 0, y: 0 }, size: null };
|
|
190
|
+
const size = clampSize(view, stored);
|
|
191
|
+
return { offset: clampOffset(view, size, stored), size };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function persistGeometry(persistKey: string | false, view: Size, offset: Point, size: Size | null) {
|
|
195
|
+
const compact = windowSize(view, size);
|
|
196
|
+
const clamped = clampOffset(view, compact, offset);
|
|
197
|
+
writeGeometry(persistKey, {
|
|
198
|
+
x: Math.round(clamped.x),
|
|
199
|
+
y: Math.round(clamped.y),
|
|
200
|
+
w: Math.round(compact.w),
|
|
201
|
+
h: Math.round(compact.h),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
142
205
|
function lockResizeCursor(cursor: string | null) {
|
|
143
206
|
document.body.style.cursor = cursor ?? "";
|
|
144
207
|
document.body.style.userSelect = cursor ? "none" : "";
|
|
@@ -227,6 +290,8 @@ export type DesktopWindowProps = {
|
|
|
227
290
|
/** Overlay auto-hide scrollbar on the body. Pass `false` when children manage their own scroll (split panes, AgentPanel). */
|
|
228
291
|
scroll?: boolean;
|
|
229
292
|
scrollbar?: OverlayScrollVisibility;
|
|
293
|
+
/** Last collapsed position and size. Default `"default"`. Pass a unique key per window, or `false` to disable. */
|
|
294
|
+
persistKey?: string | false;
|
|
230
295
|
children: ReactNode;
|
|
231
296
|
};
|
|
232
297
|
|
|
@@ -239,6 +304,7 @@ export function DesktopWindow({
|
|
|
239
304
|
onToggleExpand,
|
|
240
305
|
scroll = true,
|
|
241
306
|
scrollbar = "hover",
|
|
307
|
+
persistKey = "default",
|
|
242
308
|
children,
|
|
243
309
|
}: DesktopWindowProps) {
|
|
244
310
|
const overlayRef = useRef<HTMLDivElement>(null);
|
|
@@ -260,15 +326,12 @@ export function DesktopWindow({
|
|
|
260
326
|
orig: Rect;
|
|
261
327
|
} | null>(null);
|
|
262
328
|
const [phase, setPhase] = useState<Phase>("open");
|
|
263
|
-
const [
|
|
264
|
-
const [
|
|
329
|
+
const [view, setView] = useState<Size>(initialView);
|
|
330
|
+
const [offset, setOffset] = useState(() => restoreGeometry(persistKey, initialView()).offset);
|
|
331
|
+
const [size, setSize] = useState<Size | null>(() => restoreGeometry(persistKey, initialView()).size);
|
|
265
332
|
const [dragging, setDragging] = useState(false);
|
|
266
333
|
const [resizing, setResizing] = useState(false);
|
|
267
334
|
const sizeRef = useRef<Size | null>(null);
|
|
268
|
-
const [view, setView] = useState<Size>(() => ({
|
|
269
|
-
w: window.innerWidth,
|
|
270
|
-
h: Math.max(0, window.innerHeight - 42),
|
|
271
|
-
}));
|
|
272
335
|
const [leftSlot, setLeftSlot] = useState<HTMLDivElement | null>(null);
|
|
273
336
|
const [rightSlot, setRightSlot] = useState<HTMLDivElement | null>(null);
|
|
274
337
|
const headerChrome = useMemo(() => ({ left: leftSlot, right: rightSlot }), [leftSlot, rightSlot]);
|
|
@@ -339,6 +402,17 @@ export function DesktopWindow({
|
|
|
339
402
|
return () => observer.disconnect();
|
|
340
403
|
}, []);
|
|
341
404
|
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
if (dragging || resizing) return;
|
|
407
|
+
persistGeometry(persistKey, view, offset, size);
|
|
408
|
+
}, [dragging, resizing, offset, size, view, persistKey]);
|
|
409
|
+
|
|
410
|
+
useEffect(() => {
|
|
411
|
+
return () => {
|
|
412
|
+
persistGeometry(persistKey, viewRef.current, offsetRef.current, sizeRef.current);
|
|
413
|
+
};
|
|
414
|
+
}, [persistKey]);
|
|
415
|
+
|
|
342
416
|
useEffect(() => {
|
|
343
417
|
const onKey = (e: KeyboardEvent) => {
|
|
344
418
|
if (e.key !== "Escape" || e.defaultPrevented || isEditableTarget(e.target) || hasOpenDialog()) return;
|
|
@@ -423,16 +497,31 @@ export function DesktopWindow({
|
|
|
423
497
|
};
|
|
424
498
|
|
|
425
499
|
const startResize = (edge: Edge, e: ReactPointerEvent<HTMLElement>) => {
|
|
426
|
-
if (e.button !== 0 ||
|
|
500
|
+
if (e.button !== 0 || phase === "leaving") return;
|
|
427
501
|
e.stopPropagation();
|
|
428
502
|
e.preventDefault();
|
|
429
503
|
|
|
504
|
+
const nextView = viewRef.current;
|
|
505
|
+
const orig = expandedRef.current
|
|
506
|
+
? { x: 0, y: 0, w: nextView.w, h: nextView.h }
|
|
507
|
+
: collapsedRect(nextView, offsetRef.current, sizeRef.current);
|
|
508
|
+
|
|
509
|
+
if (expandedRef.current) {
|
|
510
|
+
const nextSize = { w: orig.w, h: orig.h };
|
|
511
|
+
const nextOffset = offsetFromRect(nextView, orig);
|
|
512
|
+
sizeRef.current = nextSize;
|
|
513
|
+
offsetRef.current = nextOffset;
|
|
514
|
+
setSize(nextSize);
|
|
515
|
+
setOffset(nextOffset);
|
|
516
|
+
onToggleExpand();
|
|
517
|
+
}
|
|
518
|
+
|
|
430
519
|
resizeRef.current = {
|
|
431
520
|
pointerId: e.pointerId,
|
|
432
521
|
edge,
|
|
433
522
|
startX: e.clientX,
|
|
434
523
|
startY: e.clientY,
|
|
435
|
-
orig
|
|
524
|
+
orig,
|
|
436
525
|
};
|
|
437
526
|
setResizing(true);
|
|
438
527
|
lockResizeCursor(HANDLES.find((handle) => handle.edge === edge)?.cursor ?? "nwse-resize");
|
|
@@ -530,7 +619,7 @@ export function DesktopWindow({
|
|
|
530
619
|
</div>
|
|
531
620
|
</div>
|
|
532
621
|
|
|
533
|
-
{!
|
|
622
|
+
{!leaving
|
|
534
623
|
? HANDLES.map((handle) => (
|
|
535
624
|
<div key={handle.edge} aria-hidden onPointerDown={(e) => startResize(handle.edge, e)} className={cn("absolute z-10 touch-none", handle.className)} />
|
|
536
625
|
))
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type { ButtonColor, ButtonGroupProps, ButtonProps, ButtonSize, ButtonType
|
|
|
4
4
|
export { Button } from "./button/Button";
|
|
5
5
|
export type { ButtonCopyProps } from "./button/ButtonCopy";
|
|
6
6
|
export { ButtonCopy } from "./button/ButtonCopy";
|
|
7
|
+
export type { EditableInputProps, EditableKeyProps } from "./input/EditableInput";
|
|
8
|
+
export { EditableInput } from "./input/EditableInput";
|
|
7
9
|
export type { InputNumberProps, InputProps, InputSize, PasswordProps, TextAreaProps, TextAreaRef } from "./input/Input";
|
|
8
10
|
export { Input, InputNumber, TextArea } from "./input/Input";
|
|
9
11
|
export type { SearchInputProps } from "./input/SearchInput";
|
|
@@ -174,6 +176,8 @@ export type { SelectOptionConfig, SelectProps, SelectValue } from "./select/Sele
|
|
|
174
176
|
export { Select, SelectOption } from "./select/Select";
|
|
175
177
|
export type { ShimmerProps } from "./shimmer/Shimmer";
|
|
176
178
|
export { Shimmer } from "./shimmer/Shimmer";
|
|
179
|
+
export type { SidebarItemType, SidebarProps, SidebarSelectInfo } from "./sidebar/Sidebar";
|
|
180
|
+
export { Sidebar } from "./sidebar/Sidebar";
|
|
177
181
|
export type { SkeletonProps } from "./skeleton/Skeleton";
|
|
178
182
|
export { Skeleton } from "./skeleton/Skeleton";
|
|
179
183
|
export type { SpinProps, SpinVariant } from "./spin/Spin";
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { Pencil } from "lucide-react";
|
|
2
|
+
import { type CSSProperties, cloneElement, isValidElement, type KeyboardEvent, type ReactElement, type ReactNode, type Ref, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { createPortal } from "react-dom";
|
|
4
|
+
import { usePopupContainer } from "../app/context";
|
|
5
|
+
import { Button } from "../button/Button";
|
|
6
|
+
import { cn } from "../lib/cn";
|
|
7
|
+
import { type CanonicalSize, type ControlSize, controlHeightVar, controlRadiusVar, getSizeTokens, useControlSize } from "../lib/sizes";
|
|
8
|
+
import { message } from "../message/message";
|
|
9
|
+
|
|
10
|
+
const POPUP_PAD: Record<CanonicalSize, number> = { small: 12, default: 16, large: 20 };
|
|
11
|
+
const FIELD_INSET: Record<CanonicalSize, string> = { small: "-4px", default: "-6px", large: "-8px" };
|
|
12
|
+
const MULTILINE_MIN: Record<CanonicalSize, number> = { small: 56, default: 72, large: 96 };
|
|
13
|
+
const GLASS_BORDER = 1;
|
|
14
|
+
|
|
15
|
+
const fieldClass = (saving: boolean) => cn("relative z-1 m-0 w-full border-0 bg-transparent text-foreground placeholder:text-muted-foreground/50 focus:outline-none", saving && "opacity-60");
|
|
16
|
+
|
|
17
|
+
type Lock = {
|
|
18
|
+
top: number;
|
|
19
|
+
left: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
padTop: number;
|
|
23
|
+
padBottom: number;
|
|
24
|
+
font: string;
|
|
25
|
+
letterSpacing: string;
|
|
26
|
+
color: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function glyphEl(el: HTMLElement): HTMLElement {
|
|
30
|
+
let node = el;
|
|
31
|
+
while (node.childElementCount === 1 && node.firstElementChild instanceof HTMLElement) {
|
|
32
|
+
node = node.firstElementChild;
|
|
33
|
+
}
|
|
34
|
+
return node;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function containingOffset(container: HTMLElement) {
|
|
38
|
+
const cs = getComputedStyle(container);
|
|
39
|
+
const fixedContaining = cs.transform !== "none" || cs.perspective !== "none" || cs.filter !== "none" || cs.backdropFilter !== "none" || cs.willChange.includes("transform");
|
|
40
|
+
if (!fixedContaining) return { top: 0, left: 0 };
|
|
41
|
+
const r = container.getBoundingClientRect();
|
|
42
|
+
return { top: r.top, left: r.left };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readLock(textEl: HTMLElement, rowEl: HTMLElement, minWidth: number | undefined): Lock {
|
|
46
|
+
const glyph = glyphEl(textEl);
|
|
47
|
+
const g = glyph.getBoundingClientRect();
|
|
48
|
+
const row = rowEl.getBoundingClientRect();
|
|
49
|
+
const cs = getComputedStyle(glyph);
|
|
50
|
+
const width = Math.max(g.width, row.right - g.left, minWidth ?? 0);
|
|
51
|
+
return {
|
|
52
|
+
top: row.top,
|
|
53
|
+
left: g.left,
|
|
54
|
+
width,
|
|
55
|
+
height: row.height,
|
|
56
|
+
padTop: g.top - row.top,
|
|
57
|
+
padBottom: row.bottom - g.bottom,
|
|
58
|
+
font: cs.font,
|
|
59
|
+
letterSpacing: cs.letterSpacing,
|
|
60
|
+
color: cs.color,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function EditorPopover({
|
|
65
|
+
textRef,
|
|
66
|
+
rowRef,
|
|
67
|
+
inputRef,
|
|
68
|
+
size,
|
|
69
|
+
multiline,
|
|
70
|
+
minWidth,
|
|
71
|
+
saving,
|
|
72
|
+
saveDisabled,
|
|
73
|
+
selectOnFocus,
|
|
74
|
+
onSave,
|
|
75
|
+
onCancel,
|
|
76
|
+
children,
|
|
77
|
+
}: {
|
|
78
|
+
textRef: Ref<HTMLDivElement>;
|
|
79
|
+
rowRef: Ref<HTMLDivElement>;
|
|
80
|
+
inputRef: { current: HTMLInputElement | HTMLTextAreaElement | null };
|
|
81
|
+
size: CanonicalSize;
|
|
82
|
+
multiline: boolean;
|
|
83
|
+
minWidth?: number;
|
|
84
|
+
saving: boolean;
|
|
85
|
+
saveDisabled?: boolean;
|
|
86
|
+
selectOnFocus?: boolean;
|
|
87
|
+
onSave: () => void;
|
|
88
|
+
onCancel: () => void;
|
|
89
|
+
children: ReactElement<{ className?: string; style?: CSSProperties; ref?: Ref<HTMLInputElement | HTMLTextAreaElement> }>;
|
|
90
|
+
}) {
|
|
91
|
+
const getContainer = usePopupContainer();
|
|
92
|
+
const [lock, setLock] = useState<Lock | null>(null);
|
|
93
|
+
const [correction, setCorrection] = useState({ x: 0, y: 0 });
|
|
94
|
+
const attempts = useRef(0);
|
|
95
|
+
|
|
96
|
+
useLayoutEffect(() => {
|
|
97
|
+
const text = typeof textRef === "object" && textRef ? textRef.current : null;
|
|
98
|
+
const row = typeof rowRef === "object" && rowRef ? rowRef.current : null;
|
|
99
|
+
if (!text || !row) return;
|
|
100
|
+
const sync = () => setLock((prev) => {
|
|
101
|
+
const next = readLock(text, row, minWidth);
|
|
102
|
+
if (
|
|
103
|
+
prev &&
|
|
104
|
+
Math.abs(prev.top - next.top) < 0.25 &&
|
|
105
|
+
Math.abs(prev.left - next.left) < 0.25 &&
|
|
106
|
+
Math.abs(prev.width - next.width) < 0.25 &&
|
|
107
|
+
Math.abs(prev.height - next.height) < 0.25 &&
|
|
108
|
+
Math.abs(prev.padTop - next.padTop) < 0.25 &&
|
|
109
|
+
Math.abs(prev.padBottom - next.padBottom) < 0.25 &&
|
|
110
|
+
prev.font === next.font &&
|
|
111
|
+
prev.letterSpacing === next.letterSpacing &&
|
|
112
|
+
prev.color === next.color
|
|
113
|
+
) {
|
|
114
|
+
return prev;
|
|
115
|
+
}
|
|
116
|
+
return next;
|
|
117
|
+
});
|
|
118
|
+
sync();
|
|
119
|
+
const ro = new ResizeObserver(sync);
|
|
120
|
+
ro.observe(text);
|
|
121
|
+
ro.observe(row);
|
|
122
|
+
window.addEventListener("scroll", sync, true);
|
|
123
|
+
window.addEventListener("resize", sync);
|
|
124
|
+
return () => {
|
|
125
|
+
ro.disconnect();
|
|
126
|
+
window.removeEventListener("scroll", sync, true);
|
|
127
|
+
window.removeEventListener("resize", sync);
|
|
128
|
+
};
|
|
129
|
+
}, [textRef, rowRef, minWidth]);
|
|
130
|
+
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
attempts.current = 0;
|
|
133
|
+
}, [lock?.top, lock?.left, lock?.padTop]);
|
|
134
|
+
|
|
135
|
+
useLayoutEffect(() => {
|
|
136
|
+
const text = typeof textRef === "object" && textRef ? textRef.current : null;
|
|
137
|
+
const input = inputRef.current;
|
|
138
|
+
if (!lock || !text || !input) return;
|
|
139
|
+
const g = glyphEl(text).getBoundingClientRect();
|
|
140
|
+
const box = input.getBoundingClientRect();
|
|
141
|
+
const cs = getComputedStyle(input);
|
|
142
|
+
const dx = g.left - (box.left + parseFloat(cs.paddingLeft));
|
|
143
|
+
const dy = g.top - (box.top + parseFloat(cs.paddingTop));
|
|
144
|
+
if (Math.abs(dx) < 0.25 && Math.abs(dy) < 0.25) {
|
|
145
|
+
attempts.current = 0;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (attempts.current >= 3) return;
|
|
149
|
+
attempts.current += 1;
|
|
150
|
+
setCorrection((current) => ({ x: current.x + dx, y: current.y + dy }));
|
|
151
|
+
}, [lock, textRef, inputRef, correction]);
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!lock) return;
|
|
155
|
+
const timer = setTimeout(() => {
|
|
156
|
+
const el = inputRef.current;
|
|
157
|
+
el?.focus();
|
|
158
|
+
if (selectOnFocus && el instanceof HTMLInputElement) el.select();
|
|
159
|
+
}, 0);
|
|
160
|
+
return () => clearTimeout(timer);
|
|
161
|
+
}, [lock != null, selectOnFocus, inputRef]);
|
|
162
|
+
|
|
163
|
+
if (!lock || !isValidElement(children)) return null;
|
|
164
|
+
|
|
165
|
+
const pad = POPUP_PAD[size];
|
|
166
|
+
const container = getContainer();
|
|
167
|
+
const origin = containingOffset(container);
|
|
168
|
+
const field = cloneElement(children, {
|
|
169
|
+
ref: inputRef,
|
|
170
|
+
style: {
|
|
171
|
+
...children.props.style,
|
|
172
|
+
boxSizing: "border-box",
|
|
173
|
+
width: "100%",
|
|
174
|
+
height: multiline ? undefined : lock.height,
|
|
175
|
+
minHeight: multiline ? Math.max(MULTILINE_MIN[size], lock.height) : undefined,
|
|
176
|
+
margin: 0,
|
|
177
|
+
border: 0,
|
|
178
|
+
paddingTop: lock.padTop,
|
|
179
|
+
paddingRight: 0,
|
|
180
|
+
paddingBottom: multiline ? 8 : lock.padBottom,
|
|
181
|
+
paddingLeft: 0,
|
|
182
|
+
font: lock.font,
|
|
183
|
+
letterSpacing: lock.letterSpacing,
|
|
184
|
+
color: lock.color,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return createPortal(
|
|
189
|
+
<div
|
|
190
|
+
className="nonla-popup-layer nonla-glass outline-none"
|
|
191
|
+
style={{
|
|
192
|
+
position: "fixed",
|
|
193
|
+
zIndex: "var(--nonla-z-popup, 1050)",
|
|
194
|
+
borderRadius: 10,
|
|
195
|
+
top: lock.top - origin.top - pad - GLASS_BORDER + correction.y,
|
|
196
|
+
left: lock.left - origin.left - pad - GLASS_BORDER + correction.x,
|
|
197
|
+
width: lock.width + (pad + GLASS_BORDER) * 2,
|
|
198
|
+
padding: pad,
|
|
199
|
+
}}
|
|
200
|
+
>
|
|
201
|
+
<div className="relative">
|
|
202
|
+
<span aria-hidden className="pointer-events-none absolute top-0 bottom-0" style={{ left: FIELD_INSET[size], right: FIELD_INSET[size], borderRadius: controlRadiusVar(size), background: "color-mix(in srgb, var(--foreground) 6%, transparent)" }} />
|
|
203
|
+
{field}
|
|
204
|
+
</div>
|
|
205
|
+
<div className="mt-3 flex items-center gap-2">
|
|
206
|
+
<Button type="primary" size={size} loading={saving} disabled={saveDisabled} onClick={onSave}>
|
|
207
|
+
Save
|
|
208
|
+
</Button>
|
|
209
|
+
<Button type="text" size={size} disabled={saving} onClick={onCancel}>
|
|
210
|
+
Cancel
|
|
211
|
+
</Button>
|
|
212
|
+
</div>
|
|
213
|
+
</div>,
|
|
214
|
+
container,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function EditButton({ label, size, onClick }: { label: string; size: CanonicalSize; onClick: () => void }) {
|
|
219
|
+
const tok = getSizeTokens(size);
|
|
220
|
+
const box = tok.icon + 8;
|
|
221
|
+
return (
|
|
222
|
+
<button
|
|
223
|
+
type="button"
|
|
224
|
+
aria-label={label}
|
|
225
|
+
title={label}
|
|
226
|
+
onClick={onClick}
|
|
227
|
+
className="inline-flex shrink-0 items-center justify-center rounded text-muted-foreground opacity-0 transition-opacity hover:bg-muted hover:text-foreground group-hover/edit:opacity-100 focus-visible:opacity-100"
|
|
228
|
+
style={{ width: box, height: box }}
|
|
229
|
+
>
|
|
230
|
+
<Pencil size={tok.icon} />
|
|
231
|
+
</button>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export type EditableInputProps = {
|
|
236
|
+
display: ReactNode;
|
|
237
|
+
editing: boolean;
|
|
238
|
+
onStartEdit: () => void;
|
|
239
|
+
onCancelEdit: () => void;
|
|
240
|
+
initialValue: string;
|
|
241
|
+
placeholder?: string;
|
|
242
|
+
type?: "text" | "password" | "textarea";
|
|
243
|
+
editLabel?: string;
|
|
244
|
+
minWidth?: number;
|
|
245
|
+
size?: ControlSize;
|
|
246
|
+
/** Datatable cells may be cleared. KV/secrets stay required. */
|
|
247
|
+
allowEmpty?: boolean;
|
|
248
|
+
onSave: (value: string) => Promise<void>;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const KEY_RE = /^[A-Z][A-Z0-9_]*$/;
|
|
252
|
+
|
|
253
|
+
export type EditableKeyProps = {
|
|
254
|
+
value: string;
|
|
255
|
+
editing: boolean;
|
|
256
|
+
onStartEdit: () => void;
|
|
257
|
+
onCancelEdit: () => void;
|
|
258
|
+
onSave: (key: string) => Promise<void>;
|
|
259
|
+
placeholder?: string;
|
|
260
|
+
editLabel?: string;
|
|
261
|
+
size?: ControlSize;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
function EditableKey({ value, editing, onStartEdit, onCancelEdit, onSave, placeholder = "VARIABLE_NAME", editLabel = "Edit key", size }: EditableKeyProps) {
|
|
265
|
+
const resolved = useControlSize(size);
|
|
266
|
+
const tok = getSizeTokens(resolved);
|
|
267
|
+
const [draft, setDraft] = useState(value);
|
|
268
|
+
const [saving, setSaving] = useState(false);
|
|
269
|
+
const textRef = useRef<HTMLDivElement>(null);
|
|
270
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
271
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
272
|
+
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
if (!editing) return;
|
|
275
|
+
setDraft(value);
|
|
276
|
+
}, [editing, value]);
|
|
277
|
+
|
|
278
|
+
const handleCancel = () => {
|
|
279
|
+
setDraft(value);
|
|
280
|
+
onCancelEdit();
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const handleSave = async () => {
|
|
284
|
+
const next = draft.trim().toUpperCase();
|
|
285
|
+
if (!next) {
|
|
286
|
+
message.error("Key is required");
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (!KEY_RE.test(next)) {
|
|
290
|
+
message.error("Key must match [A-Z][A-Z0-9_]* (e.g. API_TOKEN)");
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (next === value) {
|
|
294
|
+
onCancelEdit();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
setSaving(true);
|
|
298
|
+
try {
|
|
299
|
+
await onSave(next);
|
|
300
|
+
onCancelEdit();
|
|
301
|
+
} catch {
|
|
302
|
+
// Caller shows the error; keep the draft open.
|
|
303
|
+
} finally {
|
|
304
|
+
setSaving(false);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
309
|
+
if (e.key === "Enter") {
|
|
310
|
+
e.preventDefault();
|
|
311
|
+
void handleSave();
|
|
312
|
+
} else if (e.key === "Escape") {
|
|
313
|
+
e.preventDefault();
|
|
314
|
+
handleCancel();
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
return (
|
|
319
|
+
<div ref={rowRef} className="flex w-full min-w-0 items-center" style={{ minHeight: controlHeightVar(resolved) }}>
|
|
320
|
+
<div className="group/edit inline-flex max-w-full items-center gap-1">
|
|
321
|
+
<div ref={textRef} className={cn("min-w-0", editing && "invisible")} style={{ fontSize: tok.fontSize, lineHeight: `${tok.lineHeight}px`, minHeight: tok.lineHeight }}>
|
|
322
|
+
<code className="select-text font-mono font-semibold tracking-wide text-foreground">{value}</code>
|
|
323
|
+
</div>
|
|
324
|
+
{editing ? null : <EditButton label={editLabel} size={resolved} onClick={onStartEdit} />}
|
|
325
|
+
</div>
|
|
326
|
+
{editing ? (
|
|
327
|
+
<EditorPopover textRef={textRef} rowRef={rowRef} inputRef={inputRef} size={resolved} multiline={false} saving={saving} selectOnFocus onSave={() => void handleSave()} onCancel={handleCancel}>
|
|
328
|
+
<input type="text" value={draft} onChange={(e) => setDraft(e.target.value.toUpperCase())} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
329
|
+
</EditorPopover>
|
|
330
|
+
) : null}
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function EditableInputRoot({ display, editing, onStartEdit, onCancelEdit, initialValue, placeholder, type = "text", editLabel = "Edit value", minWidth, size, allowEmpty = false, onSave }: EditableInputProps) {
|
|
336
|
+
const resolved = useControlSize(size);
|
|
337
|
+
const tok = getSizeTokens(resolved);
|
|
338
|
+
const [draft, setDraft] = useState(initialValue);
|
|
339
|
+
const [saving, setSaving] = useState(false);
|
|
340
|
+
const textRef = useRef<HTMLDivElement>(null);
|
|
341
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
342
|
+
const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null);
|
|
343
|
+
const multiline = type === "textarea";
|
|
344
|
+
|
|
345
|
+
useEffect(() => {
|
|
346
|
+
if (!editing) return;
|
|
347
|
+
setDraft(initialValue);
|
|
348
|
+
}, [editing, initialValue]);
|
|
349
|
+
|
|
350
|
+
const handleCancel = () => {
|
|
351
|
+
setDraft(initialValue);
|
|
352
|
+
onCancelEdit();
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const handleSave = async () => {
|
|
356
|
+
if (!allowEmpty && !draft) {
|
|
357
|
+
message.error("Value is required");
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (draft === initialValue) {
|
|
361
|
+
onCancelEdit();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
setSaving(true);
|
|
365
|
+
try {
|
|
366
|
+
await onSave(draft);
|
|
367
|
+
onCancelEdit();
|
|
368
|
+
} catch {
|
|
369
|
+
// Caller shows the error; keep the draft open.
|
|
370
|
+
} finally {
|
|
371
|
+
setSaving(false);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
376
|
+
if (e.key === "Escape") {
|
|
377
|
+
e.preventDefault();
|
|
378
|
+
handleCancel();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (e.key === "Enter" && (!multiline || e.metaKey || e.ctrlKey)) {
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
void handleSave();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const field = multiline ? (
|
|
388
|
+
<textarea value={draft} rows={3} onChange={(e) => setDraft(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
389
|
+
) : (
|
|
390
|
+
<input type={type} value={draft} onChange={(e) => setDraft(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
return (
|
|
394
|
+
<div ref={rowRef} className="group/edit flex w-full min-w-0 items-center" style={{ minHeight: controlHeightVar(resolved) }}>
|
|
395
|
+
<div ref={textRef} className={cn("min-w-0 truncate", editing && "invisible")} style={{ fontSize: tok.fontSize, lineHeight: `${tok.lineHeight}px`, minHeight: tok.lineHeight }}>
|
|
396
|
+
{display}
|
|
397
|
+
</div>
|
|
398
|
+
{editing ? null : <EditButton label={editLabel} size={resolved} onClick={onStartEdit} />}
|
|
399
|
+
{editing ? (
|
|
400
|
+
<EditorPopover textRef={textRef} rowRef={rowRef} inputRef={inputRef} size={resolved} multiline={multiline} minWidth={minWidth} saving={saving} saveDisabled={!allowEmpty && !draft} onSave={() => void handleSave()} onCancel={handleCancel}>
|
|
401
|
+
{field}
|
|
402
|
+
</EditorPopover>
|
|
403
|
+
) : null}
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export const EditableInput = Object.assign(EditableInputRoot, { Key: EditableKey });
|
package/src/lib/sizes.ts
CHANGED
|
@@ -93,9 +93,12 @@ export function controlStatusClass(status?: "error" | "warning"): string {
|
|
|
93
93
|
/** Focus surface fade — Input / Select / DatePicker / TimePicker. */
|
|
94
94
|
export const controlFieldTransition = "transition-[background-color,border-color] duration-[var(--nonla-dur-fast,150ms)] ease-[var(--nonla-ease-out,cubic-bezier(0.16,1,0.3,1))] motion-reduce:transition-none";
|
|
95
95
|
|
|
96
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Outlined field. Border is `--input` (ink on page bg), not glass.
|
|
98
|
+
* Glass border is white frost — it disappears on white.
|
|
99
|
+
*/
|
|
97
100
|
export const controlFieldSurface =
|
|
98
|
-
"border border-solid border-
|
|
101
|
+
"border border-solid border-input bg-glass-bar text-foreground shadow-[inset_0_1px_0_var(--glass-highlight)] backdrop-blur-xl";
|
|
99
102
|
|
|
100
103
|
/** Focus / open border — brand (Button primary), keep fields outlined. */
|
|
101
104
|
export const controlFieldFocusBorder = "focus:border-brand focus-within:border-brand data-[state=open]:border-brand aria-expanded:border-brand";
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { type MouseEvent, type ReactNode, useMemo, useState } from "react";
|
|
2
|
+
import { FluentIcon } from "../icon/FluentIcon";
|
|
3
|
+
import { SearchInput } from "../input/SearchInput";
|
|
4
|
+
import { cn } from "../lib/cn";
|
|
5
|
+
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
6
|
+
|
|
7
|
+
export type SidebarItemType = {
|
|
8
|
+
key?: string;
|
|
9
|
+
label?: ReactNode;
|
|
10
|
+
/** Fluent icon name, or a node. */
|
|
11
|
+
icon?: ReactNode;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
type?: "item" | "group" | "divider";
|
|
14
|
+
children?: SidebarItemType[];
|
|
15
|
+
href?: string;
|
|
16
|
+
extra?: ReactNode;
|
|
17
|
+
className?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type SidebarSelectInfo = {
|
|
21
|
+
key: string;
|
|
22
|
+
item: SidebarItemType;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SidebarProps = {
|
|
26
|
+
items?: SidebarItemType[];
|
|
27
|
+
selectedKey?: string;
|
|
28
|
+
defaultSelectedKey?: string;
|
|
29
|
+
onSelect?: (info: SidebarSelectInfo) => void;
|
|
30
|
+
searchable?: boolean;
|
|
31
|
+
searchPlaceholder?: string;
|
|
32
|
+
header?: ReactNode;
|
|
33
|
+
footer?: ReactNode;
|
|
34
|
+
emptyText?: ReactNode;
|
|
35
|
+
className?: string;
|
|
36
|
+
"aria-label"?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function itemKey(item: SidebarItemType, i: number) {
|
|
40
|
+
return item.key ?? (item.type === "divider" ? `divider-${i}` : isGroup(item) ? `group-${i}` : `item-${i}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isGroup(item: SidebarItemType) {
|
|
44
|
+
return item.type === "group" || (item.type !== "item" && item.type !== "divider" && Boolean(item.children?.length));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function labelText(label: ReactNode) {
|
|
48
|
+
if (typeof label === "string" || typeof label === "number") return String(label);
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function matchesQuery(item: SidebarItemType, q: string) {
|
|
53
|
+
if (!q) return true;
|
|
54
|
+
return labelText(item.label).toLowerCase().includes(q);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function filterItems(items: SidebarItemType[], q: string): SidebarItemType[] {
|
|
58
|
+
if (!q) return items;
|
|
59
|
+
const out: SidebarItemType[] = [];
|
|
60
|
+
for (const item of items) {
|
|
61
|
+
if (item.type === "divider") {
|
|
62
|
+
if (out.length && out[out.length - 1]?.type !== "divider") out.push(item);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (isGroup(item)) {
|
|
66
|
+
const children = filterItems(item.children ?? [], q);
|
|
67
|
+
if (children.length || matchesQuery(item, q)) out.push({ ...item, children });
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (matchesQuery(item, q)) out.push(item);
|
|
71
|
+
}
|
|
72
|
+
while (out[out.length - 1]?.type === "divider") out.pop();
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function ItemIcon({ icon }: { icon?: ReactNode }) {
|
|
77
|
+
if (icon == null || icon === false) return null;
|
|
78
|
+
if (typeof icon === "string") return <FluentIcon name={icon} size={16} />;
|
|
79
|
+
return <span className="inline-flex size-4 shrink-0 items-center justify-center [&_img]:size-4 [&_svg]:size-4">{icon}</span>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function SidebarList({
|
|
83
|
+
items,
|
|
84
|
+
selected,
|
|
85
|
+
onSelect,
|
|
86
|
+
}: {
|
|
87
|
+
items: SidebarItemType[];
|
|
88
|
+
selected?: string;
|
|
89
|
+
onSelect?: SidebarProps["onSelect"];
|
|
90
|
+
}) {
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
{items.map((item, i) => {
|
|
94
|
+
const key = itemKey(item, i);
|
|
95
|
+
if (item.type === "divider") {
|
|
96
|
+
return <div key={key} className="mx-2 my-1 h-px bg-ink-line" />;
|
|
97
|
+
}
|
|
98
|
+
if (isGroup(item)) {
|
|
99
|
+
return (
|
|
100
|
+
<div key={key} className="mb-4">
|
|
101
|
+
{item.label != null && item.label !== "" ? (
|
|
102
|
+
<div className={cn("px-2 pb-1.5 pt-2 text-[11px] font-medium uppercase tracking-wider text-quaternary-foreground", item.className)}>
|
|
103
|
+
{item.label}
|
|
104
|
+
</div>
|
|
105
|
+
) : null}
|
|
106
|
+
<div className="flex flex-col gap-0.5">
|
|
107
|
+
<SidebarList items={item.children ?? []} selected={selected} onSelect={onSelect} />
|
|
108
|
+
</div>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const active = selected != null && key === selected;
|
|
114
|
+
const className = cn(
|
|
115
|
+
"flex w-full items-center gap-2 rounded-md border-0 px-2 py-1.5 text-left text-sm no-underline",
|
|
116
|
+
item.disabled ? "pointer-events-none cursor-not-allowed opacity-40" : "cursor-pointer",
|
|
117
|
+
active ? "bg-ink-active font-medium text-foreground" : "bg-transparent text-foreground/80 hover:bg-ink-hover hover:text-foreground",
|
|
118
|
+
item.className,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const body = (
|
|
122
|
+
<>
|
|
123
|
+
<ItemIcon icon={item.icon} />
|
|
124
|
+
<span className="min-w-0 flex-1 truncate">{item.label}</span>
|
|
125
|
+
{item.extra != null ? <span className="shrink-0 text-xs tabular-nums text-muted-foreground">{item.extra}</span> : null}
|
|
126
|
+
</>
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const activate = (e: MouseEvent<HTMLElement>) => {
|
|
130
|
+
if (item.disabled) {
|
|
131
|
+
e.preventDefault();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (item.href && (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0)) return;
|
|
135
|
+
if (item.href && onSelect) e.preventDefault();
|
|
136
|
+
onSelect?.({ key, item });
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
if (item.href) {
|
|
140
|
+
return (
|
|
141
|
+
<a key={key} href={item.href} aria-current={active ? "page" : undefined} aria-disabled={item.disabled || undefined} className={className} onClick={activate}>
|
|
142
|
+
{body}
|
|
143
|
+
</a>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<button key={key} type="button" aria-current={active ? "true" : undefined} disabled={item.disabled} className={className} onClick={activate}>
|
|
149
|
+
{body}
|
|
150
|
+
</button>
|
|
151
|
+
);
|
|
152
|
+
})}
|
|
153
|
+
</>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Grouped side nav — pass JSON groups + items. Router stays in the app (`onSelect` / `href`). */
|
|
158
|
+
export function Sidebar({
|
|
159
|
+
items = [],
|
|
160
|
+
selectedKey,
|
|
161
|
+
defaultSelectedKey,
|
|
162
|
+
onSelect,
|
|
163
|
+
searchable = false,
|
|
164
|
+
searchPlaceholder = "Search…",
|
|
165
|
+
header,
|
|
166
|
+
footer,
|
|
167
|
+
emptyText = "No matches.",
|
|
168
|
+
className,
|
|
169
|
+
"aria-label": ariaLabel = "Sidebar",
|
|
170
|
+
}: SidebarProps) {
|
|
171
|
+
const [query, setQuery] = useState("");
|
|
172
|
+
const [uncontrolled, setUncontrolled] = useState(defaultSelectedKey);
|
|
173
|
+
const selected = selectedKey ?? uncontrolled;
|
|
174
|
+
|
|
175
|
+
const visible = useMemo(() => filterItems(items, query.trim().toLowerCase()), [items, query]);
|
|
176
|
+
|
|
177
|
+
const handleSelect = (info: SidebarSelectInfo) => {
|
|
178
|
+
if (selectedKey === undefined) setUncontrolled(info.key);
|
|
179
|
+
onSelect?.(info);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<div className={cn("flex h-full min-h-0 flex-col", className)}>
|
|
184
|
+
{header}
|
|
185
|
+
{searchable ? (
|
|
186
|
+
<div className={cn("shrink-0 px-3 pb-2", header ? "pt-1" : "pt-3")}>
|
|
187
|
+
<SearchInput size="small" wait={0} placeholder={searchPlaceholder} onChange={setQuery} />
|
|
188
|
+
</div>
|
|
189
|
+
) : null}
|
|
190
|
+
<OverlayScroll className="min-h-0 flex-1" innerClassName={cn("px-2 pb-6", !searchable && !header && "pt-2")}>
|
|
191
|
+
<nav aria-label={ariaLabel}>
|
|
192
|
+
{visible.length === 0 ? <p className="px-2 py-4 text-sm text-muted-foreground">{emptyText}</p> : <SidebarList items={visible} selected={selected} onSelect={handleSelect} />}
|
|
193
|
+
</nav>
|
|
194
|
+
</OverlayScroll>
|
|
195
|
+
{footer}
|
|
196
|
+
</div>
|
|
197
|
+
);
|
|
198
|
+
}
|
package/src/table/Table.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CSSProperties, type Key, type ReactNode, useMemo, useState } from "react";
|
|
1
|
+
import { type CSSProperties, type Key, type ReactNode, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { Checkbox } from "../checkbox/Checkbox";
|
|
3
3
|
import { Empty } from "../empty/Empty";
|
|
4
4
|
import { cn } from "../lib/cn";
|
|
@@ -18,6 +18,11 @@ export type ColumnType<T> = {
|
|
|
18
18
|
/** Custom cell — `(value, record, index) => ReactNode`. */
|
|
19
19
|
render?: (value: any, record: T, index: number) => ReactNode;
|
|
20
20
|
className?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Take leftover container width (measured). `true` shares equally with other flex columns.
|
|
23
|
+
* Text wraps inside that width so the table does not scroll horizontally.
|
|
24
|
+
*/
|
|
25
|
+
flex?: boolean | number;
|
|
21
26
|
ellipsis?: boolean;
|
|
22
27
|
fixed?: "left" | "right";
|
|
23
28
|
/** `true` = default compare on `dataIndex`; or pass a compare fn. */
|
|
@@ -142,6 +147,23 @@ function SortIcon({ order }: { order: SortOrder | undefined }) {
|
|
|
142
147
|
);
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
type FlexLayout = { selection?: number; cols: Record<string, number> };
|
|
151
|
+
|
|
152
|
+
function flexWeight<T>(col: ColumnType<T>): number {
|
|
153
|
+
if (col.width != null) return 0;
|
|
154
|
+
if (col.flex === true) return 1;
|
|
155
|
+
if (typeof col.flex === "number" && col.flex > 0) return col.flex;
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function layoutsClose(prev: FlexLayout | null, next: FlexLayout): boolean {
|
|
160
|
+
if (!prev) return false;
|
|
161
|
+
if (Math.abs((prev.selection ?? 0) - (next.selection ?? 0)) > 0.5) return false;
|
|
162
|
+
const keys = Object.keys(next.cols);
|
|
163
|
+
if (keys.length !== Object.keys(prev.cols).length) return false;
|
|
164
|
+
return keys.every((key) => Math.abs((prev.cols[key] ?? -1) - next.cols[key]) <= 0.5);
|
|
165
|
+
}
|
|
166
|
+
|
|
145
167
|
function sizeClasses(s: CanonicalSize) {
|
|
146
168
|
if (s === "small") {
|
|
147
169
|
return {
|
|
@@ -152,15 +174,15 @@ function sizeClasses(s: CanonicalSize) {
|
|
|
152
174
|
}
|
|
153
175
|
if (s === "large") {
|
|
154
176
|
return {
|
|
155
|
-
head: "h-12 px-3 text-
|
|
156
|
-
cell: "px-3 py-3 text-
|
|
177
|
+
head: "h-12 px-3 text-base",
|
|
178
|
+
cell: "px-3 py-3 text-base",
|
|
157
179
|
check: "w-12 px-3",
|
|
158
180
|
};
|
|
159
181
|
}
|
|
160
|
-
// default —
|
|
182
|
+
// default — text-base is 14px (`--text-sm` in this theme is 13px)
|
|
161
183
|
return {
|
|
162
|
-
head: "h-10 px-2 text-
|
|
163
|
-
cell: "p-2 text-
|
|
184
|
+
head: "h-10 px-2 text-base",
|
|
185
|
+
cell: "p-2 text-base",
|
|
164
186
|
check: "w-10 px-2",
|
|
165
187
|
};
|
|
166
188
|
}
|
|
@@ -264,19 +286,101 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
264
286
|
|
|
265
287
|
const alignClass = (align?: "left" | "center" | "right") => (align === "center" ? "text-center" : align === "right" ? "text-right" : "text-left");
|
|
266
288
|
|
|
289
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
290
|
+
const columnsRef = useRef(columns);
|
|
291
|
+
columnsRef.current = columns;
|
|
292
|
+
const [flexLayout, setFlexLayout] = useState<FlexLayout | null>(null);
|
|
293
|
+
const flexSig = `${scroll?.x ?? ""}|${rowSelection ? 1 : 0}|${showHeader ? 1 : 0}|${dataSource.length}|${columns.map((col, i) => `${getColumnKey(col, i)}:${col.flex ?? ""}:${col.width ?? ""}`).join(",")}`;
|
|
294
|
+
const flexSigRef = useRef(flexSig);
|
|
295
|
+
if (flexSigRef.current !== flexSig) {
|
|
296
|
+
flexSigRef.current = flexSig;
|
|
297
|
+
if (flexLayout) setFlexLayout(null);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
useLayoutEffect(() => {
|
|
301
|
+
const container = containerRef.current;
|
|
302
|
+
const cols = columnsRef.current;
|
|
303
|
+
const wantsFlex = scroll?.x == null && cols.some((col) => flexWeight(col) > 0);
|
|
304
|
+
if (!container || !wantsFlex) {
|
|
305
|
+
setFlexLayout((prev) => (prev ? null : prev));
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let locked: { selection?: number; fixed: Record<string, number>; weights: { key: string; weight: number }[] } | null = null;
|
|
310
|
+
|
|
311
|
+
const apply = () => {
|
|
312
|
+
const client = container.clientWidth;
|
|
313
|
+
if (client <= 0) return;
|
|
314
|
+
if (!locked) {
|
|
315
|
+
const table = container.querySelector("table");
|
|
316
|
+
const row = table?.querySelector("thead > tr") ?? table?.querySelector("tbody > tr");
|
|
317
|
+
if (!row) return;
|
|
318
|
+
const cells = [...row.children] as HTMLElement[];
|
|
319
|
+
const start = rowSelection ? 1 : 0;
|
|
320
|
+
const fixed: Record<string, number> = {};
|
|
321
|
+
const weights: { key: string; weight: number }[] = [];
|
|
322
|
+
cols.forEach((col, i) => {
|
|
323
|
+
const key = getColumnKey(col, i);
|
|
324
|
+
const weight = flexWeight(col);
|
|
325
|
+
if (weight > 0) weights.push({ key, weight });
|
|
326
|
+
else fixed[key] = cells[start + i]?.getBoundingClientRect().width ?? 0;
|
|
327
|
+
});
|
|
328
|
+
if (!weights.length) return;
|
|
329
|
+
locked = {
|
|
330
|
+
selection: rowSelection ? cells[0]?.getBoundingClientRect().width : undefined,
|
|
331
|
+
fixed,
|
|
332
|
+
weights,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const used = (locked.selection ?? 0) + Object.values(locked.fixed).reduce((sum, n) => sum + n, 0);
|
|
336
|
+
if (used > client + 1) {
|
|
337
|
+
setFlexLayout((prev) => (prev ? null : prev));
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const leftover = Math.max(0, client - used);
|
|
341
|
+
const weightSum = locked.weights.reduce((sum, item) => sum + item.weight, 0);
|
|
342
|
+
const colsWidth = { ...locked.fixed };
|
|
343
|
+
for (const item of locked.weights) colsWidth[item.key] = (leftover * item.weight) / weightSum;
|
|
344
|
+
const next: FlexLayout = { selection: locked.selection, cols: colsWidth };
|
|
345
|
+
setFlexLayout((prev) => (layoutsClose(prev, next) ? prev : next));
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
apply();
|
|
349
|
+
const observer = new ResizeObserver(apply);
|
|
350
|
+
observer.observe(container);
|
|
351
|
+
return () => observer.disconnect();
|
|
352
|
+
}, [flexSig, rowSelection, scroll?.x, showHeader]);
|
|
353
|
+
|
|
354
|
+
const wantsFlex = scroll?.x == null && columns.some((col) => flexWeight(col) > 0);
|
|
355
|
+
|
|
356
|
+
const colStyle = (col: ColumnType<T>, key: string): CSSProperties => {
|
|
357
|
+
const measured = flexLayout?.cols[key];
|
|
358
|
+
if (measured != null) return { width: measured, maxWidth: measured, minWidth: 0 };
|
|
359
|
+
return { width: col.width, minWidth: col.minWidth };
|
|
360
|
+
};
|
|
361
|
+
|
|
267
362
|
return (
|
|
268
363
|
<div className={cn("relative w-full", className)}>
|
|
269
364
|
{titleNode != null ? <div className="mb-3 text-sm font-medium text-foreground">{titleNode}</div> : null}
|
|
270
365
|
|
|
271
366
|
<Spin spinning={Boolean(loading)}>
|
|
272
|
-
<div data-slot="table-container" className={cn("relative w-full overflow-x-auto", bordered && "rounded-md border border-border")} style={scroll?.x != null ? { overflowX: "auto" } : undefined}>
|
|
367
|
+
<div ref={containerRef} data-slot="table-container" className={cn("relative w-full", flexLayout ? "overflow-x-hidden" : "overflow-x-auto", bordered && "rounded-md border border-border")} style={scroll?.x != null ? { overflowX: "auto" } : undefined}>
|
|
273
368
|
<div style={scroll?.y != null ? { maxHeight: scroll.y, overflow: "auto" } : undefined}>
|
|
274
|
-
<table data-slot="table" className="
|
|
369
|
+
<table data-slot="table" className={cn("caption-bottom border-collapse text-base", flexLayout ? "w-full table-fixed" : wantsFlex ? "w-max" : "w-full")}>
|
|
370
|
+
{flexLayout ? (
|
|
371
|
+
<colgroup>
|
|
372
|
+
{rowSelection ? <col style={{ width: flexLayout.selection }} /> : null}
|
|
373
|
+
{columns.map((col, i) => {
|
|
374
|
+
const key = getColumnKey(col, i);
|
|
375
|
+
return <col key={key} style={{ width: flexLayout.cols[key] }} />;
|
|
376
|
+
})}
|
|
377
|
+
</colgroup>
|
|
378
|
+
) : null}
|
|
275
379
|
{showHeader ? (
|
|
276
380
|
<thead data-slot="table-header" className="[&_tr]:border-b [&_tr]:border-border">
|
|
277
381
|
<tr data-slot="table-row" className="border-b border-border transition-colors hover:bg-transparent">
|
|
278
382
|
{rowSelection ? (
|
|
279
|
-
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground", scroll?.y && "sticky top-0 z-10
|
|
383
|
+
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground bg-foreground/4", scroll?.y && "sticky top-0 z-10", bordered && "border-b border-border")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined}>
|
|
280
384
|
<div className="flex items-center justify-center">
|
|
281
385
|
{rowSelection.type === "radio" ? null : (
|
|
282
386
|
<Checkbox
|
|
@@ -301,10 +405,9 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
301
405
|
<th
|
|
302
406
|
key={key}
|
|
303
407
|
data-slot="table-head"
|
|
304
|
-
className={cn(sz.head, "align-middle font-medium
|
|
408
|
+
className={cn(sz.head, "align-middle font-medium text-foreground bg-foreground/4", flexLayout && flexWeight(col) > 0 ? "min-w-0" : "whitespace-nowrap", alignClass(col.align), scroll?.y && "sticky top-0 z-10", bordered && "border-b border-border", sortable && "cursor-pointer select-none", col.className, headerExtra?.className)}
|
|
305
409
|
style={{
|
|
306
|
-
|
|
307
|
-
minWidth: col.minWidth,
|
|
410
|
+
...colStyle(col, key),
|
|
308
411
|
...headerExtra?.style,
|
|
309
412
|
}}
|
|
310
413
|
onClick={sortable ? () => toggleSort(col, i) : undefined}
|
|
@@ -334,10 +437,31 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
334
437
|
const rowProps = onRow?.(record, absoluteIndex);
|
|
335
438
|
const extraClass = typeof rowClassName === "function" ? rowClassName(record, absoluteIndex) : rowClassName;
|
|
336
439
|
const selected = selectedKeys.includes(key);
|
|
440
|
+
const inSelectionMode = Boolean(rowSelection) && selectedKeys.length > 0;
|
|
441
|
+
const rowDisabled = Boolean(rowSelection?.getCheckboxProps?.(record)?.disabled);
|
|
337
442
|
return (
|
|
338
|
-
<tr
|
|
443
|
+
<tr
|
|
444
|
+
key={key}
|
|
445
|
+
data-slot="table-row"
|
|
446
|
+
data-state={selected ? "selected" : undefined}
|
|
447
|
+
className={cn("border-b border-border transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", inSelectionMode && !rowDisabled && "cursor-pointer", rowProps?.className, extraClass)}
|
|
448
|
+
style={rowProps?.style}
|
|
449
|
+
onClick={inSelectionMode ? undefined : rowProps?.onClick}
|
|
450
|
+
onClickCapture={
|
|
451
|
+
inSelectionMode && !rowDisabled
|
|
452
|
+
? (e) => {
|
|
453
|
+
if (e.target instanceof Element && e.target.closest("[data-row-select]")) return;
|
|
454
|
+
e.preventDefault();
|
|
455
|
+
e.stopPropagation();
|
|
456
|
+
if (rowSelection?.type === "radio") emitSelection([key]);
|
|
457
|
+
else if (selected) emitSelection(selectedKeys.filter((k) => k !== key));
|
|
458
|
+
else emitSelection([...selectedKeys, key]);
|
|
459
|
+
}
|
|
460
|
+
: undefined
|
|
461
|
+
}
|
|
462
|
+
>
|
|
339
463
|
{rowSelection ? (
|
|
340
|
-
<td data-slot="table-cell" className={cn(sz.cell, sz.check, "align-middle")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined} onClick={(e) => e.stopPropagation()}>
|
|
464
|
+
<td data-row-select data-slot="table-cell" className={cn(sz.cell, sz.check, "align-middle")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined} onClick={(e) => e.stopPropagation()}>
|
|
341
465
|
<div className="flex items-center justify-center">
|
|
342
466
|
{rowSelection.type === "radio" ? (
|
|
343
467
|
<input type="radio" name="nonla-table-row-select" checked={selected} disabled={rowSelection.getCheckboxProps?.(record)?.disabled} aria-label="Select row" className="size-3.5 accent-foreground" onChange={() => emitSelection([key])} />
|
|
@@ -362,8 +486,8 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
362
486
|
<td
|
|
363
487
|
key={getColumnKey(col, i)}
|
|
364
488
|
data-slot="table-cell"
|
|
365
|
-
className={cn(sz.cell, "align-middle", alignClass(col.align), col.ellipsis ? "max-w-0 truncate" : "whitespace-nowrap", col.className)}
|
|
366
|
-
style={
|
|
489
|
+
className={cn(sz.cell, "align-middle", alignClass(col.align), flexLayout && flexWeight(col) > 0 ? (col.ellipsis ? "max-w-0 truncate" : "min-w-0 whitespace-normal wrap-break-word") : col.ellipsis ? "max-w-0 truncate" : "whitespace-nowrap", col.className)}
|
|
490
|
+
style={colStyle(col, getColumnKey(col, i))}
|
|
367
491
|
title={col.ellipsis && (typeof content === "string" || typeof content === "number") ? String(content) : undefined}
|
|
368
492
|
>
|
|
369
493
|
{content}
|