Skip to content

Commit 31f9c49

Browse files
committed
Navbar: transparent at top, opaque on scroll with transition
Add navbarScroll client module toggling data-navbar-scrolled and data-navbar-home-top attributes. CSS transitions background/shadow on scroll. White links on home page over dark hero banner.
1 parent 5b6056c commit 31f9c49

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

docusaurus.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const config: Config = {
2020
organizationName: "/HaudinFlorence/", // Usually your GitHub org/user name.
2121
projectName: "quantstack.github.io", // Usually your repo name.
2222

23+
clientModules: [require.resolve("./src/clientModules/navbarScroll.ts")],
24+
2325
onBrokenLinks: "warn",
2426
onBrokenMarkdownLinks: "warn",
2527
staticDirectories: ["static"],

src/clientModules/navbarScroll.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const THRESHOLD = 20;
2+
3+
function update(): void {
4+
const scrolled = window.scrollY > THRESHOLD;
5+
const isHome = window.location.pathname === "/";
6+
document.documentElement.toggleAttribute("data-navbar-scrolled", scrolled);
7+
document.documentElement.toggleAttribute(
8+
"data-navbar-home-top",
9+
isHome && !scrolled
10+
);
11+
}
12+
13+
export function onRouteDidUpdate(): void {
14+
update();
15+
}
16+
17+
if (typeof window !== "undefined") {
18+
window.addEventListener("scroll", update, { passive: true });
19+
update();
20+
}

src/css/custom.css

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,36 @@ a.menu__link:active {
344344
transform: scale(1);
345345
}
346346

347+
/* Scroll-adaptive navbar */
348+
.navbar {
349+
transition: background-color 0.25s ease, box-shadow 0.25s ease;
350+
}
351+
352+
html:not([data-navbar-scrolled]) .navbar {
353+
background: transparent;
354+
box-shadow: none;
355+
}
356+
357+
html[data-navbar-scrolled] .navbar {
358+
background: var(--ifm-bg-neutral);
359+
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
360+
}
361+
362+
/* Home page at top: white links over dark hero */
363+
html[data-navbar-home-top] .navbar__link,
364+
html[data-navbar-home-top] .custom_navbar_item {
365+
color: white;
366+
}
367+
368+
html[data-navbar-home-top] .custom_navbar_item:hover {
369+
background-color: rgba(255, 255, 255, 0.15);
370+
color: white;
371+
}
372+
373+
html[data-navbar-home-top] .navbar__link:hover {
374+
color: var(--ifm-color-primary-p1);
375+
}
376+
347377
.navbar__inner {
348378
position: relative;
349379
}
@@ -387,7 +417,8 @@ a.menu__link:active {
387417
border-radius: 4px;
388418
}
389419

390-
.navbar__link:hover {
420+
html[data-navbar-scrolled] .navbar__link:hover,
421+
html:not([data-navbar-home-top]) .navbar__link:hover {
391422
color: black;
392423
}
393424

0 commit comments

Comments
 (0)