react-headless-dock-layout 0.4.2 → 0.4.3

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.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2;function l(i){throw new Error(`Unexpected value: ${i}`)}function N(i,t){return i===null?null:x(t,i)}function x(i,t){if(t.type==="panel")return null;if(t.type==="split")return t.left.id===i||t.right.id===i?t:_nullishCoalesce(x(i,t.left), () => (x(i,t.right)));l(t)}var M={getPlacement(i){let t=g(i)+1;return{targetId:i.id,direction:"right",ratio:t/(t+1)}}};function g(i){if(i.type==="panel")return 0;if(i.type==="split"){if(i.orientation==="horizontal")return 1+g(i.left)+g(i.right);if(i.orientation==="vertical")return g(i.left)+g(i.right);l(i.orientation)}else l(i)}var G={getPlacement(i){let t=z(i),e=N(i,t.id);return{targetId:t.id,direction:e===null?"right":e.orientation==="horizontal"?"bottom":"right",ratio:.5}}};function z(i){if(i.type==="panel")return i;if(i.type==="split")return z(i.right);l(i)}var _react = require('react');function m(i,t,e){return Math.max(t,Math.min(e,i))}function w(){return typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():Date.now().toString(36)}function u(i,t){if(!i)throw new Error(_nullishCoalesce(t, () => ("Invariant failed")))}function E(i,t){if(i===null)return[];let e=[],r=(o,n)=>{o.type==="split"?o.orientation==="horizontal"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x+n.width*o.ratio-t.gap/2),y:Math.round(n.y),width:Math.round(t.gap),height:Math.round(n.height)}),r(o.left,{x:n.x,y:n.y,width:n.width*o.ratio-t.gap/2,height:n.height}),r(o.right,{x:n.x+n.width*o.ratio+t.gap/2,y:n.y,width:n.width*(1-o.ratio)-t.gap/2,height:n.height})):o.orientation==="vertical"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x),y:Math.round(n.y+n.height*o.ratio-t.gap/2),width:Math.round(n.width),height:Math.round(t.gap)}),r(o.left,{x:n.x,y:n.y,width:n.width,height:n.height*o.ratio-t.gap/2}),r(o.right,{x:n.x,y:n.y+n.height*o.ratio+t.gap/2,width:n.width,height:n.height*(1-o.ratio)-t.gap/2})):l(o.orientation):o.type==="panel"?e.push({id:o.id,type:"panel",x:Math.round(n.x),y:Math.round(n.y),width:Math.round(n.width),height:Math.round(n.height)}):l(o)};return r(i,{x:0,y:0,width:t.size.width,height:t.size.height}),e}function f(i,t){if(i.type==="panel")return{width:_nullishCoalesce(_optionalChain([i, 'access', _2 => _2.minSize, 'optionalAccess', _3 => _3.width]), () => (0)),height:_nullishCoalesce(_optionalChain([i, 'access', _4 => _4.minSize, 'optionalAccess', _5 => _5.height]), () => (0))};if(i.type==="split"){if(i.orientation==="horizontal")return{width:f(i.left,t).width+t+f(i.right,t).width,height:Math.max(f(i.left,t).height,f(i.right,t).height)};if(i.orientation==="vertical")return{width:Math.max(f(i.left,t).width,f(i.right,t).width),height:f(i.left,t).height+t+f(i.right,t).height};l(i.orientation)}else l(i)}function P(i,t){let e=i.x+i.width/2,r=i.y+i.height/2,o=(t.x-e)/(i.width/2),n=(t.y-r)/(i.height/2);return Math.abs(o)>Math.abs(n)?o>0?"right":"left":n>0?"bottom":"top"}var v= (_class =class{__init() {this._root=null}constructor(t){;_class.prototype.__init.call(this);this._root=t}get root(){return this._root}set root(t){this._root=t}findNode(t){return this._root===null?null:this.findNodeInSubTree(t,this._root)}findNodeInSubTree(t,e){if(t===e.id)return e;if(e.type==="panel")return null;if(e.type==="split")return _nullishCoalesce(this.findNodeInSubTree(t,e.left), () => (this.findNodeInSubTree(t,e.right)));l(e)}findParentNode(t){return N(this._root,t)}replaceChildNode({parent:t,oldChildId:e,newChild:r}){if(this.findNode(e)===null)throw new Error(`Child node with id ${e} not found`);if(t.left.id===e)t.left=r;else if(t.right.id===e)t.right=r;else throw new Error(`Child node with id ${e} is not a child of the parent node with id ${t.id}`)}}, _class);var _= (_class2 =class{__init2() {this.MIN_RESIZE_RATIO=.1}__init3() {this.MAX_RESIZE_RATIO=.9}__init4() {this._listeners=new Set}__init5() {this._layoutRects=[]}__init6() {this._addPanelStrategy=M}constructor(t,e){;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);_class2.prototype.__init6.call(this);_class2.prototype.__init7.call(this);this._tree=new v(t),this._options={gap:_nullishCoalesce(_optionalChain([e, 'optionalAccess', _6 => _6.gap]), () => (10)),size:_nullishCoalesce(_optionalChain([e, 'optionalAccess', _7 => _7.size]), () => ({width:0,height:0}))},this._layoutRects=E(t,this._options)}get root(){return this._tree.root}set root(t){this._tree.root=t,this.syncLayoutRects()}get layoutRects(){return this._layoutRects}__init7() {this.subscribe=t=>(this._listeners.add(t),()=>{this._listeners.delete(t)})}setSize(t){this._options.size=t,this.syncLayoutRects()}removePanel(t){if(this._tree.root===null)throw new Error("Root node is null");let e=this._tree.findNode(t);if(e===null)throw new Error(`Node with id ${t} not found`);if(e.type!=="panel")throw new Error(`Node with id ${t} is not a panel`);if(e.id===this._tree.root.id){this._tree.root=null,this.syncLayoutRects();return}let r=this._tree.findParentNode(t);u(r!==null,"Parent node is not null");let o=r.left.id===e.id?r.right:r.left;if(r.id===this._tree.root.id){this._tree.root=o,this.syncLayoutRects();return}let n=this._tree.findParentNode(r.id);u(n!==null,"Grand parent node is not null"),this._tree.replaceChildNode({parent:n,oldChildId:r.id,newChild:o}),this.syncLayoutRects()}movePanel({sourceId:t,targetId:e,point:r}){if(this._tree.root===null)throw new Error("Root node is null");if(this._tree.root.type!=="split")throw new Error("Root node is not a split node");let o=this._tree.findNode(t);if(o===null)throw new Error(`Node with id ${t} not found`);if(o.type!=="panel")throw new Error(`Node with id ${t} is not a panel node`);let n=this._tree.findParentNode(t);u(n!==null);let a=this._tree.findNode(e);if(a===null)throw new Error(`Node with id ${e} not found`);if(a.type!=="panel")throw new Error(`Node with id ${e} is not a panel node`);let p=n.left.id===t?n.right:n.left,d=this.findRect(e);u(d!==null),u(d.type==="panel");let c=P(d,r);if(p.id===e){c==="left"?(n.orientation="horizontal",n.left=o,n.right=a):c==="right"?(n.orientation="horizontal",n.left=a,n.right=o):c==="top"?(n.orientation="vertical",n.left=o,n.right=a):c==="bottom"?(n.orientation="vertical",n.left=a,n.right=o):l(c),this.syncLayoutRects();return}let y=this._tree.findParentNode(n.id);y===null?this._tree.root=p:this._tree.replaceChildNode({parent:y,oldChildId:n.id,newChild:p});let s=this._tree.findParentNode(e);u(s!==null);let h=this.createSplitNode({direction:c,sourceNode:o,targetNode:a});this._tree.replaceChildNode({parent:s,oldChildId:e,newChild:h}),this.syncLayoutRects()}resizePanel(t,e){if(this._tree.root===null)throw new Error("Root node is null");let r=this.findRect(t);if(r===null)throw new Error(`Rect with id ${t} not found`);if(r.type!=="split")throw new Error(`Rect with id ${t} is not a split node`);let o=this._tree.findNode(t);u(o!==null,"Split node is not null"),u(o.type==="split","Split node is a split"),o.ratio=this.calculateResizeRatio(o,r,e),this.syncLayoutRects()}addPanel(t){if(this._tree.root===null){this._tree.root={id:t,type:"panel"},this.syncLayoutRects();return}let{targetId:e,direction:r,ratio:o=.5}=this._addPanelStrategy.getPlacement(this._tree.root);if(e===this._tree.root.id){this._tree.root=this.createSplitNode({direction:r,ratio:o,sourceNode:{id:t,type:"panel"},targetNode:this._tree.root}),this.syncLayoutRects();return}let n=this._tree.findNode(e);if(n===null)throw new Error(`Node with id ${e} not found`);let a=this._tree.findParentNode(e);u(a!==null,"Target node parent is not null");let p=this.createSplitNode({direction:r,sourceNode:{id:t,type:"panel"},targetNode:n,ratio:o});this._tree.replaceChildNode({parent:a,oldChildId:e,newChild:p}),this.syncLayoutRects()}calculateDropTarget({draggedPanelId:t,targetPanelId:e,point:r}){u(t!==e,"Dragged panel id is not the same as target panel id");let o=this.findRect(e);return u(o!==null&&o.type==="panel"),{id:e,direction:P(o,r)}}emit(){this._listeners.forEach(t=>{t()})}syncLayoutRects(){this._layoutRects=E(this._tree.root,this._options),this.emit()}createSplitNode({direction:t,sourceNode:e,targetNode:r,ratio:o=.5}){switch(t){case"left":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:e,right:r};case"right":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:r,right:e};case"top":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:e,right:r};case"bottom":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:r,right:e};default:l(t)}}findRect(t){return _nullishCoalesce(this._layoutRects.find(e=>e.id===t), () => (null))}getSurroundingRect(t){let e=this._tree.findNode(t);if(u(e!==null,"Node is not null"),e.type==="panel"){let r=this.findRect(t);return u(r!==null,"Rect is not null"),u(r.type==="panel","Rect is a panel"),{x:r.x,y:r.y,width:r.width,height:r.height}}else if(e.type==="split"){let r=this.getSurroundingRect(e.left.id),o=this.getSurroundingRect(e.right.id);if(e.orientation==="horizontal")return{x:r.x,y:r.y,width:r.width+this._options.gap+o.width,height:r.height};if(e.orientation==="vertical")return{x:r.x,y:r.y,width:r.width,height:r.height+this._options.gap+o.height};l(e.orientation)}else l(e)}calculateResizeRatio(t,e,r){if(e.orientation==="horizontal"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.x-o.x,p=m(a/(o.width+e.width+n.width),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.width+this._options.gap+n.width,y=(f(t.left,this._options.gap).width+this._options.gap/2)/d,s=f(t.right,this._options.gap).width,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else if(e.orientation==="vertical"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.y-o.y,p=m(a/(o.height+e.height+n.height),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.height+this._options.gap+n.height,y=(f(t.left,this._options.gap).height+this._options.gap/2)/d,s=f(t.right,this._options.gap).height,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else l(e.orientation)}}, _class2);function D(i){_react.useEffect.call(void 0, ()=>{let t=document.body.style.cursor;return document.body.style.cursor=_nullishCoalesce(i, () => ("default")),()=>{document.body.style.cursor=t}},[i])}function L(i){let t=_react.useRef.call(void 0, i);return _react.useEffect.call(void 0, ()=>{t.current=i},[i]),_react.useCallback.call(void 0, (...e)=>t.current(...e),[])}function b(i,t,e,r){let o=L(e);_react.useEffect.call(void 0, ()=>(i.addEventListener(t,o,r),()=>{i.removeEventListener(t,o,r)}),[i,t,o,r])}function T(i){let t=_react.useRef.call(void 0, null),e=L(i);return _react.useEffect.call(void 0, ()=>{let r=t.current;if(r===null)throw new Error("Ref is not attached to an element");let o=new ResizeObserver(n=>{for(let a of n)e(a)});return o.observe(r),()=>{o.disconnect()}},[e]),t}function Ct(i,t){let[e]=_react.useState.call(void 0, ()=>{let s=typeof i=="function"?i():i;return new _(s,t)}),r=_react.useSyncExternalStore.call(void 0, e.subscribe,()=>e.layoutRects),[o,n]=_react.useState.call(void 0, null),[a,p]=_react.useState.call(void 0, null),[d,c]=_react.useState.call(void 0, null),y=T(s=>{e.setSize({width:s.contentRect.width,height:s.contentRect.height})});return b(document,"mousemove",s=>{if(o===null)return;let h=y.current;if(h===null)throw new Error("containerRef is not attached to an element");let R=h.getBoundingClientRect();e.resizePanel(o.id,{x:s.clientX-R.left,y:s.clientY-R.top})}),b(document,"mouseup",()=>{o!==null&&n(null)}),D(o===null?"default":C[o.orientation]),{containerRef:y,layoutRects:r,getRectProps:s=>{if(s.type==="split")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height,cursor:C[s.orientation]},onMouseDown:()=>{n(s)}};if(s.type==="panel")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height},onMouseMove:h=>{if(a===null)return;if(a.id===s.id){c(null);return}let R=e.calculateDropTarget({draggedPanelId:a.id,targetPanelId:s.id,point:{x:h.clientX,y:h.clientY}});c(R)},onMouseUp:h=>{if(a!==null){if(a.id===s.id){p(null),c(null);return}e.movePanel({sourceId:a.id,targetId:s.id,point:{x:h.clientX,y:h.clientY}}),p(null),c(null)}}};l(s)},getDropIndicatorProps:s=>a===null||!(s.id===_optionalChain([d, 'optionalAccess', _8 => _8.id]))?null:{style:U(d.direction)},getDragHandleProps:s=>({onMouseDown:()=>{p(s)}}),draggingRect:a,addPanel:e.addPanel.bind(e),removePanel:e.removePanel.bind(e),root:e.root}}function U(i){if(i==="top")return{position:"absolute",left:0,top:0,width:"100%",height:"50%"};if(i==="bottom")return{position:"absolute",left:0,top:"50%",width:"100%",height:"50%"};if(i==="left")return{position:"absolute",left:0,top:0,width:"50%",height:"100%"};if(i==="right")return{position:"absolute",left:"50%",top:0,width:"50%",height:"100%"};l(i)}var C={horizontal:"col-resize",vertical:"row-resize"};exports.bspStrategy = G; exports.evenlyDividedHorizontalStrategy = M; exports.useDockLayout = Ct;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2;function l(i){throw new Error(`Unexpected value: ${i}`)}var P={getPlacementOnAdd(i){let t=g(i)+1;return{targetId:i.id,direction:"right",ratio:t/(t+1)}}};function g(i){if(i.type==="panel")return 0;if(i.type==="split"){if(i.orientation==="horizontal")return 1+g(i.left)+g(i.right);if(i.orientation==="vertical")return g(i.left)+g(i.right);l(i.orientation)}else l(i)}var _react = require('react');function m(i,t,e){return Math.max(t,Math.min(e,i))}function w(){return typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():Date.now().toString(36)}function u(i,t){if(!i)throw new Error(_nullishCoalesce(t, () => ("Invariant failed")))}function S(i,t){if(i===null)return[];let e=[],r=(o,n)=>{o.type==="split"?o.orientation==="horizontal"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x+n.width*o.ratio-t.gap/2),y:Math.round(n.y),width:Math.round(t.gap),height:Math.round(n.height)}),r(o.left,{x:n.x,y:n.y,width:n.width*o.ratio-t.gap/2,height:n.height}),r(o.right,{x:n.x+n.width*o.ratio+t.gap/2,y:n.y,width:n.width*(1-o.ratio)-t.gap/2,height:n.height})):o.orientation==="vertical"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x),y:Math.round(n.y+n.height*o.ratio-t.gap/2),width:Math.round(n.width),height:Math.round(t.gap)}),r(o.left,{x:n.x,y:n.y,width:n.width,height:n.height*o.ratio-t.gap/2}),r(o.right,{x:n.x,y:n.y+n.height*o.ratio+t.gap/2,width:n.width,height:n.height*(1-o.ratio)-t.gap/2})):l(o.orientation):o.type==="panel"?e.push({id:o.id,type:"panel",x:Math.round(n.x),y:Math.round(n.y),width:Math.round(n.width),height:Math.round(n.height)}):l(o)};return r(i,{x:0,y:0,width:t.size.width,height:t.size.height}),e}function f(i,t){if(i.type==="panel")return{width:_nullishCoalesce(_optionalChain([i, 'access', _2 => _2.minSize, 'optionalAccess', _3 => _3.width]), () => (0)),height:_nullishCoalesce(_optionalChain([i, 'access', _4 => _4.minSize, 'optionalAccess', _5 => _5.height]), () => (0))};if(i.type==="split"){if(i.orientation==="horizontal")return{width:f(i.left,t).width+t+f(i.right,t).width,height:Math.max(f(i.left,t).height,f(i.right,t).height)};if(i.orientation==="vertical")return{width:Math.max(f(i.left,t).width,f(i.right,t).width),height:f(i.left,t).height+t+f(i.right,t).height};l(i.orientation)}else l(i)}function x(i,t){let e=i.x+i.width/2,r=i.y+i.height/2,o=(t.x-e)/(i.width/2),n=(t.y-r)/(i.height/2);return Math.abs(o)>Math.abs(n)?o>0?"right":"left":n>0?"bottom":"top"}function M(i,t){return i===null?null:E(t,i)}function E(i,t){if(t.type==="panel")return null;if(t.type==="split")return t.left.id===i||t.right.id===i?t:_nullishCoalesce(E(i,t.left), () => (E(i,t.right)));l(t)}var N= (_class =class{__init() {this._root=null}constructor(t){;_class.prototype.__init.call(this);this._root=t}get root(){return this._root}set root(t){this._root=t}findNode(t){return this._root===null?null:this.findNodeInSubTree(t,this._root)}findNodeInSubTree(t,e){if(t===e.id)return e;if(e.type==="panel")return null;if(e.type==="split")return _nullishCoalesce(this.findNodeInSubTree(t,e.left), () => (this.findNodeInSubTree(t,e.right)));l(e)}findParentNode(t){return M(this._root,t)}replaceChildNode({parent:t,oldChildId:e,newChild:r}){if(this.findNode(e)===null)throw new Error(`Child node with id ${e} not found`);if(t.left.id===e)t.left=r;else if(t.right.id===e)t.right=r;else throw new Error(`Child node with id ${e} is not a child of the parent node with id ${t.id}`)}}, _class);var v= (_class2 =class{__init2() {this.MIN_RESIZE_RATIO=.1}__init3() {this.MAX_RESIZE_RATIO=.9}__init4() {this._listeners=new Set}__init5() {this._layoutRects=[]}constructor(t,e){;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);_class2.prototype.__init6.call(this);this._tree=new N(t),this._options={gap:_nullishCoalesce(_optionalChain([e, 'optionalAccess', _6 => _6.gap]), () => (10)),size:_nullishCoalesce(_optionalChain([e, 'optionalAccess', _7 => _7.size]), () => ({width:0,height:0})),placementStrategy:_nullishCoalesce(_optionalChain([e, 'optionalAccess', _8 => _8.placementStrategy]), () => (P))},this._layoutRects=S(t,this._options)}get root(){return this._tree.root}set root(t){this._tree.root=t,this.syncLayoutRects()}get layoutRects(){return this._layoutRects}__init6() {this.subscribe=t=>(this._listeners.add(t),()=>{this._listeners.delete(t)})}setSize(t){this._options.size=t,this.syncLayoutRects()}removePanel(t){if(this._tree.root===null)throw new Error("Root node is null");let e=this._tree.findNode(t);if(e===null)throw new Error(`Node with id ${t} not found`);if(e.type!=="panel")throw new Error(`Node with id ${t} is not a panel`);if(e.id===this._tree.root.id){this._tree.root=null,this.syncLayoutRects();return}let r=this._tree.findParentNode(t);u(r!==null,"Parent node is not null");let o=r.left.id===e.id?r.right:r.left;if(r.id===this._tree.root.id){this._tree.root=o,this.syncLayoutRects();return}let n=this._tree.findParentNode(r.id);u(n!==null,"Grand parent node is not null"),this._tree.replaceChildNode({parent:n,oldChildId:r.id,newChild:o}),this.syncLayoutRects()}movePanel({sourceId:t,targetId:e,point:r}){if(this._tree.root===null)throw new Error("Root node is null");if(this._tree.root.type!=="split")throw new Error("Root node is not a split node");let o=this._tree.findNode(t);if(o===null)throw new Error(`Node with id ${t} not found`);if(o.type!=="panel")throw new Error(`Node with id ${t} is not a panel node`);let n=this._tree.findParentNode(t);u(n!==null);let a=this._tree.findNode(e);if(a===null)throw new Error(`Node with id ${e} not found`);if(a.type!=="panel")throw new Error(`Node with id ${e} is not a panel node`);let p=n.left.id===t?n.right:n.left,d=this.findRect(e);u(d!==null),u(d.type==="panel");let c=x(d,r);if(p.id===e){c==="left"?(n.orientation="horizontal",n.left=o,n.right=a):c==="right"?(n.orientation="horizontal",n.left=a,n.right=o):c==="top"?(n.orientation="vertical",n.left=o,n.right=a):c==="bottom"?(n.orientation="vertical",n.left=a,n.right=o):l(c),this.syncLayoutRects();return}let y=this._tree.findParentNode(n.id);y===null?this._tree.root=p:this._tree.replaceChildNode({parent:y,oldChildId:n.id,newChild:p});let s=this._tree.findParentNode(e);u(s!==null);let h=this.createSplitNode({direction:c,sourceNode:o,targetNode:a});this._tree.replaceChildNode({parent:s,oldChildId:e,newChild:h}),this.syncLayoutRects()}resizePanel(t,e){if(this._tree.root===null)throw new Error("Root node is null");let r=this.findRect(t);if(r===null)throw new Error(`Rect with id ${t} not found`);if(r.type!=="split")throw new Error(`Rect with id ${t} is not a split node`);let o=this._tree.findNode(t);u(o!==null,"Split node is not null"),u(o.type==="split","Split node is a split"),o.ratio=this.calculateResizeRatio(o,r,e),this.syncLayoutRects()}addPanel(t){if(this._tree.root===null){this._tree.root={id:t,type:"panel"},this.syncLayoutRects();return}let{targetId:e,direction:r,ratio:o=.5}=this._options.placementStrategy.getPlacementOnAdd(this._tree.root);if(e===this._tree.root.id){this._tree.root=this.createSplitNode({direction:r,ratio:o,sourceNode:{id:t,type:"panel"},targetNode:this._tree.root}),this.syncLayoutRects();return}let n=this._tree.findNode(e);if(n===null)throw new Error(`Node with id ${e} not found`);let a=this._tree.findParentNode(e);u(a!==null,"Target node parent is not null");let p=this.createSplitNode({direction:r,sourceNode:{id:t,type:"panel"},targetNode:n,ratio:o});this._tree.replaceChildNode({parent:a,oldChildId:e,newChild:p}),this.syncLayoutRects()}calculateDropTarget({draggedPanelId:t,targetPanelId:e,point:r}){u(t!==e,"Dragged panel id is not the same as target panel id");let o=this.findRect(e);return u(o!==null&&o.type==="panel"),{id:e,direction:x(o,r)}}emit(){this._listeners.forEach(t=>{t()})}syncLayoutRects(){this._layoutRects=S(this._tree.root,this._options),this.emit()}createSplitNode({direction:t,sourceNode:e,targetNode:r,ratio:o=.5}){switch(t){case"left":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:e,right:r};case"right":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:r,right:e};case"top":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:e,right:r};case"bottom":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:r,right:e};default:l(t)}}findRect(t){return _nullishCoalesce(this._layoutRects.find(e=>e.id===t), () => (null))}getSurroundingRect(t){let e=this._tree.findNode(t);if(u(e!==null,"Node is not null"),e.type==="panel"){let r=this.findRect(t);return u(r!==null,"Rect is not null"),u(r.type==="panel","Rect is a panel"),{x:r.x,y:r.y,width:r.width,height:r.height}}else if(e.type==="split"){let r=this.getSurroundingRect(e.left.id),o=this.getSurroundingRect(e.right.id);if(e.orientation==="horizontal")return{x:r.x,y:r.y,width:r.width+this._options.gap+o.width,height:r.height};if(e.orientation==="vertical")return{x:r.x,y:r.y,width:r.width,height:r.height+this._options.gap+o.height};l(e.orientation)}else l(e)}calculateResizeRatio(t,e,r){if(e.orientation==="horizontal"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.x-o.x,p=m(a/(o.width+e.width+n.width),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.width+this._options.gap+n.width,y=(f(t.left,this._options.gap).width+this._options.gap/2)/d,s=f(t.right,this._options.gap).width,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else if(e.orientation==="vertical"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.y-o.y,p=m(a/(o.height+e.height+n.height),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.height+this._options.gap+n.height,y=(f(t.left,this._options.gap).height+this._options.gap/2)/d,s=f(t.right,this._options.gap).height,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else l(e.orientation)}}, _class2);function z(i){_react.useEffect.call(void 0, ()=>{let t=document.body.style.cursor;return document.body.style.cursor=_nullishCoalesce(i, () => ("default")),()=>{document.body.style.cursor=t}},[i])}function _(i){let t=_react.useRef.call(void 0, i);return _react.useEffect.call(void 0, ()=>{t.current=i},[i]),_react.useCallback.call(void 0, (...e)=>t.current(...e),[])}function b(i,t,e,r){let o=_(e);_react.useEffect.call(void 0, ()=>(i.addEventListener(t,o,r),()=>{i.removeEventListener(t,o,r)}),[i,t,o,r])}function T(i){let t=_react.useRef.call(void 0, null),e=_(i);return _react.useEffect.call(void 0, ()=>{let r=t.current;if(r===null)throw new Error("Ref is not attached to an element");let o=new ResizeObserver(n=>{for(let a of n)e(a)});return o.observe(r),()=>{o.disconnect()}},[e]),t}function zt(i,t){let[e]=_react.useState.call(void 0, ()=>{let s=typeof i=="function"?i():i;return new v(s,t)}),r=_react.useSyncExternalStore.call(void 0, e.subscribe,()=>e.layoutRects),[o,n]=_react.useState.call(void 0, null),[a,p]=_react.useState.call(void 0, null),[d,c]=_react.useState.call(void 0, null),y=T(s=>{e.setSize({width:s.contentRect.width,height:s.contentRect.height})});return b(document,"mousemove",s=>{if(o===null)return;let h=y.current;if(h===null)throw new Error("containerRef is not attached to an element");let R=h.getBoundingClientRect();e.resizePanel(o.id,{x:s.clientX-R.left,y:s.clientY-R.top})}),b(document,"mouseup",()=>{o!==null&&n(null)}),z(o===null?"default":D[o.orientation]),{containerRef:y,layoutRects:r,getRectProps:s=>{if(s.type==="split")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height,cursor:D[s.orientation]},onMouseDown:()=>{n(s)}};if(s.type==="panel")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height},onMouseMove:h=>{if(a===null)return;if(a.id===s.id){c(null);return}let R=e.calculateDropTarget({draggedPanelId:a.id,targetPanelId:s.id,point:{x:h.clientX,y:h.clientY}});c(R)},onMouseUp:h=>{if(a!==null){if(a.id===s.id){p(null),c(null);return}e.movePanel({sourceId:a.id,targetId:s.id,point:{x:h.clientX,y:h.clientY}}),p(null),c(null)}}};l(s)},getDropIndicatorProps:s=>a===null||!(s.id===_optionalChain([d, 'optionalAccess', _9 => _9.id]))?null:{style:K(d.direction)},getDragHandleProps:s=>({onMouseDown:()=>{p(s)}}),draggingRect:a,addPanel:e.addPanel.bind(e),removePanel:e.removePanel.bind(e),root:e.root}}function K(i){if(i==="top")return{position:"absolute",left:0,top:0,width:"100%",height:"50%"};if(i==="bottom")return{position:"absolute",left:0,top:"50%",width:"100%",height:"50%"};if(i==="left")return{position:"absolute",left:0,top:0,width:"50%",height:"100%"};if(i==="right")return{position:"absolute",left:"50%",top:0,width:"50%",height:"100%"};l(i)}var D={horizontal:"col-resize",vertical:"row-resize"};exports.equalWidthRightStrategy = P; exports.useDockLayout = zt;
package/dist/index.d.cts CHANGED
@@ -15,8 +15,23 @@ type Orientation = "horizontal" | "vertical";
15
15
  type Direction = "top" | "bottom" | "left" | "right";
