-
Notifications
You must be signed in to change notification settings - Fork 166
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
bungandy
wants to merge
16
commits into
kawalcovid19:main
Choose a base branch
from
bungandy:bungandy/header-hides-shows-on-scroll
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,831
−1,755
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aad26bf
feat: hides/shows header on scroll
bungandy c73d54e
perf: replace throttle vendor. remove lodash, add throttle-ts from @m…
bungandy 0acb88c
chore: add yarn
bungandy 90cfdc2
test: global-header
bungandy 083b0d5
Merge branch 'main' into bungandy/header-hides-shows-on-scroll
bungandy 0b1b88f
fix: global-header test
bungandy 9103601
Merge branch 'main' of https://github.com/kawalcovid19/wargabantuwarg…
bungandy 232fb66
Merge branch 'kawalcovid19:main' into bungandy/header-hides-shows-on-…
bungandy 95bb4e2
fix: global-header.tsx
bungandy 4e86563
fix: global-header
bungandy 523c5ea
Merge branch 'main' into bungandy/header-hides-shows-on-scroll
mazipan 711570c
chore: merge main branch
bungandy dbacd10
refactor: global-header test
bungandy e666d2e
chore: conflict with main
bungandy 03895bb
test: throttle function, delete lib/scroll-throttle
bungandy 61b7899
test: test target 80.48% still working on it
bungandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
function handleDocumentScroll() { | ||
const { scrollTop: currentScrollTop } = document.documentElement; | ||
const isScrolledDown = previousScrollTop < currentScrollTop; | ||
const isMinimumScrolled = currentScrollTop > MINIMUM_SCROLL; | ||
|
||
setScrollPosition((previousPosition) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
|
@@ -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), { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 touseRef