Skip to content

Commit f57220b

Browse files
committed
[WIP]: Missing to refactor distances, almost working the changing
1 parent 3fa5efc commit f57220b

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

src/components/RightMenu/RightMenu.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import * as React from "react";
44
import { RightMenuProps } from "./RightMenuProps";
55
import "./RightMenu.scss";
66
const RightMenu: React.FC<RightMenuProps> = (props: RightMenuProps) => {
7-
const { items, width } = props;
8-
const [selectedItem, setSelectedItem] = React.useState<number>(0);
9-
const handleChangeSelectedItem = (newIndex: number) =>
10-
setSelectedItem(newIndex);
7+
const { items, width, selectedItemProp:selectedItem=0 } = props;
8+
//const [selectedItem, setSelectedItem] = React.useState<number>(selectedItemProp);
9+
/* const handleChangeSelectedItem = (newIndex: number) =>
10+
setSelectedItem(newIndex); */
1111
const handleClickAnchor = (
1212
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
1313
onClick: Function | undefined,
1414
id: number
1515
) => {
1616
onClick?.();
17-
handleChangeSelectedItem(id);
17+
//handleChangeSelectedItem(id);
1818
};
1919
return (
2020
<List
21+
className="section-container"
2122
sx={{ width: "100%", maxWidth: width, bgcolor: "background.paper" }}
2223
component="nav"
2324
aria-labelledby="nested-list-subheader"

src/components/RightMenu/RightMenuProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface RightMenuProps {
1010
items: RightMenuItem[];
1111
width: string;
1212
baseUrl?: string;
13+
selectedItemProp?:number;
1314
}

src/components/layout-pages/MainContentContainer.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as React from "react";
44
const MainContentContainer = ({ children }) => {
55
return (
66
<Box
7+
78
sx={{
89
overflow: "hidden",
910
boxSizing: "border-box",
@@ -15,10 +16,12 @@ const MainContentContainer = ({ children }) => {
1516
}}
1617
>
1718
<Box
19+
// className="section-container"
1820
sx={{
1921
boxSizing: "border-box",
2022
width: "100%",
2123
//backgroundColor:"grey"
24+
2225
}}
2326
>
2427
{children}

src/pages/demo/FileMosaicDemoPage.jsx

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,80 @@ import DemoFileMosaicSmartImgFit from "../../components/demo-components/filemosa
3131
import CodeJSFileMosaicSmartImgFit from "../../components/demo-components/filemosaic-demo/CodeJSFileMosaicSmartImgFit";
3232

3333
const FileMosaicDemoPage = (props) => {
34+
const [selectedItem, setSelectedItem] = React.useState(0);
35+
36+
const scrollHandler = () => {
37+
let menu = document.querySelector(".section-container");
38+
39+
const rightMenuItemsWithIdSec = rightMenuItems.map((x) => {
40+
return {
41+
...x,
42+
idSec: x.referTo.split("#")[1],
43+
};
44+
});
45+
/* console.log(
46+
"scrollHandler rightMenuItemsWithIdSec",
47+
rightMenuItemsWithIdSec.map((x) => x.idSec)
48+
); */
49+
const arrElements = rightMenuItemsWithIdSec.map((x) => {
50+
return { ...x, element: document.getElementById(x.idSec) };
51+
});
52+
53+
//console.log("scrollHandler arrElements", arrElements);
54+
let pos_menu = window.pageYOffset + menu.offsetHeight;
55+
56+
//simply chech whicof them is clser to offset toip????
57+
console.log("scrollHandler pos_menu", pos_menu);
58+
59+
const arrElementsEnhanced = arrElements.map((x) => {
60+
const element = x.element;
61+
const postElement = element.offsetTop + element.offsetHeight;
62+
const distance = Math.abs(postElement - pos_menu);
63+
return { ...x, distance };
64+
});
65+
66+
console.log(
67+
"scrollHandler distances",
68+
pos_menu,
69+
arrElementsEnhanced.map(({ distance }) => distance)
70+
);
71+
72+
let min = Math.min(...arrElementsEnhanced.map((x) => x.distance));
73+
74+
arrElementsEnhanced.forEach((x) => {
75+
if (x.distance === min) {
76+
setSelectedItem(x.id);
77+
console.log("foundelement distance", x.distance);
78+
console.log("foundelement id", x.id);
79+
}
80+
});
81+
82+
console.log(
83+
"distance min",
84+
min,
85+
arrElementsEnhanced.map((x) => x.distance)
86+
);
87+
88+
// if (min === distance_A) setSelectedItem(0);
89+
//document.querySelectorAll(".Menu .Item")[0].classList.add("Highlight");
90+
// if (min === distance_B) setSelectedItem(1);
91+
// if (min === distance_C) setSelectedItem(2);
92+
/* if (min === distance_B)
93+
document.querySelectorAll(".Menu .Item")[1].classList.add("Highlight");
94+
if (min === distance_C)
95+
document.querySelectorAll(".Menu .Item")[2].classList.add("Highlight"); */
96+
};
97+
React.useEffect(() => {
98+
console.log("scrollHandler container");
99+
100+
window.addEventListener("scroll", scrollHandler);
101+
102+
return () => {
103+
console.log("foundelement","removing event");
104+
window.removeEventListener("scroll", scrollHandler);
105+
};
106+
}, []);
107+
34108
return (
35109
<React.Fragment>
36110
<MainContentContainer>
@@ -79,7 +153,10 @@ const FileMosaicDemoPage = (props) => {
79153
<CodeHighlight>{`<FileInputButton/>`} </CodeHighlight>
80154
component for allowing the user to select files. For further
81155
information of this component check out the{" "}
82-
<AnchorToTab href="/components/fileinputbutton">FileInputButton</AnchorToTab> page.
156+
<AnchorToTab href="/components/fileinputbutton">
157+
FileInputButton
158+
</AnchorToTab>{" "}
159+
page.
83160
</Alert>
84161
<br />
85162
<Alert severity="info">
@@ -392,12 +469,17 @@ const FileMosaicDemoPage = (props) => {
392469
</section>
393470
</MainContentContainer>
394471
<RightMenuContainer>
395-
<RightMenu width="240px" items={rightMenuItems} />
472+
<RightMenu
473+
width="240px"
474+
items={rightMenuItems}
475+
selectedItemProp={selectedItem}
476+
/>
396477
</RightMenuContainer>
397478
</React.Fragment>
398479
);
399480
};
400481
export default FileMosaicDemoPage;
482+
401483
const rightMenuItems = [
402484
{
403485
id: 0,

0 commit comments

Comments
 (0)