Skip to content

Commit f351ea3

Browse files
committed
Add glass overlay in header and scroll transitions
1 parent 766dd77 commit f351ea3

5 files changed

Lines changed: 121 additions & 29 deletions

File tree

‎app/components/GlassHeader.tsx‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"use client";
2+
3+
import * as stylex from "@stylexjs/stylex";
4+
5+
import { ReactNode, useEffect, useRef } from "react";
6+
7+
import { sharedStyles } from "@/app/styles";
8+
9+
// Distance in pixels from the top of the viewport where the header docks
10+
const DOCK_TOP = 16;
11+
// Extra glass below the docked header, all of which is the fade-out
12+
const GLASS_PADDING_BOTTOM = 64;
13+
// Scroll distance in pixels over which the glass fades in from the top of the page, so it doesn't cover
14+
// content sitting near the top before the page scrolls (like the subtitle on narrow screens)
15+
const GLASS_FADE_IN_DISTANCE = 32;
16+
17+
type Props = {
18+
// Docks onto the glass and stays pinned while the page scrolls
19+
children: ReactNode;
20+
// Scrolls with the page and slides under the glass
21+
subtitle?: ReactNode;
22+
};
23+
24+
/**
25+
* Renders a fixed frosted-glass band across the top of the viewport that page content scrolls under. The
26+
* docked header scrolls up with the page until it reaches the glass and then stays pinned above it.
27+
*
28+
* The pinned header and subtitle render as siblings rather than inside one wrapper because a sticky element
29+
* only stays pinned while its parent is on screen.
30+
*/
31+
export function GlassHeader({ children, subtitle }: Props) {
32+
const glassRef = useRef<HTMLDivElement>(null);
33+
const headerRef = useRef<HTMLDivElement>(null);
34+
35+
useEffect(() => {
36+
const glass = glassRef.current;
37+
const header = headerRef.current;
38+
if (!glass || !header) {
39+
return;
40+
}
41+
42+
const updateGlassOpacity = () => {
43+
glass.style.opacity = String(Math.min(window.scrollY / GLASS_FADE_IN_DISTANCE, 1));
44+
};
45+
46+
const updateGlassHeight = () => {
47+
const glassHeight = DOCK_TOP + header.offsetHeight + GLASS_PADDING_BOTTOM;
48+
// Set on the root so anchor links can offset their scroll position by the same amount
49+
document.documentElement.style.setProperty("--glass-height", `${glassHeight}px`);
50+
};
51+
52+
const resizeObserver = new ResizeObserver(updateGlassHeight);
53+
resizeObserver.observe(header);
54+
updateGlassHeight();
55+
56+
window.addEventListener("scroll", updateGlassOpacity, { passive: true });
57+
updateGlassOpacity();
58+
59+
return () => {
60+
resizeObserver.disconnect();
61+
window.removeEventListener("scroll", updateGlassOpacity);
62+
};
63+
}, []);
64+
65+
return (
66+
<>
67+
<div aria-hidden="true" ref={glassRef} {...stylex.props(styles.glass)} />
68+
<div ref={headerRef} {...stylex.props(sharedStyles.centered, styles.header)}>
69+
{children}
70+
</div>
71+
{subtitle ? <div {...stylex.props(sharedStyles.centered)}>{subtitle}</div> : null}
72+
</>
73+
);
74+
}
75+
76+
const styles = stylex.create({
77+
glass: {
78+
backdropFilter: "blur(16px) saturate(180%)",
79+
backgroundImage:
80+
"linear-gradient(to bottom, color-mix(in srgb, var(--background) 78%, transparent), color-mix(in srgb, var(--background) 45%, transparent) 70%, transparent)",
81+
// Fades the blur itself, not just the tint, along an ease-in-out curve so the glass has no visible bottom edge
82+
maskImage: `linear-gradient(to bottom, black calc(100% - ${GLASS_PADDING_BOTTOM}px), rgb(0 0 0 / 0.85) calc(100% - ${GLASS_PADDING_BOTTOM * 0.75}px), rgb(0 0 0 / 0.5) calc(100% - ${GLASS_PADDING_BOTTOM * 0.5}px), rgb(0 0 0 / 0.15) calc(100% - ${GLASS_PADDING_BOTTOM * 0.25}px), transparent)`,
83+
// Starts hidden to match the top of the page; the effect takes over once the page has loaded
84+
opacity: 0,
85+
pointerEvents: "none",
86+
position: "fixed",
87+
zIndex: 20,
88+
height: "var(--glass-height, 6rem)",
89+
left: 0,
90+
right: 0,
91+
top: 0,
92+
},
93+
header: {
94+
position: "sticky",
95+
zIndex: 21,
96+
top: `${DOCK_TOP}px`,
97+
},
98+
});

