lilact 0.13.0 → 0.13.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.
Files changed (34) hide show
  1. package/dist/lilact.development.js +230 -234
  2. package/dist/lilact.development.js.map +1 -1
  3. package/dist/lilact.development.min.js +1 -1
  4. package/dist/lilact.development.min.js.LICENSE.txt +1 -1
  5. package/dist/lilact.development.min.js.map +1 -1
  6. package/dist/lilact.production.min.js +1 -1
  7. package/dist/lilact.production.min.js.map +1 -1
  8. package/docs/assets/hierarchy.js +1 -1
  9. package/docs/assets/highlight.css +4 -4
  10. package/docs/assets/navigation.js +1 -1
  11. package/docs/assets/search.js +1 -1
  12. package/docs/classes/accessories.ErrorBoundary.html +6 -6
  13. package/docs/classes/accessories.Suspense.html +3 -3
  14. package/docs/classes/components.Component.html +1 -1
  15. package/docs/classes/components.HTMLComponent.html +1 -1
  16. package/docs/classes/components.RootComponent.html +2 -2
  17. package/docs/functions/accessories.DragHandle.html +1 -1
  18. package/docs/functions/accessories.Spinner.html +1 -1
  19. package/docs/hierarchy.html +1 -1
  20. package/docs/modules/accessories.html +1 -1
  21. package/docs/modules.html +1 -1
  22. package/docs/static/lilact.development.js +230 -234
  23. package/docs/static/lilact.development.min.js +1 -1
  24. package/docs/static/lilact.production.min.js +1 -1
  25. package/docs/variables/{pane.SplitPane.html → accessories.SplitPane.html} +3 -3
  26. package/package.json +1 -1
  27. package/root/lilact.development.js +230 -234
  28. package/root/lilact.development.min.js +1 -1
  29. package/root/lilact.production.min.js +1 -1
  30. package/src/accessories.jsx +274 -1
  31. package/src/lilact.jsx +0 -4
  32. package/typedoc.json +0 -1
  33. package/docs/modules/pane.html +0 -1
  34. package/src/pane.jsx +0 -270
@@ -28,10 +28,15 @@
28
28
 
29
29
  */
30
30
 
31
+
32
+ import { useEffect, useLayoutEffect, useMemo, useRef, useState, useImperativeHandle } from "./hooks.jsx";
33
+ import { Children, forwardRef } from "./misc.jsx";
34
+
35
+
31
36
  import {isThenable} from "./misc.jsx"
32
37
  import {setTimeout, clearTimeout} from "./timers.jsx"
33
38
  import {Component} from "./components.jsx"
34
- import {useRef, useState, useCallback} from './hooks.jsx'
39
+ import {useCallback} from './hooks.jsx'
35
40
  import {emotion} from "./lilact.jsx"
36
41
  const {css,cx} = emotion;
37
42
 
@@ -402,3 +407,271 @@ export function DragHandle({
402
407
  );
403
408
  }
404
409
 
