Commit 255cf0a 1 parent 7022b12 commit 255cf0a Copy full SHA for 255cf0a
File tree 1 file changed +23
-2
lines changed
front/src/modules/simulationResult/components/SpeedSpaceChart
1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change 1
- import { useState } from 'react' ;
1
+ import { useEffect , useRef , useState } from 'react' ;
2
2
3
3
import { SpeedSpaceChart } from '@osrd-project/ui-speedspacechart' ;
4
4
import type {
@@ -45,7 +45,8 @@ const SpeedSpaceChartContainer = ({
45
45
const [ baseHeightOfSpeedSpaceChart , setBaseHeightOfSpeedSpaceChart ] =
46
46
useState ( heightOfSpeedSpaceChart ) ;
47
47
48
- const containerWidth = document . getElementById ( 'container-SpeedSpaceChart' ) ?. clientWidth ;
48
+ const root = useRef < HTMLDivElement > ( null ) ;
49
+ const [ containerWidth , setContainerWidth ] = useState < number > ( root . current ?. clientWidth || 0 ) ;
49
50
50
51
const speedSpaceChartData = formatData (
51
52
trainSimulation ,
@@ -75,6 +76,25 @@ const SpeedSpaceChartContainer = ({
75
76
} ,
76
77
} ;
77
78
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
+
78
98
return (
79
99
< Rnd
80
100
default = { {
@@ -105,6 +125,7 @@ const SpeedSpaceChartContainer = ({
105
125
} }
106
126
>
107
127
< div
128
+ ref = { root }
108
129
id = "container-SpeedSpaceChart"
109
130
className = "chart"
110
131
style = { { height : `${ heightOfSpeedSpaceChartContainer } px` } }
You can’t perform that action at this time.
0 commit comments