Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor tab slider position calculation in App component #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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>(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 (
<main className="bg-[#f7f2f2]">
<Navbar tab={tab} setTab={setTab} left={x} sliderWidth={w} />
<Layout tab={tab} setTab={setTab} left={x} sliderWidth={w} />
</main>
);
const [tab, setTab] = useState<TabKey>(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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Refs


if (navbarElement) {
const tabElements = navbarElement.querySelectorAll(".tab");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use refs instead of query selector


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 (
<main className="bg-[#f7f2f2]">
<Navbar tab={tab} setTab={setTab} left={x} sliderWidth={w} />
<Layout tab={tab} setTab={setTab} left={x} sliderWidth={w} />
</main>
);
}

export default App;
82 changes: 41 additions & 41 deletions src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -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<NavbarProps> = ({
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 (
<div className="bg-black-1 text-white mx-auto sticky top-0 z-[10000] backdrop-saturate-180 backdrop-blur-lg md:display-none pt-2">
<div className="bg-white max-w-[900px] m-auto rounded-full text-1.8rem border-b border-gray-200 p-[3px]">
<div className="flex rounded-33px p-2 justify-between items-center text-[#111827] max-w-[900px] mx-auto">
{tabs.map(({ key, label }) => (
<div
key={key}
className={`flex items-center h-8 flex-1 cursor-pointer justify-center ${
tab === key ? "text-black" : "text-black-300"
}`}
onClick={() => setTab(key)}
>
{label}
</div>
))}
<div
className="absolute h-10 bg-[#fc205e]/40 border border-[#fc205e] rounded-full z-20"
style={{
left: `${left}px`,
width: `${sliderWidth}px`,
transition: "left 0.38s cubic-bezier(0.5, 0, 0, 0.75)",
}}
></div>
</div>
</div>
</div>
);
return (
<div className="navbar bg-black-1 text-white mx-auto sticky top-0 z-[10000] backdrop-saturate-180 backdrop-blur-lg md:display-none pt-2">
<div className="bg-white max-w-[900px] m-auto rounded-full text-1.8rem border-b border-gray-200 p-[3px]">
<div className="flex rounded-33px p-2 justify-between items-center text-[#111827] max-w-[900px] mx-auto">
{tabs.map(({ key, label }) => (
<div
key={key}
className={`tab flex items-center h-8 flex-1 cursor-pointer justify-center ${
tab === key ? "text-black" : "text-black-300"
}`}
onClick={() => setTab(key)}
>
{label}
</div>
))}
<div
className="absolute h-10 bg-[#fc205e]/40 border border-[#fc205e] rounded-full z-20"
style={{
left: `${left}px`,
width: `${sliderWidth}px`,
transition: "left 0.38s cubic-bezier(0.5, 0, 0, 0.75)",
}}
></div>
</div>
</div>
</div>
);
};

export default Navbar;