Creating diagrams with React, SVG, and css-layout


This article shows our process of creating diagrams using React, SVG, and Facebook’s css-layout library. At Rescale, we provide a web UI for our customers to set up a license proxy server for communication between our compute nodes and their on-premise license servers. A diagram is a great way to show our users how these servers communicate with each other.

Here’s an example of the diagram we created with this technique. The controls are just to show that React can re-render the diagram when state changes.


SVG is a good choice for creating diagrams within React because it’s just part of the DOM. However, laying out SVG elements is not as simple as using CSS with HTML because SVG uses its own coordinate system. That’s where Facebook’s css-layout comes in. It allows us to use CSS’s flexbox model to lay out our SVG elements.

With React, SVG, and css-layout, we are able to:

  • Dynamically change the diagram due to changes in a component’s props or state
  • Iterate quickly on requirement changes
  • Localize to other languages using our existing method of localizing the UI


To begin creating the diagram, we wireframe it using HTML with the default styles as written in css-layout’s README:

The wireframe of the diagram in HTML looks like this:

This produces:


Borders were added to each div just to show their boundaries. A couple of things to keep in mind:

  • We should only be using css-layout’s supported attributes.
  • Positioning text in SVG will still be a somewhat manual process, especially vertical centering.

Creating the css-layout tree from the wireframe

In css-layout, a node is just an object with { style: { … }, children: [ nodes ] }. The root node is considered the tree. Running computeLayout() on the tree will populate all of its nodes with a layout object that contains width, height, top, left, right, and bottom. This is what we’ll use to position the SVG elements.

Creating the tree from the wireframe is just a simple read off of the HTML and ids are given to each node so that we can access the layout property of each node by id later.

Rendering with React

We have each of our nodes populated with the layout property. Making these layout objects accessible by id is pretty simple:

The l object contains all of the layout information accessible by the id given to each node. With it we can now use a combination of , , and to render the diagram in React:

As you can see, we’re using the layout properties computed by css-layout to position the elements or set their widths and heights. Things to note:

  • The top, right, bottom, and left properties in the layout objects are relative to its parent node. In SVG, we use so that each child of the element is positioned relative to that transformation.
  • Positioning text is somewhat manual as mentioned above. The textAnchor property can handle horizontal centering, but we need to use the dy property with some magic numbers to handle vertical centering.

This technique was inspired by this blog post that uses css-layout to lay out D3 charts.

This article was written by Kenneth Chung.