‎app/components/ImageOverlay.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useDispatch, useSelector } from "react-redux";
77
import { CloseIcon } from "./Icons";
88
import { FocusTrap } from "focus-trap-react";
99
import Image from "next/image";
10+
import { createPortal } from "react-dom";
1011
import * as stylex from "@stylexjs/stylex";
1112
import { hideImageOverlay } from "@/app/store/reducers/writingSlice";
1213
import { useLockBodyScroll } from "@/app/hooks";
@@ -97,7 +98,8 @@ export function ImageOverlayContainer() {
9798
return null;
9899
}
99100

100-
return <ImageOverlay />;
101+
// Portaled to the body so the overlay escapes main's containment and covers the glass header
102+
return createPortal(<ImageOverlay />, document.body);
101103
}
102104

103105
const styles = stylex.create({
@@ -162,7 +164,7 @@ const styles = stylex.create({
162164
overlay: {
163165
backgroundColor: "var(--background)",
164166
position: "fixed",
165-
zIndex: 10,
167+
zIndex: 30,
166168
bottom: 0,
167169
height: "100%",
168170
left: 0,

‎app/components/Navbar.tsx‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HeadingLevel, getHeading } from "@/app/(writing)/utils/heading";
55

66
import Link from "next/link";
77
import { ReactNode } from "react";
8+
import { GlassHeader } from "./GlassHeader";
89
import { ThemeSwitch } from "./ThemeSwitch";
910
import { formatUTCTimestampToDateString } from "../utils/dates";
1011
import postsJson from "@/app/(writing)/posts.json";
@@ -34,7 +35,7 @@ export function Navbar() {
3435
let showThemeSwitch = true;
3536
let titleText: ReactNode | null = <h1>Nathan Thomas</h1>;
3637
let subtitleText: ReactNode | null = (
37-
<p {...stylex.props(styles.subtitle)}>
38+
<p {...stylex.props(styles.subtitle, sharedStyles.pageSection)}>
3839
by{" "}
3940
<Link aria-label="Link to Nathan's home page" href="/">
4041
Nathan Thomas
@@ -58,23 +59,20 @@ export function Navbar() {
5859
}
5960

6061
return (
61-
<header {...stylex.props(styles.header, sharedStyles.pageSection)}>
62-
<ul {...stylex.props(styles.navList)}>
63-
<li data-unstyled="true">
64-
<div {...stylex.props(styles.titleStack)}>
65-
{titleText}
66-
{!isHomePage ? subtitleText : null}
67-
</div>
68-
</li>
69-
{showThemeSwitch ? (
70-
<li data-unstyled="true">
71-
<div {...stylex.props(styles.controls)}>
72-
<ThemeSwitch />
73-
</div>
74-
</li>
75-
) : null}
76-
</ul>
77-
</header>
62+
<GlassHeader subtitle={isHomePage ? null : subtitleText}>
63+
<header {...stylex.props(styles.header, sharedStyles.pageSection)}>
64+
<ul {...stylex.props(styles.navList)}>
65+
<li data-unstyled="true">{titleText}</li>
66+
{showThemeSwitch ? (
67+
<li data-unstyled="true">
68+
<div {...stylex.props(styles.controls)}>
69+
<ThemeSwitch />
70+
</div>
71+
</li>
72+
) : null}
73+
</ul>
74+
</header>
75+
</GlassHeader>
7876
);
7977
}
8078

@@ -101,9 +99,4 @@ const styles = stylex.create({
10199
fontSize: "0.875rem",
102100
lineHeight: "1.25rem",
103101
},
104-
titleStack: {
105-
display: "flex",
106-
flexDirection: "column",
107-
lineHeight: 1,
108-
},
109102
});

‎app/globals.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ html.dark {
5555
/* Hide the page scrollbar while keeping wheel, touch, and keyboard scrolling */
5656
html {
5757
scrollbar-width: none;
58+
/* Keeps anchor targets clear of the glass header, whose height GlassHeader measures */
59+
scroll-padding-top: var(--glass-height, 0);
5860
}
5961

6062
html::-webkit-scrollbar {

‎app/layout.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type { Metadata } from "next";
1111
import { Navbar } from "@/app/components/Navbar";
1212
import { Providers } from "@/app/components/Providers";
1313
import { ReactNode } from "react";
14-
import { sharedStyles } from "@/app/styles";
1514

1615
export const metadata: Metadata = {
1716
title: "Nathan Thomas",
@@ -88,9 +87,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
8887
<Providers>
8988
<body className={`${GeistSans.variable} ${GeistMono.variable}`}>
9089
<div {...stylex.props(styles.shell)}>
91-
<div {...stylex.props(sharedStyles.centered)}>
92-
<Navbar />
93-
</div>
90+
<Navbar />
9491
<main {...stylex.props(styles.main)}>{children}</main>
9592
<div {...stylex.props(styles.footerWrap)}>
9693
<Footer />

0 commit comments

Comments
 (0)