Newer
Older
import { useEffect, useState } from "react";
import { ScrollLink } from "../components/ScrollLink";
import { Highlight, NewHighlight } from "./Highlight-functions";
// Funktion zur Erstellung der Sidebar
export function createSidebar(tabs: Array<{ tab: string; subtabs?: Array<string> }>) {
const { numsBig, numsSub } = deriveTabsData(tabs);
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const [openTab, setOpenTab] = useState<string | null>(null);
useEffect(() => {
const handleScroll = () => {
numsBig.forEach((item, ind) => {
const element = document.getElementById(item);
const subtitleElement = document.getElementById(`subtitle${ind}`);
if (element && subtitleElement) {
Highlight({ el: element }, { subtitle: subtitleElement });
}
});
numsSub.forEach((item, ind) => {
const element = document.getElementById(item);
const subtitleElement = document.getElementById(`newsubtitle${ind}`);
if (element && subtitleElement) {
NewHighlight({ el: element }, { subtitle: subtitleElement });
}
});
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [numsBig, numsSub, openTab]);
const scrolling = (tab: string) => {
const targetElement = document.getElementById(tab);
if (targetElement) {
//console.log(`Scrolling to element with ID: ${tab}`);
// Get the position of the element relative to the document
const elementRect = targetElement.getBoundingClientRect();
const elementTop = elementRect.top + window.scrollY;
// Calculate the middle of the viewport
const viewportHeight = window.innerHeight;
const scrollOffset = elementTop - (viewportHeight / 5 - targetElement.offsetHeight / 2);
/*
console.log(`Element Top: ${elementTop}`);
console.log(`Viewport Height: ${viewportHeight}`);
console.log(`Scroll Offset: ${scrollOffset}`); */
window.scrollTo({
top: scrollOffset,
behavior: "smooth"
});
} else {
console.error(`Scroll target element not found for ID: ${tab}`);
}
}
const toggleTab = (tab: string) => {
setOpenTab(openTab === tab ? null : tab);
// console.log(`Status of tab ${tab} is ${openTab}`)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
};
let subtitlenumber = 0;
return (
<>
<br />
<div className="sticky-top">
<nav className="sidebar">
{tabs.map((tab, index) => {
const tabId = `tab-${tab.tab}`;
const parentId = `parent-${tab.tab}`;
const subtitleId = `subtitle${index}`;
return (
<div key={index}>
<div id={subtitleId} className="detail-sideitem">
<div id={parentId} className="sideitem">
<a
onClick={() => {
// console.log(`Clicked on Tab ${tab.tab} with tab-name tab-${tab.tab} and parent parent-${tab.tab}`);
toggleTab(tab.tab);
// Close other tabs when a new tab is opened
tabs.forEach((t) => {
if (t.tab !== tab.tab) {
document.getElementById(`tab-${t.tab}`)!.style.display = "none";
document.getElementById(`parent-${t.tab}`)!.classList.remove("active-sideitem");
}
});
// Show or hide the selected tab
document.getElementById(tabId)!.style.display = openTab === tab.tab ? "block" : "none";
document.getElementById(parentId)!.classList.toggle("active-sideitem");
}}
>
<summary>{tab.tab}</summary>
</a>
{tab.subtabs && (
<span
id={tabId}
className="sidesubtab"
style={{ display: openTab === tab.tab ? "block" : "none" }}
>
<ul>
{tab.subtabs.map((subtab, subIndex) => {
const subTabId = `newsubtitle${subtitlenumber}`;
/* console.log(`Old subtitlenumber is ${subtitlenumber}`) */
subtitlenumber = subtitlenumber + 1;
// console.log(`New subtitlenumber is ${subtitlenumber}`)
// console.log(`Made subtab newsubtitle${subtitlenumber} that will link to ${tab.tab}${subIndex +1 }H`)
return (
<li key={subtitlenumber} id={subTabId}>
<ScrollLink label={subtab} targetId={`${tab.tab}${subIndex +1 }H`} />
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
</li>
);
})}
</ul>
</span>
)}
</div>
</div>
</div>
);
})}
</nav>
<br />
<div className="col" style={{ display: "flex", alignItems: "right" }}>
<a href="#" className="backtotop">
Back to Top ↑
</a>
</div>
</div>
</>
);
}
function deriveTabsData(tabs: Array<{ tab: string; subtabs?: Array<string> }>) {
const numsBig: string[] = [];
const numsSub: string[] = [];
tabs.forEach(tab => {
numsBig.push(`${tab.tab}H`); // z.B. RoleH, ChecksH
if (tab.subtabs) {
tab.subtabs.forEach((_index) => {
// console.log(index)
numsSub.push(`${tab.tab}${count}`); // z.B. Role1H, Role2H
count += 1;
});
}
});
return { numsBig, numsSub };
}