#Question: How to Keep Edge Length Static While Dynamically Resizing Nodes in React Flow?

1 messages · Page 1 of 1 (latest)

tiny quest
#

I have a custom node component where I dynamically adjust the size of parent nodes when a child node is clicked. This is achieved by toggling a property (isVisible) in the data object, which alters the width of the node. Here’s a snippet of the component for context:

import React from 'react';
import { Handle, Position } from 'reactflow';

const CustomNode = ({ data }) => (
  <>
    <div
      style={{
        width: data.isVisible ? '50px' : '150px',
        height: '50px',
        padding: '3px',
        backgroundColor: '#FFFFFF',
        border: data.isHighlighted ? '2px solid #002953' : '0.5px solid #C5C5C5',
        borderRadius: '10px',
        display: data.isVisible ? 'block' : 'flex',
        position: 'relative',
      }}
    >
      {/* Other UI content */}
    </div>
    <Handle
      type="target"
      position={Position.Left}
      id="leftHandle"
      style={{ opacity: '0', left: data.isVisible ? '0px' : '8px' }}
    />
    <Handle
      type="source"
      position={Position.Right}
      id="rightHandle"
      style={{ opacity: '0', right: data.isVisible ? '50px' : '0' }}
    />
  </>
);

export default CustomNode;

Here’s my challenge:

  1. When resizing the parent node (isVisible: true), the edges shrink or expand to accommodate the size of the node.
    I want the edge length to remain static, ensuring that resizing the node does not affect the positions or lengths of the edges.

Could anyone guide me on how to: Maintain static edge lengths when resizing nodes?

I’ve created a sandbox to demonstrate the issue: -
https://codesandbox.io/p/live/26283239-5bac-478d-b551-29142015579e

split crypt
split crypt
#

Try again, should be public now

tiny quest
#

What you did in code??

split crypt
#

If edges stay "static" in length, how exactly do you expect there not to be a gap? 🤔

#

That's why I said it looks strange and I'm not sure what you're even trying to accomplish ^^
A visual would help tbh

tiny quest
tiny quest
#

Hi @split crypt what mistake i am doing?? I want something similar to above video org chart