@octanejs/xyflow 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/package.json +69 -0
  4. package/src/additional-components/Background/Background.tsrx +144 -0
  5. package/src/additional-components/Background/Patterns.tsrx +32 -0
  6. package/src/additional-components/Background/index.tsrx +2 -0
  7. package/src/additional-components/Background/types.ts +60 -0
  8. package/src/additional-components/Controls/ControlButton.tsrx +34 -0
  9. package/src/additional-components/Controls/Controls.tsrx +148 -0
  10. package/src/additional-components/Controls/Icons/FitView.tsrx +7 -0
  11. package/src/additional-components/Controls/Icons/Lock.tsrx +7 -0
  12. package/src/additional-components/Controls/Icons/Minus.tsrx +5 -0
  13. package/src/additional-components/Controls/Icons/Plus.tsrx +5 -0
  14. package/src/additional-components/Controls/Icons/Unlock.tsrx +7 -0
  15. package/src/additional-components/Controls/index.tsrx +3 -0
  16. package/src/additional-components/Controls/types.ts +68 -0
  17. package/src/additional-components/EdgeToolbar/EdgeToolbar.tsrx +79 -0
  18. package/src/additional-components/EdgeToolbar/index.tsrx +2 -0
  19. package/src/additional-components/EdgeToolbar/types.ts +14 -0
  20. package/src/additional-components/MiniMap/MiniMap.tsrx +249 -0
  21. package/src/additional-components/MiniMap/MiniMapNode.tsrx +43 -0
  22. package/src/additional-components/MiniMap/MiniMapNodes.tsrx +136 -0
  23. package/src/additional-components/MiniMap/index.ts +3 -0
  24. package/src/additional-components/MiniMap/types.ts +139 -0
  25. package/src/additional-components/NodeResizer/NodeResizeControl.tsrx +250 -0
  26. package/src/additional-components/NodeResizer/NodeResizer.tsrx +102 -0
  27. package/src/additional-components/NodeResizer/index.ts +4 -0
  28. package/src/additional-components/NodeResizer/types.ts +122 -0
  29. package/src/additional-components/NodeToolbar/NodeToolbar.tsrx +141 -0
  30. package/src/additional-components/NodeToolbar/NodeToolbarPortal.tsrx +17 -0
  31. package/src/additional-components/NodeToolbar/index.tsrx +2 -0
  32. package/src/additional-components/NodeToolbar/types.ts +32 -0
  33. package/src/additional-components/index.ts +6 -0
  34. package/src/components/A11yDescriptions/index.tsrx +58 -0
  35. package/src/components/Attribution/index.tsrx +31 -0
  36. package/src/components/BatchProvider/index.tsrx +122 -0
  37. package/src/components/BatchProvider/types.ts +7 -0
  38. package/src/components/BatchProvider/useQueue.ts +62 -0
  39. package/src/components/ConnectionLine/index.tsrx +140 -0
  40. package/src/components/EdgeLabelRenderer/index.tsrx +62 -0
  41. package/src/components/EdgeWrapper/EdgeUpdateAnchors.tsrx +151 -0
  42. package/src/components/EdgeWrapper/index.tsrx +274 -0
  43. package/src/components/EdgeWrapper/utils.ts +26 -0
  44. package/src/components/Edges/BaseEdge.tsrx +76 -0
  45. package/src/components/Edges/BezierEdge.tsrx +94 -0
  46. package/src/components/Edges/EdgeAnchor.tsrx +54 -0
  47. package/src/components/Edges/EdgeText.tsrx +97 -0
  48. package/src/components/Edges/SimpleBezierEdge.tsrx +150 -0
  49. package/src/components/Edges/SmoothStepEdge.tsrx +96 -0
  50. package/src/components/Edges/StepEdge.tsrx +55 -0
  51. package/src/components/Edges/StraightEdge.tsrx +81 -0
  52. package/src/components/Edges/index.ts +11 -0
  53. package/src/components/Handle/index.tsrx +314 -0
  54. package/src/components/NodeWrapper/index.tsrx +259 -0
  55. package/src/components/NodeWrapper/useNodeObserver.ts +79 -0
  56. package/src/components/NodeWrapper/utils.tsrx +40 -0
  57. package/src/components/Nodes/DefaultNode.tsrx +17 -0
  58. package/src/components/Nodes/GroupNode.tsrx +3 -0
  59. package/src/components/Nodes/InputNode.tsrx +13 -0
  60. package/src/components/Nodes/OutputNode.tsrx +13 -0
  61. package/src/components/Nodes/utils.ts +45 -0
  62. package/src/components/NodesSelection/index.tsrx +103 -0
  63. package/src/components/Panel/index.tsrx +34 -0
  64. package/src/components/ReactFlowProvider/index.tsrx +128 -0
  65. package/src/components/SelectionListener/index.tsrx +73 -0
  66. package/src/components/StoreUpdater/index.tsrx +173 -0
  67. package/src/components/UserSelection/index.tsrx +27 -0
  68. package/src/components/ViewportPortal/index.tsrx +42 -0
  69. package/src/container/EdgeRenderer/MarkerDefinitions.tsrx +87 -0
  70. package/src/container/EdgeRenderer/MarkerSymbols.tsrx +60 -0
  71. package/src/container/EdgeRenderer/index.tsrx +98 -0
  72. package/src/container/FlowRenderer/index.tsrx +146 -0
  73. package/src/container/GraphView/index.tsrx +212 -0
  74. package/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +39 -0
  75. package/src/container/GraphView/useStylesLoadedWarning.ts +26 -0
  76. package/src/container/NodeRenderer/index.tsrx +103 -0
  77. package/src/container/NodeRenderer/useResizeObserver.ts +38 -0
  78. package/src/container/Pane/index.tsrx +397 -0
  79. package/src/container/ReactFlow/Wrapper.tsrx +66 -0
  80. package/src/container/ReactFlow/index.tsrx +352 -0
  81. package/src/container/ReactFlow/init-values.ts +4 -0
  82. package/src/container/Viewport/index.tsrx +52 -0
  83. package/src/container/ZoomPane/index.tsrx +159 -0
  84. package/src/contexts/HandleConfigContext.tsrx +38 -0
  85. package/src/contexts/NodeIdContext.ts +39 -0
  86. package/src/contexts/StoreContext.ts +8 -0
  87. package/src/custom.d.ts +13 -0
  88. package/src/hooks/slot.ts +22 -0
  89. package/src/hooks/useColorModeClass.ts +50 -0
  90. package/src/hooks/useConnection.ts +69 -0
  91. package/src/hooks/useDrag.ts +95 -0
  92. package/src/hooks/useEdges.ts +32 -0
  93. package/src/hooks/useGlobalKeyHandler.ts +55 -0
  94. package/src/hooks/useHandleConnections.ts +77 -0
  95. package/src/hooks/useInternalNode.ts +47 -0
  96. package/src/hooks/useIsomorphicLayoutEffect.ts +5 -0
  97. package/src/hooks/useKeyPress.ts +217 -0
  98. package/src/hooks/useMoveSelectedNodes.ts +82 -0
  99. package/src/hooks/useNodeConnections.ts +86 -0
  100. package/src/hooks/useNodes.ts +33 -0
  101. package/src/hooks/useNodesData.ts +66 -0
  102. package/src/hooks/useNodesEdgesState.ts +138 -0
  103. package/src/hooks/useNodesInitialized.ts +74 -0
  104. package/src/hooks/useOnEdgesChangeMiddleware.ts +46 -0
  105. package/src/hooks/useOnInitHandler.ts +27 -0
  106. package/src/hooks/useOnNodesChangeMiddleware.ts +46 -0
  107. package/src/hooks/useOnSelectionChange.ts +78 -0
  108. package/src/hooks/useOnViewportChange.ts +68 -0
  109. package/src/hooks/useReactFlow.ts +347 -0
  110. package/src/hooks/useResizeHandler.ts +54 -0
  111. package/src/hooks/useStore.ts +58 -0
  112. package/src/hooks/useUpdateNodeInternals.ts +73 -0
  113. package/src/hooks/useViewport.ts +48 -0
  114. package/src/hooks/useViewportHelper.ts +103 -0
  115. package/src/hooks/useViewportSync.ts +33 -0
  116. package/src/hooks/useVisibleEdgeIds.ts +52 -0
  117. package/src/hooks/useVisibleNodeIds.ts +36 -0
  118. package/src/index.ts +151 -0
  119. package/src/react-shim.ts +93 -0
  120. package/src/store/index.ts +516 -0
  121. package/src/store/initialState.ts +162 -0
  122. package/src/styles/base.css +516 -0
  123. package/src/styles/style.css +648 -0
  124. package/src/styles/utils.ts +9 -0
  125. package/src/types/component-props.ts +716 -0
  126. package/src/types/edges.ts +312 -0
  127. package/src/types/general.ts +239 -0
  128. package/src/types/index.ts +6 -0
  129. package/src/types/instance.ts +265 -0
  130. package/src/types/nodes.ts +144 -0
  131. package/src/types/store.ts +198 -0
  132. package/src/utils/changes.ts +329 -0
  133. package/src/utils/edges.ts +34 -0
  134. package/src/utils/general.ts +72 -0
  135. package/src/utils/index.ts +3 -0
