@labpane/os 1.2.0 → 1.4.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/dist/index.js CHANGED
@@ -1,9 +1,12 @@
1
1
  // workspace.tsx
2
2
  import {
3
- useEffect as useEffect4,
3
+ memo as memo3,
4
+ useCallback as useCallback2,
5
+ useDeferredValue,
6
+ useEffect as useEffect5,
4
7
  useId as useId2,
5
8
  useLayoutEffect as useLayoutEffect8,
6
- useMemo as useMemo2,
9
+ useMemo as useMemo3,
7
10
  useRef as useRef9,
8
11
  useState as useState5
9
12
  } from "react";
@@ -60,10 +63,15 @@ function densitySize(density) {
60
63
  );
61
64
  }
62
65
  var WorkspaceDensity = createContext(100);
66
+ var MinimumTabHeight = createContext(0);
63
67
  var fitTabWidth = (width, available, count) => Math.min(width, Math.max(128, (available - 16) / Math.max(1, count)));
64
68
  function useTabSize() {
65
69
  const density = useContext(WorkspaceDensity);
66
- return useMemo(() => densitySize(density), [density]);
70
+ const minimumHeight = useContext(MinimumTabHeight);
71
+ return useMemo(() => {
72
+ const size = densitySize(density);
73
+ return { ...size, height: Math.max(size.height, minimumHeight) };
74
+ }, [density, minimumHeight]);
67
75
  }