16
16
 
17
17
  interface LayoutManagerOptions {
18
+ /**
19
+ * Initial size of the container in pixels.
20
+ * If not provided, defaults to `{ width: 0, height: 0 }`.
21
+ * The size is automatically updated when the container is resized.
22
+ */
18
23
  size?: Size;
24
+ /**
25
+ * Gap between panels in pixels.
26
+ * This is the width/height of the split bar between panels.
27
+ * Defaults to `10` if not provided.
28
+ */
19
29
  gap?: number;
30
+ /**
31
+ * Strategy for determining where new panels are placed when added.
32
+ * Defaults to `equalWidthRightStrategy` if not provided.
33
+ */
34
+ placementStrategy?: PlacementStrategy;
20
35
  }
21
36
  interface PanelLayoutRect extends Pick<PanelNode, "id" | "type">, Rect {
22
37
  }
@@ -25,32 +40,129 @@ interface SplitLayoutRect extends Pick<SplitNode, "id" | "type" | "orientation">
25
40
  type LayoutRect = PanelLayoutRect | SplitLayoutRect;
26
41
  interface PanelNode {
27
42
  type: "panel";
43
+ /** Unique identifier for the panel. */
28
44
  id: string;
45
+ /**
46
+ * Optional minimum size constraints for the panel.
47
+ * If specified, the panel cannot be resized smaller than these dimensions.
48
+ */
29
49
  minSize?: Partial<Size>;
30
50
  }