410
+
411
+
412
+ const clamp = (n, min, max) => {
413
+ if (!Number.isFinite(n)) return n;
414
+ return Math.min(max, Math.max(min, n));
415
+ };
416
+
417
+ /**
418
+ * A split-pane container with a draggable splitter, supporting both horizontal and vertical layouts.
419
+ *
420
+ * The pane can be either:
421
+ * - **Controlled** via the `position` prop (number between `min` and `max`), or
422
+ * - **Uncontrolled** via `defaultPosition` (used as the initial position).
423
+ *
424
+ * Layout behavior:
425
+ * - `mode="horizontal"`: the `position` controls the width of the **left** pane.
426
+ * - `mode="vertical"`: the `position` controls the height of the **top** pane.
427
+ *
428
+ * Ref API:
429
+ * - Exposes imperative methods on `ref.current`:
430
+ * - `getMode()` to get the current mode
431
+ * - `setMode(mode)` to switch between `"horizontal"` and `"vertical"`
432
+ * - `getPosition()` to get the current splitter position
433
+ * - `setPosition(position)` to update the splitter position
434
+ *
435
+ * Events:
436
+ * - Calls `onSizeChange(position)` whenever the pane size/position changes (e.g., via dragging).
437
+ *
438
+ * Rendering:
439
+ * - Renders `children` into two separate containers (no portals).
440
+ *
441
+ * @param mode - Split direction: `"horizontal"` or `"vertical"`. Defaults to `"horizontal"`.
442
+ * @param position - Controlled splitter position. Normalized value within `[min, max]`.
443
+ * If provided, the component uses this value instead of internal state.
444
+ * @param defaultPosition - Initial splitter position for uncontrolled usage. Defaults to `0.5`.
445
+ * @param min - Minimum allowed position. Defaults to `0.1`.
446
+ * @param max - Maximum allowed position. Defaults to `0.9`.
447
+ * @param splitterSize - Thickness of the draggable splitter in pixels. Defaults to `8`.
448
+ * @param onSizeChange - Callback invoked when the position changes. Receives the new normalized position.
449
+ * @param style - Optional root container styles.
450
+ * @param className - Optional root container CSS class.
451
+ * @param leftPaneStyle - Optional styles applied to the left pane (or top pane in vertical mode).
452
+ * @param rightPaneStyle - Optional styles applied to the right pane (or bottom pane in vertical mode).
453
+ * @param splitterStyle - Optional styles applied to the splitter element.
454
+ * @param children - React children to be rendered into the two pane containers.
455
+ *
456
+ * @example
457
+ * ```tsx
458
+ * const ref = useRef<SplitPaneHandle>(null);
459
+ *
460
+ * <SplitPane
461
+ * ref={ref}
462
+ * mode="horizontal"
463
+ * defaultPosition={0.5}
464
+ * min={0.1}
465
+ * max={0.9}
466
+ * onSizeChange={(pos) => console.log(pos)}
467
+ * >
468
+ * <div /> <div />
469
+ * </SplitPane>
470
+ * ```
471
+ */
472
+ export const SplitPane = forwardRef(function SplitPane(
473
+ {
474
+ mode = "horizontal",
475
+ position, // controlled: number | undefined/null
476
+ defaultPosition = 0.5,
477
+ min = 0.1,
478
+ max = 0.9,
479
+ splitterSize = 8,
480
+ onSizeChange,
481
+ style,
482
+ className,
483
+ leftPaneStyle,
484
+ rightPaneStyle,
485
+ splitterStyle,
486
+ children,
487
+ },
488
+ ref
489
+ ) {
490
+ const containerRef = useRef(null);
491
+ const panes = Children.toArray(children);
492
+ const leftChild = panes[0] ?? null;
493
+ const rightChild = panes[1] ?? null;
494
+
495
+ const isControlled = position != null;
496
+
497
+ const [internalMode, setInternalMode] = useState(mode);
498
+
499
+ const [posUncontrolled, setPosUncontrolled] = useState(() =>
500
+ clamp(
501
+ position ?? defaultPosition,
502
+ min,
503
+ max
504
+ )
505
+ );
506
+
507
+ useEffect(() => {
508
+ if (mode != null) setInternalMode(mode);
509
+ }, [mode]);
510
+
511
+ // keep internal position clamped if min/max change (uncontrolled only)
512
+ useLayoutEffect(() => {
513
+ if (isControlled) return;
514
+ setPosUncontrolled((p) => clamp(p, min, max));
515
+ }, [min, max, isControlled]);
516
+
517
+ const posResolved = isControlled ? clamp(position, min, max) : posUncontrolled;
518
+
519
+ const setPosition = (next) => {
520
+ const clamped = clamp(next, min, max);
521
+ if (!isControlled) setPosUncontrolled(clamped);
522
+ onSizeChange?.(clamped);
523
+ };
524
+
525
+ const draggingRef = useRef(false);
526
+ const pointerIdRef = useRef(null);
527
+
528
+ const updateFromClient = (clientX, clientY) => {
529
+ const el = containerRef.current;
530
+ if (!el) return;
531
+
532
+ const rect = el.getBoundingClientRect();
533
+
534
+ if (internalMode === "horizontal") {
535
+ const usable = rect.width;
536
+ if (!Number.isFinite(usable) || usable <= 0) return; // no jump on init
537
+ const raw = (clientX - rect.left) / usable;
538
+ if (!Number.isFinite(raw)) return;
539
+ setPosition(raw);
540
+ } else {
541
+ const usable = rect.height;
542
+ if (!Number.isFinite(usable) || usable <= 0) return; // no jump on init
543
+ const raw = (clientY - rect.top) / usable;
544
+ if (!Number.isFinite(raw)) return;
545
+ setPosition(raw);
546
+ }
547
+ };
548
+
549
+ // stable global listeners: only act while draggingRef.current === true
550
+ useEffect(() => {
551
+ const onMove = (e) => {
552
+ if (!draggingRef.current) return;
553
+ if (pointerIdRef.current != null && e.pointerId !== pointerIdRef.current) return;
554
+ updateFromClient(e.clientX, e.clientY);
555
+ };
556
+
557
+ const stop = (e) => {
558
+ if (!draggingRef.current) return;
559
+ if (pointerIdRef.current != null && e.pointerId !== pointerIdRef.current) return;
560
+ draggingRef.current = false;
561
+ pointerIdRef.current = null;
562
+ };
563
+
564
+ window.addEventListener("pointermove", onMove, { passive: true });
565
+ window.addEventListener("pointerup", stop, { passive: true });
566
+ window.addEventListener("pointercancel", stop, { passive: true });
567
+
568
+ return () => {
569
+ window.removeEventListener("pointermove", onMove);
570
+ window.removeEventListener("pointerup", stop);
571
+ window.removeEventListener("pointercancel", stop);
572
+ };
573
+ }, [internalMode]); // updateFromClient uses internalMode
574
+
575
+ const onPointerDown = (e) => {
576
+ if (e.button != null && e.button !== 0) return;
577
+ e.preventDefault();
578
+
579
+ draggingRef.current = true;
580
+ pointerIdRef.current = e.pointerId;
581
+
582
+ try {
583
+ e.currentTarget.setPointerCapture?.(e.pointerId);
584
+ } catch {}
585
+
586
+ updateFromClient(e.clientX, e.clientY); // first update only if rect is sane
587
+ };
588
+
589
+ const onKeyDown = (e) => {
590
+ const step = 0.02;
591
+ let delta = 0;
592
+
593
+ if (internalMode === "horizontal") {
594
+ if (e.key === "ArrowLeft") delta = -step;
595
+ if (e.key === "ArrowRight") delta = step;
596
+ } else {
597
+ if (e.key === "ArrowUp") delta = -step;
598
+ if (e.key === "ArrowDown") delta = step;
599
+ }
600
+
601
+ if (delta !== 0) {
602
+ e.preventDefault();
603
+ setPosition(posResolved + delta);
604
+ }
605
+ };
606
+
607
+ const sizes = useMemo(() => {
608
+ const leftPct = `${posResolved * 100}%`;
609
+ const rightCalc = `calc(${100 - posResolved * 100}% - ${splitterSize}px)`;
610
+ return { leftPct, rightCalc };
611
+ }, [posResolved, splitterSize]);
612
+
613
+ const rootStyle = {
614
+ display: "flex",
615
+ width: "100%",
616
+ height: "100%",
617
+ overflow: "hidden",
618
+ touchAction: "none",
619
+ ...(style || {}),
620
+ flexDirection: internalMode === "horizontal" ? "row" : "column",
621
+ };
622
+
623
+ const leftPaneComputed =
624
+ internalMode === "horizontal"
625
+ ? { width: sizes.leftPct, flex: `0 0 ${sizes.leftPct}`, overflow: "auto", ...(leftPaneStyle || {}) }
626
+ : { height: sizes.leftPct, flex: `0 0 ${sizes.leftPct}`, overflow: "auto", ...(leftPaneStyle || {}) };
627
+
628
+ const rightPaneComputed =
629
+ internalMode === "horizontal"
630
+ ? { width: sizes.rightCalc, flex: "1 1 auto", overflow: "auto", ...(rightPaneStyle || {}) }
631
+ : { height: sizes.rightCalc, flex: "1 1 auto", overflow: "auto", ...(rightPaneStyle || {}) };
632
+
633
+ const splitterComputed =
634
+ internalMode === "horizontal"
635
+ ? { width: `${splitterSize}px`, transform: "translateX(-50%)", height: '100%', flex: `0 0 ${splitterSize}px`, background: "rgba(0,0,0,0.08)", cursor: "col-resize", ...(splitterStyle || {}) }
636
+ : { height: `${splitterSize}px`, transform: "translateY(-50%)", width: '100%', flex: `0 0 ${splitterSize}px`, background: "rgba(0,0,0,0.08)", cursor: "row-resize", ...(splitterStyle || {}) };
637
+
638
+ const dividerVisualStyle =
639
+ internalMode === "horizontal"
640
+ ? { height: "100%", width: "100%" }
641
+ : { width: "100%", height: "100%" };
642
+
643
+ useImperativeHandle(ref, () => ({
644
+ setPosition,
645
+ setMode: (nextMode) => setInternalMode(nextMode === "vertical" ? "vertical" : "horizontal"),
646
+ getPosition: () => posResolved,
647
+ getMode: () => internalMode,
648
+ }));
649
+
650
+ return (
651
+ <div ref={containerRef} className={className} style={rootStyle}>
652
+ <div style={leftPaneComputed}>{leftChild}</div>
653
+
654
+ <div
655
+ role="separator"
656
+ aria-orientation={internalMode === "horizontal" ? "vertical" : "horizontal"}
657
+ aria-valuemin={min}
658
+ aria-valuemax={max}
659
+ aria-valuenow={posResolved}
660
+ tabIndex={0}
661
+ onPointerDown={onPointerDown}
662
+ onPointerCancel={() => {
663
+ draggingRef.current = false;
664
+ pointerIdRef.current = null;
665
+ }}
666
+ onKeyDown={onKeyDown}
667
+ style={splitterComputed}
668
+ >
669
+ <div style={dividerVisualStyle} />
670
+ </div>
671
+
672
+ <div style={rightPaneComputed}>{rightChild}</div>
673
+ </div>
674
+ );
675
+ });
676
+
677
+
package/src/lilact.jsx CHANGED
@@ -63,7 +63,6 @@ import * as misc from './misc.jsx';
63
63
  import * as errors from './errors.jsx';
