|
| 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 | +}); |
0 commit comments