Comments (5)
Interesting, how would we go about rendering the node on the canvas like this while cursor down? Just concat into the nodes list?
Seems potentially quite render intensive to have reactflow re-render the entire list of nodes for every frame of the drag?
I will definitely have a try and report back here, just couldn't find any references to a solution to this so best case we just document a solution in this issue!
from xyflow.
Okay yep you're right @peterkogo this is possible in userland.
there are some weird visual glitches, not sure what that's about but seemingly canvas related? (see the persistent red lines that stick around sometimes)
Not sure how well it would respond to a more expensive nodes list given this may force ReactFlow to do quite a bit of rendering internally, but it definitely works :)
Key gubbins from my prototyping:
const reactFlowInstance = useReactFlow() // obviously need to wrap the component in ReactFlowProvider to hoist this value upward
const [drawing, setDrawing] = useState<false | { x: number; y: number }>(
false,
)
const [drawingPosition, setDrawingPosition] = useState<
false | { x: number; y: number }
>(false)
return <ReactFlow
nodes={
drawing && drawingPosition
? nodes.concat({
id: 'DRAWING',
type: 'box',
position: {
x: Math.min(drawingPosition.x, drawing.x),
y: Math.min(drawingPosition.y, drawing.y),
},
data: { },
style: {
width: Math.abs(drawingPosition.x - drawing.x),
height: Math.abs(drawingPosition.y - drawing.y),
},
})
: nodes
}
selectionOnDrag={false}
onPointerDown={(e) => {
const element = e.target as HTMLElement | undefined
if (element?.className.includes('react-flow__pane')) {
const x = e.clientX
const y = e.clientY
const position = reactFlowInstance.screenToFlowPosition({
x,
y,
})
setDrawing(position)
setDrawingPosition(position)
console.log('DOWN', e.target, e.clientX, e.clientY)
}
}}
onPointerLeave={() => {
setDrawing(false)
setDrawingPosition(false)
}}
onPointerMove={(e) => {
if (drawing) {
const x = e.clientX
const y = e.clientY
const position = reactFlowInstance.screenToFlowPosition({
x,
y,
})
setDrawingPosition(position)
}
}}
onPointerUp={(e) => {
console.log('UP', e.target, e.clientX, e.clientY)
setDrawing(false)
setDrawingPosition(false)
}}
/>
from xyflow.
As an addition you can also just add a CustomNode onPointerDown, and have an absolutely positioned child of the CustomNode that you resize. This should not trigger dimension updates inside ReactFlow and might be a bit more efficient.
You can also render a custom component inside the viewport through the portal. (there is an extra ViewportPortal you can use in v12 for this, but it is essentially the same thing)
With this approach you can bail out of react flows logic while resizing.
from xyflow.
Why is this currently not achievable in user-land?
If you set panOnDrag
to false whenever your node creation is happening and start listening to pointerdown & pointermove events on the React Flow dom node you should be able to achieve this
from xyflow.
Thanks for the extra details! Those do both sound like better options. Looking forward to v12 as there are so many great improvements there! 🙂
from xyflow.
Related Issues (20)
- [Svelte Flow]: Unable to change edge type HOT 1
- zoomOnPinch not working when preventScrolling is false HOT 2
- useOnSelectionChange returns an empty nodes array HOT 5
- [SvelteFlow] Bad performance in svelte-5 HOT 4
- `onbeforedelete` should give back `event` HOT 3
- [Docs] Add Bun
- [SvelteFlow] Node internals not updating dynamic handles correctly HOT 1
- Copy & Paste issue HOT 2
- Warning: Each child in a list should have a unique "key" prop. HOT 2
- Support key combinations for selection, pan and zoom HOT 1
- `Handle` is supposed to accept `isValidConnection` prop but it is not supported HOT 1
- `Minimap` cannot be interacted with on a touch device HOT 8
- onDrag node param has previous position with grid and snapToGrip enabled HOT 4
- Unable to render all the nodes and edges HOT 1
- updateNodeData does not run consistently in 12.0.0-next.12, next.13, next.14 HOT 5
- 90% GPU utilization for animated edges HOT 2
- Edges not rendering with production build. HOT 5
- Get relative node position on Edge Drop for new node with parentNode HOT 1
- It is not possible to call 'layout' with 'useNodesInitialized()' and controlled flow HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xyflow.