Skip to content
Snippets Groups Projects
Commit ab5ae403 authored by Liliana Sanfilippo's avatar Liliana Sanfilippo
Browse files

scrollTo fix

parent 39a96cc0
No related branches found
No related tags found
No related merge requests found
Pipeline #435295 failed
......@@ -3,6 +3,7 @@ import { ButtonOneEngineering } from "../components/Buttons";
import { LoremShort } from "../components/Loremipsum";
import { openElement } from "../utils/openElement";
import { H3 } from "../components/Headings";
import { useTabNavigation } from "../utils/TabNavigation";
......
......@@ -54,6 +54,7 @@ export const useTabNavigation = () => {
const location = useLocation();
const [activeTab, setActiveTab] = useState<string | null>(null);
const [activeSubTab, setActiveSubTab] = useState<string | null>(null);
// Tab-Wechsel und URL-Update
const handleTabChange = (tabId: string, subTabId?: string) => {
......@@ -74,7 +75,7 @@ export const useTabNavigation = () => {
const tabId = params.get('tab');
const subTabId = params.get('subTab');
const collapseId = params.get('collapseId');
const scrollToId = params.get('scrollTo');
// Öffne Haupt- und ggf. verschachtelten Tab
if (tabId) {
......@@ -99,6 +100,16 @@ export const useTabNavigation = () => {
openFromOtherPage(tabId)({ currentTarget: document.getElementById(tabId)! });
}
if (scrollToId) {
const element = document.getElementById(scrollToId);
if (element) {
const viewportHeight = window.innerHeight;
const targetPosition = element.getBoundingClientRect().top + window.pageYOffset;
const scrollToPosition = targetPosition - viewportHeight / 2 + element.clientHeight / 2;
window.scrollTo({ top: scrollToPosition, behavior: "smooth" });
}
}
setActiveTab(tabId);
setActiveSubTab(subTabId || null);
}, [location.search]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment