From 36d2d25542bdedf46a42e17ebb30310ae3bbb018 Mon Sep 17 00:00:00 2001 From: Guffy-san Date: Mon, 26 Aug 2024 09:56:11 +0530 Subject: [PATCH] chore: Refactor tab slider position calculation in App component --- src/App.tsx | 74 ++++++++++++++++++++++------------- src/components/navbar.tsx | 82 +++++++++++++++++++-------------------- 2 files changed, 89 insertions(+), 67 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 8d5669c..84cd4e4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,36 +1,58 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import Layout from "./components/layout"; import Navbar from "./components/navbar"; export enum TabKey { - Home = "Home", - Work = "Work", - Blog = "Blog", - Contact = "Contact", + Home = "Home", + Work = "Work", + Blog = "Blog", + Contact = "Contact", } function App() { - const [tab, setTab] = useState(TabKey.Home); - - const tabOffsets: { [key in TabKey]: number } = { - Home: 0, - Work: 1, - Blog: 2, - Contact: 3, - }; - - const baseX = 520; - const baseW = 221.5; - - const x = baseX + tabOffsets[tab] * baseW; - const w = baseW; - - return ( -
- - -
- ); + const [tab, setTab] = useState(TabKey.Home); + const [x, setX] = useState(0); + const [w, setW] = useState(0); + + const tabOffsets = { + Home: 0, + Work: 1, + Blog: 2, + Contact: 3, + }; + + useEffect(() => { + const calculateSliderPosition = () => { + const navbarElement = document.querySelector(".navbar"); + + if (navbarElement) { + const tabElements = navbarElement.querySelectorAll(".tab"); + + if (tabElements.length > 0) { + const baseX = tabElements[0].getBoundingClientRect().left; + const baseW = tabElements[0].getBoundingClientRect().width; + + setX(baseX + tabOffsets[tab] * baseW); + setW(baseW); + } + } + }; + + calculateSliderPosition(); + + window.addEventListener("resize", calculateSliderPosition); + + return () => { + window.removeEventListener("resize", calculateSliderPosition); + }; + }, [tab]); + + return ( +
+ + +
+ ); } export default App; diff --git a/src/components/navbar.tsx b/src/components/navbar.tsx index a16b5d3..25e2940 100644 --- a/src/components/navbar.tsx +++ b/src/components/navbar.tsx @@ -1,52 +1,52 @@ import { TabKey } from "../App"; interface NavbarProps { - tab: TabKey; - setTab: (tab: TabKey) => void; - left?: number; - sliderWidth?: number; + tab: TabKey; + setTab: (tab: TabKey) => void; + left?: number; + sliderWidth?: number; } const Navbar: React.FC = ({ - tab, - setTab, - left = 0, - sliderWidth = 0, + tab, + setTab, + left = 0, + sliderWidth = 0, }) => { - const tabs = [ - { key: TabKey.Home, label: "Home" }, - { key: TabKey.Work, label: "Projects" }, - { key: TabKey.Blog, label: "Writing" }, - { key: TabKey.Contact, label: "Contact" }, - ]; + const tabs = [ + { key: TabKey.Home, label: "Home" }, + { key: TabKey.Work, label: "Projects" }, + { key: TabKey.Blog, label: "Writing" }, + { key: TabKey.Contact, label: "Contact" }, + ]; - return ( -
-
-
- {tabs.map(({ key, label }) => ( -
setTab(key)} - > - {label} -
- ))} -
-
-
-
- ); + return ( +
+
+
+ {tabs.map(({ key, label }) => ( +
setTab(key)} + > + {label} +
+ ))} +
+
+
+
+ ); }; export default Navbar;