64
64
  import * as router from './router.jsx';
65
65
  import * as accessories from './accessories.jsx';
66
- import {SplitPane} from './pane.jsx';
67
66
  import {transpileJSX, transpilerConfig} from "./jsx";
68
67
 
69
68
 
@@ -80,7 +79,6 @@ export * from './misc.jsx';
80
79
  export * from './errors.jsx';
81
80
  export * from './router.jsx';
82
81
  export * from './accessories.jsx';
83
- export {SplitPane} from './pane.jsx';
84
82
  export {transpileJSX, transpilerConfig} from "./jsx";
85
83
 
86
84
 
@@ -117,8 +115,6 @@ export const Lilact =
117
115
  ...router,
118
116
  ...accessories,
119
117
 
120
- SplitPane,
121
-
122
118
  transpileJSX,
123
119
  transpilerConfig,
124
120
 
package/typedoc.json CHANGED
@@ -11,7 +11,6 @@
11
11
  ".tmp/accessories.js",
12
12
  ".tmp/components.js",
13
13
  ".tmp/errors.js",
14
- ".tmp/pane.js",
15
14
  // ".tmp/events.js",
16
15
  ".tmp/hooks.js",
17
16
  ".tmp/misc.js",
@@ -1 +0,0 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>pane | Lilact</title><meta name="description" content="Documentation for Lilact"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script><script async src="../assets/hierarchy.js" id="tsd-hierarchy-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">Lilact</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">pane</a></li></ul><h1>Module pane</h1></div><details class="tsd-panel-group tsd-member-group tsd-accordion" open><summary class="tsd-accordion-summary" data-key="section-Variables"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h2>Variables</h2></summary><dl class="tsd-member-summaries"><dt class="tsd-member-summary" id="splitpane"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Variable"><use href="../assets/icons.svg#icon-32"></use></svg><a href="../variables/pane.SplitPane.html">SplitPane</a><a href="#splitpane" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd></dl></details></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><details open class="tsd-accordion tsd-page-navigation-section"><summary class="tsd-accordion-summary" data-key="section-Variables"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Variables</summary><div><a href="#splitpane"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Variable"><use href="../assets/icons.svg#icon-32"></use></svg><span>Split<wbr/>Pane</span></a></div></details></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">Lilact</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
package/src/pane.jsx DELETED
@@ -1,270 +0,0 @@
1
- import { useEffect, useLayoutEffect, useMemo, useRef, useState, useImperativeHandle } from "./hooks.jsx";
2
- import { Children, forwardRef } from "./misc.jsx";
3
-
4
-
5
- const clamp = (n, min, max) => {
6
- if (!Number.isFinite(n)) return n;
7
- return Math.min(max, Math.max(min, n));
8
- };
9
-
10
- /**
11
- * A split-pane container with a draggable splitter, supporting both horizontal and vertical layouts.
12
- *
13
- * The pane can be either:
14
- * - **Controlled** via the `position` prop (number between `min` and `max`), or
15
- * - **Uncontrolled** via `defaultPosition` (used as the initial position).
16
- *
17
- * Layout behavior:
18
- * - `mode="horizontal"`: the `position` controls the width of the **left** pane.
19
- * - `mode="vertical"`: the `position` controls the height of the **top** pane.
20
- *
21
- * Ref API:
22
- * - Exposes imperative methods on `ref.current`:
23
- * - `getMode()` to get the current mode
24
- * - `setMode(mode)` to switch between `"horizontal"` and `"vertical"`
25
- * - `getPosition()` to get the current splitter position
26
- * - `setPosition(position)` to update the splitter position
27
- *
28
- * Events:
29
- * - Calls `onSizeChange(position)` whenever the pane size/position changes (e.g., via dragging).
30
- *
31
- * Rendering:
32
- * - Renders `children` into two separate containers (no portals).
33
- *
34
- * @param mode - Split direction: `"horizontal"` or `"vertical"`. Defaults to `"horizontal"`.
35
- * @param position - Controlled splitter position. Normalized value within `[min, max]`.
36
- * If provided, the component uses this value instead of internal state.
37
- * @param defaultPosition - Initial splitter position for uncontrolled usage. Defaults to `0.5`.
38
- * @param min - Minimum allowed position. Defaults to `0.1`.
39
- * @param max - Maximum allowed position. Defaults to `0.9`.
40
- * @param splitterSize - Thickness of the draggable splitter in pixels. Defaults to `8`.
41
- * @param onSizeChange - Callback invoked when the position changes. Receives the new normalized position.
42
- * @param style - Optional root container styles.
43
- * @param className - Optional root container CSS class.
44
- * @param leftPaneStyle - Optional styles applied to the left pane (or top pane in vertical mode).
45
- * @param rightPaneStyle - Optional styles applied to the right pane (or bottom pane in vertical mode).
46
- * @param splitterStyle - Optional styles applied to the splitter element.
47
- * @param children - React children to be rendered into the two pane containers.
48
- *
49
- * @example
50
- * ```tsx
51
- * const ref = useRef<SplitPaneHandle>(null);
52
- *
53
- * <SplitPane
54
- * ref={ref}
55
- * mode="horizontal"
56
- * defaultPosition={0.5}
57
- * min={0.1}
58
- * max={0.9}
59
- * onSizeChange={(pos) => console.log(pos)}
60
- * >
61
- * <div /> <div />
62
- * </SplitPane>
63
- * ```
64
- */
65
- export const SplitPane = forwardRef(function SplitPane(
66
- {
67
- mode = "horizontal",
68
- position, // controlled: number | undefined/null
69
- defaultPosition = 0.5,
70
- min = 0.1,
71
- max = 0.9,
72
- splitterSize = 8,
73
- onSizeChange,
74
- style,
75
- className,
76
- leftPaneStyle,
77
- rightPaneStyle,
78
- splitterStyle,
79
- children,
80
- },
81
- ref
82
- ) {
83
- const containerRef = useRef(null);
84
- const panes = Children.toArray(children);
85
- const leftChild = panes[0] ?? null;
86
- const rightChild = panes[1] ?? null;
87
-
88
- const isControlled = position != null;
89
-
90
- const [internalMode, setInternalMode] = useState(mode);
91
-
92
- const [posUncontrolled, setPosUncontrolled] = useState(() =>
93
- clamp(
94
- position ?? defaultPosition,
95
- min,
96
- max
97
- )
98
- );
99
-
100
- useEffect(() => {
101
- if (mode != null) setInternalMode(mode);
102
- }, [mode]);
103
-
104
- // keep internal position clamped if min/max change (uncontrolled only)
105
- useLayoutEffect(() => {
106
- if (isControlled) return;
107
- setPosUncontrolled((p) => clamp(p, min, max));
108
- }, [min, max, isControlled]);
109
-
110
- const posResolved = isControlled ? clamp(position, min, max) : posUncontrolled;
111
-
112
- const setPosition = (next) => {
113
- const clamped = clamp(next, min, max);
114
- if (!isControlled) setPosUncontrolled(clamped);
115
- onSizeChange?.(clamped);
116
- };
117
-
118
- const draggingRef = useRef(false);
119
- const pointerIdRef = useRef(null);
120
-
121
- const updateFromClient = (clientX, clientY) => {
122
- const el = containerRef.current;
123
- if (!el) return;
124
-
125
- const rect = el.getBoundingClientRect();
126
-
127
- if (internalMode === "horizontal") {
128
- const usable = rect.width;
129
- if (!Number.isFinite(usable) || usable <= 0) return; // no jump on init
130
- const raw = (clientX - rect.left) / usable;
131
- if (!Number.isFinite(raw)) return;
132
- setPosition(raw);
133
- } else {
134
- const usable = rect.height;
135
- if (!Number.isFinite(usable) || usable <= 0) return; // no jump on init
136
- const raw = (clientY - rect.top) / usable;
137
- if (!Number.isFinite(raw)) return;
138
- setPosition(raw);
139
- }
140
- };
141
-
142
- // stable global listeners: only act while draggingRef.current === true
143
- useEffect(() => {
144
- const onMove = (e) => {
145
- if (!draggingRef.current) return;
146
- if (pointerIdRef.current != null && e.pointerId !== pointerIdRef.current) return;
147
- updateFromClient(e.clientX, e.clientY);
148
- };
149
-
150
- const stop = (e) => {
151
- if (!draggingRef.current) return;
152
- if (pointerIdRef.current != null && e.pointerId !== pointerIdRef.current) return;
153
- draggingRef.current = false;
154
- pointerIdRef.current = null;
155
- };
156
-
157
- window.addEventListener("pointermove", onMove, { passive: true });
158
- window.addEventListener("pointerup", stop, { passive: true });
159
- window.addEventListener("pointercancel", stop, { passive: true });
160
-
161
- return () => {
162
- window.removeEventListener("pointermove", onMove);
163
- window.removeEventListener("pointerup", stop);
164
- window.removeEventListener("pointercancel", stop);
165
- };
166
- }, [internalMode]); // updateFromClient uses internalMode
167
-
168
- const onPointerDown = (e) => {
169
- if (e.button != null && e.button !== 0) return;
170
- e.preventDefault();
171
-
172
- draggingRef.current = true;
173
- pointerIdRef.current = e.pointerId;
174
-
175
- try {
176
- e.currentTarget.setPointerCapture?.(e.pointerId);
177
- } catch {}
178
-
179
- updateFromClient(e.clientX, e.clientY); // first update only if rect is sane
180
- };
181
-
182
- const onKeyDown = (e) => {
183
- const step = 0.02;
184
- let delta = 0;
185
-
186
- if (internalMode === "horizontal") {
187
- if (e.key === "ArrowLeft") delta = -step;
188
- if (e.key === "ArrowRight") delta = step;
189
- } else {
190
- if (e.key === "ArrowUp") delta = -step;
191
- if (e.key === "ArrowDown") delta = step;
192
- }
193
-
194
- if (delta !== 0) {
195
- e.preventDefault();
196
- setPosition(posResolved + delta);
197
- }
198
- };
199
-
200
- const sizes = useMemo(() => {
201
- const leftPct = `${posResolved * 100}%`;
202
- const rightCalc = `calc(${100 - posResolved * 100}% - ${splitterSize}px)`;
203
- return { leftPct, rightCalc };
204
- }, [posResolved, splitterSize]);
205
-
206
- const rootStyle = {
207
- display: "flex",
208
- width: "100%",
209
- height: "100%",
210
- overflow: "hidden",
211
- touchAction: "none",
212
- ...(style || {}),
213
- flexDirection: internalMode === "horizontal" ? "row" : "column",
214
- };
215
-
216
- const leftPaneComputed =
217
- internalMode === "horizontal"
218
- ? { width: sizes.leftPct, flex: `0 0 ${sizes.leftPct}`, overflow: "auto", ...(leftPaneStyle || {}) }
219
- : { height: sizes.leftPct, flex: `0 0 ${sizes.leftPct}`, overflow: "auto", ...(leftPaneStyle || {}) };
220
-
221
- const rightPaneComputed =
222
- internalMode === "horizontal"
223
- ? { width: sizes.rightCalc, flex: "1 1 auto", overflow: "auto", ...(rightPaneStyle || {}) }
224
- : { height: sizes.rightCalc, flex: "1 1 auto", overflow: "auto", ...(rightPaneStyle || {}) };
225
-
226
- const splitterComputed =
227
- internalMode === "horizontal"
228
- ? { width: `${splitterSize}px`, transform: "translateX(-50%)", height: '100%', flex: `0 0 ${splitterSize}px`, background: "rgba(0,0,0,0.08)", cursor: "col-resize", ...(splitterStyle || {}) }
229
- : { height: `${splitterSize}px`, transform: "translateY(-50%)", width: '100%', flex: `0 0 ${splitterSize}px`, background: "rgba(0,0,0,0.08)", cursor: "row-resize", ...(splitterStyle || {}) };
230
-
231
- const dividerVisualStyle =
232
- internalMode === "horizontal"
233
- ? { height: "100%", width: "100%" }
234
- : { width: "100%", height: "100%" };
235
-
236
- useImperativeHandle(ref, () => ({
237
- setPosition,
238
- setMode: (nextMode) => setInternalMode(nextMode === "vertical" ? "vertical" : "horizontal"),
239
- getPosition: () => posResolved,
240
- getMode: () => internalMode,
241
- }));
242
-
243
- return (
244
- <div ref={containerRef} className={className} style={rootStyle}>
245
- <div style={leftPaneComputed}>{leftChild}</div>
246
-
247
- <div
248
- role="separator"
249
- aria-orientation={internalMode === "horizontal" ? "vertical" : "horizontal"}
250
- aria-valuemin={min}
251
- aria-valuemax={max}
252
- aria-valuenow={posResolved}
253
- tabIndex={0}
254
- onPointerDown={onPointerDown}
255
- onPointerCancel={() => {
256
- draggingRef.current = false;
257
- pointerIdRef.current = null;
258
- }}
259
- onKeyDown={onKeyDown}
260
- style={splitterComputed}
261
- >
262
- <div style={dividerVisualStyle} />
263
- </div>
264
-
265
- <div style={rightPaneComputed}>{rightChild}</div>
266
- </div>
267
- );
268
- });
269
-
270
-