Skip to content

Commit 759d4f7

Browse files
committed
fix comments 20-11
1 parent b0f1076 commit 759d4f7

File tree

7 files changed

+50
-53
lines changed

7 files changed

+50
-53
lines changed

storybook/.storybook/main.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ const config: StorybookConfig = {
2424
},
2525
staticDirs: ['../public'],
2626
logLevel: 'debug',
27-
async viteFinal(config) {
28-
return mergeConfig(config, {
29-
resolve: {
30-
preserveSymlinks: true,
31-
},
32-
});
33-
},
27+
// async viteFinal(config) {
28+
// return mergeConfig(config, {
29+
// resolve: {
30+
// preserveSymlinks: true,
31+
// },
32+
// });
33+
// },
3434
};
3535
export default config;
3636

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable import/no-unresolved */
21
import React, { useRef, useState } from 'react';
32

43
import { EyeClosed, Telescope } from '@osrd-project/ui-icons';
@@ -12,7 +11,6 @@ import '@osrd-project/ui-manchette/dist/theme.css';
1211
import '@osrd-project/ui-manchette-with-spacetimechart/dist/theme.css';
1312

1413
import Menu, { type MenuItem } from './Menu';
15-
import useElementInView from './useElementInView';
1614
import { SAMPLE_WAYPOINTS, SAMPLE_PATHS_DATA } from '../assets/sampleData';
1715
import useManchettesWithSpaceTimeChart from '../hooks/useManchetteWithSpaceTimeChart';
1816

