1
- import React , { useEffect , useState } from 'react' ;
1
+ import React , { useLayoutEffect , useRef , useState } from 'react' ;
2
2
3
3
import { ZoomIn , ZoomOut } from '@osrd-project/ui-icons' ;
4
4
import cx from 'classnames' ;
@@ -7,9 +7,6 @@ import { INITIAL_OP_LIST_HEIGHT, MAX_ZOOM_Y, MIN_ZOOM_Y } from './consts';
7
7
import WaypointList from './WaypointList' ;
8
8
import type { InteractiveWaypoint , WaypointMenuData } from '../types' ;
9
9
10
- const WAYPOINT_MENU_LEFT_OFFSET = 8 ;
11
- const WAYPOINT_MENU_TOP_OFFSET = - 2 ;
12
-
13
10
type ManchetteProps = {
14
11
waypoints : InteractiveWaypoint [ ] ;
15
12
waypointMenuData ?: WaypointMenuData ;
@@ -36,33 +33,34 @@ const Manchette = ({
36
33
height = INITIAL_OP_LIST_HEIGHT ,
37
34
} : ManchetteProps ) => {
38
35
const [ menuPosition , setMenuPosition ] = useState < number > ( ) ;
36
+ const activeWaypointRef = useRef < HTMLDivElement > ( null ) ;
39
37
40
38
// Allow to track the menu position after we might have scrolled in the page
41
- useEffect ( ( ) => {
39
+ useLayoutEffect ( ( ) => {
42
40
const manchetteWrapperPosition =
43
41
waypointMenuData ?. manchetteWrapperRef ?. current ?. getBoundingClientRect ( ) . top ;
44
- const waypointPosition =
45
- waypointMenuData ?. activeWaypointRef ?. current ?. getBoundingClientRect ( ) . top ;
42
+ const waypointPosition = activeWaypointRef ?. current ?. getBoundingClientRect ( ) . top ;
46
43
47
44
if ( ! manchetteWrapperPosition || ! waypointPosition ) {
48
45
setMenuPosition ( undefined ) ;
49
46
} else {
50
- setMenuPosition ( window . scrollY * 2 + manchetteWrapperPosition + waypointPosition ) ;
47
+ setMenuPosition ( waypointPosition - manchetteWrapperPosition ) ;
51
48
}
52
49
} , [ waypointMenuData ] ) ;
53
50
54
51
return (
55
52
< div className = "manchette-container" >
56
- { waypointMenuData ?. menu && menuPosition && (
57
- < div
58
- className = "menu-wrapper"
59
- style = { { top : menuPosition + WAYPOINT_MENU_TOP_OFFSET , left : WAYPOINT_MENU_LEFT_OFFSET } }
60
- >
53
+ { waypointMenuData ?. menu && waypointMenuData . activeWaypointId && (
54
+ < div className = "menu-wrapper" style = { { top : menuPosition } } >
61
55
{ waypointMenuData . menu }
62
56
</ div >
63
57
) }
64
58
< div className = "bg-white-100 border-r border-grey-30" style = { { minHeight : `${ height } px` } } >
65
- < WaypointList waypoints = { waypoints } activeWaypointId = { waypointMenuData ?. activeWaypointId } />
59
+ < WaypointList
60
+ waypoints = { waypoints }
61
+ activeWaypointId = { waypointMenuData ?. activeWaypointId }
62
+ activeWaypointRef = { activeWaypointRef }
63
+ />
66
64
{ children }
67
65
</ div >
68
66
< div className = "manchette-actions" >
0 commit comments