@@ -0,0 +1,250 @@
1
+ import { useRef, useEffect, memo, useCallback } from 'octane';
2
+ import cc from 'classcat';
3
+ import { shallow } from '@octanejs/zustand/shallow';
4
+ import {
5
+ XYResizer,
6
+ ResizeControlVariant,
7
+ type XYResizerInstance,
8
+ type XYResizerChange,
9
+ type XYResizerChildChange,
10
+ type NodeChange,
11
+ type NodeDimensionChange,
12
+ type NodePositionChange,
13
+ handleExpandParent,
14
+ evaluateAbsolutePosition,
15
+ ParentExpandChild,
16
+ XYPosition,
17
+ ControlPosition,
18
+ } from '@xyflow/system';
19
+
20
+ import { useStoreApi, useStore } from '../../hooks/useStore';
21
+ import { useNodeId } from '../../contexts/NodeIdContext';
22
+ import type { ResizeControlProps, ResizeControlLineProps } from './types';
23
+ import { ReactFlowState } from '../../types';
24
+
25
+ const scaleSelector = (calculateScale: boolean) => (store: ReactFlowState) => calculateScale
26
+ ? `${Math.max(1 / store.transform[2], 1)}`
27
+ : undefined;
28
+
29
+ const defaultPositions: Record<ResizeControlVariant, ControlPosition> = {
30
+ [ResizeControlVariant.Line]: 'right',
31
+ [ResizeControlVariant.Handle]: 'bottom-right',
32
+ };
33
+
34
+ function ResizeControl({
35
+ nodeId,
36
+ position,
37
+ variant = ResizeControlVariant.Handle,
38
+ className,
39
+ style = undefined,
40
+ children,
41
+ color,
42
+ minWidth = 10,
43
+ minHeight = 10,
44
+ maxWidth = Number.MAX_VALUE,
45
+ maxHeight = Number.MAX_VALUE,
46
+ keepAspectRatio = false,
47
+ resizeDirection,
48
+ autoScale = true,
49
+ shouldResize,
50
+ onResizeStart,
51
+ onResize,
52
+ onResizeEnd,
53
+ }: ResizeControlProps) {
54
+ const contextNodeId = useNodeId();
55
+ const id =
56
+ typeof nodeId === 'string' ? nodeId : contextNodeId;
57
+ const store = useStoreApi();
58
+ const resizeControlRef = useRef<HTMLDivElement | null>(null);
59
+ const isHandleControl = variant === ResizeControlVariant.Handle;
60
+ const scale = useStore(
61
+ useCallback(scaleSelector(isHandleControl && autoScale), [isHandleControl, autoScale]),
62
+ shallow,
63
+ );
64
+ const resizer = useRef<XYResizerInstance | null>(null);
65
+ const controlPosition = position ?? defaultPositions[variant];
66
+
67
+ useEffect(() => {
68
+ if (!resizeControlRef.current || !id) {
69
+ return;
70
+ }
71
+
72
+ if (!resizer.current) {
73
+ resizer.current = XYResizer({
74
+ domNode: resizeControlRef.current,
75
+ nodeId: id,
76
+ getStoreItems: () => {
77
+ const { nodeLookup, transform, snapGrid, snapToGrid, nodeOrigin, domNode } =
78
+ store.getState();
79
+ return {
80
+ nodeLookup,
81
+ transform,
82
+ snapGrid,
83
+ snapToGrid,
84
+ nodeOrigin,
85
+ paneDomNode: domNode,
86
+ };
87
+ },
88
+ onChange: (change: XYResizerChange, childChanges: XYResizerChildChange[]) => {
89
+ const { triggerNodeChanges, nodeLookup, parentLookup, nodeOrigin } = store.getState();
90
+ const changes: NodeChange[] = [];
91
+ const nextPosition = { x: change.x, y: change.y };
92
+ const node = nodeLookup.get(id);
93
+
94
+ if (node && node.expandParent && node.parentId) {
95
+ const origin = node.origin ?? nodeOrigin;
96
+ const width = change.width ?? node.measured.width ?? 0;
97
+ const height = change.height ?? node.measured.height ?? 0;
98
+
99
+ const child: ParentExpandChild = {
100
+ id: node.id,
101
+ parentId: node.parentId,
102
+ rect: {
103
+ width,
104
+ height,
105
+ ...evaluateAbsolutePosition(
106
+ {
107
+ x: change.x ?? node.position.x,
108
+ y: change.y ?? node.position.y,
109
+ },
110
+ { width, height },
111
+ node.parentId,
112
+ nodeLookup,
113
+ origin,
114
+ ),
115
+ },
116
+ };
117
+
118
+ const parentExpandChanges = handleExpandParent(
119
+ [child],
120
+ nodeLookup,
121
+ parentLookup,
122
+ nodeOrigin,
123
+ );
124
+ changes.push(...parentExpandChanges);
125
+
126
+ /*
127
+ * when the parent was expanded by the child node, its position will be clamped at
128
+ * 0,0 when node origin is 0,0 and to width, height if it's 1,1
129
+ */
130
+ nextPosition.x = change.x ? Math.max(origin[0] * width, change.x) : undefined;
131
+ nextPosition.y = change.y ? Math.max(origin[1] * height, change.y) : undefined;
132
+ }
133
+
134
+ if (nextPosition.x !== undefined && nextPosition.y !== undefined) {
135
+ const positionChange: NodePositionChange = {
136
+ id,
137
+ type: 'position',
138
+ position: { ...nextPosition as XYPosition },
139
+ };
140
+ changes.push(positionChange);
141
+ }
142
+
143
+ if (change.width !== undefined && change.height !== undefined) {
144
+ const setAttributes =
145
+ !resizeDirection
146
+ ? true
147
+ : resizeDirection === 'horizontal'
148
+ ? 'width'
149
+ : 'height';
150
+ const dimensionChange: NodeDimensionChange = {
151
+ id,
152
+ type: 'dimensions',
153
+ resizing: true,
154
+ setAttributes,
155
+ dimensions: {
156
+ width: change.width,
157
+ height: change.height,
158
+ },
159
+ };
160
+
161
+ changes.push(dimensionChange);
162
+ }
163
+
164
+ for (const childChange of childChanges) {
165
+ const positionChange: NodePositionChange = {
166
+ ...childChange,
167
+ type: 'position',
168
+ };
169
+
170
+ changes.push(positionChange);
171
+ }
172
+
173
+ triggerNodeChanges(changes);
174
+ },
175
+ onEnd: ({ width, height }) => {
176
+ const dimensionChange: NodeDimensionChange = {
177
+ id: id,
178
+ type: 'dimensions',
179
+ resizing: false,
180
+ dimensions: {
181
+ width,
182
+ height,
183
+ },
184
+ };
185
+ store.getState().triggerNodeChanges([dimensionChange]);
186
+ },
187
+ });
188
+ }
189
+
190
+ resizer.current.update({
191
+ controlPosition,
192
+ boundaries: {
193
+ minWidth,
194
+ minHeight,
195
+ maxWidth,
196
+ maxHeight,
197
+ },
198
+ keepAspectRatio,
199
+ resizeDirection,
200
+ onResizeStart,
201
+ onResize,
202
+ onResizeEnd,
203
+ shouldResize,
204
+ });
205
+
206
+ return () => {
207
+ resizer.current?.destroy();
208
+ };
209
+ }, [
210
+ controlPosition,
211
+ minWidth,
212
+ minHeight,
213
+ maxWidth,
214
+ maxHeight,
215
+ keepAspectRatio,
216
+ onResizeStart,
217
+ onResize,
218
+ onResizeEnd,
219
+ shouldResize,
220
+ ]);
221
+
222
+ const positionClassNames = controlPosition.split('-');
223
+
224
+ return <div
225
+ className={cc([
226
+ 'react-flow__resize-control',
227
+ 'nodrag',
228
+ ...positionClassNames,
229
+ variant,
230
+ className,
231
+ ])}
232
+ ref={resizeControlRef}
233
+ style={{
234
+ ...style,
235
+ scale,
236
+ ...color && { [isHandleControl ? 'backgroundColor' : 'borderColor']: color },
237
+ }}
238
+ >{children}</div>;
239
+ }
240
+
241
+ export function ResizeControlLine(props: ResizeControlLineProps) {
242
+ return <ResizeControl {...props} variant={ResizeControlVariant.Line} />;
243
+ }
244
+
245
+ /**
246
+ * To create your own resizing UI, you can use the `NodeResizeControl` component where you can pass children (such as icons).
247
+ * @public
248
+ *
249
+ */
250
+ export const NodeResizeControl = memo(ResizeControl);
@@ -0,0 +1,102 @@
1
+ import {
2
+ ResizeControlVariant,
3
+ XY_RESIZER_HANDLE_POSITIONS,
4
+ XY_RESIZER_LINE_POSITIONS,
5
+ } from '@xyflow/system';
6
+
7
+ import { NodeResizeControl } from './NodeResizeControl.tsrx';
8
+ import type { NodeResizerProps } from './types';
9
+
10
+ /**
11
+ * The `<NodeResizer />` component can be used to add a resize functionality to your
12
+ * nodes. It renders draggable controls around the node to resize in all directions.
13
+ * @public
14
+ *
15
+ * @example
16
+ *```jsx
17
+ *import { memo } from 'octane';
18
+ *import { Handle, Position, NodeResizer } from '@xyflow/react';
19
+ *
20
+ *function ResizableNode({ data }) {
21
+ * return (
22
+ * <>
23
+ * <NodeResizer minWidth={100} minHeight={30} />
24
+ * <Handle type="target" position={Position.Left} />
25
+ * <div style={{ padding: 10 }}>{data.label}</div>
26
+ * <Handle type="source" position={Position.Right} />
27
+ * </>
28
+ * );
29
+ *};
30
+ *
31
+ *export default memo(ResizableNode);
32
+ *```
33
+ */
34
+ export function NodeResizer({
35
+ nodeId,
36
+ isVisible = true,
37
+ handleClassName,
38
+ handleStyle,
39
+ lineClassName,
40
+ lineStyle,
41
+ color,
42
+ minWidth = 10,
43
+ minHeight = 10,
44
+ maxWidth = Number.MAX_VALUE,
45
+ maxHeight = Number.MAX_VALUE,
46
+ keepAspectRatio = false,
47
+ autoScale = true,
48
+ shouldResize,
49
+ onResizeStart,
50
+ onResize,
51
+ onResizeEnd,
52
+ }: NodeResizerProps) {
53
+ if (!isVisible) {
54
+ return null;
55
+ }
56
+
57
+ return <>
58
+ {XY_RESIZER_LINE_POSITIONS.map(
59
+ (position) =>
60
+ <NodeResizeControl
61
+ key={position}
62
+ className={lineClassName}
63
+ style={lineStyle}
64
+ nodeId={nodeId}
65
+ position={position}
66
+ variant={ResizeControlVariant.Line}
67
+ color={color}
68
+ minWidth={minWidth}
69
+ minHeight={minHeight}
70
+ maxWidth={maxWidth}
71
+ maxHeight={maxHeight}
72
+ onResizeStart={onResizeStart}
73
+ keepAspectRatio={keepAspectRatio}
74
+ autoScale={autoScale}
75
+ shouldResize={shouldResize}
76
+ onResize={onResize}
77
+ onResizeEnd={onResizeEnd}
78
+ />,
79
+ )}
80
+ {XY_RESIZER_HANDLE_POSITIONS.map(
81
+ (position) =>
82
+ <NodeResizeControl
83
+ key={position}
84
+ className={handleClassName}
85
+ style={handleStyle}
86
+ nodeId={nodeId}
87
+ position={position}
88
+ color={color}
89
+ minWidth={minWidth}
90
+ minHeight={minHeight}
91
+ maxWidth={maxWidth}
92
+ maxHeight={maxHeight}
93
+ onResizeStart={onResizeStart}
94
+ keepAspectRatio={keepAspectRatio}
95
+ autoScale={autoScale}
96
+ shouldResize={shouldResize}
97
+ onResize={onResize}
98
+ onResizeEnd={onResizeEnd}
99
+ />,
100
+ )}
101
+ </>;
102
+ }
@@ -0,0 +1,4 @@
1
+ export { NodeResizer } from './NodeResizer.tsrx';
2
+ export { NodeResizeControl } from './NodeResizeControl.tsrx';
3
+
4
+ export * from './types';
@@ -0,0 +1,122 @@
1
+ import type { CSSProperties, OctaneNode } from '../../react-shim.js';
2
+ import type {
3
+ ControlPosition,
4
+ ControlLinePosition,
5
+ ResizeControlVariant,
6
+ ResizeControlDirection,
7
+ ShouldResize,
8
+ OnResizeStart,
9
+ OnResize,
10
+ OnResizeEnd,
11
+ } from '@xyflow/system';
12
+
13
+ /**
14
+ * @expand
15
+ */
16
+ export type NodeResizerProps = {
17
+ /**
18
+ * Id of the node it is resizing.
19
+ * @remarks optional if used inside custom node
20
+ */
21
+ nodeId?: string;
22
+ /** Color of the resize handle. */
23
+ color?: string;
24
+ /** Class name applied to handle. */
25
+ handleClassName?: string;
26
+ /** Style applied to handle. */
27
+ handleStyle?: CSSProperties;
28
+ /** Class name applied to line. */
29
+ lineClassName?: string;
30
+ /** Style applied to line. */
31
+ lineStyle?: CSSProperties;
32
+ /**
33
+ * Are the controls visible.
34
+ * @default true
35
+ */
36
+ isVisible?: boolean;
37
+ /**
38
+ * Minimum width of node.
39
+ * @default 10
40
+ */
41
+ minWidth?: number;
42
+ /**
43
+ * Minimum height of node.
44
+ * @default 10
45
+ */
46
+ minHeight?: number;
47
+ /**
48
+ * Maximum width of node.
49
+ * @default Number.MAX_VALUE
50
+ */
51
+ maxWidth?: number;
52
+ /**
53
+ * Maximum height of node.
54
+ * @default Number.MAX_VALUE
55
+ */
56
+ maxHeight?: number;
57
+ /**
58
+ * Keep aspect ratio when resizing.
59
+ * @default false
60
+ */
61
+ keepAspectRatio?: boolean;
62
+ /**
63
+ * Scale the controls with the zoom level.
64
+ * @default true
65
+ */
66
+ autoScale?: boolean;
67
+ /** Callback to determine if node should resize. */
68
+ shouldResize?: ShouldResize;
69
+ /** Callback called when resizing starts. */
70
+ onResizeStart?: OnResizeStart;
71
+ /** Callback called when resizing. */
72
+ onResize?: OnResize;
73
+ /** Callback called when resizing ends. */
74
+ onResizeEnd?: OnResizeEnd;
75
+ };
76
+
77
+ /**
78
+ * @expand
79
+ */
80
+ export type ResizeControlProps = Pick<
81
+ NodeResizerProps,
82
+ | 'nodeId'
83
+ | 'color'
84
+ | 'minWidth'
85
+ | 'minHeight'
86
+ | 'maxWidth'
87
+ | 'maxHeight'
88
+ | 'keepAspectRatio'
89
+ | 'shouldResize'
90
+ | 'autoScale'
91
+ | 'onResizeStart'
92
+ | 'onResize'
93
+ | 'onResizeEnd'
94
+ > & {
95
+ /**
96
+ * Position of the control.
97
+ * @example ControlPosition.TopLeft, ControlPosition.TopRight,
98
+ * ControlPosition.BottomLeft, ControlPosition.BottomRight
99
+ */
100
+ position?: ControlPosition;
101
+ /**
102
+ * Variant of the control.
103
+ * @default "handle"
104
+ * @example ResizeControlVariant.Handle, ResizeControlVariant.Line
105
+ */
106
+ variant?: ResizeControlVariant;
107
+ /**
108
+ * The direction the user can resize the node.
109
+ * If not provided, the user can resize in any direction.
110
+ */
111
+ resizeDirection?: ResizeControlDirection;
112
+ className?: string;
113
+ style?: CSSProperties;
114
+ children?: OctaneNode;
115
+ };
116
+
117
+ /**
118
+ * @expand
119
+ */
120
+ export type ResizeControlLineProps = Omit<ResizeControlProps, 'resizeDirection'> & {
121
+ position?: ControlLinePosition;
122
+ };
@@ -0,0 +1,141 @@
1
+ import { useCallback } from 'octane';
2
+ import type { CSSProperties } from '../../react-shim.js';
3
+ import cc from 'classcat';
4
+ import { shallow } from '@octanejs/zustand/shallow';
5
+ import {
6
+ Position,
7
+ getNodeToolbarTransform,
8
+ getInternalNodesBounds,
9
+ NodeLookup,
10
+ } from '@xyflow/system';
11
+
12
+ import { InternalNode, ReactFlowState } from '../../types';
13
+ import { useStore } from '../../hooks/useStore';
14
+ import { useNodeId } from '../../contexts/NodeIdContext';
15
+ import { NodeToolbarPortal } from './NodeToolbarPortal.tsrx';
16
+ import type { NodeToolbarProps } from './types';
17
+
18
+ const nodeEqualityFn = (a?: InternalNode, b?: InternalNode) =>
19
+ a?.internals.positionAbsolute.x !== b?.internals.positionAbsolute.x ||
20
+ a?.internals.positionAbsolute.y !== b?.internals.positionAbsolute.y ||
21
+ a?.measured.width !== b?.measured.width ||
22
+ a?.measured.height !== b?.measured.height ||
23
+ a?.selected !== b?.selected ||
24
+ a?.internals.z !== b?.internals.z;
25
+
26
+ const nodesEqualityFn = (a: NodeLookup, b: NodeLookup) => {
27
+ if (a.size !== b.size) {
28
+ return false;
29
+ }
30
+
31
+ for (const [key, node] of a) {
32
+ if (nodeEqualityFn(node, b.get(key))) {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ return true;
38
+ };
39
+
40
+ const storeSelector = (state: ReactFlowState) => ({
41
+ x: state.transform[0],
42
+ y: state.transform[1],
43
+ zoom: state.transform[2],
44
+ selectedNodesCount: state.nodes.filter((node) => node.selected).length,
45
+ });
46
+
47
+ /**
48
+ * This component can render a toolbar or tooltip to one side of a custom node. This
49
+ * toolbar doesn't scale with the viewport so that the content is always visible.
50
+ *
51
+ * @public
52
+ * @example
53
+ * ```jsx
54
+ *import { memo } from 'octane';
55
+ *import { Handle, Position, NodeToolbar } from '@xyflow/react';
56
+ *
57
+ *function CustomNode({ data }) {
58
+ * return (
59
+ * <>
60
+ * <NodeToolbar isVisible={data.toolbarVisible} position={data.toolbarPosition}>
61
+ * <button>delete</button>
62
+ * <button>copy</button>
63
+ * <button>expand</button>
64
+ * </NodeToolbar>
65
+ *
66
+ * <div style={{ padding: '10px 20px' }}>
67
+ * {data.label}
68
+ * </div>
69
+ *
70
+ * <Handle type="target" position={Position.Left} />
71
+ * <Handle type="source" position={Position.Right} />
72
+ * </>
73
+ * );
74
+ *};
75
+ *
76
+ *export default memo(CustomNode);
77
+ *```
78
+ * @remarks By default, the toolbar is only visible when a node is selected. If multiple
79
+ * nodes are selected it will not be visible to prevent overlapping toolbars or
80
+ * clutter. You can override this behavior by setting the `isVisible` prop to `true`.
81
+ */
82
+ export function NodeToolbar({
83
+ nodeId,
84
+ children,
85
+ className,
86
+ style,
87
+ isVisible,
88
+ position = Position.Top,
89
+ offset = 10,
90
+ align = 'center',
91
+ ...rest
92
+ }: NodeToolbarProps) {
93
+ const contextNodeId = useNodeId();
94
+
95
+ const nodesSelector = useCallback((state: ReactFlowState): NodeLookup => {
96
+ const nodeIds = Array.isArray(nodeId) ? nodeId : [nodeId || contextNodeId || ''];
97
+ const internalNodes = nodeIds.reduce<NodeLookup>((res, id) => {
98
+ const node = state.nodeLookup.get(id);
99
+ if (node) {
100
+ res.set(node.id, node);
101
+ }
102
+
103
+ return res;
104
+ }, new Map());
105
+
106
+ return internalNodes;
107
+ }, [nodeId, contextNodeId]);
108
+ const nodes = useStore(nodesSelector, nodesEqualityFn);
109
+ const { x, y, zoom, selectedNodesCount } = useStore(storeSelector, shallow);
110
+
111
+ // if isVisible is not set, we show the toolbar only if its node is selected and no other node is selected
112
+ const isActive =
113
+ typeof isVisible === 'boolean'
114
+ ? isVisible
115
+ : nodes.size === 1 && nodes.values().next().value?.selected && selectedNodesCount === 1;
116
+
117
+ if (!isActive || !nodes.size) {
118
+ return null;
119
+ }
120
+
121
+ const nodeRect = getInternalNodesBounds(nodes);
122
+ const nodesArray = Array.from(nodes.values());
123
+ const zIndex = Math.max(...nodesArray.map((node) => node.internals.z + 1));
124
+
125
+ const wrapperStyle: CSSProperties = {
126
+ position: 'absolute',
127
+ transform: getNodeToolbarTransform(nodeRect, { x, y, zoom }, position, offset, align),
128
+ zIndex,
129
+ ...style,
130
+ };
131
+
132
+ return <NodeToolbarPortal>
133
+ <div
134
+ style={wrapperStyle}
135
+ className={cc(['react-flow__node-toolbar', className])}
136
+ {...rest}
137
+ // @todo: check if we could only do this for non-prod envs
138
+ data-id={nodesArray.reduce((acc, node) => `${acc}${node.id} `, '').trim()}
139
+ >{children}</div>
140
+ </NodeToolbarPortal>;
141
+ }
@@ -0,0 +1,17 @@
1
+ import { OctaneNode } from 'octane';
2
+ import { createPortal } from 'octane';
3
+
4
+ import type { ReactFlowState } from '../../types';
5
+ import { useStore } from '../../hooks/useStore';
6
+
7
+ const selector = (state: ReactFlowState) => state.domNode?.querySelector('.react-flow__renderer');
8
+
9
+ export function NodeToolbarPortal({ children }: { children: OctaneNode }) {
10
+ const wrapperRef = useStore(selector);
11
+
12
+ if (!wrapperRef) {
13
+ return null;
14
+ }
15
+
16
+ return createPortal(children, wrapperRef);
17
+ }
@@ -0,0 +1,2 @@
1
+ export { NodeToolbar } from './NodeToolbar.tsrx';
2
+ export type { NodeToolbarProps } from './types';
@@ -0,0 +1,32 @@
1
+ import type { HTMLAttributes } from '../../react-shim.js';
2
+ import type { Position, Align } from '@xyflow/system';
3
+
4
+ /**
5
+ * @expand
6
+ */
7
+ export type NodeToolbarProps = HTMLAttributes<HTMLDivElement> & {
8
+ /**
9
+ * By passing in an array of node id's you can render a single tooltip for a group or collection
10
+ * of nodes.
11
+ */
12
+ nodeId?: string | string[];
13
+ /** If `true`, node toolbar is visible even if node is not selected. */
14
+ isVisible?: boolean;
15
+ /**
16
+ * Position of the toolbar relative to the node.
17
+ * @default Position.Top
18
+ * @example Position.TopLeft, Position.TopRight, Position.BottomLeft, Position.BottomRight
19
+ */
20
+ position?: Position;
21
+ /**
22
+ * The space between the node and the toolbar, measured in pixels.
23
+ * @default 10
24
+ */
25
+ offset?: number;
26
+ /**
27
+ * Align the toolbar relative to the node.
28
+ * @default "center"
29
+ * @example Align.Start, Align.Center, Align.End
30
+ */
31
+ align?: Align;
32
+ };
@@ -0,0 +1,6 @@
1
+ export * from './Background/index.tsrx';
2
+ export * from './Controls/index.tsrx';
3
+ export * from './MiniMap/index';
4
+ export * from './NodeResizer/index';
5
+ export * from './NodeToolbar/index.tsrx';
6
+ export * from './EdgeToolbar/index.tsrx';