projectstorm / react-diagrams

a super simple, no-nonsense diagramming library written in react that just works
https://projectstorm.cloud/react-diagrams
MIT License
8.45k stars 1.16k forks source link

Remove setTimeout from demo-dagre to avoid layout breaks #1025

Closed TenTakano closed 4 months ago

TenTakano commented 6 months ago

Checklist

What, why and how?

I removed 'setTimeout' from the demo-dagre sample to avoid layout breaks or flickering when the page is loaded. These issues have been documented below, and it currently seems that they have not yet been fundamentally resolved. Additionally, I have identified another related issue: in some cases, the positions of nodes and links are reset to the origin.

Layout breaks occur due to re-rendering components using CanvasWidget or improper sequencing in the usage of DagreEngine. This re-rendering causes the recreation of DiagramEngine. Consequently, the forceUpdate event in CanvasWidget is sometimes lost. To avoid this issue, DiagramEngine should be maintained.

The reason for flickering is that nodes are placed at the origin until DagreEngine distributes items after the timer ticks. DagreEngine's distribution should run after rendering because it needs the width and height of nodes to calculate their positions accurately. The positions of ports are set in the useEffect (equivalent to componentDidMount) phase, which directly refers to DOM components. Therefore, I have moved the timing of DagreEngine's calculations to useLayoutEffect, which executes between render and useEffect.

Feel good image:

image

changeset-bot[bot] commented 6 months ago

🦋 Changeset detected

Latest commit: 1be40737f5ce6460061e41c2dbc7ccd4fe4f2a33

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------------------------------------ | ----- | | @projectstorm/react-diagrams-gallery | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR