@protolabsai/ui 0.17.0 → 0.18.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/package.json +1 -1
- package/src/AppShell.full.stories.tsx +18 -3
- package/src/app-shell.tsx +66 -21
- package/src/styles/app-shell.css +7 -0
package/package.json
CHANGED
|
@@ -46,6 +46,7 @@ export const Full: Story = {
|
|
|
46
46
|
const [activeRight, setActiveRight] = useState("inbox");
|
|
47
47
|
const [rightWidth, setRightWidth] = useState(360);
|
|
48
48
|
const [collapsed, setCollapsed] = useState(false);
|
|
49
|
+
const [leftCollapsed, setLeftCollapsed] = useState(false);
|
|
49
50
|
const leftItems = order.left.map((id) => BY_ID.get(id)!);
|
|
50
51
|
const rightItems = order.right.map((id) => BY_ID.get(id)!);
|
|
51
52
|
const mobile: MobileItem[] = [...LEFT, ...RIGHT].map((i) => ({ id: i.id, label: i.label, icon: i.icon }));
|
|
@@ -58,8 +59,10 @@ export const Full: Story = {
|
|
|
58
59
|
activeLeft={activeLeft}
|
|
59
60
|
activeRight={activeRight}
|
|
60
61
|
onSelect={(side, id) => {
|
|
61
|
-
if (side === "left")
|
|
62
|
-
|
|
62
|
+
if (side === "left") {
|
|
63
|
+
setActiveLeft(id);
|
|
64
|
+
setLeftCollapsed(false);
|
|
65
|
+
} else {
|
|
63
66
|
setActiveRight(id);
|
|
64
67
|
setCollapsed(false);
|
|
65
68
|
}
|
|
@@ -68,9 +71,21 @@ export const Full: Story = {
|
|
|
68
71
|
onRightWidthChange={setRightWidth}
|
|
69
72
|
rightCollapsed={collapsed}
|
|
70
73
|
onCollapse={setCollapsed}
|
|
74
|
+
leftCollapsed={leftCollapsed}
|
|
75
|
+
onLeftCollapse={setLeftCollapsed}
|
|
71
76
|
leftContent={
|
|
72
77
|
<>
|
|
73
|
-
<PanelHeader
|
|
78
|
+
<PanelHeader
|
|
79
|
+
title={activeLeft}
|
|
80
|
+
actions={
|
|
81
|
+
<>
|
|
82
|
+
<Button size="sm">Action</Button>
|
|
83
|
+
<Button size="sm" variant="ghost" onClick={() => setLeftCollapsed(true)}>
|
|
84
|
+
Close
|
|
85
|
+
</Button>
|
|
86
|
+
</>
|
|
87
|
+
}
|
|
88
|
+
/>
|
|
74
89
|
{surfaceBody(activeLeft)}
|
|
75
90
|
</>
|
|
76
91
|
}
|
package/src/app-shell.tsx
CHANGED
|
@@ -274,6 +274,14 @@ export type AppShellProps = {
|
|
|
274
274
|
onCollapse?: (collapsed: boolean) => void;
|
|
275
275
|
minRightWidth?: number;
|
|
276
276
|
maxRightWidth?: number;
|
|
277
|
+
/** Controlled left-column collapse — drives the left close button and the
|
|
278
|
+
* overdrag-left snap. Mirrors `rightCollapsed`/`onCollapse`. */
|
|
279
|
+
leftCollapsed?: boolean;
|
|
280
|
+
onLeftCollapse?: (collapsed: boolean) => void;
|
|
281
|
+
/** Min left-column width (px). The single divider holds the left column here
|
|
282
|
+
* on the way in; dragging `OVERDRAG` px further snaps the left column closed
|
|
283
|
+
* (it stays `leftCollapsed` until the host restores it). */
|
|
284
|
+
minLeftWidth?: number;
|
|
277
285
|
/** Mobile (<breakpoint) config. Omit to disable the mobile shell. */
|
|
278
286
|
mobileItems?: MobileItem[];
|
|
279
287
|
mobileActiveId?: string;
|
|
@@ -309,6 +317,9 @@ export function AppShell({
|
|
|
309
317
|
onCollapse,
|
|
310
318
|
minRightWidth = 280,
|
|
311
319
|
maxRightWidth = 720,
|
|
320
|
+
leftCollapsed = false,
|
|
321
|
+
onLeftCollapse,
|
|
322
|
+
minLeftWidth = 280,
|
|
312
323
|
mobileItems,
|
|
313
324
|
mobileActiveId,
|
|
314
325
|
onMobileSelect,
|
|
@@ -321,25 +332,54 @@ export function AppShell({
|
|
|
321
332
|
const isMobile = useIsMobile(mobileBreakpoint);
|
|
322
333
|
|
|
323
334
|
// ── resize handle ──
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
335
|
+
// One divider, zero-sum: the right column is width-controlled and the left
|
|
336
|
+
// flexes to fill, so dragging grows one column as it shrinks the other. Each
|
|
337
|
+
// side is held at its min on the way in; OVERDRAG px past that min snaps that
|
|
338
|
+
// side closed (collapse is controlled — the host restores it).
|
|
339
|
+
const OVERDRAG = 64;
|
|
340
|
+
const leftColRef = useRef<HTMLElement | null>(null);
|
|
341
|
+
const drag = useRef<{ startX: number; startW: number; startLeftW: number } | null>(null);
|
|
342
|
+
/** Resize `rightWidth` to `raw` px, holding both columns at their minimums. */
|
|
343
|
+
const commit = useCallback(
|
|
344
|
+
(raw: number, startLeftW: number, startW: number) => {
|
|
345
|
+
const upper = Math.max(minRightWidth, Math.min(maxRightWidth, startLeftW + startW - minLeftWidth));
|
|
346
|
+
onRightWidthChange(Math.min(upper, Math.max(minRightWidth, raw)));
|
|
347
|
+
},
|
|
348
|
+
[minRightWidth, maxRightWidth, minLeftWidth, onRightWidthChange],
|
|
328
349
|
);
|
|
329
350
|
const onPointerDown = useCallback(
|
|
330
351
|
(e: ReactPointerEvent<HTMLDivElement>) => {
|
|
331
352
|
e.preventDefault();
|
|
332
|
-
drag.current = { startX: e.clientX, startW: rightWidth };
|
|
353
|
+
drag.current = { startX: e.clientX, startW: rightWidth, startLeftW: leftColRef.current?.clientWidth ?? 0 };
|
|
333
354
|
e.currentTarget.setPointerCapture(e.pointerId);
|
|
334
355
|
},
|
|
335
356
|
[rightWidth],
|
|
336
357
|
);
|
|
337
358
|
const onPointerMove = useCallback(
|
|
338
359
|
(e: ReactPointerEvent<HTMLDivElement>) => {
|
|
339
|
-
|
|
340
|
-
|
|
360
|
+
const d = drag.current;
|
|
361
|
+
if (!d) return;
|
|
362
|
+
const raw = d.startW + (d.startX - e.clientX);
|
|
363
|
+
const predictedLeft = d.startLeftW - (raw - d.startW);
|
|
364
|
+
const endDrag = () => {
|
|
365
|
+
drag.current = null;
|
|
366
|
+
e.currentTarget.releasePointerCapture?.(e.pointerId);
|
|
367
|
+
};
|
|
368
|
+
// Overdrag toward the right rail → snap the right column closed.
|
|
369
|
+
if (onCollapse && raw < minRightWidth - OVERDRAG) {
|
|
370
|
+
endDrag();
|
|
371
|
+
onCollapse(true);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
// Overdrag toward the left rail → snap the left column closed.
|
|
375
|
+
if (onLeftCollapse && predictedLeft < minLeftWidth - OVERDRAG) {
|
|
376
|
+
endDrag();
|
|
377
|
+
onLeftCollapse(true);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
commit(raw, d.startLeftW, d.startW);
|
|
341
381
|
},
|
|
342
|
-
[
|
|
382
|
+
[minRightWidth, minLeftWidth, onCollapse, onLeftCollapse, commit],
|
|
343
383
|
);
|
|
344
384
|
const onPointerUp = useCallback((e: ReactPointerEvent<HTMLDivElement>) => {
|
|
345
385
|
drag.current = null;
|
|
@@ -347,16 +387,14 @@ export function AppShell({
|
|
|
347
387
|
}, []);
|
|
348
388
|
const onKeyDown = useCallback(
|
|
349
389
|
(e: ReactKeyboardEvent<HTMLDivElement>) => {
|
|
390
|
+
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
|
|
391
|
+
e.preventDefault();
|
|
350
392
|
const step = e.shiftKey ? 48 : 16;
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
} else if (e.key === "ArrowRight") {
|
|
355
|
-
e.preventDefault();
|
|
356
|
-
onRightWidthChange(clamp(rightWidth - step));
|
|
357
|
-
}
|
|
393
|
+
const startLeftW = leftColRef.current?.clientWidth ?? 0;
|
|
394
|
+
// ArrowLeft grows the right column (shrinks the left); ArrowRight reverses.
|
|
395
|
+
commit(rightWidth + (e.key === "ArrowLeft" ? step : -step), startLeftW, rightWidth);
|
|
358
396
|
},
|
|
359
|
-
[rightWidth,
|
|
397
|
+
[rightWidth, commit],
|
|
360
398
|
);
|
|
361
399
|
|
|
362
400
|
// ── rail drag-and-drop (only when onRailReorder is provided) ──
|
|
@@ -450,6 +488,8 @@ export function AppShell({
|
|
|
450
488
|
}
|
|
451
489
|
|
|
452
490
|
const showRight = !rightCollapsed && rightItems.length > 0;
|
|
491
|
+
const showLeft = !leftCollapsed;
|
|
492
|
+
const showHandle = showLeft && showRight;
|
|
453
493
|
const ctxLeft = onRailContextMenu ? (e: ReactMouseEvent, id: string) => onRailContextMenu("left", e, id) : undefined;
|
|
454
494
|
const ctxRight = onRailContextMenu ? (e: ReactMouseEvent, id: string) => onRailContextMenu("right", e, id) : undefined;
|
|
455
495
|
|
|
@@ -458,16 +498,21 @@ export function AppShell({
|
|
|
458
498
|
{header != null && <div className="pl-appshell__header">{header}</div>}
|
|
459
499
|
<div className="pl-appshell">
|
|
460
500
|
{leftRail}
|
|
461
|
-
|
|
462
|
-
|
|
501
|
+
{showLeft && (
|
|
502
|
+
<main ref={leftColRef} className="pl-appshell__col pl-appshell__col--left">
|
|
503
|
+
{leftContent}
|
|
504
|
+
</main>
|
|
505
|
+
)}
|
|
506
|
+
{showHandle && (
|
|
463
507
|
<div
|
|
464
508
|
className="pl-appshell__handle"
|
|
465
509
|
role="separator"
|
|
466
510
|
aria-orientation="vertical"
|
|
467
|
-
aria-label="Resize
|
|
511
|
+
aria-label="Resize panels"
|
|
468
512
|
aria-valuenow={rightWidth}
|
|
469
513
|
aria-valuemin={minRightWidth}
|
|
470
514
|
aria-valuemax={maxRightWidth}
|
|
515
|
+
aria-valuetext={`Right panel ${rightWidth}px`}
|
|
471
516
|
tabIndex={0}
|
|
472
517
|
onPointerDown={onPointerDown}
|
|
473
518
|
onPointerMove={onPointerMove}
|
|
@@ -478,8 +523,8 @@ export function AppShell({
|
|
|
478
523
|
)}
|
|
479
524
|
{showRight && (
|
|
480
525
|
<aside
|
|
481
|
-
className="pl-appshell__col pl-appshell__col--right"
|
|
482
|
-
style={{ flex: `0 0 ${rightWidth}px`, width: rightWidth }}
|
|
526
|
+
className={cx("pl-appshell__col pl-appshell__col--right", !showLeft && "pl-appshell__col--fill")}
|
|
527
|
+
style={showLeft ? { flex: `0 0 ${rightWidth}px`, width: rightWidth } : undefined}
|
|
483
528
|
>
|
|
484
529
|
{rightContent}
|
|
485
530
|
</aside>
|
package/src/styles/app-shell.css
CHANGED
|
@@ -222,6 +222,13 @@
|
|
|
222
222
|
border-left: var(--pl-border-width) solid var(--pl-color-border);
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
/* When the left column is collapsed, the surviving column fills the stage
|
|
226
|
+
(no fixed width, no divider) instead of sitting at its controlled width. */
|
|
227
|
+
.pl-appshell__col--fill {
|
|
228
|
+
flex: 1 1 auto;
|
|
229
|
+
border-left: none;
|
|
230
|
+
}
|
|
231
|
+
|
|
225
232
|
.pl-appshell__handle {
|
|
226
233
|
flex: 0 0 auto;
|
|
227
234
|
width: 5px;
|