Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hides/shows header on scroll #592

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions components/layout/__tests__/global-header.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { render } from "@testing-library/react";
import { throttle } from "@martinstark/throttle-ts";
import { GlobalHeader } from "../global-header";

jest.mock("next/router", () => require("next-router-mock"));

describe("GlobalHeader", () => {
test("should run throttle function 1 time", () => {
const func = jest.fn();
const [throttleFunc] = throttle(func, 200);

throttleFunc();
expect(func).toHaveBeenCalledTimes(1);
});

it("renders correctly", () => {
const { container } = render(<GlobalHeader />);

expect(container.firstChild).toMatchInlineSnapshot(`
<header
class="flex items-center justify-center fixed w-full h-16 z-40 bg-brand-500 shadow-md"
class="flex items-center justify-center fixed w-full h-16 z-40 bg-brand-500 shadow-md transition duration-200"
>
<div
class="w-full sm:max-w-xl mx-auto flex items-center justify-between h-full px-4"
Expand Down Expand Up @@ -66,7 +75,7 @@ describe("GlobalHeader", () => {
>
<button
aria-expanded="false"
class="flex items-center justify-center rounded-md h-10 w-10 ml-4 hover:bg-gray-100 hover:bg-opacity-10 focus:bg-gray-100 focus:bg-opacity-10"
class="flex items-center justify-center rounded-md h-10 w-10 ml-4 hover:bg-gray-100 hover:bg-opacity-10 focus:bg-gray-100 focus:bg-opacity-10 focus:outline-none appearance-none"
id="headlessui-popover-button-1"
type="button"
>
Expand Down
48 changes: 45 additions & 3 deletions components/layout/global-header.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
import { createElement, useRef } from "react";
import { createElement, useRef, useState, useEffect } from "react";

import { Popover } from "@headlessui/react";
import Link from "next/link";
import clsx from "clsx";
import { throttle } from "@martinstark/throttle-ts";
import { Container } from "../ui/container";
import { NavigationMenuPopover, navMenuButtonIcon } from "./navigation-menu";
import { WBWLogoWhite } from "~/components/ui/wbw-logo";

const MINIMUM_SCROLL = 200; // 64 height header
const TIMEOUT_DELAY = 150;

export function GlobalHeader() {
const popoverButtonRef = useRef<HTMLButtonElement>(null);

const [shouldHideHeader, setShouldHideHeader] = useState(false);
const [, setScrollPosition] = useState(0);
let previousScrollTop = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try useRef to save the previous render value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need setScrollPosition in this case, just change to useRef


function handleDocumentScroll() {
const { scrollTop: currentScrollTop } = document.documentElement;
const isScrolledDown = previousScrollTop < currentScrollTop;
const isMinimumScrolled = currentScrollTop > MINIMUM_SCROLL;

setScrollPosition((previousPosition) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not use same name for arguments and upper scope variables, it cause a confusing

previousScrollTop = previousPosition;
return currentScrollTop;
});

setTimeout(() => {
setShouldHideHeader(isScrolledDown && isMinimumScrolled);
}, TIMEOUT_DELAY);
}

const [handleDocumentScrollThrottled] = throttle(handleDocumentScroll, 200);

useEffect(() => {
window.addEventListener("scroll", handleDocumentScrollThrottled);

return () =>
window.removeEventListener("scroll", handleDocumentScrollThrottled);
}, []);

const hiddenStyle = shouldHideHeader
? "-translate-y-full overflow-y-hidden"
: "";

return (
<header className="flex items-center justify-center fixed w-full h-16 z-40 bg-brand-500 shadow-md">
<header
className={clsx(
"flex items-center justify-center fixed w-full h-16 z-40 bg-brand-500 shadow-md transition duration-200",
hiddenStyle,
)}
>
<Container className="flex items-center justify-between h-full px-4">
<Link href="/">
<a className="align-middle">
Expand All @@ -22,7 +64,7 @@ export function GlobalHeader() {
{({ open }) => (
<>
<Popover.Button
className="flex items-center justify-center rounded-md h-10 w-10 ml-4 hover:bg-gray-100 hover:bg-opacity-10 focus:bg-gray-100 focus:bg-opacity-10"
className="flex items-center justify-center rounded-md h-10 w-10 ml-4 hover:bg-gray-100 hover:bg-opacity-10 focus:bg-gray-100 focus:bg-opacity-10 focus:outline-none appearance-none"
ref={popoverButtonRef}
>
{createElement(navMenuButtonIcon(open), {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@headlessui/react": "^1.3.0",
"@heroicons/react": "1.0.2",
"@martinstark/throttle-ts": "^1.2.3",
bungandy marked this conversation as resolved.
Show resolved Hide resolved
"@netlify/plugin-nextjs": "3.7.1",
"@tailwindcss/forms": "^0.3.3",
"@tailwindcss/typography": "0.4.1",
Expand Down Expand Up @@ -81,7 +82,7 @@
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
"frontmatter-markdown-loader": "3.6.3",
"husky": "7.0.1",
"husky": "^7.0.1",
"identity-obj-proxy": "3.0.0",
"is-ci-cli": "2.2.0",
"jest": "27.0.6",
Expand Down
Loading