Skip to content

Commit 255cf0a

Browse files
committed
front: fix speedspacechart-resize-behavior
Signed-off-by: Yohh <durandyohan@zaclys.net>
1 parent 7022b12 commit 255cf0a

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

front/src/modules/simulationResult/components/SpeedSpaceChart/SpeedSpaceChartContainer.tsx

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22

33
import { SpeedSpaceChart } from '@osrd-project/ui-speedspacechart';
44
import type {
@@ -45,7 +45,8 @@ const SpeedSpaceChartContainer = ({
4545
const [baseHeightOfSpeedSpaceChart, setBaseHeightOfSpeedSpaceChart] =
4646
useState(heightOfSpeedSpaceChart);
4747

48-
const containerWidth = document.getElementById('container-SpeedSpaceChart')?.clientWidth;
48+
const root = useRef<HTMLDivElement>(null);
49+
const [containerWidth, setContainerWidth] = useState<number>(root.current?.clientWidth || 0);
4950

5051
const speedSpaceChartData = formatData(
5152
trainSimulation,
@@ -75,6 +76,25 @@ const SpeedSpaceChartContainer = ({
7576
},
7677
};
7778

79+
useEffect(() => {
80+
const updateCanvasSize = () => {
81+
if (root.current) {
82+
setContainerWidth(root.current.clientWidth);
83+
}
84+
};
85+
86+
const resizeObserver = new ResizeObserver(updateCanvasSize);
87+
if (root.current) {
88+
resizeObserver.observe(root.current);
89+
}
90+
91+
return () => {
92+
if (root.current) {
93+
resizeObserver.unobserve(root.current);
94+
}
95+
};
96+
}, []);
97+
7898
return (
7999
<Rnd
80100
default={{
@@ -105,6 +125,7 @@ const SpeedSpaceChartContainer = ({
105125
}}
106126
>
107127
<div
128+
ref={root}
108129
id="container-SpeedSpaceChart"
109130
className="chart"
110131
style={{ height: `${heightOfSpeedSpaceChartContainer}px` }}

0 commit comments

Comments
 (0)