-
Notifications
You must be signed in to change notification settings - Fork 31
fix(GlobalHeader): Consolidate animated headers #1454
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1430b39
trying some things
katiezutter 4a6fc79
consolidate to one header
katiezutter 71db239
remove animation story
katiezutter 3f72f36
fix scroll up transition issue
katiezutter 71eee7f
Merge branch 'main' into kz-EGG-822-header-a11y
katiezutter 30d2382
Merge branch 'je-header-animation' into kz-EGG-822-header-a11y
katiezutter 8652d94
fix isInsideHeaderRegion to account for mobile
katiezutter 96a9f7c
use beautiful new theme.elements.headerHeight
katiezutter 8ed87c0
remove unnecessary variables
katiezutter 0abe828
remove slide up/down transitions
katiezutter ffe59c0
useTheme, useWindowScroll
jcenglish 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
99 changes: 11 additions & 88 deletions
99
packages/gamut-labs/src/experimental/GlobalHeader/AnimatedGlobalHeader/index.tsx
This file contains hidden or 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,106 +1,29 @@ | ||
| import { Box } from '@codecademy/gamut'; | ||
| import { useTheme } from '@emotion/react'; | ||
| import cx from 'classnames'; | ||
| import React, { useCallback, useEffect, useState } from 'react'; | ||
| import React from 'react'; | ||
| import { useWindowScroll } from 'react-use'; | ||
|
|
||
| import { GlobalHeaderProps } from '..'; | ||
| import { BasicGlobalHeader } from '../BasicGlobalHeader'; | ||
| import styles from './styles.module.scss'; | ||
|
|
||
| const defaultScrollingState = { | ||
| isInHeaderRegion: true, | ||
| isScrollingDown: true, | ||
| isScrollingDownFromHeaderRegion: true, | ||
| prevScrollPosition: typeof window === 'undefined' ? 0 : window?.pageYOffset, | ||
| }; | ||
|
|
||
| export const AnimatedGlobalHeader: React.FC<GlobalHeaderProps> = (props) => { | ||
| const [scrollingState, setScrollingState] = useState(defaultScrollingState); | ||
|
|
||
| const { | ||
| isScrollingDownFromHeaderRegion, | ||
| isInHeaderRegion, | ||
| isScrollingDown, | ||
| prevScrollPosition, | ||
| } = scrollingState; | ||
|
|
||
| const handleScrolling = useCallback(() => { | ||
| const currentScrollPosition = | ||
| typeof window === 'undefined' ? 0 : window?.pageYOffset; | ||
| const { y } = useWindowScroll(); | ||
|
|
||
| // handle down/up scrolling | ||
| if (currentScrollPosition > prevScrollPosition) { | ||
| setScrollingState((prevState) => { | ||
| return { | ||
| ...prevState, | ||
| isScrollingDown: true, | ||
| prevScrollPosition: currentScrollPosition, | ||
| }; | ||
| }); | ||
| } else { | ||
| setScrollingState((prevState) => { | ||
| return { | ||
| ...prevState, | ||
| isScrollingDown: false, | ||
| prevScrollPosition: currentScrollPosition, | ||
| }; | ||
| }); | ||
| } | ||
| const isInHeaderRegion = y === 0; | ||
|
|
||
| // handle static header region | ||
| if (currentScrollPosition === 0) { | ||
| setScrollingState((prevState) => ({ | ||
| ...prevState, | ||
| isInHeaderRegion: true, | ||
| isScrollingDownFromHeaderRegion: true, | ||
| })); | ||
| } else { | ||
| setScrollingState((prevState) => ({ | ||
| ...prevState, | ||
| isInHeaderRegion: false, | ||
| isScrollingDownFromHeaderRegion: | ||
| prevState.isScrollingDown && | ||
| prevState.isScrollingDownFromHeaderRegion, | ||
| })); | ||
| } | ||
| }, [prevScrollPosition]); | ||
|
|
||
| useEffect(() => { | ||
| window?.addEventListener('scroll', handleScrolling, { | ||
| passive: true, | ||
| }); | ||
|
|
||
| // returned function will be called on component unmount | ||
| return () => { | ||
| window?.removeEventListener('scroll', handleScrolling); | ||
| }; | ||
| }, [handleScrolling]); | ||
| const theme = useTheme(); | ||
|
|
||
| return ( | ||
| <> | ||
| <Box height={theme.elements.headerHeight}> | ||
| <BasicGlobalHeader | ||
| {...props} | ||
| className={cx( | ||
| styles.staticHeader, | ||
| !isScrollingDownFromHeaderRegion && styles.visuallyHide | ||
| )} | ||
| /> | ||
| <BasicGlobalHeader | ||
| {...props} | ||
| className={cx( | ||
| styles.stickyHeader, | ||
| // scrolling down from top | ||
| isScrollingDownFromHeaderRegion && [styles.visuallyHide], | ||
| // scrolling down | ||
| isScrollingDown && [styles.translateUp, styles.transitionSlide], | ||
| // scrolling up | ||
| !isScrollingDown && | ||
| !isInHeaderRegion && [ | ||
| styles.translateDown, | ||
| styles.transitionSlide, | ||
| styles.visuallyShow, | ||
| ], | ||
| isInHeaderRegion && [styles.transitionOpacity, styles.visuallyHide] | ||
| isInHeaderRegion && styles.transitionFadeOut | ||
| )} | ||
| {...props} | ||
| /> | ||
| </> | ||
| </Box> | ||
| ); | ||
| }; | ||
36 changes: 4 additions & 32 deletions
36
packages/gamut-labs/src/experimental/GlobalHeader/AnimatedGlobalHeader/styles.module.scss
This file contains hidden or 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,44 +1,16 @@ | ||
| @import "~@codecademy/gamut-styles/utils"; | ||
|
|
||
| .staticHeader { | ||
| background: transparent; | ||
| border-bottom: 1px solid transparent; | ||
| position: static; | ||
| } | ||
|
|
||
| .stickyHeader { | ||
| border-bottom: 1px solid $color-navy; | ||
| background-color: white; | ||
| background-color: $color-white; | ||
| position: fixed; | ||
| top: 0; | ||
| z-index: 2; | ||
| width: 100%; | ||
| } | ||
|
|
||
| .visuallyShow { | ||
| opacity: 1; | ||
| } | ||
|
|
||
| .visuallyHide { | ||
| opacity: 0; | ||
| } | ||
|
|
||
| .translateDown { | ||
| transform: translateY(0); | ||
| } | ||
|
|
||
| .translateUp { | ||
| transform: translateY(-100%); | ||
| } | ||
|
|
||
| .transitionOpacity { | ||
| transition: opacity 0.5s ease-in-out; | ||
| } | ||
|
|
||
| .transitionSlide { | ||
| transition: transform 0.15s ease-in-out; | ||
| } | ||
|
|
||
| .transitionFade { | ||
| .transitionFadeOut { | ||
| background: transparent; | ||
| border-bottom: 1px solid transparent; | ||
| transition: background 0.5s ease-in-out, border-bottom 0.5s ease-in-out; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.