-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transparent nav / nav color options
- Loading branch information
1 parent
83dfe82
commit b65948e
Showing
11 changed files
with
339 additions
and
69 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {memo} from 'react'; | ||
import {useCart} from '@shopify/hydrogen-react'; | ||
|
||
import {Svg} from '~/components/Svg'; | ||
import {useMenu} from '~/hooks'; | ||
|
||
export const NavigationCart = memo( | ||
({className = '', color}: {className?: string; color?: string}) => { | ||
const {totalQuantity = 0} = useCart(); | ||
const {openCart} = useMenu(); | ||
|
||
return ( | ||
<div className="relative flex items-center"> | ||
<button | ||
aria-label="Open cart" | ||
className={`w-5 text-text ${className}`} | ||
onClick={openCart} | ||
style={{color}} | ||
type="button" | ||
> | ||
<Svg | ||
className="w-full text-current" | ||
src="/svgs/cart.svg#cart" | ||
title="Cart" | ||
viewBox="0 0 24 24" | ||
/> | ||
</button> | ||
|
||
<p | ||
className="text-label-sm w-4 whitespace-nowrap pl-px font-bold text-current" | ||
style={{color}} | ||
> | ||
({totalQuantity || 0}) | ||
</p> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
NavigationCart.displayName = 'NavigationCart'; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {memo} from 'react'; | ||
|
||
import {Link} from '~/components/Link'; | ||
import {Svg} from '~/components/Svg'; | ||
|
||
export const NavigationLogo = memo( | ||
({className = '', color}: {className?: string; color?: string}) => { | ||
return ( | ||
<Link | ||
aria-label="Go to homepage" | ||
className={`text-text ${className}`} | ||
style={{color}} | ||
to="/" | ||
> | ||
<Svg | ||
className="h-8 text-current" | ||
src="/svgs/pack-logo.svg#pack-logo" | ||
title="Storefront logo" | ||
viewBox="0 0 44 34" | ||
/> | ||
</Link> | ||
); | ||
}, | ||
); | ||
|
||
NavigationLogo.displayName = 'NavigationLogo'; |
77 changes: 77 additions & 0 deletions
77
app/components/Header/Navigation/TransparentNavigation.tsx
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {memo, useEffect, useState} from 'react'; | ||
|
||
import {HEADER_NAVIGATION} from '~/lib/constants'; | ||
import {useSettings} from '~/hooks'; | ||
|
||
import {NavigationCart} from './NavigationCart'; | ||
import {NavigationLogo} from './NavigationLogo'; | ||
|
||
export const TransparentNavigation = memo(() => { | ||
const {header} = useSettings(); | ||
|
||
const [isScrolled, setIsScrolled] = useState(false); | ||
|
||
const {logoPositionDesktop, transparentNav} = { | ||
...header?.menu, | ||
}; | ||
const { | ||
iconColor = 'var(--white)', | ||
isChangeColorsOnScroll = true, | ||
pixelOffsetChangeColors = 100, | ||
scrolledBgColor = 'var(--background)', | ||
scrolledIconColor = 'var(--text)', | ||
} = {...transparentNav}; | ||
const gridColsClassDesktop = | ||
logoPositionDesktop === 'center' | ||
? 'lg:grid-cols-[1fr_auto_1fr]' | ||
: 'lg:grid-cols-[auto_1fr_auto]'; | ||
const logoOrderClassDesktop = | ||
logoPositionDesktop === 'center' ? 'lg:order-2' : 'lg:order-1'; | ||
const menuOrderClassDesktop = | ||
logoPositionDesktop === 'center' ? 'lg:order-1' : 'lg:order-2'; | ||
|
||
useEffect(() => { | ||
const setScrolledNav = () => { | ||
const scrolledNavIsActive = window.scrollY > pixelOffsetChangeColors; | ||
if (isScrolled && !scrolledNavIsActive) setIsScrolled(false); | ||
else if (!isScrolled && scrolledNavIsActive) setIsScrolled(true); | ||
}; | ||
|
||
if (!isChangeColorsOnScroll) { | ||
if (isScrolled) setIsScrolled(false); | ||
window.removeEventListener('scroll', setScrolledNav); | ||
return; | ||
} | ||
|
||
window.addEventListener('scroll', setScrolledNav); | ||
return () => { | ||
window.removeEventListener('scroll', setScrolledNav); | ||
}; | ||
}, [isChangeColorsOnScroll, isScrolled, pixelOffsetChangeColors]); | ||
|
||
return ( | ||
<nav | ||
className={`px-contained relative z-[1] grid flex-1 grid-cols-[1fr_auto_1fr] gap-4 transition md:gap-8 ${gridColsClassDesktop}`} | ||
data-comp={HEADER_NAVIGATION} | ||
style={{backgroundColor: isScrolled ? scrolledBgColor : 'transparent'}} | ||
> | ||
<div className={`order-2 flex items-center ${logoOrderClassDesktop}`}> | ||
<NavigationLogo | ||
className="transition" | ||
color={isScrolled ? scrolledIconColor : iconColor} | ||
/> | ||
</div> | ||
|
||
<div className={`order-1 flex items-center ${menuOrderClassDesktop}`} /> | ||
|
||
<div className="order-3 flex items-center justify-end gap-4 md:gap-5"> | ||
<NavigationCart | ||
className="transition" | ||
color={isScrolled ? scrolledIconColor : iconColor} | ||
/> | ||
</div> | ||
</nav> | ||
); | ||
}); | ||
|
||
TransparentNavigation.displayName = 'TransparentNavigation'; |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {Navigation} from './Navigation'; | ||
export {TransparentNavigation} from './TransparentNavigation'; |
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.