@@ -34,9 +32,6 @@ const ManchetteWithSpaceTimeWrapper = ({
3432
const manchetteWithSpaceTimeChartRef = useRef<HTMLDivElement>(null);
3533

3634
const [activeWaypointId, setActiveWaypointPointId] = useState<string>();
37-
const [activeWaypointRef, setActiveWaypointRef] = useState<React.RefObject<HTMLDivElement>>();
38-
39-
const isElementInView = useElementInView(activeWaypointRef, manchetteWithSpaceTimeChartRef);
4035

4136
const menuItems: MenuItem[] = [
4237
{
@@ -57,12 +52,8 @@ const ManchetteWithSpaceTimeWrapper = ({
5752
},
5853
];
5954

60-
const handleWaypointClick = (
61-
waypointId: string,
62-
waypointRef: React.RefObject<HTMLDivElement>
63-
) => {
55+
const handleWaypointClick = (waypointId: string) => {
6456
setActiveWaypointPointId(waypointId);
65-
setActiveWaypointRef(waypointRef);
6657
};
6758

6859
const { manchetteProps, spaceTimeChartProps, handleScroll } = useManchettesWithSpaceTimeChart(
@@ -93,7 +84,7 @@ const ManchetteWithSpaceTimeWrapper = ({
9384
waypointMenuData={{
9485
activeWaypointId,
9586
menu: <Menu items={menuItems} />,
96-
isWaypointInView: isElementInView,
87+
scrollableParentRef: manchetteWithSpaceTimeChartRef,
9788
}}
9889
/>
9990
<div

ui-manchette/src/components/Waypoint.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ import React, { useRef } from 'react';
22

33
import cx from 'classnames';
44

5+
import useElementInView from '../hooks/useElementInView';
56
import { type InteractiveWaypoint } from '../types';
67
import '@osrd-project/ui-core/dist/theme.css';
78
import { positionMmToKm } from '../utils';
89

10+
const MANCHETTE_ACTIONS_HEIGHT = '-40px';
11+
912
type WaypointProps = {
1013
waypoint: InteractiveWaypoint;
1114
isActive: boolean;
1215
waypointMenu?: React.ReactNode;
13-
waypointsContainerRef: React.RefObject<HTMLDivElement>;
14-
isWaypointInView?: boolean;
16+
scrollableParentRef?: React.RefObject<HTMLElement>;
1517
};
1618

1719
const Waypoint = ({
1820
waypoint: { name, secondaryCode, id, position, display, onClick },
1921
isActive,
2022
waypointMenu,
21-
isWaypointInView,
23+
scrollableParentRef,
2224
}: WaypointProps) => {
2325
const waypointRef = useRef<HTMLDivElement>(null);
2426

27+
const isWaypointInView = useElementInView(waypointRef, scrollableParentRef, {
28+
threshold: 1,
29+
rootMargin: `0px 0px ${MANCHETTE_ACTIONS_HEIGHT} 0px`,
30+
});
31+
2532
if (!display) return null;
2633

2734
return (
@@ -31,7 +38,7 @@ const Waypoint = ({
3138
})}
3239
id={id}
3340
onClick={() => {
34-
if (onClick) onClick(id, waypointRef);
41+
if (onClick) onClick(id);
3542
}}
3643
>
3744
<div ref={waypointRef} className="waypoint-position justify-self-start text-end">
+19-24
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 Waypoint from './Waypoint';
44
import type { WaypointMenuData, InteractiveWaypoint } from '../types';
@@ -8,28 +8,23 @@ type WaypointListProps = {
88
waypointMenuData?: WaypointMenuData;
99
};
1010

11-
const WaypointList = ({ waypoints, waypointMenuData }: WaypointListProps) => {
12-
const waypointsContainerRef = useRef<HTMLDivElement>(null);
13-
14-
return (
15-
<div ref={waypointsContainerRef} className="waypoint-list ">
16-
{waypoints.map((waypoint) => (
17-
<div
18-
key={waypoint.id}
19-
className="waypoint-wrapper flex flex-col justify-start"
20-
style={waypoint.styles}
21-
>
22-
<Waypoint
23-
waypoint={waypoint}
24-
isActive={waypointMenuData?.activeWaypointId === waypoint.id}
25-
waypointMenu={waypointMenuData?.menu}
26-
waypointsContainerRef={waypointsContainerRef}
27-
isWaypointInView={waypointMenuData?.isWaypointInView}
28-
/>
29-
</div>
30-
))}
31-
</div>
32-
);
33-
};
11+
const WaypointList = ({ waypoints, waypointMenuData }: WaypointListProps) => (
12+
<div className="waypoint-list ">
13+
{waypoints.map((waypoint) => (
14+
<div
15+
key={waypoint.id}
16+
className="waypoint-wrapper flex flex-col justify-start"
17+
style={waypoint.styles}
18+
>
19+
<Waypoint
20+
waypoint={waypoint}
21+
isActive={waypointMenuData?.activeWaypointId === waypoint.id}
22+
waypointMenu={waypointMenuData?.menu}
23+
scrollableParentRef={waypointMenuData?.scrollableParentRef}
24+
/>
25+
</div>
26+
))}
27+
</div>
28+
);
3429

3530
export default WaypointList;

ui-manchette-with-spacetimechart/src/stories/useElementInView.ts ui-manchette/src/hooks/useElementInView.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { useState, useEffect, type RefObject } from 'react';
2-
3-
const MANCHETTE_ACTIONS_HEIGHT = '-40px';
4-
52
/**
63
* Check if an element is in the viewport of a container
74
* @param ref the element we want to observe
@@ -16,7 +13,7 @@ const useElementInView = <T extends HTMLElement>(
1613
containerRef?: RefObject<HTMLElement>,
1714
options: IntersectionObserverInit = {
1815
threshold: 1,
19-
rootMargin: `0px 0px ${MANCHETTE_ACTIONS_HEIGHT} 0px`,
16+
rootMargin: `0px`,
2017
}
2118
): boolean => {
2219
const [isInView, setIsInView] = useState(false);
@@ -25,7 +22,12 @@ const useElementInView = <T extends HTMLElement>(
2522
const currentRef = ref?.current;
2623
const root = containerRef?.current || null;
2724

28-
if (!currentRef) return;
25+
if (!currentRef) {
26+
// If the user implementing ui-manchette doesn't provide a ref to handle the menu visibility,
27+
// the menu should always be visible
28+
setIsInView(true);
29+
return;
30+
}
2931

3032
const observer = new IntersectionObserver(
3133
([entry]) => {

ui-manchette/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import './components/consts';
55
export { default as Waypoint } from './components/Waypoint';
66
export { default as WaypointList } from './components/WaypointList';
77
export { default as Manchette } from './components/Manchette';
8+
export { WaypointMenuData } from './types';

ui-manchette/src/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ 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-
isWaypointInView?: boolean;
19+
// Ref of the scrollable parent of the waypoints list to enable the menu hidding behavior
20+
scrollableParentRef?: React.RefObject<HTMLElement>;
2021
};
2122

2223
export type ProjectPathTrainResult = {

0 commit comments

Comments
 (0)