Skip to content

Commit ea8174d

Browse files
committed
fixup! ui-manchette: fix waypoint menu positioning
1 parent cda656b commit ea8174d

File tree

6 files changed

+27
-34
lines changed

6 files changed

+27
-34
lines changed

ui-manchette-with-spacetimechart/src/stories/base-with-waypoint-menu.stories.tsx

+2-11
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ const ManchetteWithSpaceTimeWrapper = ({
3636
const manchetteWithSpaceTimeChartRef = useRef<HTMLDivElement>(null);
3737
// Allow us to compute the position of the menu in Manchette component
3838
const manchetteWithSpaceTimeCharWrappertRef = useRef<HTMLDivElement>(null);
39-
const [activeWaypointRef, setActiveWaypointPointRef] =
40-
useState<React.RefObject<HTMLDivElement>>();
41-
// Allow us now which waypoint has been clicked and change its style
39+
// Allow us to know which waypoint has been clicked and change its style
4240
const [activeWaypointId, setActiveWaypointId] = useState<string>();
4341

4442
const menuItems: MenuItem[] = [
@@ -47,7 +45,6 @@ const ManchetteWithSpaceTimeWrapper = ({
4745
icon: <EyeClosed />,
4846
onClick: (e: React.MouseEvent) => {
4947
e.stopPropagation();
50-
setActiveWaypointPointRef(undefined);
5148
setActiveWaypointId(undefined);
5249
},
5350
},
@@ -56,18 +53,13 @@ const ManchetteWithSpaceTimeWrapper = ({
5653
icon: <Telescope />,
5754
onClick: (e: React.MouseEvent) => {
5855
e.stopPropagation();
59-
setActiveWaypointPointRef(undefined);
6056
setActiveWaypointId(undefined);
6157
},
6258
},
6359
];
6460

65-
const handleWaypointClick = (
66-
waypointId: string,
67-
waypointRef: React.RefObject<HTMLDivElement>
68-
) => {
61+
const handleWaypointClick = (waypointId: string) => {
6962
setActiveWaypointId(waypointId);
70-
setActiveWaypointPointRef(waypointRef);
7163
};
7264

7365
const { manchetteProps, spaceTimeChartProps, handleScroll } = useManchettesWithSpaceTimeChart(
@@ -102,7 +94,6 @@ const ManchetteWithSpaceTimeWrapper = ({
10294
waypointMenuData={{
10395
menu: <Menu items={menuItems} />,
10496
activeWaypointId,
105-
activeWaypointRef,
10697
manchetteWrapperRef: manchetteWithSpaceTimeCharWrappertRef,
10798
}}
10899
/>

ui-manchette/src/components/Manchette.tsx

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useLayoutEffect, useRef, useState } from 'react';
22

33
import { ZoomIn, ZoomOut } from '@osrd-project/ui-icons';
44
import cx from 'classnames';
@@ -7,9 +7,6 @@ import { INITIAL_OP_LIST_HEIGHT, MAX_ZOOM_Y, MIN_ZOOM_Y } from './consts';
77
import WaypointList from './WaypointList';
88
import type { InteractiveWaypoint, WaypointMenuData } from '../types';
99

10-
const WAYPOINT_MENU_LEFT_OFFSET = 8;
11-
const WAYPOINT_MENU_TOP_OFFSET = -2;
12-
1310
type ManchetteProps = {
1411
waypoints: InteractiveWaypoint[];
1512
waypointMenuData?: WaypointMenuData;
@@ -36,33 +33,34 @@ const Manchette = ({
3633
height = INITIAL_OP_LIST_HEIGHT,
3734
}: ManchetteProps) => {
3835
const [menuPosition, setMenuPosition] = useState<number>();
36+
const activeWaypointRef = useRef<HTMLDivElement>(null);
3937

4038
// Allow to track the menu position after we might have scrolled in the page
41-
useEffect(() => {
39+
useLayoutEffect(() => {
4240
const manchetteWrapperPosition =
4341
waypointMenuData?.manchetteWrapperRef?.current?.getBoundingClientRect().top;
44-
const waypointPosition =
45-
waypointMenuData?.activeWaypointRef?.current?.getBoundingClientRect().top;
42+
const waypointPosition = activeWaypointRef?.current?.getBoundingClientRect().top;
4643

4744
if (!manchetteWrapperPosition || !waypointPosition) {
4845
setMenuPosition(undefined);
4946
} else {
50-
setMenuPosition(window.scrollY * 2 + manchetteWrapperPosition + waypointPosition);
47+
setMenuPosition(waypointPosition - manchetteWrapperPosition);
5148
}
5249
}, [waypointMenuData]);
5350

5451
return (
5552
<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 }}>
6155
{waypointMenuData.menu}
6256
</div>
6357
)}
6458
<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+
/>
6664
{children}
6765
</div>
6866
<div className="manchette-actions">

ui-manchette/src/components/Waypoint.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from 'react';
1+
import React from 'react';
22

33
import cx from 'classnames';
44

@@ -8,36 +8,37 @@ import { positionMmToKm } from '../utils';
88

99
type WaypointProps = {
1010
waypoint: InteractiveWaypoint;
11+
nameRef?: React.RefObject<HTMLDivElement>;
1112
isActive: boolean;
1213
isMenuActive?: boolean;
1314
};
1415

1516
const Waypoint = ({
1617
waypoint: { name, secondaryCode, id, position, display, onClick },
18+
nameRef,
1719
isActive,
1820
isMenuActive,
1921
}: WaypointProps) => {
20-
const waypointRef = useRef<HTMLDivElement>(null);
21-
2222
if (!display) return null;
2323

2424
return (
2525
<div
26-
ref={waypointRef}
2726
className={cx('flex waypoint items-baseline', {
2827
'waypoint-active': isActive,
2928
'menu-active': isMenuActive,
3029
})}
3130
id={id}
3231
onClick={() => {
33-
if (onClick && !isMenuActive) onClick(id, waypointRef);
32+
if (onClick && !isMenuActive) onClick(id);
3433
}}
3534
>
3635
<div className="waypoint-position justify-self-start text-end">
3736
{positionMmToKm(position)}
3837
</div>
3938

40-
<div className="waypoint-name mx-2 justify-self-start">{name}</div>
39+
<div ref={nameRef} className="waypoint-name mx-2 justify-self-start">
40+
{name}
41+
</div>
4142
<div className="waypoint-separator"></div>
4243
<div className="waypoint-ch font-mono justify-self-end">{secondaryCode}</div>
4344
<div className="waypoint-separator"></div>

ui-manchette/src/components/WaypointList.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import type { InteractiveWaypoint } from '../types';
66
type WaypointListProps = {
77
waypoints: InteractiveWaypoint[];
88
activeWaypointId?: string;
9+
activeWaypointRef?: React.RefObject<HTMLDivElement>;
910
};
1011

11-
const WaypointList = ({ waypoints, activeWaypointId }: WaypointListProps) => (
12+
const WaypointList = ({ waypoints, activeWaypointId, activeWaypointRef }: WaypointListProps) => (
1213
<div className="waypoint-list ">
1314
{waypoints.map((waypoint) => (
1415
<div
@@ -18,6 +19,7 @@ const WaypointList = ({ waypoints, activeWaypointId }: WaypointListProps) => (
1819
>
1920
<Waypoint
2021
waypoint={waypoint}
22+
nameRef={activeWaypointId === waypoint.id ? activeWaypointRef : undefined}
2123
isActive={activeWaypointId === waypoint.id}
2224
isMenuActive={!!activeWaypointId}
2325
/>

ui-manchette/src/styles/manchette.css

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
.menu-wrapper {
33
position: absolute;
44
z-index: 12;
5+
left: 8px;
6+
margin-top: 28px;
57
}
68

79
.manchette-actions {

ui-manchette/src/types.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ export type Waypoint = {
1010
export type InteractiveWaypoint = Waypoint & {
1111
styles?: CSSProperties;
1212
display?: boolean;
13-
onClick?: (waypointId: string, waypointRef: React.RefObject<HTMLDivElement>) => void;
13+
onClick?: (waypointId: string) => void;
1414
};
1515

1616
export type WaypointMenuData = {
1717
menu: React.ReactNode;
1818
activeWaypointId?: string;
19-
activeWaypointRef?: React.RefObject<HTMLDivElement>;
2019
manchetteWrapperRef: React.RefObject<HTMLDivElement>;
2120
};
2221

0 commit comments

Comments
 (0)