devnonla-ui 0.20.0 → 0.20.2
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/dist/chat/index.js +1 -0
- package/dist/files/geist-mono-cyrillic-ext-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-cyrillic-ext-wght-normal.woff2 +0 -0
- package/dist/files/geist-mono-cyrillic-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-cyrillic-wght-normal.woff2 +0 -0
- package/dist/files/geist-mono-latin-ext-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-latin-ext-wght-normal.woff2 +0 -0
- package/dist/files/geist-mono-latin-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-latin-wght-normal.woff2 +0 -0
- package/dist/files/geist-mono-symbols2-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-symbols2-wght-normal.woff2 +0 -0
- package/dist/files/geist-mono-vietnamese-wght-italic.woff2 +0 -0
- package/dist/files/geist-mono-vietnamese-wght-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-opsz-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-opsz-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-standard-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-standard-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-wght-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-ext-wght-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-opsz-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-opsz-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-standard-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-standard-normal.woff2 +0 -0
- package/dist/files/inter-cyrillic-wght-italic.woff2 +0 -0
- package/dist/files/inter-cyrillic-wght-normal.woff2 +0 -0
- package/dist/files/inter-greek-ext-opsz-italic.woff2 +0 -0
- package/dist/files/inter-greek-ext-opsz-normal.woff2 +0 -0
- package/dist/files/inter-greek-ext-standard-italic.woff2 +0 -0
- package/dist/files/inter-greek-ext-standard-normal.woff2 +0 -0
- package/dist/files/inter-greek-ext-wght-italic.woff2 +0 -0
- package/dist/files/inter-greek-ext-wght-normal.woff2 +0 -0
- package/dist/files/inter-greek-opsz-italic.woff2 +0 -0
- package/dist/files/inter-greek-opsz-normal.woff2 +0 -0
- package/dist/files/inter-greek-standard-italic.woff2 +0 -0
- package/dist/files/inter-greek-standard-normal.woff2 +0 -0
- package/dist/files/inter-greek-wght-italic.woff2 +0 -0
- package/dist/files/inter-greek-wght-normal.woff2 +0 -0
- package/dist/files/inter-latin-ext-opsz-italic.woff2 +0 -0
- package/dist/files/inter-latin-ext-opsz-normal.woff2 +0 -0
- package/dist/files/inter-latin-ext-standard-italic.woff2 +0 -0
- package/dist/files/inter-latin-ext-standard-normal.woff2 +0 -0
- package/dist/files/inter-latin-ext-wght-italic.woff2 +0 -0
- package/dist/files/inter-latin-ext-wght-normal.woff2 +0 -0
- package/dist/files/inter-latin-opsz-italic.woff2 +0 -0
- package/dist/files/inter-latin-opsz-normal.woff2 +0 -0
- package/dist/files/inter-latin-standard-italic.woff2 +0 -0
- package/dist/files/inter-latin-standard-normal.woff2 +0 -0
- package/dist/files/inter-latin-wght-italic.woff2 +0 -0
- package/dist/files/inter-latin-wght-normal.woff2 +0 -0
- package/dist/files/inter-vietnamese-opsz-italic.woff2 +0 -0
- package/dist/files/inter-vietnamese-opsz-normal.woff2 +0 -0
- package/dist/files/inter-vietnamese-standard-italic.woff2 +0 -0
- package/dist/files/inter-vietnamese-standard-normal.woff2 +0 -0
- package/dist/files/inter-vietnamese-wght-italic.woff2 +0 -0
- package/dist/files/inter-vietnamese-wght-normal.woff2 +0 -0
- package/dist/index.js +1 -0
- package/dist/markdown-editor/index.js +1 -0
- package/dist/nonla-ui.css +2 -0
- package/package.json +14 -4
- package/src/alert/Alert.tsx +31 -34
- package/src/button/Button.tsx +5 -4
- package/src/index.ts +2 -0
- package/src/markdown-editor/MarkdownTable.tsx +8 -5
- package/src/markdown-editor/MarkdownViewer.tsx +1 -1
- package/src/scroll/OverlayScroll.tsx +1 -1
- package/src/standalone.css +3 -0
- package/src/styles.css +135 -29
- package/src/tag/Tag.tsx +2 -2
- package/src/theme.ts +4 -4
- package/src/tree/Tree.tsx +272 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { type KeyboardEvent, type ReactNode, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Icon, isIconName } from "../icon/Icon";
|
|
3
|
+
import { cn } from "../lib/cn";
|
|
4
|
+
|
|
5
|
+
export type TreeNode = {
|
|
6
|
+
key: string;
|
|
7
|
+
title: ReactNode;
|
|
8
|
+
/** An `Icon` name, or a node. */
|
|
9
|
+
icon?: ReactNode;
|
|
10
|
+
children?: TreeNode[];
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** `false` makes the row open and close (and fire `onExpand`) even without `children`, for lazy loading. */
|
|
13
|
+
isLeaf?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type TreeSelectInfo = { node: TreeNode; selected: boolean };
|
|
17
|
+
export type TreeExpandInfo = { node: TreeNode; expanded: boolean };
|
|
18
|
+
|
|
19
|
+
export type TreeProps = {
|
|
20
|
+
treeData: TreeNode[];
|
|
21
|
+
/** Draw guide lines from each parent to its children. Default `true`. */
|
|
22
|
+
showLine?: boolean;
|
|
23
|
+
expandedKeys?: string[];
|
|
24
|
+
defaultExpandedKeys?: string[];
|
|
25
|
+
/** Open every parent on first render (uncontrolled only). */
|
|
26
|
+
defaultExpandAll?: boolean;
|
|
27
|
+
onExpand?: (keys: string[], info: TreeExpandInfo) => void;
|
|
28
|
+
selectedKeys?: string[];
|
|
29
|
+
defaultSelectedKeys?: string[];
|
|
30
|
+
onSelect?: (keys: string[], info: TreeSelectInfo) => void;
|
|
31
|
+
/** Allow picking rows. Default `true`. */
|
|
32
|
+
selectable?: boolean;
|
|
33
|
+
/** Pick several rows (click toggles). Default `false`. */
|
|
34
|
+
multiple?: boolean;
|
|
35
|
+
/** Custom row content. Defaults to `title`. */
|
|
36
|
+
titleRender?: (node: TreeNode) => ReactNode;
|
|
37
|
+
/** Row height and indent: `small` 24px, `default` 32px, `large` 40px (the control sizes). */
|
|
38
|
+
size?: "small" | "default" | "large";
|
|
39
|
+
className?: string;
|
|
40
|
+
"aria-label"?: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Row height, indent (one level's width) and icon slot per size. Guide lines run down the middle of the
|
|
45
|
+
* parent's icon slot; the indent is wider than the slot, and that extra width is the length of each elbow.
|
|
46
|
+
*/
|
|
47
|
+
const SIZES = {
|
|
48
|
+
small: { height: 24, indent: 24, slot: 20 },
|
|
49
|
+
default: { height: 32, indent: 28, slot: 24 },
|
|
50
|
+
large: { height: 40, indent: 28, slot: 24 },
|
|
51
|
+
} as const;
|
|
52
|
+
const ELBOW_RADIUS = 5;
|
|
53
|
+
|
|
54
|
+
type Row = {
|
|
55
|
+
node: TreeNode;
|
|
56
|
+
depth: number;
|
|
57
|
+
last: boolean;
|
|
58
|
+
/** Per ancestor column: does that branch continue below this row? */
|
|
59
|
+
rails: boolean[];
|
|
60
|
+
parentKey: string | null;
|
|
61
|
+
hasChildren: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function collectParents(nodes: TreeNode[], out: string[] = []) {
|
|
65
|
+
for (const n of nodes) {
|
|
66
|
+
if (n.children?.length) {
|
|
67
|
+
out.push(n.key);
|
|
68
|
+
collectParents(n.children, out);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function flatten(nodes: TreeNode[], open: Set<string>, depth = 0, rails: boolean[] = [], parentKey: string | null = null, out: Row[] = []) {
|
|
75
|
+
nodes.forEach((node, i) => {
|
|
76
|
+
const last = i === nodes.length - 1;
|
|
77
|
+
const hasChildren = Boolean(node.children?.length) || node.isLeaf === false;
|
|
78
|
+
out.push({ node, depth, last, rails, parentKey, hasChildren });
|
|
79
|
+
if (node.children?.length && open.has(node.key)) flatten(node.children, open, depth + 1, [...rails, !last], node.key, out);
|
|
80
|
+
});
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The row's hover / selected box is inset this much top and bottom. Guide lines stay outside the box:
|
|
86
|
+
* an elbow stops 1px before its left edge, a parent's stub starts at its bottom edge.
|
|
87
|
+
*/
|
|
88
|
+
const BOX_INSET = 2;
|
|
89
|
+
|
|
90
|
+
/** Left padding inside the row's box before the icon. Guide lines are shifted by the same amount, so they stay centered on the icon. */
|
|
91
|
+
const ICON_PAD = 2;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* One row's guide lines: a rail for every ancestor branch that continues, an elbow into this row
|
|
95
|
+
* (`├` or a rounded `└` for the last child), and a stub down from an open parent.
|
|
96
|
+
*/
|
|
97
|
+
function Guides({ row, height, indent, slot, open }: { row: Row; height: number; indent: number; slot: number; open: boolean }) {
|
|
98
|
+
const { depth, last, rails, hasChildren } = row;
|
|
99
|
+
// Exactly on the icon's center and the row's middle (no half-pixel nudge: with a 1.25px stroke it only pushed the line off-center).
|
|
100
|
+
const mid = height / 2;
|
|
101
|
+
const cx = (c: number) => c * indent + ICON_PAD + slot / 2;
|
|
102
|
+
const paths: string[] = [];
|
|
103
|
+
// Ancestor rails (columns 0..depth-2 belong to ancestors; rails[c+1] says whether that ancestor has later siblings).
|
|
104
|
+
for (let c = 0; c < depth - 1; c++) if (rails[c + 1]) paths.push(`M${cx(c)} 0V${height}`);
|
|
105
|
+
if (depth > 0) {
|
|
106
|
+
const x = cx(depth - 1);
|
|
107
|
+
// The row's box starts at depth * indent; stop just short of it so the line never runs under the hover fill.
|
|
108
|
+
const end = depth * indent - 1;
|
|
109
|
+
paths.push(last ? `M${x} 0V${mid - ELBOW_RADIUS}Q${x} ${mid} ${x + ELBOW_RADIUS} ${mid}H${end}` : `M${x} 0V${height}M${x} ${mid}H${end}`);
|
|
110
|
+
}
|
|
111
|
+
// Stub from an open parent down to its first child, from the bottom edge of the parent's box.
|
|
112
|
+
if (open && hasChildren) paths.push(`M${cx(depth)} ${height - BOX_INSET}V${height}`);
|
|
113
|
+
if (!paths.length) return null;
|
|
114
|
+
return (
|
|
115
|
+
<svg aria-hidden width={(depth + 1) * indent} height={height} className="pointer-events-none absolute top-0 left-0 overflow-visible">
|
|
116
|
+
{/* Butt caps: rails of stacked rows meet edge to edge. Round caps overlap by half a pixel and the translucent ink doubles there. */}
|
|
117
|
+
<path d={paths.join("")} fill="none" stroke="var(--nonla-tree-line)" strokeWidth="1.25" strokeLinecap="butt" strokeLinejoin="round" />
|
|
118
|
+
</svg>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Hierarchical list with SVG guide lines, expand / collapse, selection, and arrow-key navigation (WAI-ARIA tree). */
|
|
123
|
+
export function Tree({
|
|
124
|
+
treeData,
|
|
125
|
+
showLine = true,
|
|
126
|
+
expandedKeys,
|
|
127
|
+
defaultExpandedKeys,
|
|
128
|
+
defaultExpandAll = false,
|
|
129
|
+
onExpand,
|
|
130
|
+
selectedKeys,
|
|
131
|
+
defaultSelectedKeys,
|
|
132
|
+
onSelect,
|
|
133
|
+
selectable = true,
|
|
134
|
+
multiple = false,
|
|
135
|
+
titleRender,
|
|
136
|
+
size = "default",
|
|
137
|
+
className,
|
|
138
|
+
"aria-label": ariaLabel,
|
|
139
|
+
}: TreeProps) {
|
|
140
|
+
const [innerOpen, setInnerOpen] = useState<string[]>(() => defaultExpandedKeys ?? (defaultExpandAll ? collectParents(treeData) : []));
|
|
141
|
+
const openKeys = expandedKeys ?? innerOpen;
|
|
142
|
+
const open = useMemo(() => new Set(openKeys), [openKeys]);
|
|
143
|
+
const [innerSel, setInnerSel] = useState<string[]>(defaultSelectedKeys ?? []);
|
|
144
|
+
const selKeys = selectedKeys ?? innerSel;
|
|
145
|
+
const selected = useMemo(() => new Set(selKeys), [selKeys]);
|
|
146
|
+
const rows = useMemo(() => flatten(treeData, open), [treeData, open]);
|
|
147
|
+
const [focusKey, setFocusKey] = useState<string | null>(null);
|
|
148
|
+
const rowRefs = useRef(new Map<string, HTMLDivElement>());
|
|
149
|
+
const { height, indent, slot } = SIZES[size];
|
|
150
|
+
|
|
151
|
+
const toggle = (row: Row, next = !open.has(row.node.key)) => {
|
|
152
|
+
if (!row.hasChildren || row.node.disabled || next === open.has(row.node.key)) return;
|
|
153
|
+
const keys = next ? [...openKeys, row.node.key] : openKeys.filter((k) => k !== row.node.key);
|
|
154
|
+
if (expandedKeys === undefined) setInnerOpen(keys);
|
|
155
|
+
onExpand?.(keys, { node: row.node, expanded: next });
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const pick = (row: Row) => {
|
|
159
|
+
if (!selectable || row.node.disabled) return;
|
|
160
|
+
const was = selected.has(row.node.key);
|
|
161
|
+
const keys = multiple ? (was ? selKeys.filter((k) => k !== row.node.key) : [...selKeys, row.node.key]) : was ? [] : [row.node.key];
|
|
162
|
+
if (selectedKeys === undefined) setInnerSel(keys);
|
|
163
|
+
onSelect?.(keys, { node: row.node, selected: !was });
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const focusRow = (key: string | undefined) => {
|
|
167
|
+
if (!key) return;
|
|
168
|
+
setFocusKey(key);
|
|
169
|
+
rowRefs.current.get(key)?.focus();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
173
|
+
// The row the key event came from; falls back to the remembered focus.
|
|
174
|
+
const fromKey = (e.target as HTMLElement).closest<HTMLElement>("[data-tree-key]")?.dataset.treeKey;
|
|
175
|
+
const i = rows.findIndex((r) => r.node.key === (fromKey ?? focusKey ?? rows[0]?.node.key));
|
|
176
|
+
const row = rows[i];
|
|
177
|
+
if (!row) return;
|
|
178
|
+
const isOpen = open.has(row.node.key);
|
|
179
|
+
switch (e.key) {
|
|
180
|
+
case "ArrowDown":
|
|
181
|
+
focusRow(rows[i + 1]?.node.key);
|
|
182
|
+
break;
|
|
183
|
+
case "ArrowUp":
|
|
184
|
+
focusRow(rows[i - 1]?.node.key);
|
|
185
|
+
break;
|
|
186
|
+
case "Home":
|
|
187
|
+
focusRow(rows[0]?.node.key);
|
|
188
|
+
break;
|
|
189
|
+
case "End":
|
|
190
|
+
focusRow(rows[rows.length - 1]?.node.key);
|
|
191
|
+
break;
|
|
192
|
+
case "ArrowRight":
|
|
193
|
+
if (row.hasChildren && !isOpen) toggle(row, true);
|
|
194
|
+
else if (isOpen) focusRow(rows[i + 1]?.node.key);
|
|
195
|
+
break;
|
|
196
|
+
case "ArrowLeft":
|
|
197
|
+
if (row.hasChildren && isOpen) toggle(row, false);
|
|
198
|
+
else focusRow(row.parentKey ?? undefined);
|
|
199
|
+
break;
|
|
200
|
+
case "Enter":
|
|
201
|
+
case " ":
|
|
202
|
+
pick(row);
|
|
203
|
+
break;
|
|
204
|
+
default:
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
e.preventDefault();
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const tabKey = focusKey && rows.some((r) => r.node.key === focusKey) ? focusKey : (rows.find((r) => selected.has(r.node.key)) ?? rows[0])?.node.key;
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<div role="tree" aria-label={ariaLabel} aria-multiselectable={multiple || undefined} onKeyDown={onKeyDown} className={cn("flex flex-col text-sm text-foreground", className)}>
|
|
214
|
+
{rows.map((row) => {
|
|
215
|
+
const { node, depth, hasChildren } = row;
|
|
216
|
+
const isOpen = open.has(node.key);
|
|
217
|
+
const isSel = selected.has(node.key);
|
|
218
|
+
return (
|
|
219
|
+
<div
|
|
220
|
+
key={node.key}
|
|
221
|
+
ref={(el) => {
|
|
222
|
+
if (el) rowRefs.current.set(node.key, el);
|
|
223
|
+
else rowRefs.current.delete(node.key);
|
|
224
|
+
}}
|
|
225
|
+
role="treeitem"
|
|
226
|
+
data-tree-key={node.key}
|
|
227
|
+
aria-level={depth + 1}
|
|
228
|
+
aria-expanded={hasChildren ? isOpen : undefined}
|
|
229
|
+
aria-selected={selectable ? isSel : undefined}
|
|
230
|
+
aria-disabled={node.disabled || undefined}
|
|
231
|
+
tabIndex={node.key === tabKey ? 0 : -1}
|
|
232
|
+
onFocus={() => setFocusKey(node.key)}
|
|
233
|
+
onClick={() => {
|
|
234
|
+
setFocusKey(node.key);
|
|
235
|
+
// The row is the toggle: a click opens or closes a parent, and picks it.
|
|
236
|
+
toggle(row);
|
|
237
|
+
pick(row);
|
|
238
|
+
}}
|
|
239
|
+
// Fast repeated clicks would otherwise select the title text.
|
|
240
|
+
onMouseDown={(e) => {
|
|
241
|
+
if (e.detail > 1) e.preventDefault();
|
|
242
|
+
}}
|
|
243
|
+
className={cn("group/tree-row relative flex shrink-0 select-none items-center outline-none", node.disabled ? "cursor-not-allowed" : "cursor-pointer")}
|
|
244
|
+
style={{ height, paddingLeft: depth * indent }}
|
|
245
|
+
>
|
|
246
|
+
{showLine ? <Guides row={row} height={height} indent={indent} slot={slot} open={isOpen} /> : null}
|
|
247
|
+
<span
|
|
248
|
+
className={cn(
|
|
249
|
+
"flex h-[calc(100%-4px)] min-w-0 flex-1 items-center rounded-md transition-colors duration-(--nonla-dur-fast)",
|
|
250
|
+
// With an icon, its slot is centered on the guide line (no left padding). Without one, the text starts
|
|
251
|
+
// right inside the box, so a tree with no icons is not pushed out by empty slots.
|
|
252
|
+
node.icon == null ? "pr-1.5 pl-2" : "pr-1.5",
|
|
253
|
+
size === "small" ? "gap-1.5" : "gap-2",
|
|
254
|
+
isSel ? "bg-ink-active font-medium" : !node.disabled && "group-hover/tree-row:bg-ink-hover",
|
|
255
|
+
// Dim only the content: the guide lines in this row belong to the whole branch and keep their ink.
|
|
256
|
+
node.disabled && "opacity-45",
|
|
257
|
+
"group-focus-visible/tree-row:ring-2 group-focus-visible/tree-row:ring-focus-ring",
|
|
258
|
+
)}
|
|
259
|
+
>
|
|
260
|
+
{node.icon == null ? null : (
|
|
261
|
+
<span className={cn("inline-flex h-4 shrink-0 items-center justify-center text-muted-foreground [&_svg]:size-4", size === "small" ? "-mr-0.5" : "-mr-1.5")} style={{ width: slot, marginLeft: ICON_PAD }}>
|
|
262
|
+
{isIconName(node.icon) ? <Icon name={node.icon} size={16} /> : node.icon}
|
|
263
|
+
</span>
|
|
264
|
+
)}
|
|
265
|
+
<span className="min-w-0 truncate">{titleRender ? titleRender(node) : node.title}</span>
|
|
266
|
+
</span>
|
|
267
|
+
</div>
|
|
268
|
+
);
|
|
269
|
+
})}
|
|
270
|
+
</div>
|
|
271
|
+
);
|
|
272
|
+
}
|