68
76
  function useWorkspaceIds() {
69
77
  const prefix = useContext(WorkspaceId);
@@ -75,20 +83,25 @@ function useWorkspaceIds() {
75
83
  var TabLinks = createContext(/* @__PURE__ */ new Map());
76
84
  function useTabLinks() {
77
85
  const links = useContext(TabLinks);
78
- return {
79
- register: (key2) => (element) => {
80
- if (element) links.set(key2, element);
81
- else links.delete(key2);
82
- },
83
- get: (key2) => links.get(key2),
84
- focus: (key2) => requestAnimationFrame(() => links.get(key2)?.focus())
85
- };
86
+ return useMemo(
87
+ () => ({
88
+ register: (key2) => (element) => {
89
+ if (element) links.set(key2, element);
90
+ else links.delete(key2);
91
+ },
92
+ get: (key2) => links.get(key2),
93
+ focus: (key2) => requestAnimationFrame(() => links.get(key2)?.focus())
94
+ }),
95
+ [links]
96
+ );
86
97
  }
87
98
 
88
99
  // surface.tsx
89
100
  import {
90
101
  Fragment,
102
+ memo,
91
103
  useLayoutEffect,
104
+ useMemo as useMemo2,
92
105
  useRef,
93
106
  useState
94
107
  } from "react";
@@ -268,6 +281,31 @@ function hasOpenOverlay() {
268
281
  return !!full && full !== document.documentElement || !!document.querySelector(overlays);
269
282
  }
270
283
 
284
+ // frame-task.ts
285
+ function frameTask(run) {
286
+ let frame2;
287
+ let pending2;
288
+ const flush2 = () => {
289
+ if (frame2 !== void 0) cancelAnimationFrame(frame2);
290
+ frame2 = void 0;
291
+ const next = pending2;
292
+ pending2 = void 0;
293
+ if (next) run(next.value);
294
+ };
295
+ return {
296
+ schedule(value) {
297
+ pending2 = { value };
298
+ if (frame2 === void 0) frame2 = requestAnimationFrame(flush2);
299
+ },
300
+ flush: flush2,
301
+ cancel() {
302
+ if (frame2 !== void 0) cancelAnimationFrame(frame2);
303
+ frame2 = void 0;
304
+ pending2 = void 0;
305
+ }
306
+ };
307
+ }
308
+
271
309
  // surface.tsx
272
310
  import { jsx, jsxs } from "react/jsx-runtime";
273
311
  var position = (box) => ({
@@ -276,12 +314,33 @@ var position = (box) => ({
276
314
  width: box.width,
277
315
  height: box.height
278
316
  });
317
+ var PaneHeader = memo(function PaneHeader2({
318
+ pane,
319
+ first,
320
+ last,
321
+ docking,
322
+ insertion,
323
+ board,
324
+ render
325
+ }) {
326
+ const ids = useWorkspaceIds();
327
+ return render(
328
+ pane,
329
+ first,
330
+ last,
331
+ { ...docking, insertion },
332
+ (key2 = pane.active) => board.current?.querySelector(
333
+ key2 ? `#${CSS.escape(ids.panel(key2))}` : `[data-pane-sheet="${CSS.escape(pane.id)}"]:not([hidden]):not([data-drag-content])`
334
+ ) ?? null
335
+ );
336
+ });
279
337
  function WorkspaceSurface({
280
338
  layout,
281
339
  change,
282
340
  header,
283
341
  content,
284
- menu
342
+ menu,
343
+ titlebarArea
285
344
  }) {
286
345
  const ids = useWorkspaceIds();
287
346
  const {
@@ -296,6 +355,15 @@ function WorkspaceSurface({
296
355
  const [preview, setPreview] = useState();
297
356
  const previewRef = useRef(void 0);
298
357
  const [resizing, setResizing] = useState(false);
358
+ const [resizeDraft, setResizeDraft] = useState();
359
+ const resizeDraftRef = useRef(void 0);
360
+ const displayedLayout = resizeDraft?.base === layout ? resizeDraft.layout : layout;
361
+ const cancelResize = () => {
362
+ resizePointer.cancel();
363
+ resizeDraftRef.current = void 0;
364
+ setResizeDraft(void 0);
365
+ setResizing(false);
366
+ };
299
367
  const geometry = [
300
368
  size.width,
301
369
  size.height,
@@ -309,19 +377,29 @@ function WorkspaceSurface({
309
377
  previousGeometry.current = geometry;
310
378
  }, [geometry]);
311
379
  useLayoutEffect(() => {
312
- const observer = new ResizeObserver(([entry]) => {
380
+ const observer2 = new ResizeObserver(([entry]) => {
313
381
  const { width, height } = entry.contentRect;
314
382
  setSize({ width, height });
315
383
  });
316
- observer.observe(board.current);
317
- return () => observer.disconnect();
384
+ observer2.observe(board.current);
385
+ return () => observer2.disconnect();
318
386
  }, []);
319
387
  const { leaves, dividers } = layoutBoxes(
320
- layout,
388
+ displayedLayout,
321
389
  size.width,
322
390
  size.height,
323
391
  gutter2
324
- ), required = minimumSize(layout, gutter2);
392
+ ), required = minimumSize(displayedLayout, gutter2);
393
+ const [resizePointer] = useState(
394
+ () => frameTask((resize) => resize())
395
+ );
396
+ useLayoutEffect(() => {
397
+ addEventListener("blur", cancelResize);
398
+ return () => {
399
+ resizePointer.cancel();
400
+ removeEventListener("blur", cancelResize);
401
+ };
402
+ }, [resizePointer]);
325
403
  const show = (next) => {
326
404
  if (previewRef.current?.pane === next?.pane && previewRef.current?.edge === next?.edge && previewRef.current?.index === next?.index && previewRef.current?.key === next?.key)
327
405
  return;
@@ -412,14 +490,36 @@ function WorkspaceSurface({
412
490
  }
413
491
  };
414
492
  const lifted = preview?.key;
493
+ const operations = useRef(docking);
494
+ useLayoutEffect(() => {
495
+ operations.current = docking;
496
+ });
497
+ const paneCount = leaves.length;
498
+ const stableDocking = useMemo2(
499
+ () => ({
500
+ paneCount,
501
+ move: (drag) => operations.current.move(drag),
502
+ drop: () => operations.current.drop(),
503
+ cancel: () => operations.current.cancel(),
504
+ split: (key2, edge) => operations.current.split(key2, edge),
505
+ next: (key2) => operations.current.next(key2)
506
+ }),
507
+ [paneCount]
508
+ );
509
+ const sourcePreview = useMemo2(() => {
510
+ if (!lifted) return;
511
+ const pane = panes(displayedLayout).find(
512
+ (pane2) => pane2.tabs.includes(lifted)
513
+ );
514
+ if (!pane) return;
515
+ const remaining = pane.tabs.filter((key2) => key2 !== lifted);
516
+ return {
517
+ ...pane,
518
+ active: remaining[Math.min(pane.tabs.indexOf(lifted), remaining.length - 1)]
519
+ };
520
+ }, [displayedLayout, lifted]);
415
521
  const regions = leaves.map(({ pane, ...box }) => {
416
- if (lifted && pane.tabs.includes(lifted)) {
417
- const remaining = pane.tabs.filter((key2) => key2 !== lifted);
418
- pane = {
419
- ...pane,
420
- active: remaining[Math.min(pane.tabs.indexOf(lifted), remaining.length - 1)]
421
- };
422
- }
522
+ if (sourcePreview?.id === pane.id) pane = sourcePreview;
423
523
  if (preview?.pane !== pane.id || !preview.edge) return { pane, box };
424
524
  const drop = dropRegion(box, preview.edge, gutter2);
425
525
  if (preview.edge === "left")
@@ -447,41 +547,59 @@ function WorkspaceSurface({
447
547
  minHeight: required.y
448
548
  },
449
549
  children: [
450
- regions.map(({ pane, box }, index) => /* @__PURE__ */ jsx(
451
- "div",
452
- {
453
- "data-pane-header": pane.id,
454
- "data-top": box.y === 0 || void 0,
455
- className: "lab-pane-header",
456
- style: {
457
- ...position({ ...box, height: tabHeight }),
458
- transitionDuration: transition ? "var(--workspace-duration, 180ms)" : "0ms"
459
- },
460
- children: header(
461
- pane,
462
- index === 0,
463
- pane.id === topRight,
464
- {
465
- ...docking,
466
- insertion: preview?.pane === pane.id ? preview.index : void 0
550
+ regions.map(({ pane, box }, index) => {
551
+ const overlay = titlebarArea && box.y < titlebarArea.height;
552
+ const left = overlay ? Math.max(box.x, titlebarArea.left) : box.x;
553
+ const right = overlay ? Math.min(box.x + box.width, titlebarArea.right) : box.x + box.width;
554
+ return /* @__PURE__ */ jsx(
555
+ "div",
556
+ {
557
+ "data-pane-header": pane.id,
558
+ "data-top": box.y === 0 || void 0,
559
+ className: "lab-pane-header",
560
+ "data-titlebar-overlay": overlay ? "" : void 0,
561
+ style: {
562
+ ...position({
563
+ ...box,
564
+ x: left,
565
+ width: Math.max(0, right - left),
566
+ height: tabHeight
567
+ }),
568
+ transitionDuration: transition && !overlay ? "var(--workspace-duration, 180ms)" : "0ms"
467
569
  },
468
- (key2 = pane.active) => board.current?.querySelector(
469
- key2 ? `[id="${CSS.escape(ids.panel(key2))}"]` : `[data-pane-sheet="${CSS.escape(pane.id)}"]:not([hidden]):not([data-drag-content])`
470
- ) ?? null
471
- )
472
- },
473
- pane.id
474
- )),
570
+ children: /* @__PURE__ */ jsx(
571
+ PaneHeader,
572
+ {
573
+ pane,
574
+ first: index === 0,
575
+ last: pane.id === topRight,
576
+ docking: stableDocking,
577
+ insertion: preview?.pane === pane.id ? preview.index : void 0,
578
+ board,
579
+ render: header
580
+ }
581
+ )
582
+ },
583
+ pane.id
584
+ );
585
+ }),
475
586
  content(regions, transition, lifted),
476
587
  dividers.map(({ split, bounds, ratio, low, high, ...box }) => {
477
588
  const axis = split.axis, horizontal = axis === "x", dimension = horizontal ? "width" : "height";
478
589
  const primary = panes(split.first).filter((p) => p.active);
479
- const resize = (value) => change(
480
- updateLayout(layout, split.id, (node) => ({
590
+ const resize = (value, preview2 = false) => {
591
+ const next = updateLayout(displayedLayout, split.id, (node) => ({
481
592
  ...node,
482
593
  ratio: Math.max(low, Math.min(high, value))
483
- }))
484
- );
594
+ }));
595
+ if (preview2) {
596
+ resizeDraftRef.current = { base: layout, layout: next };
597
+ setResizeDraft(resizeDraftRef.current);
598
+ } else {
599
+ cancelResize();
600
+ change(next);
601
+ }
602
+ };
485
603
  const target = /* @__PURE__ */ jsx(
486
604
  "div",
487
605
  {
@@ -512,13 +630,24 @@ function WorkspaceSurface({
512
630
  onPointerMove: (event) => {
513
631
  if (!event.currentTarget.hasPointerCapture(event.pointerId))
514
632
  return;
515
- const b = board.current.getBoundingClientRect();
516
- const coordinate = horizontal ? event.clientX - b.left : event.clientY - b.top;
517
- resize(
518
- (coordinate - bounds[axis]) / (bounds[dimension] - gutter2[axis])
519
- );
633
+ const { clientX, clientY } = event;
634
+ resizePointer.schedule(() => {
635
+ const b = board.current.getBoundingClientRect();
636
+ const coordinate = horizontal ? clientX - b.left : clientY - b.top;
637
+ resize(
638
+ (coordinate - bounds[axis]) / (bounds[dimension] - gutter2[axis]),
639
+ true
640
+ );
641
+ });
520
642
  },
521
- onLostPointerCapture: () => setResizing(false),
643
+ onPointerUp: () => {
644
+ resizePointer.flush();
645
+ const draft = resizeDraftRef.current;
646
+ if (draft?.base === layout) change(draft.layout);
647
+ cancelResize();
648
+ },
649
+ onPointerCancel: cancelResize,
650
+ onLostPointerCapture: cancelResize,
522
651
  onKeyDown: (event) => {
523
652
  if (hasOpenOverlay()) return;
524
653
  const step = event.shiftKey ? 0.1 : 0.02;
@@ -588,6 +717,7 @@ var ChevronRightIcon = icon(4 / 3, "m6 4 4 4-4 4");
588
717
  var XIcon = icon(7 / 6, "m12 4-8 8", "m4 4 8 8");
589
718
 
590
719
  // frame.tsx
720
+ import { memo as memo2 } from "react";
591
721
  import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
592
722
  var reach = 16;
593
723
  var shoulder = [
@@ -650,7 +780,7 @@ function paneShape(leftGap, rightGap, bottom, morph = true, size = densityPreset
650
780
  const leftCorner = morph && leftGap < reach ? "" : ` A15.5 15.5 0 0 1 ${left + 15.5} ${baseline} H0`;
651
781
  return `${tabShape(morph ? leftGap : reach, morph ? rightGap : reach, size)[1]}${rightCorner} V${bottom - 15.5} A15.5 15.5 0 0 1 ${right - 15.5} ${bottom} H${left + 15.5} A15.5 15.5 0 0 1 ${left} ${bottom - 15.5} V${cornerBottom}${leftCorner} Z`;
652
782
  }
653
- function TabSurface({
783
+ var TabSurface = memo2(function TabSurface2({
654
784
  selected,
655
785
  keyboardFocus = false,
656
786
  width
@@ -686,94 +816,204 @@ function TabSurface({
686
816
  ]
687
817
  }
688
818
  );
689
- }
819
+ });
690
820
 
691
821
  // drag-content.ts
692
822
  import { useEffect, useRef as useRef2 } from "react";
823
+
824
+ // geometry-task.ts
825
+ var pending = /* @__PURE__ */ new Set();
826
+ var observed = /* @__PURE__ */ new Map();
827
+ var animations = /* @__PURE__ */ new Map();
828
+ var observer;
829
+ var frame;
830
+ var microtask = false;
831
+ function flush() {
832
+ if (frame !== void 0) cancelAnimationFrame(frame);
833
+ frame = void 0;
834
+ for (const [element, animation] of animations) {
835
+ for (const task of observed.get(element) ?? []) pending.add(task);
836
+ if (!element.isConnected || animation.playState !== "running")
837
+ animations.delete(element);
838
+ }
839
+ const reads = [...pending];
840
+ pending.clear();
841
+ const writes = reads.map((task) => {
842
+ const resized = task.resized;
843
+ task.resized = false;
844
+ return task.read(resized);
845
+ });
846
+ for (const write of writes) write?.();
847
+ if (animations.size && frame === void 0)
848
+ frame = requestAnimationFrame(flush);
849
+ }
850
+ function followGeometry(element, animation) {
851
+ animations.set(element, animation);
852
+ if (frame === void 0) frame = requestAnimationFrame(flush);
853
+ }
854
+ function geometryTask(read2) {
855
+ const task = { read: read2, resized: false };
856
+ return {
857
+ schedule() {
858
+ pending.add(task);
859
+ if (frame === void 0) frame = requestAnimationFrame(flush);
860
+ },
861
+ // Wait for layout effects, but measure before paint.
862
+ commit(resized = false) {
863
+ task.resized ||= resized;
864
+ pending.add(task);
865
+ if (microtask) return;
866
+ microtask = true;
867
+ queueMicrotask(() => {
868
+ microtask = false;
869
+ flush();
870
+ });
871
+ },
872
+ flush() {
873
+ pending.add(task);
874
+ flush();
875
+ },
876
+ cancel() {
877
+ pending.delete(task);
878
+ task.resized = false;
879
+ if (!pending.size && !animations.size && frame !== void 0) {
880
+ cancelAnimationFrame(frame);
881
+ frame = void 0;
882
+ }
883
+ },
884
+ observe(elements) {
885
+ observer ??= new ResizeObserver((entries) => {
886
+ for (const { target } of entries)
887
+ for (const task2 of observed.get(target) ?? []) {
888
+ task2.resized = true;
889
+ pending.add(task2);
890
+ }
891
+ flush();
892
+ });
893
+ for (const element of elements) {
894
+ let callbacks = observed.get(element);
895
+ if (!callbacks) {
896
+ observed.set(element, callbacks = /* @__PURE__ */ new Set());
897
+ observer.observe(element);
898
+ }
899
+ callbacks.add(task);
900
+ }
901
+ return () => {
902
+ for (const element of elements) {
903
+ const callbacks = observed.get(element);
904
+ callbacks?.delete(task);
905
+ if (!callbacks?.size) {
906
+ observed.delete(element);
907
+ observer?.unobserve(element);
908
+ }
909
+ }
910
+ if (!observed.size) {
911
+ observer?.disconnect();
912
+ observer = void 0;
913
+ }
914
+ };
915
+ }
916
+ };
917
+ }
918
+
919
+ // drag-content.ts
693
920
  function useDragContent() {
694
921
  const generation = useRef2(0);
695
922
  const current = useRef2(void 0);
696
- const release = (animate = true) => {
923
+ const prepareRelease = (animate = true) => {
697
924
  const preview = current.current;
698
925
  if (!preview) return;
699
- current.current = void 0;
700
- const version = ++generation.current;
701
926
  const { element, inert } = preview;
702
927
  const from = element.getBoundingClientRect();
703
- element.removeAttribute("data-drag-content");
704
- element.inert = inert;
705
- element.style.transition = "none";
706
- for (const name of ["x", "y", "width", "height"])
707
- element.style.removeProperty(`--lab-drag-${name}`);
708
- requestAnimationFrame(() => {
709
- if (version !== generation.current) return;
710
- const to = element.getBoundingClientRect();
711
- element.style.removeProperty("transition");
712
- if (!animate || !element.isConnected || !to.width || matchMedia("(prefers-reduced-motion: reduce)").matches)
713
- return;
714
- const resized = Math.abs(from.width - to.width) > 0.5 || Math.abs(from.height - to.height) > 0.5;
715
- element.animate(
716
- [
717
- {
718
- transform: `translate(${from.left - to.left}px, ${from.top - to.top}px)`,
719
- width: `${from.width}px`,
720
- height: `${from.height}px`,
721
- filter: "blur(0)",
722
- opacity: 1
723
- },
724
- {
725
- filter: resized ? "blur(3px)" : "blur(0)",
726
- opacity: resized ? 0.85 : 1,
727
- offset: 0.35
728
- },
729
- {
730
- transform: "none",
731
- width: `${to.width}px`,
732
- height: `${to.height}px`,
733
- filter: "blur(0)",
734
- opacity: 1
735
- }
736
- ],
737
- { duration: 220, easing: "cubic-bezier(.2,.8,.2,1)" }
738
- );
739
- });
928
+ return () => {
929
+ current.current = void 0;
930
+ const version = ++generation.current;
931
+ element.removeAttribute("data-drag-content");
932
+ element.inert = inert;
933
+ element.style.transition = "none";
934
+ element.style.removeProperty("transform");
935
+ for (const name of ["width", "height"])
936
+ element.style.removeProperty(`--lab-drag-${name}`);
937
+ requestAnimationFrame(() => {
938
+ if (version !== generation.current) return;
939
+ const to = element.getBoundingClientRect();
940
+ element.style.removeProperty("transition");
941
+ if (!animate || !element.isConnected || !to.width || matchMedia("(prefers-reduced-motion: reduce)").matches)
942
+ return;
943
+ const resized = Math.abs(from.width - to.width) > 0.5 || Math.abs(from.height - to.height) > 0.5;
944
+ const animation = element.animate(
945
+ [
946
+ {
947
+ transform: `translate(${from.left - to.left}px, ${from.top - to.top}px)`,
948
+ width: `${from.width}px`,
949
+ height: `${from.height}px`,
950
+ filter: "blur(0)",
951
+ opacity: 1
952
+ },
953
+ {
954
+ filter: resized ? "blur(3px)" : "blur(0)",
955
+ opacity: resized ? 0.85 : 1,
956
+ offset: 0.35
957
+ },
958
+ {
959
+ transform: "none",
960
+ width: `${to.width}px`,
961
+ height: `${to.height}px`,
962
+ filter: "blur(0)",
963
+ opacity: 1
964
+ }
965
+ ],
966
+ { duration: 220, easing: "cubic-bezier(.2,.8,.2,1)" }
967
+ );
968
+ followGeometry(element, animation);
969
+ });
970
+ };
740
971
  };
972
+ const release = (animate = true) => prepareRelease(animate)?.();
741
973
  useEffect(() => () => release(false), []);
742
974
  return {
743
975
  release,
744
- lift(element, tab, fallback) {
745
- if (current.current?.element !== element) {
746
- release(false);
747
- generation.current++;
748
- element.getAnimations().forEach((animation) => animation.cancel());
749
- const bounds = element.hidden ? fallback : element.getBoundingClientRect();
750
- current.current = {
751
- element,
752
- width: bounds.width,
753
- height: bounds.height,
754
- offset: Math.max(
755
- 8,
756
- Math.min(
757
- tab.getBoundingClientRect().left - bounds.left,
758
- bounds.width - tab.offsetWidth - 8
759
- )
760
- ),
761
- inert: element.inert
762
- };
763
- for (const [name, value] of Object.entries({
764
- width: `${bounds.width}px`,
765
- height: `${bounds.height}px`
766
- }))
767
- element.style.setProperty(`--lab-drag-${name}`, String(value));
768
- element.inert = true;
769
- element.setAttribute("data-drag-content", "");
770
- }
771
- return current.current;
976
+ prepareRelease,
977
+ prepareLift(element, tab, fallback) {
978
+ if (current.current?.element === element)
979
+ return { ...current.current, apply: () => {
980
+ } };
981
+ const releasePrevious = prepareRelease(false);
982
+ const animations2 = element.getAnimations();
983
+ const bounds = element.hidden ? fallback : element.getBoundingClientRect();
984
+ const preview = {
985
+ element,
986
+ width: bounds.width,
987
+ height: bounds.height,
988
+ offset: Math.max(
989
+ 8,
990
+ Math.min(
991
+ tab.getBoundingClientRect().left - bounds.left,
992
+ bounds.width - tab.offsetWidth - 8
993
+ )
994
+ ),
995
+ inert: element.inert
996
+ };
997
+ return {
998
+ ...preview,
999
+ apply: () => {
1000
+ releasePrevious?.();
1001
+ generation.current++;
1002
+ animations2.forEach((animation) => animation.cancel());
1003
+ current.current = preview;
1004
+ for (const [name, value] of Object.entries({
1005
+ width: `${bounds.width}px`,
1006
+ height: `${bounds.height}px`
1007
+ }))
1008
+ element.style.setProperty(`--lab-drag-${name}`, String(value));
1009
+ element.inert = true;
1010
+ element.setAttribute("data-drag-content", "");
1011
+ }
1012
+ };
772
1013
  },
773
1014
  move(x, y) {
774
1015
  const element = current.current?.element;
775
- element?.style.setProperty("--lab-drag-x", `${x}px`);
776
- element?.style.setProperty("--lab-drag-y", `${y}px`);
1016
+ element?.style.setProperty("transform", `translate3d(${x}px, ${y}px, 0)`);
777
1017
  }
778
1018
  };
779
1019
  }
@@ -781,8 +1021,11 @@ function useDragContent() {
781
1021
  // tabs.tsx
782
1022
  import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
783
1023
  var motion = { duration: 160, easing: "cubic-bezier(.2,.8,.2,1)" };
1024
+ var pathData = (path, value) => {
1025
+ if (path.getAttribute("d") !== value) path.setAttribute("d", value);
1026
+ };
784
1027
  function WorkspaceTabs({
785
- items,
1028
+ items: sourceItems,
786
1029
  active,
787
1030
  onClose,
788
1031
  onDuplicate,
@@ -793,25 +1036,25 @@ function WorkspaceTabs({
793
1036
  docking,
794
1037
  menu
795
1038
  }) {
1039
+ const [draftOrder, setDraftOrder] = React.useState();
1040
+ const itemOrder = new Map(draftOrder?.map((key2, index) => [key2, index]));
1041
+ const items = draftOrder ? sourceItems.slice().sort(
1042
+ (a, b) => (itemOrder.get(a.key) ?? Infinity) - (itemOrder.get(b.key) ?? Infinity)
1043
+ ) : sourceItems;
796
1044
  const ids = useWorkspaceIds();
797
1045
  const tabLinks = useTabLinks();
798
1046
  const content = useDragContent();
799
1047
  const density = useTabSize();
800
1048
  const [metrics, setMetrics] = React.useState({
801
- available: Infinity,
1049
+ width: density.width,
802
1050
  overflow: false,
803
1051
  left: false,
804
1052
  right: false
805
1053
  });
806
1054
  const measured = React.useRef(metrics);
807
- const { available } = metrics;
808
1055
  const size = {
809
1056
  ...density,
810
- width: fitTabWidth(
811
- density.width,
812
- available,
813
- items.length + (docking.insertion === void 0 ? 0 : 1)
814
- )
1057
+ width: metrics.width
815
1058
  }, { width: tabWidth, height, contentHeight } = size, width = tabWidth + 16;
816
1059
  const links = items.map(({ key: key2, label }) => [key2, label]);
817
1060
  const tabOrder = JSON.stringify(items.map(({ key: key2 }) => key2));
@@ -830,6 +1073,7 @@ function WorkspaceTabs({
830
1073
  if (element) element.inert = true;
831
1074
  }, []);
832
1075
  const frameRef = React.useRef(null);
1076
+ const frameLayer = dragBoundary?.closest(".lab-board");
833
1077
  const maskedSheet = React.useRef(null);
834
1078
  const maskedOutline = React.useRef(null);
835
1079
  const restoreFrame = () => {
@@ -839,97 +1083,125 @@ function WorkspaceTabs({
839
1083
  maskedOutline.current = null;
840
1084
  };
841
1085
  const settling = React.useRef(void 0);
1086
+ const animations2 = React.useRef(/* @__PURE__ */ new Map());
1087
+ const transitions = React.useRef(/* @__PURE__ */ new Map());
842
1088
  const positions = React.useRef({
843
1089
  order: "",
844
1090
  tabs: /* @__PURE__ */ new Map()
845
1091
  });
846
- const frame = React.useRef(0);
847
1092
  const suppressClick = React.useRef(false);
848
- const tabElement = (key2) => navRef.current?.querySelector(
849
- `a[data-tab="${CSS.escape(key2)}"]`
850
- )?.parentElement;
851
- const reveal = (key2 = active) => {
1093
+ const tabElement = (key2) => tabLinks.get(key2)?.parentElement;
1094
+ const readReveal = (key2 = active) => {
852
1095
  const nav = navRef.current, tab = tabElement(key2);
853
1096
  if (!nav || !tab || drag.current?.moved || docking.insertion !== void 0)
854
1097
  return;
855
- nav.scrollLeft = Math.max(
1098
+ const left = Math.max(
856
1099
  tab.offsetLeft + tab.offsetWidth + 8 - nav.clientWidth,
857
1100
  Math.min(nav.scrollLeft, tab.offsetLeft - 8)
858
1101
  );
1102
+ if (left === nav.scrollLeft) return;
1103
+ return {
1104
+ delta: left - nav.scrollLeft,
1105
+ apply: () => {
1106
+ nav.scrollLeft = left;
1107
+ }
1108
+ };
859
1109
  };
860
- const draw = () => {
861
- const current = drag.current, ghost2 = ghostRef.current, nav = navRef.current, bounds = dragBoundary?.getBoundingClientRect(), floating = !!ghost2 && !!current?.detached, tab = (floating ? null : ghost2) ?? tabElement(active), frame2 = frameRef.current;
862
- if (ghost2 && current?.moved && bounds) {
1110
+ const reveal = (key2 = active) => readReveal(key2)?.apply();
1111
+ const readDrawing = (scrollDelta = 0) => {
1112
+ const current = drag.current, ghost2 = ghostRef.current, nav = navRef.current, bounds = dragBoundary?.getBoundingClientRect(), floating = !!ghost2 && !!current?.detached, tab = (floating ? null : ghost2) ?? tabElement(active), frame2 = frameRef.current, sheet = surface(), sheetBox = sheet?.getBoundingClientRect(), navBox = nav?.getBoundingClientRect(), origin = frame2?.parentElement?.getBoundingClientRect();
1113
+ let box = tab && (tab !== ghost2 || !current?.moved) ? tab.getBoundingClientRect() : void 0;
1114
+ if (box && tab !== ghost2 && scrollDelta)
1115
+ box = new DOMRect(box.x - scrollDelta, box.y, box.width, box.height);
1116
+ const lift = ghost2 && current?.moved && floating ? (() => {
863
1117
  const sheet2 = surface(current.key);
864
- const preview = floating && sheet2 ? content.lift(sheet2, ghost2, current.sheet) : void 0;
865
- const left2 = floating ? 8 : bounds.left + 8, right2 = floating ? innerWidth - 8 : bounds.right - 8;
866
- const x2 = Math.max(
867
- left2,
868
- Math.min(current.x - current.offset, right2 - ghost2.offsetWidth)
869
- );
870
- const y = floating ? Math.max(
871
- 0,
872
- Math.min(current.y - current.offsetY, innerHeight - height - 8)
873
- ) : nav.getBoundingClientRect().top;
874
- ghost2.style.left = `${x2}px`;
875
- ghost2.style.top = `${y}px`;
876
- if (preview) {
877
- content.move(x2 - preview.offset, y + height - 1);
878
- const left3 = preview.offset - 8, right3 = preview.width - preview.offset - tabWidth - 8;
879
- ghost2.querySelector("svg > path").setAttribute("d", tabShape(left3, right3, size)[0]);
880
- const outline = ghost2.querySelector("[data-tab-outline]");
881
- outline.style.visibility = "visible";
882
- outline.setAttribute(
883
- "d",
884
- paneShape(left3, right3, height + preview.height - 1.5, true, size)
1118
+ return sheet2 ? content.prepareLift(sheet2, ghost2, current.sheet) : void 0;
1119
+ })() : void 0;
1120
+ const release = ghost2 && current?.moved && !lift ? content.prepareRelease() : void 0;
1121
+ return () => {
1122
+ if (ghost2 && current?.moved && bounds) {
1123
+ const preview = lift;
1124
+ preview?.apply();
1125
+ const left2 = floating ? 8 : bounds.left + 8, right2 = floating ? innerWidth - 8 : bounds.right - 8;
1126
+ const x2 = Math.max(
1127
+ left2,
1128
+ Math.min(current.x - current.offset, right2 - tabWidth)
885
1129
  );
886
- } else content.release();
887
- }
888
- restoreFrame();
889
- if (!tab || !bounds || !frame2) {
890
- if (frame2) frame2.style.visibility = "hidden";
891
- return;
892
- }
893
- const box = tab.getBoundingClientRect();
894
- const sheet = surface(), sheetBox = sheet?.getBoundingClientRect();
895
- const attached = Math.abs(box.top - bounds.top) < 0.5 && !!sheet;
896
- const left = attached ? box.left - 8 - sheetBox.left : 16, right = attached ? sheetBox.right - box.right - 8 : 16;
897
- if (ghost2 && !floating) {
898
- const [fill, outline] = tabShape(
899
- Math.max(0, left),
900
- Math.max(0, right),
901
- size
1130
+ const y = floating ? Math.max(
1131
+ 0,
1132
+ Math.min(current.y - current.offsetY, innerHeight - height - 8)
1133
+ ) : navBox.top;
1134
+ if (tab === ghost2) box = new DOMRect(x2, y, tabWidth, height);
1135
+ ghost2.style.translate = `${x2}px ${y}px`;
1136
+ if (preview) {
1137
+ content.move(x2 - preview.offset, y + height - 1);
1138
+ const left3 = preview.offset - 8, right3 = preview.width - preview.offset - tabWidth - 8;
1139
+ pathData(
1140
+ ghost2.querySelector("svg > path"),
1141
+ tabShape(left3, right3, size)[0]
1142
+ );
1143
+ const outline = ghost2.querySelector("[data-tab-outline]");
1144
+ outline.style.visibility = "visible";
1145
+ pathData(
1146
+ outline,
1147
+ paneShape(left3, right3, height + preview.height - 1.5, true, size)
1148
+ );
1149
+ } else release?.();
1150
+ }
1151
+ if (!tab || !box || !bounds || !frame2) {
1152
+ restoreFrame();
1153
+ if (frame2) frame2.style.visibility = "hidden";
1154
+ return;
1155
+ }
1156
+ const attached = Math.abs(box.top - bounds.top) < 0.5 && !!sheet;
1157
+ const left = attached ? box.left - 8 - sheetBox.left : 16, right = attached ? sheetBox.right - box.right - 8 : 16;
1158
+ if (ghost2 && !floating) {
1159
+ const [fill, outline] = tabShape(
1160
+ Math.max(0, left),
1161
+ Math.max(0, right),
1162
+ size
1163
+ );
1164
+ pathData(ghost2.querySelector("svg > path"), fill);
1165
+ pathData(ghost2.querySelector("[data-tab-outline]"), outline);
1166
+ }
1167
+ const stroke = tab.querySelector("[data-tab-outline]");
1168
+ if (maskedOutline.current !== stroke)
1169
+ maskedOutline.current?.style.removeProperty("visibility");
1170
+ stroke.style.visibility = attached ? "hidden" : "visible";
1171
+ maskedOutline.current = stroke;
1172
+ frame2.style.visibility = attached ? "visible" : "hidden";
1173
+ if (maskedSheet.current !== (attached ? sheet : null))
1174
+ maskedSheet.current?.style.removeProperty("border-color");
1175
+ maskedSheet.current = attached ? sheet : null;
1176
+ if (!attached) return;
1177
+ sheet.style.borderColor = "transparent";
1178
+ maskedSheet.current = sheet;
1179
+ const x = box.left - 8, bottom = sheetBox.bottom - box.top - 0.5;
1180
+ frame2.style.left = `${x - origin.left}px`;
1181
+ frame2.style.top = `${box.top - origin.top}px`;
1182
+ const viewport = ghost2 && !floating ? sheetBox : navBox, clipLeft = Math.max(-left, viewport.left - x), clipRight = Math.min(width + right, viewport.right - x);
1183
+ frame2.style.clipPath = `polygon(${clipLeft}px 0px, ${clipRight}px 0px, ${clipRight}px ${height - 1}px, ${width + right}px ${height - 1}px, ${width + right}px ${bottom + 1}px, ${-left}px ${bottom + 1}px, ${-left}px ${height - 1}px, ${clipLeft}px ${height - 1}px)`;
1184
+ pathData(
1185
+ frame2.querySelector("path"),
1186
+ paneShape(left, right, bottom, !!ghost2 && !floating, size) + ((!ghost2 || floating) && clipLeft > 0 ? ` M0 ${height - 0.5} H${Math.min(width, clipLeft)}` : "") + ((!ghost2 || floating) && clipRight < width ? ` M${Math.max(0, clipRight)} ${height - 0.5} H${width}` : "")
902
1187
  );
903
- ghost2.querySelector("svg > path").setAttribute("d", fill);
904
- ghost2.querySelector("[data-tab-outline]").setAttribute("d", outline);
905
- }
906
- const stroke = tab.querySelector("[data-tab-outline]");
907
- stroke.style.visibility = attached ? "hidden" : "visible";
908
- maskedOutline.current = stroke;
909
- frame2.style.visibility = attached ? "visible" : "hidden";
910
- if (!attached) return;
911
- sheet.style.borderColor = "transparent";
912
- maskedSheet.current = sheet;
913
- const x = box.left - 8, bottom = sheetBox.bottom - box.top - 0.5;
914
- const origin = frame2.parentElement.getBoundingClientRect();
915
- frame2.style.left = `${x - origin.left}px`;
916
- frame2.style.top = `${box.top - origin.top}px`;
917
- const viewport = ghost2 && !floating ? sheetBox : nav.getBoundingClientRect(), clipLeft = Math.max(-left, viewport.left - x), clipRight = Math.min(width + right, viewport.right - x);
918
- frame2.style.clipPath = `polygon(${clipLeft}px 0px, ${clipRight}px 0px, ${clipRight}px ${height - 1}px, ${width + right}px ${height - 1}px, ${width + right}px ${bottom + 1}px, ${-left}px ${bottom + 1}px, ${-left}px ${height - 1}px, ${clipLeft}px ${height - 1}px)`;
919
- frame2.querySelector("path").setAttribute(
920
- "d",
921
- paneShape(left, right, bottom, !!ghost2 && !floating, size) + ((!ghost2 || floating) && clipLeft > 0 ? ` M0 ${height - 0.5} H${Math.min(width, clipLeft)}` : "") + ((!ghost2 || floating) && clipRight < width ? ` M${Math.max(0, clipRight)} ${height - 0.5} H${width}` : "")
922
- );
1188
+ };
923
1189
  };
924
1190
  const animate = (element, from) => {
925
- const transform = getComputedStyle(element).transform;
926
- if (transform !== "none") from += new DOMMatrixReadOnly(transform).m41;
927
- element.getAnimations().forEach((animation) => animation.cancel());
928
- if (!matchMedia("(prefers-reduced-motion: reduce)").matches)
929
- element.animate(
1191
+ animations2.current.get(element)?.cancel();
1192
+ if (!matchMedia("(prefers-reduced-motion: reduce)").matches) {
1193
+ const animation = element.animate(
930
1194
  [{ transform: `translateX(${from}px)` }, { transform: "none" }],
931
1195
  motion
932
1196
  );
1197
+ animations2.current.set(element, animation);
1198
+ const remove = () => {
1199
+ if (animations2.current.get(element) === animation)
1200
+ animations2.current.delete(element);
1201
+ };
1202
+ animation.onfinish = remove;
1203
+ animation.oncancel = remove;
1204
+ }
933
1205
  };
934
1206
  const settle = (key2) => requestAnimationFrame(() => {
935
1207
  if (drag.current) return;
@@ -953,25 +1225,34 @@ function WorkspaceTabs({
953
1225
  latest.current.refresh();
954
1226
  });
955
1227
  const finish = (cancel = false) => {
1228
+ if (cancel) pointer.current.cancel();
1229
+ else pointer.current.flush();
956
1230
  const current = drag.current;
957
1231
  if (!current) return;
958
1232
  drag.current = void 0;
1233
+ pointer.current.cancel();
959
1234
  suppressClick.current = current.moved;
960
1235
  content.release();
961
1236
  if (cancel) {
962
1237
  docking.cancel();
963
- if (current.moved) onReorder(current.order);
964
1238
  announce("");
965
1239
  }
966
- if (current.moved && (cancel || !docking.drop())) settle(current.key);
1240
+ const dropped = !cancel && current.moved && docking.drop();
1241
+ if (!cancel && !dropped && current.previewOrder)
1242
+ onReorder(current.previewOrder);
1243
+ setDraftOrder(void 0);
1244
+ if (current.moved && !dropped) settle(current.key);
967
1245
  else setDragging(void 0);
968
1246
  };
969
- const move = (from, to) => {
1247
+ const move = (from, to, preview = false) => {
970
1248
  const order = links.map(([key2]) => key2), index = order.indexOf(to);
971
1249
  if (from === to || index < 0) return;
972
1250
  order.splice(order.indexOf(from), 1);
973
1251
  order.splice(index, 0, from);
974
- onReorder(order);
1252
+ if (preview && drag.current) {
1253
+ drag.current.previewOrder = order;
1254
+ setDraftOrder(order);
1255
+ } else onReorder(order);
975
1256
  announce(`${labels.get(from)}: position ${index + 1} of ${order.length}`);
976
1257
  };
977
1258
  const split = (key2, edge) => {
@@ -1010,63 +1291,121 @@ function WorkspaceTabs({
1010
1291
  }
1011
1292
  event.preventDefault();
1012
1293
  };
1013
- const measure = () => {
1294
+ const readMetrics = () => {
1014
1295
  const nav = navRef.current, strip = stripRef.current;
1015
1296
  if (!nav || !strip || !dragBoundary) return;
1016
- const trailing = strip.parentElement.nextElementSibling?.getBoundingClientRect().width ?? 0;
1297
+ const toolbarStyle = getComputedStyle(dragBoundary);
1298
+ const gap = parseFloat(toolbarStyle.columnGap) || 0;
1299
+ let trailing = parseFloat(toolbarStyle.paddingRight) || 0;
1300
+ for (let element = strip.parentElement.nextElementSibling; element; element = element.nextElementSibling)
1301
+ trailing += element.getBoundingClientRect().width + gap;
1017
1302
  const availableWidth = Math.max(
1018
1303
  0,
1019
- dragBoundary.getBoundingClientRect().right - strip.getBoundingClientRect().left - trailing - 4
1304
+ dragBoundary.getBoundingClientRect().right - strip.getBoundingClientRect().left - trailing
1020
1305
  );
1021
1306
  const inserting = docking.insertion === void 0 ? 0 : 1;
1022
1307
  const count = items.length - (drag.current?.detached ? 1 : 0) + inserting;
1308
+ const fittedWidth = fitTabWidth(
1309
+ density.width,
1310
+ availableWidth,
1311
+ items.length + inserting
1312
+ );
1023
1313
  const next = {
1024
- available: availableWidth,
1025
- overflow: count * fitTabWidth(density.width, availableWidth, items.length + inserting) + 16 > availableWidth + 1,
1314
+ width: fittedWidth,
1315
+ overflow: count * fittedWidth + 16 > availableWidth + 1,
1026
1316
  left: nav.scrollLeft > 1,
1027
1317
  right: nav.scrollLeft + nav.clientWidth < nav.scrollWidth - 1
1028
1318
  };
1029
1319
  const current = measured.current;
1030
- if (next.available !== current.available || next.overflow !== current.overflow || next.left !== current.left || next.right !== current.right) {
1031
- measured.current = next;
1032
- setMetrics(next);
1033
- }
1320
+ return () => {
1321
+ if (next.width !== current.width || next.overflow !== current.overflow || next.left !== current.left || next.right !== current.right) {
1322
+ measured.current = next;
1323
+ setMetrics(next);
1324
+ }
1325
+ };
1034
1326
  };
1035
- const follow = () => {
1036
- const nav = navRef.current;
1037
- if ([surface(), nav, ghostRef.current].some(
1038
- (element) => element?.getAnimations({ subtree: element === nav }).some((animation) => animation.playState === "running")
1039
- ))
1040
- frame.current = requestAnimationFrame(() => latest.current.refresh());
1327
+ const runningAnimations = () => {
1328
+ for (const target of transitions.current.keys())
1329
+ if (target instanceof Node && !target.isConnected)
1330
+ transitions.current.delete(target);
1331
+ return transitions.current.size > 0 || settling.current?.playState === "running" || [...animations2.current.values()].some(
1332
+ (animation) => animation.playState === "running"
1333
+ );
1041
1334
  };
1042
- const refresh = (resized = false) => {
1043
- cancelAnimationFrame(frame.current);
1044
- measure();
1045
- if (resized) reveal();
1046
- draw();
1047
- follow();
1335
+ const readFrame = (resized = false) => {
1336
+ const metrics2 = readMetrics();
1337
+ const scroll = resized ? readReveal() : void 0;
1338
+ const follow = runningAnimations();
1339
+ const draw = readDrawing(scroll?.delta);
1340
+ return () => {
1341
+ metrics2?.();
1342
+ scroll?.apply();
1343
+ draw();
1344
+ if (follow) geometry.schedule();
1345
+ };
1346
+ };
1347
+ const refresh = (resized = false) => geometry.commit(resized);
1348
+ const movePointer = () => {
1349
+ const current = drag.current, nav = navRef.current;
1350
+ if (!current?.moved || !nav) return;
1351
+ current.detached = docking.move(current);
1352
+ if (!current.detached) {
1353
+ const { left, right } = nav.getBoundingClientRect();
1354
+ const scroll = current.x < left + 24 ? -16 : current.x > right - 24 ? 16 : 0;
1355
+ const before = nav.scrollLeft;
1356
+ if (scroll) nav.scrollLeft += scroll;
1357
+ const index = Math.max(
1358
+ 0,
1359
+ Math.min(
1360
+ items.length - 1,
1361
+ Math.floor(
1362
+ (Math.max(left + 8, Math.min(current.x, right - 8)) - left + nav.scrollLeft - 8) / tabWidth
1363
+ )
1364
+ )
1365
+ );
1366
+ move(current.key, items[index].key, true);
1367
+ if (scroll && before !== nav.scrollLeft)
1368
+ pointer.current.schedule(void 0);
1369
+ }
1370
+ geometry.flush();
1048
1371
  };
1049
- const latest = React.useRef({ refresh, finish });
1372
+ const latest = React.useRef({ refresh, finish, movePointer, readFrame });
1373
+ const [geometry] = React.useState(
1374
+ () => geometryTask((resized) => latest.current.readFrame(resized))
1375
+ );
1376
+ const pointer = React.useRef(
1377
+ void 0
1378
+ );
1379
+ if (!pointer.current)
1380
+ pointer.current = frameTask(() => latest.current.movePointer());
1050
1381
  const detached = !!dragging && !!drag.current?.detached;
1051
1382
  React.useLayoutEffect(() => {
1052
- latest.current = { refresh, finish };
1383
+ latest.current = { refresh, finish, movePointer, readFrame };
1384
+ });
1385
+ React.useLayoutEffect(() => {
1053
1386
  const order = JSON.stringify([tabOrder, docking.insertion]);
1054
1387
  const previous = positions.current;
1055
1388
  positions.current = { order, tabs: /* @__PURE__ */ new Map() };
1056
- for (const element of navRef.current.children) {
1057
- const key2 = element.querySelector("a")?.dataset.tab;
1058
- if (!key2) continue;
1389
+ const slots = items.map(({ key: key2 }) => {
1390
+ const element = tabElement(key2);
1059
1391
  const left = element.offsetLeft, from = previous.tabs.get(key2);
1060
- if (previous.order !== order && key2 !== dragging && from !== void 0 && from !== left)
1061
- animate(element, from - left);
1392
+ const displaced = previous.order !== order && key2 !== dragging && from !== void 0 && from !== left;
1393
+ const transform = displaced ? getComputedStyle(element).transform : "none";
1394
+ const offset = transform === "none" ? 0 : new DOMMatrixReadOnly(transform).m41;
1395
+ return {
1396
+ key: key2,
1397
+ element,
1398
+ left,
1399
+ delta: displaced ? from - left + offset : 0
1400
+ };
1401
+ });
1402
+ for (const { key: key2, element, left, delta } of slots) {
1403
+ if (delta) animate(element, delta);
1062
1404
  positions.current.tabs.set(key2, left);
1063
1405
  }
1064
- cancelAnimationFrame(frame.current);
1065
- draw();
1066
- follow();
1067
- });
1406
+ }, [tabOrder, docking.insertion, tabWidth, dragging, detached]);
1068
1407
  React.useLayoutEffect(
1069
- () => refresh(),
1408
+ () => refresh(true),
1070
1409
  // oxlint-disable-next-line react-hooks/exhaustive-deps -- the inputs refresh measures
1071
1410
  [
1072
1411
  dragBoundary,
@@ -1074,43 +1413,84 @@ function WorkspaceTabs({
1074
1413
  tabOrder,
1075
1414
  docking.insertion,
1076
1415
  tabWidth,
1416
+ density.width,
1077
1417
  height,
1078
1418
  metrics.overflow,
1079
1419
  dragging,
1080
1420
  detached
1081
1421
  ]
1082
1422
  );
1083
- React.useLayoutEffect(() => reveal(), [active, tabOrder, docking.insertion]);
1084
1423
  React.useLayoutEffect(() => {
1085
1424
  const nav = navRef.current;
1086
- const resize = () => latest.current.refresh(true), scroll = () => latest.current.refresh(), blur = () => latest.current.finish(true);
1425
+ const board = dragBoundary?.closest(".lab-board");
1426
+ const trackedTransitions = transitions.current;
1427
+ const transition = (event) => {
1428
+ const target = event.target;
1429
+ const name = event.propertyName;
1430
+ if ([
1431
+ "color",
1432
+ "background-color",
1433
+ "fill",
1434
+ "stroke",
1435
+ "opacity",
1436
+ "filter",
1437
+ "box-shadow"
1438
+ ].includes(name))
1439
+ return;
1440
+ if (event.type === "transitionrun") {
1441
+ if (target !== surface() && !(target instanceof Node && dragBoundary?.contains(target)))
1442
+ return;
1443
+ const properties = transitions.current.get(target) ?? /* @__PURE__ */ new Set();
1444
+ properties.add(name);
1445
+ transitions.current.set(target, properties);
1446
+ geometry.schedule();
1447
+ } else {
1448
+ const properties = transitions.current.get(target);
1449
+ properties?.delete(name);
1450
+ if (!properties?.size) transitions.current.delete(target);
1451
+ }
1452
+ };
1453
+ for (const name of ["transitionrun", "transitionend", "transitioncancel"])
1454
+ board?.addEventListener(name, transition);
1455
+ const scroll = () => latest.current.refresh(), blur = () => latest.current.finish(true);
1087
1456
  const wheel = (event) => {
1088
1457
  if (event.ctrlKey || Math.abs(event.deltaX) >= Math.abs(event.deltaY) || nav.scrollWidth <= nav.clientWidth)
1089
1458
  return;
1090
1459
  event.preventDefault();
1091
1460
  nav.scrollLeft += event.deltaY * (event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? nav.clientWidth : 1);
1092
1461
  };
1093
- const observer = new ResizeObserver(resize);
1094
- for (const element of [
1095
- nav,
1096
- stripRef.current,
1097
- dragBoundary,
1098
- ...dragBoundary?.children ?? [],
1099
- surface()
1100
- ])
1101
- if (element) observer.observe(element);
1462
+ const unobserve = geometry.observe(
1463
+ [
1464
+ nav,
1465
+ stripRef.current,
1466
+ dragBoundary,
1467
+ ...dragBoundary?.children ?? [],
1468
+ surface()
1469
+ ].filter((element) => !!element)
1470
+ );
1102
1471
  addEventListener("scroll", scroll, true);
1103
1472
  addEventListener("blur", blur);
1104
1473
  nav.addEventListener("wheel", wheel, { passive: false });
1105
1474
  return () => {
1106
- observer.disconnect();
1475
+ for (const name of ["transitionrun", "transitionend", "transitioncancel"])
1476
+ board?.removeEventListener(name, transition);
1477
+ trackedTransitions.clear();
1478
+ unobserve();
1107
1479
  removeEventListener("scroll", scroll, true);
1108
1480
  removeEventListener("blur", blur);
1109
1481
  nav.removeEventListener("wheel", wheel);
1110
- cancelAnimationFrame(frame.current);
1482
+ geometry.cancel();
1111
1483
  restoreFrame();
1112
1484
  };
1113
1485
  }, [active, dragBoundary]);
1486
+ React.useEffect(
1487
+ () => () => {
1488
+ pointer.current.cancel();
1489
+ settling.current?.cancel();
1490
+ for (const animation of animations2.current.values()) animation.cancel();
1491
+ },
1492
+ []
1493
+ );
1114
1494
  const scrollButton = (direction) => metrics.overflow && /* @__PURE__ */ jsx4(
1115
1495
  "button",
1116
1496
  {
@@ -1142,6 +1522,8 @@ function WorkspaceTabs({
1142
1522
  onKeyDownCapture: () => setPointerFocus(false),
1143
1523
  onBlurCapture: () => setPointerFocus(false),
1144
1524
  onPointerDownCapture: () => {
1525
+ pointer.current.cancel();
1526
+ setDraftOrder(void 0);
1145
1527
  content.release(false);
1146
1528
  settling.current?.cancel();
1147
1529
  setDragging(void 0);
@@ -1162,36 +1544,24 @@ function WorkspaceTabs({
1162
1544
  if (!current.moved && Math.hypot(x - current.x, event.clientY - current.y) < 6)
1163
1545
  return;
1164
1546
  if (!current.moved && active !== current.key) onSelect(current.key);
1547
+ if (!current.moved) setDragging(current.key);
1165
1548
  current.moved = true;
1166
1549
  if (!nav.hasPointerCapture(event.pointerId))
1167
1550
  nav.setPointerCapture(event.pointerId);
1168
- setDragging(current.key);
1169
1551
  event.preventDefault();
1170
1552
  current.x = x;
1171
1553
  current.y = event.clientY;
1172
- current.detached = docking.move(current);
1173
- if (!current.detached) {
1174
- const { left, right } = nav.getBoundingClientRect();
1175
- nav.scrollLeft += x < left + 24 ? -16 : x > right - 24 ? 16 : 0;
1176
- const index = Math.max(
1177
- 0,
1178
- Math.min(
1179
- items.length - 1,
1180
- Math.floor(
1181
- (Math.max(left + 8, Math.min(x, right - 8)) - left + nav.scrollLeft - 8) / tabWidth
1182
- )
1183
- )
1184
- );
1185
- move(current.key, items[index].key);
1186
- }
1187
- refresh();
1554
+ pointer.current.schedule(void 0);
1188
1555
  },
1189
1556
  onPointerUp: () => finish(),
1190
1557
  onPointerCancel: () => finish(true),
1558
+ onLostPointerCapture: (event) => {
1559
+ if (event.target === event.currentTarget) finish(true);
1560
+ },
1191
1561
  className: "lab-tablist",
1192
1562
  children: [
1193
1563
  links.map(([key2, text], index) => {
1194
- const actions = [
1564
+ const actions = menu ? [
1195
1565
  ...onDuplicate ? [
1196
1566
  {
1197
1567
  label: "Duplicate tab",
@@ -1223,7 +1593,7 @@ function WorkspaceTabs({
1223
1593
  })),
1224
1594
  null,
1225
1595
  { label: "Close tab", onSelect: () => onClose(key2) }
1226
- ];
1596
+ ] : [];
1227
1597
  const target = /* @__PURE__ */ jsxs3(
1228
1598
  "div",
1229
1599
  {
@@ -1283,8 +1653,7 @@ function WorkspaceTabs({
1283
1653
  x: event.clientX,
1284
1654
  y: event.clientY,
1285
1655
  detached: false,
1286
- moved: false,
1287
- order: links.map(([path]) => path)
1656
+ moved: false
1288
1657
  };
1289
1658
  },
1290
1659
  className: "lab-tab-link",
@@ -1325,18 +1694,21 @@ function WorkspaceTabs({
1325
1694
  "Arrow keys switch tabs. Home and End select the first and last tab. Delete closes a tab. Alt plus Left or Right reorders. Alt plus Shift and Left, Right, or Down splits a pane. F6 moves between panes; Shift plus F6 moves back.",
1326
1695
  menu && " Right-click or press Shift plus F10 for tab actions without dragging."
1327
1696
  ] }),
1328
- active && /* @__PURE__ */ jsx4(
1329
- "svg",
1330
- {
1331
- ref: frameRef,
1332
- "aria-hidden": "true",
1333
- "data-pane-frame": true,
1334
- viewBox: `0 0 ${width} ${height}`,
1335
- strokeWidth: size.borderWidth,
1336
- style: { width, height },
1337
- className: "lab-pane-frame",
1338
- children: /* @__PURE__ */ jsx4("path", { className: "lab-outline" })
1339
- }
1697
+ active && frameLayer && createPortal(
1698
+ /* @__PURE__ */ jsx4(
1699
+ "svg",
1700
+ {
1701
+ ref: frameRef,
1702
+ "aria-hidden": "true",
1703
+ "data-pane-frame": true,
1704
+ viewBox: `0 0 ${width} ${height}`,
1705
+ strokeWidth: size.borderWidth,
1706
+ style: { width, height },
1707
+ className: "lab-pane-frame",
1708
+ children: /* @__PURE__ */ jsx4("path", { className: "lab-outline" })
1709
+ }
1710
+ ),
1711
+ frameLayer
1340
1712
  ),
1341
1713
  dragging && createPortal(
1342
1714
  /* @__PURE__ */ jsxs3(
@@ -1565,7 +1937,7 @@ function useTabHistories(tabs) {
1565
1937
  }
1566
1938
 
1567
1939
  // browser-history.ts
1568
- import { useEffect as useEffect3, useLayoutEffect as useLayoutEffect7, useRef as useRef8 } from "react";
1940
+ import { useEffect as useEffect4, useLayoutEffect as useLayoutEffect7, useRef as useRef8 } from "react";
1569
1941
  var read = (state) => state?.labpane;
1570
1942
  function createBrowserHistory(front) {
1571
1943
  const session = Date.now();
@@ -1642,7 +2014,7 @@ function useBrowserHistory(front) {
1642
2014
  const browser = useRef8(
1643
2015
  void 0
1644
2016
  );
1645
- useEffect3(() => {
2017
+ useEffect4(() => {
1646
2018
  if (!front) return;
1647
2019
  const { sync, pop } = browser.current ??= createBrowserHistory(
1648
2020
  () => latest.current?.get()
@@ -1674,6 +2046,7 @@ function WorkspaceContent({
1674
2046
  leading,
1675
2047
  trailing,
1676
2048
  titleBar,
2049
+ windowControlsOverlay,
1677
2050
  toolbar,
1678
2051
  menu,
1679
2052
  confirmClose,
@@ -1685,18 +2058,27 @@ function WorkspaceContent({
1685
2058
  empty
1686
2059
  }) {
1687
2060
  const links = useTabLinks();
1688
- const { height, contentHeight, logoSize } = useTabSize();
2061
+ const tabsByKey = useMemo3(
2062
+ () => new Map(tabs.map((tab) => [tab.key, tab])),
2063
+ [tabs]
2064
+ );
2065
+ const {
2066
+ height: tabHeight,
2067
+ contentHeight,
2068
+ logoSize
2069
+ } = useMemo3(() => densitySize(density), [density]);
1689
2070
  const corner2 = useRef9(null);
1690
2071
  const [reserve, setReserve] = useState5(0);
1691
2072
  const hasTrailing = !!trailing;
1692
2073
  useLayoutEffect8(() => {
1693
2074
  const element = corner2.current;
1694
2075
  if (!element) return setReserve(0);
1695
- const observer = new ResizeObserver(() => setReserve(element.offsetWidth));
1696
- observer.observe(element);
1697
- return () => observer.disconnect();
2076
+ const observer2 = new ResizeObserver(() => setReserve(element.offsetWidth));
2077
+ observer2.observe(element);
2078
+ return () => observer2.disconnect();
1698
2079
  }, [hasTrailing]);
1699
- const root = useRef9(null);
2080
+ const ownRoot = useRef9(null);
2081
+ const root = windowControlsOverlay?.root ?? ownRoot;
1700
2082
  const [inset, setInset] = useState5(0);
1701
2083
  useLayoutEffect8(() => {
1702
2084
  if (!titleBar) return setInset(0);
@@ -1704,7 +2086,9 @@ function WorkspaceContent({
1704
2086
  measure();
1705
2087
  addEventListener("resize", measure);
1706
2088
  return () => removeEventListener("resize", measure);
1707
- }, [titleBar]);
2089
+ }, [titleBar, root]);
2090
+ const titlebarArea = windowControlsOverlay?.area;
2091
+ const height = Math.max(tabHeight, titlebarArea?.height ?? 0);
1708
2092
  const preferredPane = useRef9(void 0);
1709
2093
  const visibleLayout = reconcileLayout(
1710
2094
  layout,
@@ -1718,10 +2102,10 @@ function WorkspaceContent({
1718
2102
  const front = active ?? panes(visibleLayout)[0]?.active;
1719
2103
  const openers = useOpeners(tabs, front);
1720
2104
  const historyOf = useTabHistories(tabs);
1721
- const frontTab = tabs.find((tab) => tab.key === front);
2105
+ const frontTab = tabsByKey.get(front ?? "");
1722
2106
  useBrowserHistory(history2 && frontTab ? historyOf(frontTab) : void 0);
1723
2107
  const dirty = useRef9(/* @__PURE__ */ new Map());
1724
- useEffect4(() => {
2108
+ useEffect5(() => {
1725
2109
  const warn = (event) => {
1726
2110
  if (dirty.current.size) event.preventDefault();
1727
2111
  };
@@ -1732,7 +2116,7 @@ function WorkspaceContent({
1732
2116
  const pane = panes(visibleLayout).find((p) => p.tabs.includes(key2));
1733
2117
  if (!pane) return;
1734
2118
  const remaining = pane.tabs.filter((k) => k !== key2);
1735
- const opener = tabs.find((tab) => tab.key === key2)?.opener;
2119
+ const opener = tabsByKey.get(key2)?.opener;
1736
2120
  const neighbor = next ?? remaining.find((key3) => key3 === opener) ?? remaining[Math.min(pane.tabs.indexOf(key2), remaining.length - 1)] ?? tabs.find((t) => t.key !== key2)?.key;
1737
2121
  onClose(key2, neighbor);
1738
2122
  requestAnimationFrame(
@@ -1745,7 +2129,7 @@ function WorkspaceContent({
1745
2129
  }
1746
2130
  function requestClose(key2, next) {
1747
2131
  if (![...dirty.current.values()].includes(key2)) return close(key2, next);
1748
- const tab = tabs.find((tab2) => tab2.key === key2);
2132
+ const tab = tabsByKey.get(key2);
1749
2133
  const element = links.get(key2);
1750
2134
  Promise.resolve(
1751
2135
  confirmClose ? confirmClose(tab) : confirm(`Discard changes in ${tab.label}?`)
@@ -1755,19 +2139,20 @@ function WorkspaceContent({
1755
2139
  });
1756
2140
  }
1757
2141
  function leave(key2) {
1758
- const opener = openers.get(key2) ?? tabs.find((tab) => tab.key === key2)?.opener;
1759
- requestClose(
1760
- key2,
1761
- tabs.some((tab) => tab.key === opener) ? opener : void 0
1762
- );
2142
+ const opener = openers.get(key2) ?? tabsByKey.get(key2)?.opener;
2143
+ requestClose(key2, tabsByKey.has(opener ?? "") ? opener : void 0);
1763
2144
  }
2145
+ const services = useRef9(leave);
2146
+ useLayoutEffect8(() => {
2147
+ services.current = leave;
2148
+ });
1764
2149
  const paneOf = (pane) => ({
1765
2150
  id: pane.id,
1766
2151
  tabs: pane.tabs,
1767
2152
  active: pane.active,
1768
2153
  focus: links.focus
1769
2154
  });
1770
- return /* @__PURE__ */ jsx6(Fragment3, { children: /* @__PURE__ */ jsxs5(
2155
+ return /* @__PURE__ */ jsx6(MinimumTabHeight.Provider, { value: titlebarArea?.height ?? 0, children: /* @__PURE__ */ jsxs5(
1771
2156
  "div",
1772
2157
  {
1773
2158
  ref: root,
@@ -1775,6 +2160,7 @@ function WorkspaceContent({
1775
2160
  "aria-label": "Workspace",
1776
2161
  "data-density": density,
1777
2162
  "data-title-bar": titleBar || void 0,
2163
+ "data-window-controls-overlay": titlebarArea ? "" : void 0,
1778
2164
  style: {
1779
2165
  "--lab-tab-height": `${height}px`,
1780
2166
  "--lab-tab-content-height": `${contentHeight}px`,
@@ -1799,10 +2185,19 @@ function WorkspaceContent({
1799
2185
  selected[(index + (event.shiftKey ? selected.length - 1 : 1)) % selected.length]?.focus();
1800
2186
  },
1801
2187
  children: [
1802
- trailing && /* @__PURE__ */ jsx6("div", { ref: corner2, className: "lab-trailing", style: { height }, children: trailing }),
2188
+ trailing && /* @__PURE__ */ jsx6(
2189
+ "div",
2190
+ {
2191
+ ref: corner2,
2192
+ className: "lab-trailing",
2193
+ style: { height, right: titlebarArea?.insetRight },
2194
+ children: trailing
2195
+ }
2196
+ ),
1803
2197
  /* @__PURE__ */ jsx6(
1804
2198
  WorkspaceSurface,
1805
2199
  {
2200
+ titlebarArea,
1806
2201
  layout: visibleLayout,
1807
2202
  change,
1808
2203
  menu,
@@ -1818,7 +2213,7 @@ function WorkspaceContent({
1818
2213
  },
1819
2214
  docking,
1820
2215
  surface,
1821
- items: pane.tabs.map((key2) => tabs.find((t) => t.key === key2)),
2216
+ items: pane.tabs.map((key2) => tabsByKey.get(key2)),
1822
2217
  active: pane.active,
1823
2218
  onSelect,
1824
2219
  onDuplicate: onDuplicate ? (key2) => {
@@ -1864,19 +2259,17 @@ function WorkspaceContent({
1864
2259
  if (active !== tab.key) onSelect(tab.key);
1865
2260
  },
1866
2261
  children: /* @__PURE__ */ jsx6(
1867
- TabScope,
2262
+ TabBody,
1868
2263
  {
2264
+ tab,
2265
+ pane: region.pane,
2266
+ focus: links.focus,
1869
2267
  active: tab.key === front,
1870
2268
  visible: !hidden,
1871
- leave: () => leave(tab.key),
1872
2269
  history: historyOf(tab),
1873
- children: /* @__PURE__ */ jsx6(
1874
- UnsavedChanges.Provider,
1875
- {
1876
- value: (source, changed) => changed ? dirty.current.set(source, tab.key) : dirty.current.delete(source),
1877
- children: children(tab, paneOf(region.pane))
1878
- }
1879
- )
2270
+ services,
2271
+ dirty: dirty.current,
2272
+ render: children
1880
2273
  }
1881
2274
  )
1882
2275
  },
@@ -1900,16 +2293,39 @@ function WorkspaceContent({
1900
2293
  }
1901
2294
  ) });
1902
2295
  }
1903
- function TabScope({
2296
+ var TabBody = memo3(function TabBody2({
2297
+ tab,
2298
+ pane,
2299
+ focus,
1904
2300
  active,
1905
2301
  visible,
1906
- leave,
1907
2302
  history: history2,
1908
- children
2303
+ services,
2304
+ dirty,
2305
+ render
1909
2306
  }) {
1910
- const state = useMemo2(() => ({ active, visible }), [active, visible]);
1911
- return /* @__PURE__ */ jsx6(TabStateContext.Provider, { value: state, children: /* @__PURE__ */ jsx6(LeaveTab.Provider, { value: leave, children: /* @__PURE__ */ jsx6(TabHistoryContext.Provider, { value: history2, children }) }) });
1912
- }
2307
+ const key2 = tab.key;
2308
+ const [leave] = useState5(() => () => services.current(key2));
2309
+ const report = useCallback2(
2310
+ (source, changed) => {
2311
+ if (changed) dirty.set(source, key2);
2312
+ else dirty.delete(source);
2313
+ },
2314
+ [dirty, key2]
2315
+ );
2316
+ const requested = useMemo3(
2317
+ () => ({ render, tab, pane, focus }),
2318
+ [render, tab, pane, focus]
2319
+ );
2320
+ const deferred = useDeferredValue(requested);
2321
+ const inputs = visible ? requested : deferred;
2322
+ const content = useMemo3(
2323
+ () => inputs.render(inputs.tab, { ...inputs.pane, focus: inputs.focus }),
2324
+ [inputs]
2325
+ );
2326
+ const state = useMemo3(() => ({ active, visible }), [active, visible]);
2327
+ return /* @__PURE__ */ jsx6(TabStateContext.Provider, { value: state, children: /* @__PURE__ */ jsx6(LeaveTab.Provider, { value: leave, children: /* @__PURE__ */ jsx6(TabHistoryContext.Provider, { value: history2, children: /* @__PURE__ */ jsx6(UnsavedChanges.Provider, { value: report, children: content }) }) }) });
2328
+ });
1913
2329
  function TabPanel({
1914
2330
  tab,
1915
2331
  hidden,
@@ -1986,14 +2402,14 @@ function WorkspaceFrame({
1986
2402
  setDraft(void 0);
1987
2403
  };
1988
2404
  useLayoutEffect9(() => {
1989
- const observer = new ResizeObserver(() => {
2405
+ const observer2 = new ResizeObserver(() => {
1990
2406
  setAvailable(viewport.current.clientWidth);
1991
2407
  cancel();
1992
2408
  });
1993
- observer.observe(viewport.current);
2409
+ observer2.observe(viewport.current);
1994
2410
  addEventListener("blur", cancel);
1995
2411
  return () => {
1996
- observer.disconnect();
2412
+ observer2.disconnect();
1997
2413
  removeEventListener("blur", cancel);
1998
2414
  };
1999
2415
  }, []);
@@ -2072,7 +2488,7 @@ function WorkspaceFrame({
2072
2488
 
2073
2489
  // window.tsx
2074
2490
  import {
2075
- useEffect as useEffect5,
2491
+ useEffect as useEffect6,
2076
2492
  useLayoutEffect as useLayoutEffect10,
2077
2493
  useRef as useRef11,
2078
2494
  useState as useState7
@@ -2090,7 +2506,7 @@ var interactive = "a, button, input, select, textarea, label, summary, [contente
2090
2506
  var draggable = (target) => target instanceof Element && !target.closest(interactive) && (!!target.closest(".lab-toolbar, [data-window-drag]") || target.matches(".lab-workspace") || !!target.querySelector(".lab-workspace"));
2091
2507
  function useFullscreen(host) {
2092
2508
  const [full, setFull] = useState7(() => isFullscreen(host));
2093
- useEffect5(() => {
2509
+ useEffect6(() => {
2094
2510
  const update = () => setFull(isFullscreen(host));
2095
2511
  update();
2096
2512
  window.addEventListener("resize", update);
@@ -2103,7 +2519,7 @@ function useFullscreen(host) {
2103
2519
  return [full, (on) => setFullscreen(host, on)];
2104
2520
  }
2105
2521
  function useWindowFrame(host) {
2106
- useEffect5(() => {
2522
+ useEffect6(() => {
2107
2523
  if (!host) return;
2108
2524
  const root = document.documentElement;
2109
2525
  let armed = false;
@@ -2189,9 +2605,9 @@ function TrafficLights({ host }) {
2189
2605
  element.style.width = `${end - element.getBoundingClientRect().left - gap}px`;
2190
2606
  };
2191
2607
  place();
2192
- const observer = new ResizeObserver(place);
2193
- observer.observe(toolbar);
2194
- return () => observer.disconnect();
2608
+ const observer2 = new ResizeObserver(place);
2609
+ observer2.observe(toolbar);
2610
+ return () => observer2.disconnect();
2195
2611
  }, [host, shown]);
2196
2612
  return shown ? /* @__PURE__ */ jsx8("div", { ref: spacer, className: "lab-traffic-lights" }) : null;
2197
2613
  }
@@ -2213,7 +2629,7 @@ function WindowControls({ host }) {
2213
2629
  if (spacer.current) setTop(spacer.current.getBoundingClientRect().top);
2214
2630
  };
2215
2631
  useLayoutEffect10(measure);
2216
- useEffect5(() => {
2632
+ useEffect6(() => {
2217
2633
  window.addEventListener("resize", measure);
2218
2634
  return () => window.removeEventListener("resize", measure);
2219
2635
  }, []);