bumbeishvili / org-chart

Highly customizable org chart. Integrations available for Angular, React, Vue
https://stackblitz.com/edit/web-platform-o5t1ha
MIT License
928 stars 330 forks source link

How to return to the previous position when the modal is closed ? #262

Closed DCUndairstand closed 1 year ago

DCUndairstand commented 1 year ago

Hello, Here's my problem, when I click on an entity the modal with metadata is displayed at the right of the window with a slide left animation, when it is open I use a state [previousTransform, setPreviousTransform] that keeps the position of the orgchart before the modal is opened. Then I call the setCentered method to center on the clicked entity. When I closed the modal, I modify the position of the flowchart using the previousTransform data. It works but when I want to move on the orgchart, the position of the orgchart is set back to the last known position when the modal was open instead of moving from the last assigned position, why ?

Here's my code : `

const [previousTransform, setPreviousTransform] = useState(null);

const handleShow = (): boolean | void => {

const chartContainer = document.getElementById('org').firstElementChild.firstElementChild;

if(chartContainer.getAttribute('transform') == null){

  chartContainer.setAttribute('transform', 'translate(0,0) scale(1)');
  setPreviousTransform(chartContainer.getAttribute('transform'));

 } else {

    setPreviousTransform(chartContainer.getAttribute('transform'));

 };

setCardShow(false);

setTimeout(() => { setCardShow(true) }, 150);
};

const handleClose = (): boolean | void => {setCardShow(false);

const chartContainer = document.getElementById('org').firstElementChild.firstElementChild;
chartContainer.setAttribute('transform', previousTransform);

}

const handleTransitionChange = () => {
if(cardShow){

  chartRef.current.setCentered(employeeId).render() ; 

}  
};

`

Thank you in advance !

bumbeishvili commented 1 year ago

Hi, it's not that simple.

The state of that movement is not bound to that chartContainer element.

In your place, I'd check how the zoom in is implemented

https://github.com/bumbeishvili/org-chart/blob/c8ebb0490ecfcf1424c16a8528a4a835d3efde53/src/d3-org-chart.js#L1397-L1400

And would try to find a solution which involves translateBy instead of scaleBy which is used in zoom in

https://github.com/d3/d3-zoom#zoom_scaleBy