31
51
  interface SplitNode {
32
52
  type: "split";
53
+ /** Unique identifier for the split node. */
33
54
  id: string;
55
+ /** Left child node (or top child for vertical splits). */
34
56
  left: LayoutNode;
57
+ /** Right child node (or bottom child for vertical splits). */
35
58
  right: LayoutNode;
59
+ /**
60
+ * Orientation of the split.
61
+ * - `"horizontal"`: splits left/right
62
+ * - `"vertical"`: splits top/bottom
63
+ */
36
64
  orientation: Orientation;
65
+ /**
66
+ * Ratio determining how space is divided between left and right children.
67
+ * Value between 0 and 1, where:
68
+ * - `0.5` means equal space
69
+ * - `< 0.5` means left child gets less space
70
+ * - `> 0.5` means left child gets more space
71
+ */
37
72
  ratio: number;
38
73
  }
39
74
  type LayoutNode = PanelNode | SplitNode;
40
75
 
41
- interface AddPanelStrategy {
42
- getPlacement(root: LayoutNode): {
76
+ interface PlacementStrategy {
77
+ /**
78
+ * Calculates the placement for a new panel based on the current layout tree.
79
+ *
80
+ * @param root - The root node of the current layout tree.
81
+ * @returns Placement configuration specifying:
82
+ * - `targetId`: The ID of the panel that will be split to make room for the new panel
83
+ * - `direction`: The direction in which to split (`"top"`, `"bottom"`, `"left"`, or `"right"`)
84
+ * - `ratio`: The ratio for dividing space (0-1, where 0.5 is equal split)
85
+ */
86
+ getPlacementOnAdd(root: LayoutNode): {
43
87
  targetId: string;
44
88
  direction: Direction;
45
89
  ratio: number;
46
90
  };
47
91
  }
48
- declare const evenlyDividedHorizontalStrategy: AddPanelStrategy;
49
- declare const bspStrategy: AddPanelStrategy;
92
+ /**
93
+ * Default placement strategy that adds panels to the right with equal widths.
94
+ * New panels are added by splitting the root panel horizontally, maintaining equal widths
95
+ * for all panels.
96
+ */
97
+ declare const equalWidthRightStrategy: PlacementStrategy;
50
98
 
99
+ /**
100
+ * Main hook for managing a dock layout.
101
+ *
102
+ * This hook provides all the necessary state and functions to create and manage
103
+ * a resizable, draggable dock layout system. It handles panel addition/removal,
104
+ * drag-and-drop, and resize operations.
105
+ *
106
+ * @param initialRoot - Initial layout tree root node. Can be:
107
+ * - `null` for an empty layout
108
+ * - A `LayoutNode` object
109
+ * - A function that returns `LayoutNode | null` (useful for lazy initialization)
110
+ * @param options - Optional configuration for the layout manager.
111
+ * @returns An object containing layout state and helper functions.
112
+ *
113
+ * @example
114
+ * ```tsx
115
+ * // Start with an empty layout
116
+ * const {
117
+ * containerRef,
118
+ * layoutRects,
119
+ * addPanel,
120
+ * removePanel,
121
+ * getRectProps,
122
+ * getDragHandleProps,
123
+ * } = useDockLayout<HTMLDivElement>(null);
124
+ * ```
125
+ *
126
+ * @example
127
+ * ```tsx
128
+ * // Load initial layout from localStorage
129
+ * const {
130
+ * containerRef,
131
+ * layoutRects,
132
+ * addPanel,
133
+ * removePanel,
134
+ * getRectProps,
135
+ * getDragHandleProps,
136
+ * } = useDockLayout<HTMLDivElement>(() => {
137
+ * const saved = localStorage.getItem("layout");
138
+ * if (saved === null) {
139
+ * return null;
140
+ * }
141
+ * return JSON.parse(saved);
142
+ * });
143
+ * ```
144
+ */
51
145
  declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode | null | (() => LayoutNode | null), options?: LayoutManagerOptions): {
146
+ /**
147
+ * Ref callback that must be attached to the container element.
148
+ * The container should have `position: relative` styling.
149
+ * The layout will automatically resize when the container size changes.
150
+ */
52
151
  containerRef: react.RefObject<T | null>;
152
+ /**
153
+ * Array of layout rectangles representing all panels and split bars.
154
+ * Each rectangle contains position, size, and type information.
155
+ * Use this to render your panels and split bars.
156
+ */
53
157
  layoutRects: LayoutRect[];
158
+ /**
159
+ * Returns props (style and event handlers) for a given layout rectangle.
160
+ * For split bars, returns style with cursor and onMouseDown handler for resizing.
161
+ * For panels, returns style and onMouseMove/onMouseUp handlers for drag-and-drop.
162
+ *
163
+ * @param rect - The layout rectangle to get props for.
164
+ * @returns An object with `style` and event handler props.
165
+ */
54
166
  getRectProps: (rect: LayoutRect) => {
55
167
  readonly style: {
56
168
  readonly position: "absolute";
@@ -76,6 +188,14 @@ declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode |
76
188
  readonly onMouseUp: (event: MouseEvent<T>) => void;
77
189
  readonly onMouseDown?: undefined;
78
190
  };
191
+ /**
192
+ * Returns props for rendering a drop indicator overlay on a panel.
193
+ * The drop indicator shows where a dragged panel will be placed.
194
+ * Returns `null` if no panel is being dragged or if this panel is not the drop target.
195
+ *
196
+ * @param rect - The panel layout rectangle to get drop indicator props for.
197
+ * @returns An object with `style` for the drop indicator, or `null` if not applicable.
198
+ */
79
199
  getDropIndicatorProps: (rect: PanelLayoutRect) => {
80
200
  style: {
81
201
  readonly position: "absolute";
@@ -103,13 +223,44 @@ declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode |
103
223
  readonly height: "100%";
104
224
  };
105
225
  } | null;
226
+ /**
227
+ * Returns props for a drag handle element.
228
+ * Attach these props to a button or element that users can drag to move panels.
229
+ *
230
+ * @param rect - The panel layout rectangle to get drag handle props for.
231
+ * @returns An object with `onMouseDown` handler that initiates dragging.
232
+ */
106
233
  getDragHandleProps: (rect: PanelLayoutRect) => {
107
234
  onMouseDown: () => void;
108
235
  };
236
+ /**
237
+ * The currently dragging panel rectangle, or `null` if no panel is being dragged.
238
+ * Use this to apply visual feedback (e.g., reduce opacity) to the dragging panel.
239
+ */
109
240
  draggingRect: PanelLayoutRect | null;
241
+ /**
242
+ * Adds a new panel to the layout.
243
+ * The panel will be placed according to the configured placement strategy.
244
+ *
245
+ * @param id - Unique identifier for the new panel.
246
+ * @throws {Error} If the target node with the ID returned by the placement strategy is not found in the layout tree.
247
+ */
110
248
  addPanel: (id: string) => void;
249
+ /**
250
+ * Removes a panel from the layout.
251
+ * If it's the last panel, the layout becomes empty (root becomes `null`).
252
+ * The layout tree is automatically restructured to remove empty splits.
253
+ *
254
+ * @param id - The ID of the panel to remove.
255
+ * @throws {Error} If the panel is not found or if the root is null.
256
+ */
111
257
  removePanel: (id: string) => void;
258
+ /**
259
+ * The current root node of the layout tree.
260
+ * Use this to serialize the layout state (e.g., `JSON.stringify(root)`).
261
+ * Can be `null` if the layout is empty.
262
+ */
112
263
  root: LayoutNode | null;
113
264
  };
114
265
 
115
- export { type AddPanelStrategy, type LayoutManagerOptions, type LayoutNode, type LayoutRect, type PanelLayoutRect, type PanelNode, type SplitLayoutRect, type SplitNode, bspStrategy, evenlyDividedHorizontalStrategy, useDockLayout };
266
+ export { type LayoutManagerOptions, type LayoutNode, type LayoutRect, type PanelLayoutRect, type PanelNode, type PlacementStrategy, type SplitLayoutRect, type SplitNode, equalWidthRightStrategy, useDockLayout };
package/dist/index.d.ts CHANGED
@@ -15,8 +15,23 @@ type Orientation = "horizontal" | "vertical";
15
15
  type Direction = "top" | "bottom" | "left" | "right";
16
16
 
17
17
  interface LayoutManagerOptions {
18
+ /**
19
+ * Initial size of the container in pixels.
20
+ * If not provided, defaults to `{ width: 0, height: 0 }`.
21
+ * The size is automatically updated when the container is resized.
22
+ */
18
23
  size?: Size;
24
+ /**
25
+ * Gap between panels in pixels.
26
+ * This is the width/height of the split bar between panels.
27
+ * Defaults to `10` if not provided.
28
+ */
19
29
  gap?: number;
30
+ /**
31
+ * Strategy for determining where new panels are placed when added.
32
+ * Defaults to `equalWidthRightStrategy` if not provided.
33
+ */
34
+ placementStrategy?: PlacementStrategy;
20
35
  }
21
36
  interface PanelLayoutRect extends Pick<PanelNode, "id" | "type">, Rect {
22
37
  }
@@ -25,32 +40,129 @@ interface SplitLayoutRect extends Pick<SplitNode, "id" | "type" | "orientation">
25
40
  type LayoutRect = PanelLayoutRect | SplitLayoutRect;
26
41
  interface PanelNode {
27
42
  type: "panel";
43
+ /** Unique identifier for the panel. */
28
44
  id: string;
45
+ /**
46
+ * Optional minimum size constraints for the panel.
47
+ * If specified, the panel cannot be resized smaller than these dimensions.
48
+ */
29
49
  minSize?: Partial<Size>;
30
50
  }
31
51
  interface SplitNode {
32
52
  type: "split";
53
+ /** Unique identifier for the split node. */
33
54
  id: string;
55
+ /** Left child node (or top child for vertical splits). */
34
56
  left: LayoutNode;
57
+ /** Right child node (or bottom child for vertical splits). */
35
58
  right: LayoutNode;
59
+ /**
60
+ * Orientation of the split.
61
+ * - `"horizontal"`: splits left/right
62
+ * - `"vertical"`: splits top/bottom
63
+ */
36
64
  orientation: Orientation;
65
+ /**
66
+ * Ratio determining how space is divided between left and right children.
67
+ * Value between 0 and 1, where:
68
+ * - `0.5` means equal space
69
+ * - `< 0.5` means left child gets less space
70
+ * - `> 0.5` means left child gets more space
71
+ */
37
72
  ratio: number;
38
73
  }
39
74
  type LayoutNode = PanelNode | SplitNode;
40
75
 
41
- interface AddPanelStrategy {
42
- getPlacement(root: LayoutNode): {
76
+ interface PlacementStrategy {
77
+ /**
78
+ * Calculates the placement for a new panel based on the current layout tree.
79
+ *
80
+ * @param root - The root node of the current layout tree.
81
+ * @returns Placement configuration specifying:
82
+ * - `targetId`: The ID of the panel that will be split to make room for the new panel
83
+ * - `direction`: The direction in which to split (`"top"`, `"bottom"`, `"left"`, or `"right"`)
84
+ * - `ratio`: The ratio for dividing space (0-1, where 0.5 is equal split)
85
+ */
86
+ getPlacementOnAdd(root: LayoutNode): {
43
87
  targetId: string;
44
88
  direction: Direction;
45
89
  ratio: number;
46
90
  };
47
91
  }
48
- declare const evenlyDividedHorizontalStrategy: AddPanelStrategy;
49
- declare const bspStrategy: AddPanelStrategy;
92
+ /**
93
+ * Default placement strategy that adds panels to the right with equal widths.
94
+ * New panels are added by splitting the root panel horizontally, maintaining equal widths
95
+ * for all panels.
96
+ */
97
+ declare const equalWidthRightStrategy: PlacementStrategy;
50
98
 
99
+ /**
100
+ * Main hook for managing a dock layout.
101
+ *
102
+ * This hook provides all the necessary state and functions to create and manage
103
+ * a resizable, draggable dock layout system. It handles panel addition/removal,
104
+ * drag-and-drop, and resize operations.
105
+ *
106
+ * @param initialRoot - Initial layout tree root node. Can be:
107
+ * - `null` for an empty layout
108
+ * - A `LayoutNode` object
109
+ * - A function that returns `LayoutNode | null` (useful for lazy initialization)
110
+ * @param options - Optional configuration for the layout manager.
111
+ * @returns An object containing layout state and helper functions.
112
+ *
113
+ * @example
114
+ * ```tsx
115
+ * // Start with an empty layout
116
+ * const {
117
+ * containerRef,
118
+ * layoutRects,
119
+ * addPanel,
120
+ * removePanel,
121
+ * getRectProps,
122
+ * getDragHandleProps,
123
+ * } = useDockLayout<HTMLDivElement>(null);
124
+ * ```
125
+ *
126
+ * @example
127
+ * ```tsx
128
+ * // Load initial layout from localStorage
129
+ * const {
130
+ * containerRef,
131
+ * layoutRects,
132
+ * addPanel,
133
+ * removePanel,
134
+ * getRectProps,
135
+ * getDragHandleProps,
136
+ * } = useDockLayout<HTMLDivElement>(() => {
137
+ * const saved = localStorage.getItem("layout");
138
+ * if (saved === null) {
139
+ * return null;
140
+ * }
141
+ * return JSON.parse(saved);
142
+ * });
143
+ * ```
144
+ */
51
145
  declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode | null | (() => LayoutNode | null), options?: LayoutManagerOptions): {
146
+ /**
147
+ * Ref callback that must be attached to the container element.
148
+ * The container should have `position: relative` styling.
149
+ * The layout will automatically resize when the container size changes.
150
+ */
52
151
  containerRef: react.RefObject<T | null>;
152
+ /**
153
+ * Array of layout rectangles representing all panels and split bars.
154
+ * Each rectangle contains position, size, and type information.
155
+ * Use this to render your panels and split bars.
156
+ */
53
157
  layoutRects: LayoutRect[];
158
+ /**
159
+ * Returns props (style and event handlers) for a given layout rectangle.
160
+ * For split bars, returns style with cursor and onMouseDown handler for resizing.
161
+ * For panels, returns style and onMouseMove/onMouseUp handlers for drag-and-drop.
162
+ *
163
+ * @param rect - The layout rectangle to get props for.
164
+ * @returns An object with `style` and event handler props.
165
+ */
54
166
  getRectProps: (rect: LayoutRect) => {
55
167
  readonly style: {
56
168
  readonly position: "absolute";
@@ -76,6 +188,14 @@ declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode |
76
188
  readonly onMouseUp: (event: MouseEvent<T>) => void;
77
189
  readonly onMouseDown?: undefined;
78
190
  };
191
+ /**
192
+ * Returns props for rendering a drop indicator overlay on a panel.
193
+ * The drop indicator shows where a dragged panel will be placed.
194
+ * Returns `null` if no panel is being dragged or if this panel is not the drop target.
195
+ *
196
+ * @param rect - The panel layout rectangle to get drop indicator props for.
197
+ * @returns An object with `style` for the drop indicator, or `null` if not applicable.
198
+ */
79
199
  getDropIndicatorProps: (rect: PanelLayoutRect) => {
80
200
  style: {
81
201
  readonly position: "absolute";
@@ -103,13 +223,44 @@ declare function useDockLayout<T extends HTMLElement>(initialRoot: LayoutNode |
103
223
  readonly height: "100%";
104
224
  };
105
225
  } | null;
226
+ /**
227
+ * Returns props for a drag handle element.
228
+ * Attach these props to a button or element that users can drag to move panels.
229
+ *
230
+ * @param rect - The panel layout rectangle to get drag handle props for.
231
+ * @returns An object with `onMouseDown` handler that initiates dragging.
232
+ */
106
233
  getDragHandleProps: (rect: PanelLayoutRect) => {
107
234
  onMouseDown: () => void;
108
235
  };
236
+ /**
237
+ * The currently dragging panel rectangle, or `null` if no panel is being dragged.
238
+ * Use this to apply visual feedback (e.g., reduce opacity) to the dragging panel.
239
+ */
109
240
  draggingRect: PanelLayoutRect | null;
241
+ /**
242
+ * Adds a new panel to the layout.
243
+ * The panel will be placed according to the configured placement strategy.
244
+ *
245
+ * @param id - Unique identifier for the new panel.
246
+ * @throws {Error} If the target node with the ID returned by the placement strategy is not found in the layout tree.
247
+ */
110
248
  addPanel: (id: string) => void;
249
+ /**
250
+ * Removes a panel from the layout.
251
+ * If it's the last panel, the layout becomes empty (root becomes `null`).
252
+ * The layout tree is automatically restructured to remove empty splits.
253
+ *
254
+ * @param id - The ID of the panel to remove.
255
+ * @throws {Error} If the panel is not found or if the root is null.
256
+ */
111
257
  removePanel: (id: string) => void;
258
+ /**
259
+ * The current root node of the layout tree.
260
+ * Use this to serialize the layout state (e.g., `JSON.stringify(root)`).
261
+ * Can be `null` if the layout is empty.
262
+ */
112
263
  root: LayoutNode | null;
113
264
  };
114
265
 
115
- export { type AddPanelStrategy, type LayoutManagerOptions, type LayoutNode, type LayoutRect, type PanelLayoutRect, type PanelNode, type SplitLayoutRect, type SplitNode, bspStrategy, evenlyDividedHorizontalStrategy, useDockLayout };
266
+ export { type LayoutManagerOptions, type LayoutNode, type LayoutRect, type PanelLayoutRect, type PanelNode, type PlacementStrategy, type SplitLayoutRect, type SplitNode, equalWidthRightStrategy, useDockLayout };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- function l(i){throw new Error(`Unexpected value: ${i}`)}function N(i,t){return i===null?null:x(t,i)}function x(i,t){if(t.type==="panel")return null;if(t.type==="split")return t.left.id===i||t.right.id===i?t:x(i,t.left)??x(i,t.right);l(t)}var M={getPlacement(i){let t=g(i)+1;return{targetId:i.id,direction:"right",ratio:t/(t+1)}}};function g(i){if(i.type==="panel")return 0;if(i.type==="split"){if(i.orientation==="horizontal")return 1+g(i.left)+g(i.right);if(i.orientation==="vertical")return g(i.left)+g(i.right);l(i.orientation)}else l(i)}var G={getPlacement(i){let t=z(i),e=N(i,t.id);return{targetId:t.id,direction:e===null?"right":e.orientation==="horizontal"?"bottom":"right",ratio:.5}}};function z(i){if(i.type==="panel")return i;if(i.type==="split")return z(i.right);l(i)}import{useState as S,useSyncExternalStore as k}from"react";function m(i,t,e){return Math.max(t,Math.min(e,i))}function w(){return typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():Date.now().toString(36)}function u(i,t){if(!i)throw new Error(t??"Invariant failed")}function E(i,t){if(i===null)return[];let e=[],r=(o,n)=>{o.type==="split"?o.orientation==="horizontal"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x+n.width*o.ratio-t.gap/2),y:Math.round(n.y),width:Math.round(t.gap),height:Math.round(n.height)}),r(o.left,{x:n.x,y:n.y,width:n.width*o.ratio-t.gap/2,height:n.height}),r(o.right,{x:n.x+n.width*o.ratio+t.gap/2,y:n.y,width:n.width*(1-o.ratio)-t.gap/2,height:n.height})):o.orientation==="vertical"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x),y:Math.round(n.y+n.height*o.ratio-t.gap/2),width:Math.round(n.width),height:Math.round(t.gap)}),r(o.left,{x:n.x,y:n.y,width:n.width,height:n.height*o.ratio-t.gap/2}),r(o.right,{x:n.x,y:n.y+n.height*o.ratio+t.gap/2,width:n.width,height:n.height*(1-o.ratio)-t.gap/2})):l(o.orientation):o.type==="panel"?e.push({id:o.id,type:"panel",x:Math.round(n.x),y:Math.round(n.y),width:Math.round(n.width),height:Math.round(n.height)}):l(o)};return r(i,{x:0,y:0,width:t.size.width,height:t.size.height}),e}function f(i,t){if(i.type==="panel")return{width:i.minSize?.width??0,height:i.minSize?.height??0};if(i.type==="split"){if(i.orientation==="horizontal")return{width:f(i.left,t).width+t+f(i.right,t).width,height:Math.max(f(i.left,t).height,f(i.right,t).height)};if(i.orientation==="vertical")return{width:Math.max(f(i.left,t).width,f(i.right,t).width),height:f(i.left,t).height+t+f(i.right,t).height};l(i.orientation)}else l(i)}function P(i,t){let e=i.x+i.width/2,r=i.y+i.height/2,o=(t.x-e)/(i.width/2),n=(t.y-r)/(i.height/2);return Math.abs(o)>Math.abs(n)?o>0?"right":"left":n>0?"bottom":"top"}var v=class{_root=null;constructor(t){this._root=t}get root(){return this._root}set root(t){this._root=t}findNode(t){return this._root===null?null:this.findNodeInSubTree(t,this._root)}findNodeInSubTree(t,e){if(t===e.id)return e;if(e.type==="panel")return null;if(e.type==="split")return this.findNodeInSubTree(t,e.left)??this.findNodeInSubTree(t,e.right);l(e)}findParentNode(t){return N(this._root,t)}replaceChildNode({parent:t,oldChildId:e,newChild:r}){if(this.findNode(e)===null)throw new Error(`Child node with id ${e} not found`);if(t.left.id===e)t.left=r;else if(t.right.id===e)t.right=r;else throw new Error(`Child node with id ${e} is not a child of the parent node with id ${t.id}`)}};var _=class{MIN_RESIZE_RATIO=.1;MAX_RESIZE_RATIO=.9;_tree;_options;_listeners=new Set;_layoutRects=[];_addPanelStrategy=M;constructor(t,e){this._tree=new v(t),this._options={gap:e?.gap??10,size:e?.size??{width:0,height:0}},this._layoutRects=E(t,this._options)}get root(){return this._tree.root}set root(t){this._tree.root=t,this.syncLayoutRects()}get layoutRects(){return this._layoutRects}subscribe=t=>(this._listeners.add(t),()=>{this._listeners.delete(t)});setSize(t){this._options.size=t,this.syncLayoutRects()}removePanel(t){if(this._tree.root===null)throw new Error("Root node is null");let e=this._tree.findNode(t);if(e===null)throw new Error(`Node with id ${t} not found`);if(e.type!=="panel")throw new Error(`Node with id ${t} is not a panel`);if(e.id===this._tree.root.id){this._tree.root=null,this.syncLayoutRects();return}let r=this._tree.findParentNode(t);u(r!==null,"Parent node is not null");let o=r.left.id===e.id?r.right:r.left;if(r.id===this._tree.root.id){this._tree.root=o,this.syncLayoutRects();return}let n=this._tree.findParentNode(r.id);u(n!==null,"Grand parent node is not null"),this._tree.replaceChildNode({parent:n,oldChildId:r.id,newChild:o}),this.syncLayoutRects()}movePanel({sourceId:t,targetId:e,point:r}){if(this._tree.root===null)throw new Error("Root node is null");if(this._tree.root.type!=="split")throw new Error("Root node is not a split node");let o=this._tree.findNode(t);if(o===null)throw new Error(`Node with id ${t} not found`);if(o.type!=="panel")throw new Error(`Node with id ${t} is not a panel node`);let n=this._tree.findParentNode(t);u(n!==null);let a=this._tree.findNode(e);if(a===null)throw new Error(`Node with id ${e} not found`);if(a.type!=="panel")throw new Error(`Node with id ${e} is not a panel node`);let p=n.left.id===t?n.right:n.left,d=this.findRect(e);u(d!==null),u(d.type==="panel");let c=P(d,r);if(p.id===e){c==="left"?(n.orientation="horizontal",n.left=o,n.right=a):c==="right"?(n.orientation="horizontal",n.left=a,n.right=o):c==="top"?(n.orientation="vertical",n.left=o,n.right=a):c==="bottom"?(n.orientation="vertical",n.left=a,n.right=o):l(c),this.syncLayoutRects();return}let y=this._tree.findParentNode(n.id);y===null?this._tree.root=p:this._tree.replaceChildNode({parent:y,oldChildId:n.id,newChild:p});let s=this._tree.findParentNode(e);u(s!==null);let h=this.createSplitNode({direction:c,sourceNode:o,targetNode:a});this._tree.replaceChildNode({parent:s,oldChildId:e,newChild:h}),this.syncLayoutRects()}resizePanel(t,e){if(this._tree.root===null)throw new Error("Root node is null");let r=this.findRect(t);if(r===null)throw new Error(`Rect with id ${t} not found`);if(r.type!=="split")throw new Error(`Rect with id ${t} is not a split node`);let o=this._tree.findNode(t);u(o!==null,"Split node is not null"),u(o.type==="split","Split node is a split"),o.ratio=this.calculateResizeRatio(o,r,e),this.syncLayoutRects()}addPanel(t){if(this._tree.root===null){this._tree.root={id:t,type:"panel"},this.syncLayoutRects();return}let{targetId:e,direction:r,ratio:o=.5}=this._addPanelStrategy.getPlacement(this._tree.root);if(e===this._tree.root.id){this._tree.root=this.createSplitNode({direction:r,ratio:o,sourceNode:{id:t,type:"panel"},targetNode:this._tree.root}),this.syncLayoutRects();return}let n=this._tree.findNode(e);if(n===null)throw new Error(`Node with id ${e} not found`);let a=this._tree.findParentNode(e);u(a!==null,"Target node parent is not null");let p=this.createSplitNode({direction:r,sourceNode:{id:t,type:"panel"},targetNode:n,ratio:o});this._tree.replaceChildNode({parent:a,oldChildId:e,newChild:p}),this.syncLayoutRects()}calculateDropTarget({draggedPanelId:t,targetPanelId:e,point:r}){u(t!==e,"Dragged panel id is not the same as target panel id");let o=this.findRect(e);return u(o!==null&&o.type==="panel"),{id:e,direction:P(o,r)}}emit(){this._listeners.forEach(t=>{t()})}syncLayoutRects(){this._layoutRects=E(this._tree.root,this._options),this.emit()}createSplitNode({direction:t,sourceNode:e,targetNode:r,ratio:o=.5}){switch(t){case"left":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:e,right:r};case"right":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:r,right:e};case"top":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:e,right:r};case"bottom":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:r,right:e};default:l(t)}}findRect(t){return this._layoutRects.find(e=>e.id===t)??null}getSurroundingRect(t){let e=this._tree.findNode(t);if(u(e!==null,"Node is not null"),e.type==="panel"){let r=this.findRect(t);return u(r!==null,"Rect is not null"),u(r.type==="panel","Rect is a panel"),{x:r.x,y:r.y,width:r.width,height:r.height}}else if(e.type==="split"){let r=this.getSurroundingRect(e.left.id),o=this.getSurroundingRect(e.right.id);if(e.orientation==="horizontal")return{x:r.x,y:r.y,width:r.width+this._options.gap+o.width,height:r.height};if(e.orientation==="vertical")return{x:r.x,y:r.y,width:r.width,height:r.height+this._options.gap+o.height};l(e.orientation)}else l(e)}calculateResizeRatio(t,e,r){if(e.orientation==="horizontal"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.x-o.x,p=m(a/(o.width+e.width+n.width),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.width+this._options.gap+n.width,y=(f(t.left,this._options.gap).width+this._options.gap/2)/d,s=f(t.right,this._options.gap).width,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else if(e.orientation==="vertical"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.y-o.y,p=m(a/(o.height+e.height+n.height),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.height+this._options.gap+n.height,y=(f(t.left,this._options.gap).height+this._options.gap/2)/d,s=f(t.right,this._options.gap).height,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else l(e.orientation)}};import{useEffect as I}from"react";function D(i){I(()=>{let t=document.body.style.cursor;return document.body.style.cursor=i??"default",()=>{document.body.style.cursor=t}},[i])}import{useEffect as W}from"react";import{useCallback as O,useEffect as A,useRef as $}from"react";function L(i){let t=$(i);return A(()=>{t.current=i},[i]),O((...e)=>t.current(...e),[])}function b(i,t,e,r){let o=L(e);W(()=>(i.addEventListener(t,o,r),()=>{i.removeEventListener(t,o,r)}),[i,t,o,r])}import{useEffect as H,useRef as K}from"react";function T(i){let t=K(null),e=L(i);return H(()=>{let r=t.current;if(r===null)throw new Error("Ref is not attached to an element");let o=new ResizeObserver(n=>{for(let a of n)e(a)});return o.observe(r),()=>{o.disconnect()}},[e]),t}function Ct(i,t){let[e]=S(()=>{let s=typeof i=="function"?i():i;return new _(s,t)}),r=k(e.subscribe,()=>e.layoutRects),[o,n]=S(null),[a,p]=S(null),[d,c]=S(null),y=T(s=>{e.setSize({width:s.contentRect.width,height:s.contentRect.height})});return b(document,"mousemove",s=>{if(o===null)return;let h=y.current;if(h===null)throw new Error("containerRef is not attached to an element");let R=h.getBoundingClientRect();e.resizePanel(o.id,{x:s.clientX-R.left,y:s.clientY-R.top})}),b(document,"mouseup",()=>{o!==null&&n(null)}),D(o===null?"default":C[o.orientation]),{containerRef:y,layoutRects:r,getRectProps:s=>{if(s.type==="split")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height,cursor:C[s.orientation]},onMouseDown:()=>{n(s)}};if(s.type==="panel")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height},onMouseMove:h=>{if(a===null)return;if(a.id===s.id){c(null);return}let R=e.calculateDropTarget({draggedPanelId:a.id,targetPanelId:s.id,point:{x:h.clientX,y:h.clientY}});c(R)},onMouseUp:h=>{if(a!==null){if(a.id===s.id){p(null),c(null);return}e.movePanel({sourceId:a.id,targetId:s.id,point:{x:h.clientX,y:h.clientY}}),p(null),c(null)}}};l(s)},getDropIndicatorProps:s=>a===null||!(s.id===d?.id)?null:{style:U(d.direction)},getDragHandleProps:s=>({onMouseDown:()=>{p(s)}}),draggingRect:a,addPanel:e.addPanel.bind(e),removePanel:e.removePanel.bind(e),root:e.root}}function U(i){if(i==="top")return{position:"absolute",left:0,top:0,width:"100%",height:"50%"};if(i==="bottom")return{position:"absolute",left:0,top:"50%",width:"100%",height:"50%"};if(i==="left")return{position:"absolute",left:0,top:0,width:"50%",height:"100%"};if(i==="right")return{position:"absolute",left:"50%",top:0,width:"50%",height:"100%"};l(i)}var C={horizontal:"col-resize",vertical:"row-resize"};export{G as bspStrategy,M as evenlyDividedHorizontalStrategy,Ct as useDockLayout};
1
+ function l(i){throw new Error(`Unexpected value: ${i}`)}var P={getPlacementOnAdd(i){let t=g(i)+1;return{targetId:i.id,direction:"right",ratio:t/(t+1)}}};function g(i){if(i.type==="panel")return 0;if(i.type==="split"){if(i.orientation==="horizontal")return 1+g(i.left)+g(i.right);if(i.orientation==="vertical")return g(i.left)+g(i.right);l(i.orientation)}else l(i)}import{useState as L,useSyncExternalStore as k}from"react";function m(i,t,e){return Math.max(t,Math.min(e,i))}function w(){return typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():Date.now().toString(36)}function u(i,t){if(!i)throw new Error(t??"Invariant failed")}function S(i,t){if(i===null)return[];let e=[],r=(o,n)=>{o.type==="split"?o.orientation==="horizontal"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x+n.width*o.ratio-t.gap/2),y:Math.round(n.y),width:Math.round(t.gap),height:Math.round(n.height)}),r(o.left,{x:n.x,y:n.y,width:n.width*o.ratio-t.gap/2,height:n.height}),r(o.right,{x:n.x+n.width*o.ratio+t.gap/2,y:n.y,width:n.width*(1-o.ratio)-t.gap/2,height:n.height})):o.orientation==="vertical"?(e.push({id:o.id,type:"split",orientation:o.orientation,x:Math.round(n.x),y:Math.round(n.y+n.height*o.ratio-t.gap/2),width:Math.round(n.width),height:Math.round(t.gap)}),r(o.left,{x:n.x,y:n.y,width:n.width,height:n.height*o.ratio-t.gap/2}),r(o.right,{x:n.x,y:n.y+n.height*o.ratio+t.gap/2,width:n.width,height:n.height*(1-o.ratio)-t.gap/2})):l(o.orientation):o.type==="panel"?e.push({id:o.id,type:"panel",x:Math.round(n.x),y:Math.round(n.y),width:Math.round(n.width),height:Math.round(n.height)}):l(o)};return r(i,{x:0,y:0,width:t.size.width,height:t.size.height}),e}function f(i,t){if(i.type==="panel")return{width:i.minSize?.width??0,height:i.minSize?.height??0};if(i.type==="split"){if(i.orientation==="horizontal")return{width:f(i.left,t).width+t+f(i.right,t).width,height:Math.max(f(i.left,t).height,f(i.right,t).height)};if(i.orientation==="vertical")return{width:Math.max(f(i.left,t).width,f(i.right,t).width),height:f(i.left,t).height+t+f(i.right,t).height};l(i.orientation)}else l(i)}function x(i,t){let e=i.x+i.width/2,r=i.y+i.height/2,o=(t.x-e)/(i.width/2),n=(t.y-r)/(i.height/2);return Math.abs(o)>Math.abs(n)?o>0?"right":"left":n>0?"bottom":"top"}function M(i,t){return i===null?null:E(t,i)}function E(i,t){if(t.type==="panel")return null;if(t.type==="split")return t.left.id===i||t.right.id===i?t:E(i,t.left)??E(i,t.right);l(t)}var N=class{_root=null;constructor(t){this._root=t}get root(){return this._root}set root(t){this._root=t}findNode(t){return this._root===null?null:this.findNodeInSubTree(t,this._root)}findNodeInSubTree(t,e){if(t===e.id)return e;if(e.type==="panel")return null;if(e.type==="split")return this.findNodeInSubTree(t,e.left)??this.findNodeInSubTree(t,e.right);l(e)}findParentNode(t){return M(this._root,t)}replaceChildNode({parent:t,oldChildId:e,newChild:r}){if(this.findNode(e)===null)throw new Error(`Child node with id ${e} not found`);if(t.left.id===e)t.left=r;else if(t.right.id===e)t.right=r;else throw new Error(`Child node with id ${e} is not a child of the parent node with id ${t.id}`)}};var v=class{MIN_RESIZE_RATIO=.1;MAX_RESIZE_RATIO=.9;_tree;_options;_listeners=new Set;_layoutRects=[];constructor(t,e){this._tree=new N(t),this._options={gap:e?.gap??10,size:e?.size??{width:0,height:0},placementStrategy:e?.placementStrategy??P},this._layoutRects=S(t,this._options)}get root(){return this._tree.root}set root(t){this._tree.root=t,this.syncLayoutRects()}get layoutRects(){return this._layoutRects}subscribe=t=>(this._listeners.add(t),()=>{this._listeners.delete(t)});setSize(t){this._options.size=t,this.syncLayoutRects()}removePanel(t){if(this._tree.root===null)throw new Error("Root node is null");let e=this._tree.findNode(t);if(e===null)throw new Error(`Node with id ${t} not found`);if(e.type!=="panel")throw new Error(`Node with id ${t} is not a panel`);if(e.id===this._tree.root.id){this._tree.root=null,this.syncLayoutRects();return}let r=this._tree.findParentNode(t);u(r!==null,"Parent node is not null");let o=r.left.id===e.id?r.right:r.left;if(r.id===this._tree.root.id){this._tree.root=o,this.syncLayoutRects();return}let n=this._tree.findParentNode(r.id);u(n!==null,"Grand parent node is not null"),this._tree.replaceChildNode({parent:n,oldChildId:r.id,newChild:o}),this.syncLayoutRects()}movePanel({sourceId:t,targetId:e,point:r}){if(this._tree.root===null)throw new Error("Root node is null");if(this._tree.root.type!=="split")throw new Error("Root node is not a split node");let o=this._tree.findNode(t);if(o===null)throw new Error(`Node with id ${t} not found`);if(o.type!=="panel")throw new Error(`Node with id ${t} is not a panel node`);let n=this._tree.findParentNode(t);u(n!==null);let a=this._tree.findNode(e);if(a===null)throw new Error(`Node with id ${e} not found`);if(a.type!=="panel")throw new Error(`Node with id ${e} is not a panel node`);let p=n.left.id===t?n.right:n.left,d=this.findRect(e);u(d!==null),u(d.type==="panel");let c=x(d,r);if(p.id===e){c==="left"?(n.orientation="horizontal",n.left=o,n.right=a):c==="right"?(n.orientation="horizontal",n.left=a,n.right=o):c==="top"?(n.orientation="vertical",n.left=o,n.right=a):c==="bottom"?(n.orientation="vertical",n.left=a,n.right=o):l(c),this.syncLayoutRects();return}let y=this._tree.findParentNode(n.id);y===null?this._tree.root=p:this._tree.replaceChildNode({parent:y,oldChildId:n.id,newChild:p});let s=this._tree.findParentNode(e);u(s!==null);let h=this.createSplitNode({direction:c,sourceNode:o,targetNode:a});this._tree.replaceChildNode({parent:s,oldChildId:e,newChild:h}),this.syncLayoutRects()}resizePanel(t,e){if(this._tree.root===null)throw new Error("Root node is null");let r=this.findRect(t);if(r===null)throw new Error(`Rect with id ${t} not found`);if(r.type!=="split")throw new Error(`Rect with id ${t} is not a split node`);let o=this._tree.findNode(t);u(o!==null,"Split node is not null"),u(o.type==="split","Split node is a split"),o.ratio=this.calculateResizeRatio(o,r,e),this.syncLayoutRects()}addPanel(t){if(this._tree.root===null){this._tree.root={id:t,type:"panel"},this.syncLayoutRects();return}let{targetId:e,direction:r,ratio:o=.5}=this._options.placementStrategy.getPlacementOnAdd(this._tree.root);if(e===this._tree.root.id){this._tree.root=this.createSplitNode({direction:r,ratio:o,sourceNode:{id:t,type:"panel"},targetNode:this._tree.root}),this.syncLayoutRects();return}let n=this._tree.findNode(e);if(n===null)throw new Error(`Node with id ${e} not found`);let a=this._tree.findParentNode(e);u(a!==null,"Target node parent is not null");let p=this.createSplitNode({direction:r,sourceNode:{id:t,type:"panel"},targetNode:n,ratio:o});this._tree.replaceChildNode({parent:a,oldChildId:e,newChild:p}),this.syncLayoutRects()}calculateDropTarget({draggedPanelId:t,targetPanelId:e,point:r}){u(t!==e,"Dragged panel id is not the same as target panel id");let o=this.findRect(e);return u(o!==null&&o.type==="panel"),{id:e,direction:x(o,r)}}emit(){this._listeners.forEach(t=>{t()})}syncLayoutRects(){this._layoutRects=S(this._tree.root,this._options),this.emit()}createSplitNode({direction:t,sourceNode:e,targetNode:r,ratio:o=.5}){switch(t){case"left":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:e,right:r};case"right":return{id:w(),type:"split",orientation:"horizontal",ratio:o,left:r,right:e};case"top":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:e,right:r};case"bottom":return{id:w(),type:"split",orientation:"vertical",ratio:o,left:r,right:e};default:l(t)}}findRect(t){return this._layoutRects.find(e=>e.id===t)??null}getSurroundingRect(t){let e=this._tree.findNode(t);if(u(e!==null,"Node is not null"),e.type==="panel"){let r=this.findRect(t);return u(r!==null,"Rect is not null"),u(r.type==="panel","Rect is a panel"),{x:r.x,y:r.y,width:r.width,height:r.height}}else if(e.type==="split"){let r=this.getSurroundingRect(e.left.id),o=this.getSurroundingRect(e.right.id);if(e.orientation==="horizontal")return{x:r.x,y:r.y,width:r.width+this._options.gap+o.width,height:r.height};if(e.orientation==="vertical")return{x:r.x,y:r.y,width:r.width,height:r.height+this._options.gap+o.height};l(e.orientation)}else l(e)}calculateResizeRatio(t,e,r){if(e.orientation==="horizontal"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.x-o.x,p=m(a/(o.width+e.width+n.width),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.width+this._options.gap+n.width,y=(f(t.left,this._options.gap).width+this._options.gap/2)/d,s=f(t.right,this._options.gap).width,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else if(e.orientation==="vertical"){let o=this.getSurroundingRect(t.left.id),n=this.getSurroundingRect(t.right.id),a=r.y-o.y,p=m(a/(o.height+e.height+n.height),this.MIN_RESIZE_RATIO,this.MAX_RESIZE_RATIO),d=o.height+this._options.gap+n.height,y=(f(t.left,this._options.gap).height+this._options.gap/2)/d,s=f(t.right,this._options.gap).height,h=(d-(s+this._options.gap/2))/d;return m(p,y,h)}else l(e.orientation)}};import{useEffect as C}from"react";function z(i){C(()=>{let t=document.body.style.cursor;return document.body.style.cursor=i??"default",()=>{document.body.style.cursor=t}},[i])}import{useEffect as W}from"react";import{useCallback as I,useEffect as O,useRef as A}from"react";function _(i){let t=A(i);return O(()=>{t.current=i},[i]),I((...e)=>t.current(...e),[])}function b(i,t,e,r){let o=_(e);W(()=>(i.addEventListener(t,o,r),()=>{i.removeEventListener(t,o,r)}),[i,t,o,r])}import{useEffect as $,useRef as H}from"react";function T(i){let t=H(null),e=_(i);return $(()=>{let r=t.current;if(r===null)throw new Error("Ref is not attached to an element");let o=new ResizeObserver(n=>{for(let a of n)e(a)});return o.observe(r),()=>{o.disconnect()}},[e]),t}function zt(i,t){let[e]=L(()=>{let s=typeof i=="function"?i():i;return new v(s,t)}),r=k(e.subscribe,()=>e.layoutRects),[o,n]=L(null),[a,p]=L(null),[d,c]=L(null),y=T(s=>{e.setSize({width:s.contentRect.width,height:s.contentRect.height})});return b(document,"mousemove",s=>{if(o===null)return;let h=y.current;if(h===null)throw new Error("containerRef is not attached to an element");let R=h.getBoundingClientRect();e.resizePanel(o.id,{x:s.clientX-R.left,y:s.clientY-R.top})}),b(document,"mouseup",()=>{o!==null&&n(null)}),z(o===null?"default":D[o.orientation]),{containerRef:y,layoutRects:r,getRectProps:s=>{if(s.type==="split")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height,cursor:D[s.orientation]},onMouseDown:()=>{n(s)}};if(s.type==="panel")return{style:{position:"absolute",left:s.x,top:s.y,width:s.width,height:s.height},onMouseMove:h=>{if(a===null)return;if(a.id===s.id){c(null);return}let R=e.calculateDropTarget({draggedPanelId:a.id,targetPanelId:s.id,point:{x:h.clientX,y:h.clientY}});c(R)},onMouseUp:h=>{if(a!==null){if(a.id===s.id){p(null),c(null);return}e.movePanel({sourceId:a.id,targetId:s.id,point:{x:h.clientX,y:h.clientY}}),p(null),c(null)}}};l(s)},getDropIndicatorProps:s=>a===null||!(s.id===d?.id)?null:{style:K(d.direction)},getDragHandleProps:s=>({onMouseDown:()=>{p(s)}}),draggingRect:a,addPanel:e.addPanel.bind(e),removePanel:e.removePanel.bind(e),root:e.root}}function K(i){if(i==="top")return{position:"absolute",left:0,top:0,width:"100%",height:"50%"};if(i==="bottom")return{position:"absolute",left:0,top:"50%",width:"100%",height:"50%"};if(i==="left")return{position:"absolute",left:0,top:0,width:"50%",height:"100%"};if(i==="right")return{position:"absolute",left:"50%",top:0,width:"50%",height:"100%"};l(i)}var D={horizontal:"col-resize",vertical:"row-resize"};export{P as equalWidthRightStrategy,zt as useDockLayout};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-headless-dock-layout",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "A lightweight, headless dock layout library for React.",
5
5
  "type": "module",
6
6
  "exports": {