Skip to content

Commit

Permalink
front: fix speedspacechart-resize-behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Yohh <[email protected]>
  • Loading branch information
Yohh committed Dec 5, 2024
1 parent 7022b12 commit 255cf0a
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { SpeedSpaceChart } from '@osrd-project/ui-speedspacechart';
import type {
Expand Down Expand Up @@ -45,7 +45,8 @@ const SpeedSpaceChartContainer = ({
const [baseHeightOfSpeedSpaceChart, setBaseHeightOfSpeedSpaceChart] =
useState(heightOfSpeedSpaceChart);

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

const speedSpaceChartData = formatData(
trainSimulation,
Expand Down Expand Up @@ -75,6 +76,25 @@ const SpeedSpaceChartContainer = ({
},
};

useEffect(() => {
const updateCanvasSize = () => {
if (root.current) {
setContainerWidth(root.current.clientWidth);
}
};

const resizeObserver = new ResizeObserver(updateCanvasSize);
if (root.current) {
resizeObserver.observe(root.current);
}

return () => {
if (root.current) {
resizeObserver.unobserve(root.current);
}
};
}, []);

return (
<Rnd
default={{
Expand Down Expand Up @@ -105,6 +125,7 @@ const SpeedSpaceChartContainer = ({
}}
>
<div
ref={root}
id="container-SpeedSpaceChart"
className="chart"
style={{ height: `${heightOfSpeedSpaceChartContainer}px` }}
Expand Down

0 comments on commit 255cf0a

Please sign in to comment.