1
0
Fork 0
freesewing/sites/org/components/header/index.mjs

153 lines
4.7 KiB
JavaScript
Raw Normal View History

// Hooks
import { useState, useEffect, useContext } from 'react'
import { useTranslation } from 'next-i18next'
// Context
import { ModalContext } from 'shared/context/modal-context.mjs'
import { LoadingContext } from 'shared/context/loading-context.mjs'
// Components
2023-04-07 17:10:54 +02:00
import {
DesignIcon,
DocsIcon,
MenuIcon,
SearchIcon,
ShowcaseIcon,
UserIcon,
ThemeIcon,
I18nIcon,
2023-04-30 21:24:35 +02:00
MeasureIcon,
PageIcon,
2023-05-18 13:56:50 +02:00
PlusIcon,
2023-04-07 17:10:54 +02:00
} from 'shared/components/icons.mjs'
import { Ribbon } from 'shared/components/ribbon.mjs'
import { ModalThemePicker, ns as themeNs } from 'shared/components/modal/theme-picker.mjs'
import { ModalLocalePicker, ns as localeNs } from 'shared/components/modal/locale-picker.mjs'
2023-04-09 15:57:25 +02:00
import { ModalMenu } from 'site/components/navigation/modal-menu.mjs'
2023-05-18 13:56:50 +02:00
import { NavButton, NavSpacer, colors } from 'shared/components/header.mjs'
2023-04-07 17:10:54 +02:00
export const ns = ['header', 'sections', ...themeNs, ...localeNs]
2023-04-09 15:57:25 +02:00
const NavIcons = ({ setModal, setSearch }) => {
2023-04-07 17:10:54 +02:00
const { t } = useTranslation(['header'])
const iconSize = 'h-6 w-6 lg:h-12 lg:w-12'
return (
<>
<NavButton onClick={() => setModal(<ModalMenu />)} label={t('header:menu')} color={colors[0]}>
2023-04-07 17:10:54 +02:00
<MenuIcon className={iconSize} />
</NavButton>
<NavSpacer />
<NavButton href="/designs" label={t('header:designs')} color={colors[1]}>
2023-04-07 17:10:54 +02:00
<DesignIcon className={iconSize} />
</NavButton>
<NavButton
2023-04-30 21:24:35 +02:00
href="/patterns"
label={t('header:patterns')}
color={colors[2]}
2023-04-07 17:10:54 +02:00
extraClasses="hidden lg:flex"
>
2023-04-30 21:24:35 +02:00
<PageIcon className={iconSize} />
2023-04-07 17:10:54 +02:00
</NavButton>
2023-04-09 15:57:25 +02:00
<NavButton
2023-04-30 21:24:35 +02:00
href="/sets"
label={t('header:sets')}
color={colors[3]}
2023-04-09 15:57:25 +02:00
extraClasses="hidden lg:flex"
>
2023-04-30 21:24:35 +02:00
<MeasureIcon className={iconSize} />
2023-04-07 17:10:54 +02:00
</NavButton>
2023-04-09 15:57:25 +02:00
<NavButton
2023-04-30 21:24:35 +02:00
href="/showcase"
label={t('header:showcase')}
color={colors[4]}
2023-04-09 15:57:25 +02:00
extraClasses="hidden lg:flex"
>
2023-04-30 21:24:35 +02:00
<ShowcaseIcon className={iconSize} />
2023-04-07 17:10:54 +02:00
</NavButton>
<NavButton
2023-04-30 21:24:35 +02:00
href="/docs"
label={t('header:docs')}
color={colors[5]}
2023-04-07 17:10:54 +02:00
extraClasses="hidden lg:flex"
>
2023-04-30 21:24:35 +02:00
<DocsIcon className={iconSize} />
2023-04-07 17:10:54 +02:00
</NavButton>
<NavSpacer />
<NavButton
onClick={() => setModal(<ModalThemePicker />)}
2023-04-07 17:10:54 +02:00
label={t('header:theme')}
color={colors[6]}
2023-04-07 17:10:54 +02:00
>
<ThemeIcon className={iconSize} />
</NavButton>
2023-04-07 17:33:45 +02:00
<NavButton
onClick={() => setModal(<ModalLocalePicker />)}
2023-04-07 17:33:45 +02:00
label={t('header:language')}
color={colors[7]}
2023-04-07 17:33:45 +02:00
>
2023-04-07 17:10:54 +02:00
<I18nIcon className={iconSize} />
</NavButton>
<NavButton onClick={() => setSearch(true)} label={t('header:search')} color={colors[8]}>
2023-04-07 17:10:54 +02:00
<SearchIcon className={iconSize} />
</NavButton>
<NavSpacer />
<NavButton href="/account" label={t('header:account')} color={colors[9]}>
2023-04-07 17:10:54 +02:00
<UserIcon className={iconSize} />
</NavButton>
2023-05-18 13:56:50 +02:00
<NavButton href="/new" label={t('header:new')} color={colors[10]}>
<PlusIcon className={iconSize} />
</NavButton>
2023-04-07 17:10:54 +02:00
</>
)
}
export const Header = ({ setSearch }) => {
const { setModal } = useContext(ModalContext)
const { loading } = useContext(LoadingContext)
const [prevScrollPos, setPrevScrollPos] = useState(0)
const [show, setShow] = useState(true)
useEffect(() => {
if (typeof window !== 'undefined') {
2022-01-20 09:07:38 +01:00
const handleScroll = () => {
2022-11-23 21:42:22 +01:00
const curScrollPos = typeof window !== 'undefined' ? window.pageYOffset : 0
2022-01-20 09:07:38 +01:00
if (curScrollPos >= prevScrollPos) {
if (show && curScrollPos > 20) setShow(false)
2022-11-23 21:42:22 +01:00
} else setShow(true)
2022-01-20 09:07:38 +01:00
setPrevScrollPos(curScrollPos)
}
window.addEventListener('scroll', handleScroll)
return () => window.removeEventListener('scroll', handleScroll)
}
2022-01-20 09:07:38 +01:00
}, [prevScrollPos, show])
return (
2022-11-23 21:42:22 +01:00
<header
className={`
fixed bottom-0 lg:bottom-auto lg:top-0 left-0
bg-neutral
w-full
z-30
transition-transform
${show || loading ? '' : 'fixed bottom-0 lg:top-0 left-0 translate-y-36 lg:-translate-y-36'}
2022-11-23 21:42:22 +01:00
drop-shadow-xl
`}
>
2022-12-24 18:16:09 +01:00
<div className="m-auto md:px-8">
<div className="p-0 flex flex-row gap-2 justify-between text-neutral-content items-center">
2023-04-07 17:10:54 +02:00
{/* Non-mobile content */}
2023-06-09 19:11:45 +02:00
<div className="hidden lg:flex lg:flex-row lg:justify-between items-center xl:justify-center w-full">
<NavIcons setModal={setModal} setSearch={setSearch} />
2022-11-23 21:42:22 +01:00
</div>
2023-04-07 17:10:54 +02:00
{/* Mobile content */}
<div className="flex lg:hidden flex-row items-center justify-between w-full">
<NavIcons setModal={setModal} setSearch={setSearch} />
</div>
</div>
2022-11-23 21:42:22 +01:00
</div>
<Ribbon />
2022-11-23 21:42:22 +01:00
</header>
)
}