-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2626 from wbkd/feat/resize-node
Feat: Add NodeResizer package
- Loading branch information
Showing
36 changed files
with
858 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@reactflow/node-toolbar': patch | ||
--- | ||
|
||
Get nodeId from React Flow context if it is not passed explicitly as prop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@reactflow/core': patch | ||
--- | ||
|
||
Export the useNodeId hook, refactor how changes are applied and create a helper function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@reactflow/node-resizer': major | ||
--- | ||
|
||
Add a node resizer component that can be used to resize a custom node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/vite-app/src/examples/NodeResizer/CustomResizer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { memo, FC, CSSProperties } from 'react'; | ||
import { Handle, Position, NodeProps } from 'reactflow'; | ||
|
||
import { NodeResizer, NodeResizeControl } from '@reactflow/node-resizer'; | ||
import '@reactflow/node-resizer/dist/style.css'; | ||
import ResizeIcon from './ResizeIcon'; | ||
|
||
const controlStyle = { | ||
background: 'transparent', | ||
border: 'none', | ||
}; | ||
|
||
const CustomNode: FC<NodeProps> = ({ id, data }) => { | ||
return ( | ||
<> | ||
<NodeResizeControl style={controlStyle}> | ||
<ResizeIcon /> | ||
</NodeResizeControl> | ||
|
||
<Handle type="target" position={Position.Left} /> | ||
<div>{data.label}</div> | ||
<Handle type="source" position={Position.Right} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(CustomNode); |
19 changes: 19 additions & 0 deletions
19
examples/vite-app/src/examples/NodeResizer/CustomResizer2.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { memo, FC } from 'react'; | ||
import { Handle, Position, NodeProps } from 'reactflow'; | ||
|
||
import { NodeResizer, NodeResizeControl } from '@reactflow/node-resizer'; | ||
import '@reactflow/node-resizer/dist/style.css'; | ||
|
||
const CustomNode: FC<NodeProps> = ({ id, data }) => { | ||
return ( | ||
<> | ||
<NodeResizeControl color="red" position="top" /> | ||
<NodeResizeControl color="red" position="bottom" /> | ||
<Handle type="target" position={Position.Left} /> | ||
<div style={{ padding: 10 }}>{data.label}</div> | ||
<Handle type="source" position={Position.Right} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(CustomNode); |
18 changes: 18 additions & 0 deletions
18
examples/vite-app/src/examples/NodeResizer/NodeResizerNode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { memo, FC } from 'react'; | ||
import { Handle, Position, NodeProps } from 'reactflow'; | ||
|
||
import { NodeResizer } from '@reactflow/node-resizer'; | ||
import '@reactflow/node-resizer/dist/style.css'; | ||
|
||
const CustomNode: FC<NodeProps> = ({ id, data, selected }) => { | ||
return ( | ||
<> | ||
<NodeResizer isVisible={selected} /> | ||
<Handle type="target" position={Position.Left} /> | ||
<div>{data.label}</div> | ||
<Handle type="source" position={Position.Right} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(CustomNode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function ResizeIcon() { | ||
return ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="8" | ||
height="8" | ||
viewBox="0 0 24 24" | ||
strokeWidth="2" | ||
stroke="currentColor" | ||
fill="none" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
style={{ position: 'absolute', right: 2, bottom: 2 }} | ||
> | ||
<path stroke="none" d="M0 0h24v24H0z" fill="none" /> | ||
<polyline points="16 20 20 20 20 16" /> | ||
<line x1="14" y1="14" x2="20" y2="20" /> | ||
<polyline points="8 4 4 4 4 8" /> | ||
<line x1="4" y1="4" x2="10" y2="10" /> | ||
</svg> | ||
); | ||
} | ||
|
||
export default ResizeIcon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { CSSProperties, useCallback, useState } from 'react'; | ||
import ReactFlow, { Controls, addEdge, Position, Connection, useNodesState, useEdgesState, Panel } from 'reactflow'; | ||
|
||
import NodeResizerNode from './NodeResizerNode'; | ||
import CustomResizer from './CustomResizer'; | ||
import CustomResizer2 from './CustomResizer2'; | ||
|
||
const nodeTypes = { | ||
resizer: NodeResizerNode, | ||
customResizer: CustomResizer, | ||
customResizer2: CustomResizer2, | ||
}; | ||
|
||
const initialEdges = [ | ||
{ | ||
id: 'e1-2', | ||
source: '1', | ||
target: '2', | ||
}, | ||
]; | ||
|
||
const initialNodes = [ | ||
{ | ||
id: '1', | ||
type: 'input', | ||
data: { label: 'An input node' }, | ||
position: { x: 0, y: 0 }, | ||
sourcePosition: Position.Right, | ||
}, | ||
{ | ||
id: '2', | ||
type: 'resizer', | ||
data: { label: 'default resizer' }, | ||
position: { x: 250, y: 0 }, | ||
style: { | ||
width: 200, | ||
height: 150, | ||
border: '1px solid #222', | ||
fontSize: 10, | ||
}, | ||
}, | ||
{ | ||
id: '3', | ||
type: 'customResizer', | ||
data: { label: 'resize control with child component' }, | ||
position: { x: 250, y: 150 }, | ||
style: { border: '1px solid #222', fontSize: 10, width: 100 }, | ||
parentNode: '2', | ||
}, | ||
{ | ||
id: '4', | ||
type: 'customResizer2', | ||
data: { label: 'resize controls' }, | ||
position: { x: 100, y: 150 }, | ||
style: { border: '1px solid #222', fontSize: 10 }, | ||
parentNode: '2', | ||
}, | ||
{ | ||
id: '5', | ||
type: 'customResizer2', | ||
data: { label: 'min width and height' }, | ||
position: { x: 100, y: 150 }, | ||
style: { border: '1px solid #222', fontSize: 10 }, | ||
}, | ||
]; | ||
|
||
const CustomNodeFlow = () => { | ||
const [snapToGrid, setSnapToGrid] = useState(false); | ||
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); | ||
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); | ||
|
||
const onConnect = useCallback( | ||
(connection: Connection) => setEdges((eds) => addEdge({ ...connection }, eds)), | ||
[setEdges] | ||
); | ||
|
||
return ( | ||
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
onConnect={onConnect} | ||
nodeTypes={nodeTypes} | ||
minZoom={-5} | ||
maxZoom={5} | ||
snapToGrid={snapToGrid} | ||
> | ||
<Controls /> | ||
<Panel position="bottom-right"> | ||
<button onClick={() => setSnapToGrid(!snapToGrid)}>snapToGrid: {snapToGrid ? 'on' : 'off'}</button> | ||
</Panel> | ||
</ReactFlow> | ||
); | ||
}; | ||
|
||
export default CustomNodeFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { createContext } from 'react'; | ||
import { createContext, useContext } from 'react'; | ||
|
||
export const NodeIdContext = createContext<string | null>(null); | ||
export const Provider = NodeIdContext.Provider; | ||
export const Consumer = NodeIdContext.Consumer; | ||
|
||
export const useNodeId = (): string | null => { | ||
const nodeId = useContext(NodeIdContext); | ||
return nodeId; | ||
}; | ||
|
||
export default NodeIdContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { useStoreApi } from './useStore'; | ||
import type { UseDragEvent } from '../types'; | ||
|
||
function useGetPointerPosition() { | ||
const store = useStoreApi(); | ||
|
||
// returns the pointer position projected to the RF coordinate system | ||
const getPointerPosition = useCallback(({ sourceEvent }: UseDragEvent) => { | ||
const { transform, snapGrid, snapToGrid } = store.getState(); | ||
const x = sourceEvent.touches ? sourceEvent.touches[0].clientX : sourceEvent.clientX; | ||
const y = sourceEvent.touches ? sourceEvent.touches[0].clientY : sourceEvent.clientY; | ||
|
||
const pointerPos = { | ||
x: (x - transform[0]) / transform[2], | ||
y: (y - transform[1]) / transform[2], | ||
}; | ||
|
||
// we need the snapped position in order to be able to skip unnecessary drag events | ||
return { | ||
xSnapped: snapToGrid ? snapGrid[0] * Math.round(pointerPos.x / snapGrid[0]) : pointerPos.x, | ||
ySnapped: snapToGrid ? snapGrid[1] * Math.round(pointerPos.y / snapGrid[1]) : pointerPos.y, | ||
...pointerPos, | ||
}; | ||
}, []); | ||
|
||
return getPointerPosition; | ||
} | ||
|
||
export default useGetPointerPosition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.