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

Fix Header Links #9

Closed
wants to merge 14 commits into from
Closed
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
95 changes: 0 additions & 95 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@
"@mui/icons-material": "^5.10.6",
"@mui/material": "^5.10.6",
"@vercel/analytics": "^1.0.1",
"axios": "^0.27.2",
"bootstrap": "^5.2.1",
"i18next": "^21.9.2",
"i18next-browser-languagedetector": "^6.1.5",
"jquery": "^3.6.1",
"react": "^18.2.0",
"react-bootstrap": "^2.5.0",
"react-cookie": "^4.1.1",
Expand All @@ -49,11 +47,9 @@
"react-router-dom": "^6.12.0",
"react-scripts": "^5.0.1",
"sass": "^1.54.9",
"swiper": "^8.4.2",
"typescript": "^4.8.3"
},
"devDependencies": {
"@types/jquery": "^3.5.14",
"@types/node": "^18.7.18",
"@types/react": "^18.0.20",
"@types/react-dom": "^18.0.6",
Expand Down
72 changes: 68 additions & 4 deletions src/components/common/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useEffect, useState } from "react";
import { AppBar, Container, Stack, Typography } from "@mui/material";
import { Nav, Navbar } from "react-bootstrap";
import { MaterialUISwitch } from "./ThemeSwitch";
import { LanguageSwitcher } from "./LanguageSwitcher";
import { useTranslation } from "react-i18next";
import { Image } from "./Text";
import { Sections } from "../../type/page";
import Meta, { constructTitle } from "./Meta";

const sections = [
{key: "welcome", href: `#${Sections.Welcome}`},
{key: "about", href: `#${Sections.About}`},
{key: "skills", href: `#${Sections.Skills}`},
{key: "stats", href: `#${Sections.Stats}`},
Expand All @@ -20,11 +21,60 @@
}
export default function Header({toggleTheme, checked}: Props) {
const {t} = useTranslation("components");

const initialSection = window?.location.href.split("#")[1];
const [activeSection, setActiveSection] = useState(initialSection || "#");

useEffect(() => {
const handleScroll = () => {
const scrollPosition = window.scrollY;
const sections: NodeListOf<HTMLElement> = document.querySelectorAll("section:not(.noIndex)");

sections.forEach((section) => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const EM = 16;

if (
scrollPosition >= sectionTop - 6 * EM - 50 && // Adjust the offset as needed
scrollPosition < sectionTop + sectionHeight - 6 * EM - 50 // Adjust the offset as needed
) {
setActiveSection(section.id);
}
});
};

window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);

useEffect(() => {
const handleUrlAnchorChange = () => {
const sectionFromUrl = window.location.hash.substr(1);
setActiveSection(sectionFromUrl);
};

window.addEventListener("hashchange", handleUrlAnchorChange);
return () => {
window.removeEventListener("hashchange", handleUrlAnchorChange);
};
}, []);

useEffect(() => {
window.location.hash = activeSection;
}, [activeSection]);

const pageTitle = constructTitle();
document.title = pageTitle;

return (
<AppBar position="fixed" enableColorOnDark>
<Meta />
<Container>
<Navbar collapseOnSelect expand="md" bg="none" variant="dark">
<Navbar.Brand href="/">
<Navbar.Brand href="#">
<Image src="/assets/logo-white-transparent.svg" alt="logo" width={50} height={50} />
<Typography component="span" variant="h6" sx={{marginLeft: "1rem"}}>
{t("header.title")}
Expand All @@ -37,9 +87,9 @@
<Nav className="me-auto" />
<Nav>
{sections.map(page => (
<Nav.Link key={page.key} href={page.href}>
<Link key={page.key} href={page.href} active={activeSection === page.key}>
{t(`header.links.${page.key}`)}
</Nav.Link>
</Link>
))}
</Nav>
<Stack direction="row" sx={{alignItems: "center", justifyContent: "start"}}>
Expand All @@ -52,3 +102,17 @@
</AppBar>
);
}

type LinkProps = {
children: React.ReactNode;
href: string;
active: boolean;
}

function Link({children, href, active}: LinkProps) {

Check warning on line 112 in src/components/common/Header.tsx

View workflow job for this annotation

GitHub Actions / lint

'href' is defined but never used
return (
<a /*href={href}*/ className={(active ? "active" : "") + " nav-link"}>
{children}
</a>
);
}
19 changes: 17 additions & 2 deletions src/components/common/Meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
import { Helmet } from "react-helmet";
import { useTranslation } from "react-i18next";
import { PageUrl } from "../../type/page";
import { useState } from "react";

export const constructTitle = () => {
const {t} = useTranslation("components");

const pageTitle = (function() {
const {t} = useTranslation("meta");
return t("title");
})();
const currentSection = window?.location.href.split("#")[1];
const section = currentSection ? `${t(`header.links.${currentSection}`)}` : null;

return `${section ? `${section} |` : ""} ${pageTitle}`;
};

export default function Meta() {
const {t} = useTranslation("meta");

const title = t("title");
const description = t("description");

const [title, setTitle] = useState(constructTitle());

Check warning on line 26 in src/components/common/Meta.tsx

View workflow job for this annotation

GitHub Actions / lint

'setTitle' is assigned a value but never used

const openGraph = {
title: `${title} | Nicholas Krebs`,
title,
description,
image: `${PageUrl}/assets/logo-white-transparent.svg`,
url: `${PageUrl}`,
Expand Down
5 changes: 3 additions & 2 deletions src/components/common/Section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Card } from "@mui/material";
type Props = {
name: string,
noCard?: boolean
noindex?: boolean
children: React.ReactNode,
}
export default function Section({name, noCard, children}: Props) {
export default function Section({name, noCard, noindex, children}: Props) {
return (
<section id={name}>
<section id={name} className={noindex ? "noIndex" : undefined}>
{noCard ? children : <Card sx={{padding: "2rem"}}>{children}</Card>}
</section>
);
Expand Down
3 changes: 1 addition & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Box, Container, createTheme, CssBaseline, ThemeProvider, useMediaQuery
import { useCookies } from "react-cookie";
import { inject } from "@vercel/analytics";
import { BrowserRouter, Route, Routes } from "react-router-dom";

// styles
import "bootstrap/dist/css/bootstrap.min.css";
import "./styles/main.sass";

Expand Down Expand Up @@ -89,6 +87,7 @@ function App() {
<Box sx={{height: "150px", marginTop: "3rem"}} />
<Container sx={{mt: "2rem", mb: "2rem"}}>
<main>
<section id="" style={{height: "1rem"}} />
<RoutingWrapper>
{sections.map((section, index) => (
<React.Fragment key={index}>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function Welcome() {
const {t} = useTranslation("sections");

return (
<Section name={Sections.Welcome} noCard>
<Section name={Sections.Welcome} noCard noindex>
<Typography variant="h3" component="h1" sx={{color: "primary.main"}}>
{t(`${Sections.Welcome}.welcome`)}
</Typography>
Expand Down
1 change: 0 additions & 1 deletion src/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default function de(): Translation {
header: {
title: "Nicholas Krebs",
links: {
welcome: "Willkommen",
about: "Über",
skills: "Skills",
stats: "Statistik",
Expand Down
1 change: 0 additions & 1 deletion src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default function en(): Translation {
header: {
title: "Nicholas Krebs",
links: {
welcome: "Welcome",
about: "About",
skills: "Skills",
stats: "Stats",
Expand Down
Loading
Loading