Coder Social home page Coder Social logo

Comments (4)

moklick avatar moklick commented on June 19, 2024

You could use onNodesChange for this. Whenever nodes change their dimensions, a change of type "dimensions" gets fired:

const onNodesChange = useCallback((changes) => {
  setNodes((nds) => {
    const nextNodes = applyNodeChanges(changes, nds);
    
    if (changes.some((change) => change.type === 'dimension')) {
      // layout nodes
    }

    return nextNodes;
  });
}, []);

from xyflow.

LiorLivyatan avatar LiorLivyatan commented on June 19, 2024

Thank you for the super fast answer!

Unfortunately the div inside the node is being changed, but I do not get any changes (except for selected change).
I also do not understand how to trigger the layout change.

Thanks again!

from xyflow.

moklick avatar moklick commented on June 19, 2024

You don't have to do anything. React Flow adds a resize observer to every node and should send the changes automatically. It's fine to resize an inner div, but it's important that the outer node doesn't have a fixed width or height and also changes it's size based on the content.

Are you using v11 or v12? Could you send a codesandbox or repo we could check out?

from xyflow.

LiorLivyatan avatar LiorLivyatan commented on June 19, 2024

Thank you, I think I understand what is happening.

I am using dagre to position the nodes and edges, and this indeed has a fixed height and width as per the example from the docs:

const dagreGraph = new dagre.graphlib.Graph()
dagreGraph.setDefaultEdgeLabel(() => ({}))

const nodeWidth = 250
const nodeHeight = 60

const getLayoutedElements = (nodes: any, edges: any, direction = 'TB') => {
  dagreGraph.setGraph({ rankdir: direction })

  nodes.forEach((node: any) => {
    dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight })
  })

  edges.forEach((edge: any) => {
    dagreGraph.setEdge(edge.source, edge.target)
  })

  dagre.layout(dagreGraph)

  nodes.forEach((node: any) => {
    const nodeWithPosition = dagreGraph.node(node.id)
    node.targetPosition = 'top'
    node.sourcePosition = 'bottom'

    node.position = {
      x: nodeWithPosition.x - nodeWidth / 2,
      y: nodeWithPosition.y - nodeHeight / 2,
    }

    return node
  })

  return { nodes, edges }
}

Is it possible to let dagre "decide" about the height and width of the nodes instead of hardcoding them?

Thanks! :)

from xyflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.