react-headless-dock-layout 0.4.1 → 0.4.2
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/README.md +44 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -115,3 +115,47 @@ function App() {
|
|
|
115
115
|
);
|
|
116
116
|
}
|
|
117
117
|
```
|
|
118
|
+
|
|
119
|
+
## Advanced
|
|
120
|
+
|
|
121
|
+
### Placement Strategies
|
|
122
|
+
|
|
123
|
+
A placement strategy determines where new panels are placed when added. The default strategy is `equalWidthRightStrategy`, which adds panels to the right with equal widths.
|
|
124
|
+
|
|
125
|
+
To use a custom strategy, pass it in the options. Here's an example that adds panels to the rightmost panel, alternating split directions:
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
import { useDockLayout, type PlacementStrategy, type LayoutNode, type PanelNode, type SplitNode } from 'react-headless-dock-layout';
|
|
129
|
+
|
|
130
|
+
function findRightMostPanel(node: LayoutNode): PanelNode {
|
|
131
|
+
if (node.type === "panel") return node;
|
|
132
|
+
if (node.type === "split") return findRightMostPanel(node.right);
|
|
133
|
+
throw new Error("Unexpected node type");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function findParentNode(root: LayoutNode, id: string): SplitNode | null {
|
|
137
|
+
function find(node: LayoutNode): SplitNode | null {
|
|
138
|
+
if (node.type === "panel") return null;
|
|
139
|
+
if (node.left.id === id || node.right.id === id) return node;
|
|
140
|
+
return find(node.left) ?? find(node.right);
|
|
141
|
+
}
|
|
142
|
+
return find(root);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const myStrategy: PlacementStrategy = {
|
|
146
|
+
getPlacementOnAdd(root) {
|
|
147
|
+
const rightMostPanel = findRightMostPanel(root);
|
|
148
|
+
const parentNode = findParentNode(root, rightMostPanel.id);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
targetId: rightMostPanel.id,
|
|
152
|
+
direction: parentNode?.orientation === "horizontal" ? "bottom" : "right",
|
|
153
|
+
ratio: 0.5,
|
|
154
|
+
};
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const { addPanel } = useDockLayout(null, {
|
|
159
|
+
placementStrategy: myStrategy,
|
|
160
|
+
});
|
|
161
|
+
```
|