forge
Explore
Log in

Don't you dare go Hollow

hezarfen_landing

Clone
★ 0

0 watching0 forksTypeScript581.6 KiB94b1804Burak Boduroğlu2 months ago15 commits

Header.tsx141 lines, 4.9 KiBHistoryRawDownload
1import { A, useLocation } from "@solidjs/router";
2import { createEffect, createSignal, For, on, onCleanup, Show } from "solid-js";
3import Logo from "./Logo";
4import { PixelClose, PixelMenu } from "./icons/pixel";
5import { anchors, routes, site } from "~/config";
6import { t } from "~/i18n";
7
8/**
9 * The site is one page, so the bar is a table of contents rather than a set of
10 * routes: every label is an in-page anchor, and the band currently under the
11 * header is the one marked as current.
12 */
13export default function Header() {
14 const location = useLocation();
15 const [open, setOpen] = createSignal(false);
16 const [active, setActive] = createSignal<string | null>(null);
17
18 /**
19 * The bar carries four stops; the sheet carries every one. "Nasıl çalışır"
20 * is the diagram page rather than the band of the same name — the band is a
21 * summary, and anyone who clicks the label wants the long version.
22 */
23 const primary = [
24 { href: anchors.products, label: t.nav.products },
25 { href: routes.how, label: t.nav.theory },
26 { href: anchors.audience, label: t.nav.audience },
27 { href: anchors.about, label: t.nav.about },
28 ];
29
30 const all = [
31 { href: anchors.problem, label: t.nav.problem },
32 ...primary,
33 { href: anchors.platform, label: t.nav.platform },
34 { href: anchors.roles, label: t.nav.roles },
35 { href: anchors.trust, label: t.nav.trust },
36 { href: anchors.faq, label: t.nav.faq },
37 { href: routes.contact, label: t.nav.contact },
38 ];
39
40 /** Anchors carry a fragment; the two standing pages do not. */
41 const id = (href: string) => href.split("#")[1] ?? "";
42 /* A band is only current on the page it lives on: on the diagram or the form
43 page, a stale "Ürünler" mark would say the click never left home. */
44 const isCurrent = (href: string) =>
45 href.includes("#")
46 ? location.pathname === routes.home && active() === id(href)
47 : location.pathname === href;
48
49 /**
50 * Which band is current. The observer's top margin is the header's own
51 * height, so a section counts as "under the bar" rather than merely on
52 * screen, and the bottom margin keeps a band that is only just visible from
53 * taking the mark from the one filling the viewport.
54 */
55 createEffect(
56 on(
57 () => location.pathname,
58 pathname => {
59 setActive(null);
60 // The header outlives the page under it, so the bands are looked up
61 // again on every page change: arriving home from the form page has to
62 // find sections that did not exist when the header mounted.
63 if (pathname !== routes.home || !("IntersectionObserver" in window)) return;
64
65 const sections = all
66 .filter(item => item.href.includes("#"))
67 .map(item => document.getElementById(id(item.href)))
68 .filter((node): node is HTMLElement => node !== null);
69
70 const observer = new IntersectionObserver(
71 entries => {
72 const visible = entries
73 .filter(entry => entry.isIntersecting)
74 .sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top);
75 if (visible.length > 0) setActive(visible[0]!.target.id);
76 },
77 { rootMargin: "-72px 0px -55% 0px", threshold: 0 },
78 );
79
80 sections.forEach(section => observer.observe(section));
81 onCleanup(() => observer.disconnect());
82 },
83 ),
84 );
85
86 return (
87 <header class="hz-header">
88 <div class="hz-header-inner">
89 <Logo />
90
91 <nav class="hz-nav" aria-label={t.nav.menu}>
92 <For each={primary}>
93 {item => (
94 <a
95 href={item.href}
96 class={isCurrent(item.href) ? "is-current" : undefined}
97 aria-current={isCurrent(item.href) ? "true" : undefined}
98 >
99 {item.label}
100 </a>
101 )}
102 </For>
103 </nav>
104
105 <div class="hz-header-actions">
106 <A href={routes.contact} class="hz-btn hz-btn-primary">
107 {t.nav.demo}
108 </A>
109 <button
110 type="button"
111 class="hz-menu-btn"
112 aria-expanded={open()}
113 aria-controls="site-menu"
114 aria-label={open() ? t.nav.close : t.nav.menu}
115 onClick={() => setOpen(!open())}
116 >
117 <Show when={open()} fallback={<PixelMenu class="size-5" />}>
118 <PixelClose class="size-5" />
119 </Show>
120 </button>
121 </div>
122 </div>
123
124 <Show when={open()}>
125 <div id="site-menu" class="hz-sheet">
126 <For each={all}>
127 {item => (
128 <a href={item.href} onClick={() => setOpen(false)}>
129 {item.label}
130 </a>
131 )}
132 </For>
133 <A href={routes.contact} onClick={() => setOpen(false)} class="hz-btn hz-btn-primary">
134 {t.nav.demo}
135 </A>
136 <p class="hz-small mt-3 px-3">{site.email}</p>
137 </div>
138 </Show>
139 </header>
140 );
141}