devnonla-ui 0.7.0 → 0.7.1
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/package.json +1 -1
- package/src/input/EditableInput.tsx +2 -2
- package/src/scroll/OverlayScroll.tsx +20 -12
- package/src/table/Table.tsx +42 -13
package/package.json
CHANGED
|
@@ -203,10 +203,10 @@ function EditorPopover({
|
|
|
203
203
|
{field}
|
|
204
204
|
</div>
|
|
205
205
|
<div className="mt-3 flex items-center gap-2">
|
|
206
|
-
<Button type="primary" size=
|
|
206
|
+
<Button type="primary" size="small" loading={saving} disabled={saveDisabled} onClick={onSave}>
|
|
207
207
|
Save
|
|
208
208
|
</Button>
|
|
209
|
-
<Button type="text" size=
|
|
209
|
+
<Button type="text" size="small" disabled={saving} onClick={onCancel}>
|
|
210
210
|
Cancel
|
|
211
211
|
</Button>
|
|
212
212
|
</div>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ReactNode, type PointerEvent as ReactPointerEvent, type Ref, type UIEventHandler, useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
1
|
+
import { type CSSProperties, type ReactNode, type PointerEvent as ReactPointerEvent, type Ref, type UIEventHandler, useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
3
|
|
|
4
4
|
export type OverlayScrollVisibility = "hover" | "always";
|
|
@@ -12,10 +12,13 @@ export type OverlayScrollProps = {
|
|
|
12
12
|
/** Size to content (honors max-height on `className`) instead of filling the parent. */
|
|
13
13
|
autoHeight?: boolean;
|
|
14
14
|
className?: string;
|
|
15
|
+
style?: CSSProperties;
|
|
15
16
|
innerClassName?: string;
|
|
16
17
|
children?: ReactNode;
|
|
17
18
|
onScroll?: UIEventHandler<HTMLDivElement>;
|
|
18
19
|
scrollRef?: Ref<HTMLDivElement>;
|
|
20
|
+
/** Keep the vertical thumb below this many pixels (sticky table header). */
|
|
21
|
+
insetTop?: number;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
type Thumb = { offset: number; size: number; shown: boolean };
|
|
@@ -29,11 +32,12 @@ function assignRef(ref: Ref<HTMLDivElement> | undefined, node: HTMLDivElement |
|
|
|
29
32
|
else ref.current = node;
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
function measure(scroll: number, scrollSize: number, client: number): Thumb {
|
|
35
|
+
function measure(scroll: number, scrollSize: number, client: number, inset = 0): Thumb {
|
|
36
|
+
const track = Math.max(0, client - inset);
|
|
33
37
|
const overflow = scrollSize - client;
|
|
34
|
-
if (overflow <= 1) return HIDDEN;
|
|
35
|
-
const size = Math.max(MIN_THUMB, (
|
|
36
|
-
const offset = (scroll / overflow) * (
|
|
38
|
+
if (overflow <= 1 || track <= 1) return HIDDEN;
|
|
39
|
+
const size = Math.min(track, Math.max(MIN_THUMB, (track / scrollSize) * track));
|
|
40
|
+
const offset = (scroll / overflow) * Math.max(0, track - size);
|
|
37
41
|
return { offset, size, shown: true };
|
|
38
42
|
}
|
|
39
43
|
|
|
@@ -42,7 +46,7 @@ function canScrollX(el: HTMLDivElement) {
|
|
|
42
46
|
return overflow === "auto" || overflow === "scroll";
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
export function OverlayScroll({ visibility = "hover", autoHeight = false, className, innerClassName, children, onScroll, scrollRef }: OverlayScrollProps) {
|
|
49
|
+
export function OverlayScroll({ visibility = "hover", autoHeight = false, className, style, innerClassName, children, onScroll, scrollRef, insetTop = 0 }: OverlayScrollProps) {
|
|
46
50
|
const elRef = useRef<HTMLDivElement | null>(null);
|
|
47
51
|
const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
48
52
|
const vThumbRef = useRef<Thumb>(HIDDEN);
|
|
@@ -54,10 +58,13 @@ export function OverlayScroll({ visibility = "hover", autoHeight = false, classN
|
|
|
54
58
|
vThumbRef.current = vThumb;
|
|
55
59
|
hThumbRef.current = hThumb;
|
|
56
60
|
|
|
61
|
+
const insetTopRef = useRef(insetTop);
|
|
62
|
+
insetTopRef.current = insetTop;
|
|
63
|
+
|
|
57
64
|
const updateThumb = useCallback(() => {
|
|
58
65
|
const el = elRef.current;
|
|
59
66
|
if (!el) return;
|
|
60
|
-
setVThumb(measure(el.scrollTop, el.scrollHeight, el.clientHeight));
|
|
67
|
+
setVThumb(measure(el.scrollTop, el.scrollHeight, el.clientHeight, insetTopRef.current));
|
|
61
68
|
setHThumb(canScrollX(el) ? measure(el.scrollLeft, el.scrollWidth, el.clientWidth) : HIDDEN);
|
|
62
69
|
}, []);
|
|
63
70
|
|
|
@@ -79,7 +86,7 @@ export function OverlayScroll({ visibility = "hover", autoHeight = false, classN
|
|
|
79
86
|
const child = el.firstElementChild;
|
|
80
87
|
if (child) ro.observe(child);
|
|
81
88
|
return () => ro.disconnect();
|
|
82
|
-
}, [updateThumb, innerClassName, autoHeight]);
|
|
89
|
+
}, [updateThumb, innerClassName, autoHeight, insetTop]);
|
|
83
90
|
|
|
84
91
|
useLayoutEffect(() => {
|
|
85
92
|
return () => {
|
|
@@ -104,8 +111,9 @@ export function OverlayScroll({ visibility = "hover", autoHeight = false, classN
|
|
|
104
111
|
e.preventDefault();
|
|
105
112
|
e.stopPropagation();
|
|
106
113
|
|
|
114
|
+
const inset = axis === "y" ? insetTopRef.current : 0;
|
|
107
115
|
const overflow = axis === "y" ? el.scrollHeight - el.clientHeight : el.scrollWidth - el.clientWidth;
|
|
108
|
-
const track = (axis === "y" ? el.clientHeight : el.clientWidth) - current.size;
|
|
116
|
+
const track = (axis === "y" ? el.clientHeight - inset : el.clientWidth) - current.size;
|
|
109
117
|
if (overflow <= 0 || track <= 0) return;
|
|
110
118
|
|
|
111
119
|
const start = axis === "y" ? e.clientY : e.clientX;
|
|
@@ -145,7 +153,7 @@ export function OverlayScroll({ visibility = "hover", autoHeight = false, classN
|
|
|
145
153
|
);
|
|
146
154
|
|
|
147
155
|
return (
|
|
148
|
-
<div className={cn("group/scroll relative min-h-0 min-w-0 w-full overflow-hidden", className)} data-scrolling={thumbVisible ? "true" : undefined}>
|
|
156
|
+
<div className={cn("group/scroll relative min-h-0 min-w-0 w-full overflow-hidden", className)} style={style} data-scrolling={thumbVisible ? "true" : undefined}>
|
|
149
157
|
<div
|
|
150
158
|
ref={setNode}
|
|
151
159
|
onScroll={handleScroll}
|
|
@@ -154,10 +162,10 @@ export function OverlayScroll({ visibility = "hover", autoHeight = false, classN
|
|
|
154
162
|
{children}
|
|
155
163
|
</div>
|
|
156
164
|
{vThumb.shown ? (
|
|
157
|
-
<div aria-hidden onPointerDown={onThumbPointerDown("y")} className={thumbClass} style={{ right: 2, width: "var(--nonla-scrollbar-size)", height: vThumb.size, transform: `translateY(${vThumb.offset}px)` }} />
|
|
165
|
+
<div aria-hidden onPointerDown={onThumbPointerDown("y")} className={thumbClass} style={{ top: insetTop, right: 2, width: "var(--nonla-scrollbar-size)", height: vThumb.size, transform: `translateY(${vThumb.offset}px)` }} />
|
|
158
166
|
) : null}
|
|
159
167
|
{hThumb.shown ? (
|
|
160
|
-
<div aria-hidden onPointerDown={onThumbPointerDown("x")} className={thumbClass} style={{ bottom: 2, height: "var(--nonla-scrollbar-size)", width: hThumb.size, transform: `translateX(${hThumb.offset}px)` }} />
|
|
168
|
+
<div aria-hidden onPointerDown={onThumbPointerDown("x")} className={thumbClass} style={{ left: 0, bottom: 2, height: "var(--nonla-scrollbar-size)", width: hThumb.size, transform: `translateX(${hThumb.offset}px)` }} />
|
|
161
169
|
) : null}
|
|
162
170
|
</div>
|
|
163
171
|
);
|
package/src/table/Table.tsx
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type CSSProperties, type Key, type ReactNode, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
1
|
+
import { type CSSProperties, type Key, type ReactNode, type Ref, 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";
|
|
5
5
|
import { type CanonicalSize, type ControlSize, useControlSize } from "../lib/sizes";
|
|
6
6
|
import { Pagination, type PaginationProps } from "../pagination/Pagination";
|
|
7
|
+
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
7
8
|
import { Spin } from "../spin/Spin";
|
|
8
9
|
|
|
9
10
|
export type SortOrder = "ascend" | "descend" | null;
|
|
@@ -164,6 +165,15 @@ function layoutsClose(prev: FlexLayout | null, next: FlexLayout): boolean {
|
|
|
164
165
|
return keys.every((key) => Math.abs((prev.cols[key] ?? -1) - next.cols[key]) <= 0.5);
|
|
165
166
|
}
|
|
166
167
|
|
|
168
|
+
function TableBodyScroll({ x, y, scrollRef, insetTop = 0, children }: { x?: number | string; y?: number | string; scrollRef: Ref<HTMLDivElement>; insetTop?: number; children: ReactNode }) {
|
|
169
|
+
if (y == null) return <div ref={scrollRef}>{children}</div>;
|
|
170
|
+
return (
|
|
171
|
+
<OverlayScroll autoHeight visibility="hover" scrollRef={scrollRef} insetTop={insetTop} style={{ maxHeight: y }} innerClassName={x != null ? "overflow-x-auto" : undefined}>
|
|
172
|
+
{children}
|
|
173
|
+
</OverlayScroll>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
167
177
|
function sizeClasses(s: CanonicalSize) {
|
|
168
178
|
if (s === "small") {
|
|
169
179
|
return {
|
|
@@ -287,10 +297,11 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
287
297
|
const alignClass = (align?: "left" | "center" | "right") => (align === "center" ? "text-center" : align === "right" ? "text-right" : "text-left");
|
|
288
298
|
|
|
289
299
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
300
|
+
const scrollBodyRef = useRef<HTMLDivElement>(null);
|
|
290
301
|
const columnsRef = useRef(columns);
|
|
291
302
|
columnsRef.current = columns;
|
|
292
303
|
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(",")}`;
|
|
304
|
+
const flexSig = `${scroll?.x ?? ""}|${scroll?.y ?? ""}|${rowSelection ? 1 : 0}|${showHeader ? 1 : 0}|${dataSource.length}|${columns.map((col, i) => `${getColumnKey(col, i)}:${col.flex ?? ""}:${col.width ?? ""}`).join(",")}`;
|
|
294
305
|
const flexSigRef = useRef(flexSig);
|
|
295
306
|
if (flexSigRef.current !== flexSig) {
|
|
296
307
|
flexSigRef.current = flexSig;
|
|
@@ -309,7 +320,9 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
309
320
|
let locked: { selection?: number; fixed: Record<string, number>; weights: { key: string; weight: number }[] } | null = null;
|
|
310
321
|
|
|
311
322
|
const apply = () => {
|
|
312
|
-
const
|
|
323
|
+
const scroller = scrollBodyRef.current;
|
|
324
|
+
const scrollbar = scroller && scroller.scrollHeight > scroller.clientHeight + 1 ? scroller.offsetWidth - scroller.clientWidth : 0;
|
|
325
|
+
const client = container.clientWidth - Math.max(0, scrollbar);
|
|
313
326
|
if (client <= 0) return;
|
|
314
327
|
if (!locked) {
|
|
315
328
|
const table = container.querySelector("table");
|
|
@@ -348,11 +361,26 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
348
361
|
apply();
|
|
349
362
|
const observer = new ResizeObserver(apply);
|
|
350
363
|
observer.observe(container);
|
|
364
|
+
if (scrollBodyRef.current) observer.observe(scrollBodyRef.current);
|
|
351
365
|
return () => observer.disconnect();
|
|
352
|
-
}, [flexSig, rowSelection, scroll?.x, showHeader]);
|
|
366
|
+
}, [flexSig, rowSelection, scroll?.x, scroll?.y, showHeader]);
|
|
353
367
|
|
|
354
368
|
const wantsFlex = scroll?.x == null && columns.some((col) => flexWeight(col) > 0);
|
|
355
369
|
|
|
370
|
+
const headSticky = scroll?.y != null;
|
|
371
|
+
const [thumbInset, setThumbInset] = useState(0);
|
|
372
|
+
useLayoutEffect(() => {
|
|
373
|
+
if (!headSticky || !showHeader) {
|
|
374
|
+
setThumbInset(0);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const head = scrollBodyRef.current?.querySelector("thead");
|
|
378
|
+
const next = head ? Math.ceil(head.getBoundingClientRect().height) + 4 : 0;
|
|
379
|
+
setThumbInset((prev) => (prev === next ? prev : next));
|
|
380
|
+
}, [headSticky, showHeader, size, flexLayout]);
|
|
381
|
+
/** Opaque fill. `bg-foreground/4` is 4% alpha, so body text shows through a sticky header. */
|
|
382
|
+
const headBg: CSSProperties | undefined = headSticky ? { backgroundColor: "var(--nonla-bg)" } : undefined;
|
|
383
|
+
|
|
356
384
|
const colStyle = (col: ColumnType<T>, key: string): CSSProperties => {
|
|
357
385
|
const measured = flexLayout?.cols[key];
|
|
358
386
|
if (measured != null) return { width: measured, maxWidth: measured, minWidth: 0 };
|
|
@@ -365,8 +393,8 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
365
393
|
|
|
366
394
|
<Spin spinning={Boolean(loading)}>
|
|
367
395
|
<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}>
|
|
368
|
-
<
|
|
369
|
-
<table data-slot="table" className={cn("caption-bottom border-
|
|
396
|
+
<TableBodyScroll y={scroll?.y} x={scroll?.x} scrollRef={scrollBodyRef} insetTop={thumbInset}>
|
|
397
|
+
<table data-slot="table" className={cn("caption-bottom text-base", headSticky ? "border-separate border-spacing-0" : "border-collapse", flexLayout ? "w-full table-fixed" : wantsFlex ? "w-max" : "w-full")}>
|
|
370
398
|
{flexLayout ? (
|
|
371
399
|
<colgroup>
|
|
372
400
|
{rowSelection ? <col style={{ width: flexLayout.selection }} /> : null}
|
|
@@ -377,10 +405,10 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
377
405
|
</colgroup>
|
|
378
406
|
) : null}
|
|
379
407
|
{showHeader ? (
|
|
380
|
-
<thead data-slot="table-header" className="[&_tr]:border-b [&_tr]:border-border">
|
|
408
|
+
<thead data-slot="table-header" className={cn("[&_tr]:border-b [&_tr]:border-border", headSticky && "relative z-20")}>
|
|
381
409
|
<tr data-slot="table-row" className="border-b border-border transition-colors hover:bg-transparent">
|
|
382
410
|
{rowSelection ? (
|
|
383
|
-
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground
|
|
411
|
+
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground", headSticky ? "sticky top-0 z-10 border-b border-border" : "bg-foreground/4", bordered && "border-b border-border")} style={{ ...(selectionColWidth != null ? { width: selectionColWidth } : undefined), ...headBg }}>
|
|
384
412
|
<div className="flex items-center justify-center">
|
|
385
413
|
{rowSelection.type === "radio" ? null : (
|
|
386
414
|
<Checkbox
|
|
@@ -405,9 +433,10 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
405
433
|
<th
|
|
406
434
|
key={key}
|
|
407
435
|
data-slot="table-head"
|
|
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),
|
|
436
|
+
className={cn(sz.head, "align-middle font-medium text-foreground", headSticky ? "sticky top-0 z-10 border-b border-border" : "bg-foreground/4", flexLayout && flexWeight(col) > 0 ? "min-w-0" : "whitespace-nowrap", alignClass(col.align), bordered && "border-b border-border", sortable && "cursor-pointer select-none", col.className, headerExtra?.className)}
|
|
409
437
|
style={{
|
|
410
438
|
...colStyle(col, key),
|
|
439
|
+
...headBg,
|
|
411
440
|
...headerExtra?.style,
|
|
412
441
|
}}
|
|
413
442
|
onClick={sortable ? () => toggleSort(col, i) : undefined}
|
|
@@ -423,7 +452,7 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
423
452
|
</thead>
|
|
424
453
|
) : null}
|
|
425
454
|
|
|
426
|
-
<tbody data-slot="table-body" className="[&_tr:last-child]:border-0">
|
|
455
|
+
<tbody data-slot="table-body" className={cn("[&_tr:last-child]:border-0", headSticky && "relative z-0")}>
|
|
427
456
|
{pageData.length === 0 ? (
|
|
428
457
|
<tr data-slot="table-row" className="border-b border-border">
|
|
429
458
|
<td data-slot="table-cell" colSpan={colCount} className={cn(sz.cell, "text-center align-middle")}>
|
|
@@ -461,7 +490,7 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
461
490
|
}
|
|
462
491
|
>
|
|
463
492
|
{rowSelection ? (
|
|
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()}>
|
|
493
|
+
<td data-row-select data-slot="table-cell" className={cn(sz.cell, sz.check, "align-middle", headSticky && "border-b border-border")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined} onClick={(e) => e.stopPropagation()}>
|
|
465
494
|
<div className="flex items-center justify-center">
|
|
466
495
|
{rowSelection.type === "radio" ? (
|
|
467
496
|
<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])} />
|
|
@@ -486,7 +515,7 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
486
515
|
<td
|
|
487
516
|
key={getColumnKey(col, i)}
|
|
488
517
|
data-slot="table-cell"
|
|
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)}
|
|
518
|
+
className={cn(sz.cell, "align-middle", headSticky && "border-b border-border", 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
519
|
style={colStyle(col, getColumnKey(col, i))}
|
|
491
520
|
title={col.ellipsis && (typeof content === "string" || typeof content === "number") ? String(content) : undefined}
|
|
492
521
|
>
|
|
@@ -510,7 +539,7 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
510
539
|
</tfoot>
|
|
511
540
|
) : null}
|
|
512
541
|
</table>
|
|
513
|
-
</
|
|
542
|
+
</TableBodyScroll>
|
|
514
543
|
</div>
|
|
515
544
|
</Spin>
|
|
516
545
|
|