Skip to content

Commit c136b44

Browse files
committed
feat: [#109] responsive navigation
1 parent 41da581 commit c136b44

File tree

12 files changed

+329
-158
lines changed

12 files changed

+329
-158
lines changed
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Loading

src/components/navigation/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { buildTopNav } from "./top-nav";
2+
import { buildMobileNav } from "./mobile-nav";
3+
4+
export type LinkItem = [
5+
name: string,
6+
path: string,
7+
]
8+
9+
const navLinks: LinkItem[] = [
10+
['Side Quests', '/projects'],
11+
['About', '/about'],
12+
['Logs', '/logs'],
13+
['Contact', '/contact'],
14+
]
15+
16+
//-----------------------------------------------------------------------
17+
18+
export function buildNavigation() {
19+
const navBox = document.createElement("div");
20+
navBox.appendChild(buildTopNav(navLinks));
21+
navBox.appendChild(buildMobileNav(navLinks));
22+
return navBox
23+
}
24+

src/components/logo/index.ts renamed to src/components/navigation/logo.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import LOGO from './assets/cd-labs-logo.png';
22

33
// --------------------------------------------------------------------------------
44

5-
export function buildMainLogo() {
5+
export function renderMainLogo() {
66
const logoContainer = document.createElement('div');
77
const logoLink = document.createElement('a');
88
const logoImg = document.createElement('img');
@@ -23,15 +23,15 @@ export function buildMainLogo() {
2323
return logoContainer;
2424
}
2525

26-
// --------------------------------------------------------------------------------
27-
2826
export function changeLogoOnScroll() {
29-
const logoContainer = document.querySelector('.main-logo') as HTMLElement;
30-
if (!logoContainer) return;
31-
32-
if (window.scrollY > 50) {
33-
logoContainer.classList.add('scrolled');
34-
} else {
35-
logoContainer.classList.remove('scrolled');
36-
}
27+
const logoContainers = document.querySelectorAll('.main-logo') as NodeListOf<HTMLElement>;
28+
if (!logoContainers.length) return;
29+
30+
logoContainers.forEach(logoContainer => {
31+
if (window.scrollY > 50) {
32+
logoContainer.classList.add('scrolled');
33+
} else {
34+
logoContainer.classList.remove('scrolled');
35+
}
36+
});
3737
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import MENU_ICON from './assets/menu-icon.svg';
2+
import { renderMainLogo } from './logo';
3+
import { LinkItem } from "./";
4+
5+
//-----------------------------------------------------------------------
6+
7+
export function buildMobileNav(links : LinkItem[]) {
8+
const mobileNav = document.createElement("div");
9+
mobileNav.id = "mobile-nav";
10+
11+
const menuButtonElement = menuButton();
12+
const menuElement = mobileMenu(links);
13+
14+
mobileNav.appendChild(menuElement);
15+
mobileNav.appendChild(renderMainLogo());
16+
mobileNav.appendChild(menuButtonElement);
17+
18+
menuButtonElement.addEventListener('click', () => {
19+
mobileNav.classList.toggle('menu-open');
20+
});
21+
22+
return mobileNav;
23+
}
24+
25+
//-----------------------------------------------------------------------
26+
27+
function menuButton() {
28+
const menuButton = document.createElement("button");
29+
menuButton.id = "menu-button";
30+
31+
const menuImg = document.createElement("img");
32+
menuImg.src = MENU_ICON;
33+
menuImg.alt = "Menu Icon";
34+
35+
menuButton.appendChild(menuImg);
36+
return menuButton;
37+
}
38+
39+
function mobileMenu(links: LinkItem[]) {
40+
const menu = document.createElement("ul");
41+
menu.className = "mobile-menu";
42+
43+
links.forEach(link => {
44+
const [name, path] = link;
45+
const li = document.createElement("li");
46+
const a = document.createElement("a");
47+
48+
a.href = path;
49+
a.textContent = name;
50+
51+
li.appendChild(a);
52+
menu.appendChild(li);
53+
})
54+
55+
return menu;
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { LinkItem } from "./";
2+
import { renderMainLogo } from "./logo";
3+
4+
//-----------------------------------------------------------------------
5+
6+
export function buildTopNav(links: LinkItem[]) {
7+
const topNav = document.createElement("div");
8+
const barBox = document.createElement("div");
9+
const ul = document.createElement("ul");
10+
11+
topNav.id = "top-nav";
12+
barBox.className = "bar-box";
13+
14+
links.forEach(link => {
15+
const [name, path] = link;
16+
const li = document.createElement("li");
17+
const a = document.createElement("a");
18+
19+
a.href = path;
20+
a.textContent = name;
21+
22+
li.appendChild(a);
23+
ul.appendChild(li);
24+
})
25+
26+
barBox.appendChild(ul);
27+
topNav.appendChild(renderMainLogo());
28+
topNav.appendChild(barBox);
29+
return topNav
30+
}
31+

src/components/top-nav/index.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {buildMainLogo, changeLogoOnScroll} from "./components/logo";
21
import { router } from "./core/router";
32
import {homeView, aboutView, buildViewBase, renderView} from "./views";
4-
import {buildTopNav} from "./components/top-nav";
3+
import {buildNavigation} from "./components/navigation";
4+
import {changeLogoOnScroll} from "./components/navigation/logo";
55

66

77
const routes = [
@@ -16,10 +16,8 @@ routes.forEach(route => router.registerRoute(route.path, route.handler));
1616
function init() {
1717
const body = document.getElementsByTagName("body")[0];
1818
const contentPage = buildViewBase();
19-
const mainLogo = buildMainLogo();
20-
const nav = buildTopNav();
19+
const nav = buildNavigation();
2120

22-
body.appendChild(mainLogo);
2321
body.appendChild(nav);
2422
window.addEventListener('scroll', changeLogoOnScroll);
2523

src/styles/components/logo.css

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)