webapp/src/components/Nav/Header.tsx

160 lines
5.5 KiB
TypeScript
Raw Normal View History

import { For, Show, createSignal, createEffect, onMount, onCleanup } from 'solid-js'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-09-09 11:53:35 +00:00
import { Modal } from './Modal'
2022-10-25 16:25:42 +00:00
import { AuthModal } from './AuthModal'
import { useModalStore } from '../../stores/ui'
2023-02-17 09:21:02 +00:00
import { router, ROUTES, useRouter } from '../../stores/router'
2022-09-28 21:08:14 +00:00
import styles from './Header.module.scss'
2022-09-22 09:37:49 +00:00
import { getPagePath } from '@nanostores/router'
2022-09-28 21:08:14 +00:00
import { clsx } from 'clsx'
import { HeaderAuth } from './HeaderAuth'
2023-01-31 13:58:28 +00:00
import { getShareUrl, SharePopup } from '../Article/SharePopup'
import { getDescription } from '../../utils/meta'
2023-02-09 22:39:52 +00:00
import { Snackbar } from './Snackbar'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
type Props = {
title?: string
2023-04-17 10:31:20 +00:00
slug?: string
2022-09-23 21:29:32 +00:00
isHeaderFixed?: boolean
articleBody?: string
cover?: string
2023-04-17 10:31:20 +00:00
scrollToComments?: (value: boolean) => void
2022-09-22 09:37:49 +00:00
}
export const Header = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
const resources: { name: string; route: keyof typeof ROUTES }[] = [
{ name: t('zine'), route: 'home' },
{ name: t('feed'), route: 'feed' },
{ name: t('topics'), route: 'topics' }
]
2022-09-09 11:53:35 +00:00
// signals
2022-09-15 18:18:07 +00:00
const [getIsScrollingBottom, setIsScrollingBottom] = createSignal(false)
2022-09-15 19:41:05 +00:00
const [getIsScrolled, setIsScrolled] = createSignal(false)
2022-09-09 11:53:35 +00:00
const [fixed, setFixed] = createSignal(false)
2022-10-25 15:36:32 +00:00
const [isSharePopupVisible, setIsSharePopupVisible] = createSignal(false)
const [isProfilePopupVisible, setIsProfilePopupVisible] = createSignal(false)
2022-10-25 16:25:42 +00:00
const { modal } = useModalStore()
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
const { page } = useRouter()
2022-09-22 09:37:49 +00:00
2022-09-09 11:53:35 +00:00
// methods
2022-10-25 15:36:32 +00:00
const toggleFixed = () => setFixed((oldFixed) => !oldFixed)
2022-09-09 11:53:35 +00:00
// effects
let windowScrollTop = 0
2022-09-09 11:53:35 +00:00
createEffect(() => {
2022-11-04 13:58:42 +00:00
const mainContent = document.querySelector('.main-content') as HTMLDivElement
if (fixed() || modal() !== null) {
windowScrollTop = window.scrollY
mainContent.style.marginTop = `-${windowScrollTop}px`
}
2022-10-25 16:25:42 +00:00
document.body.classList.toggle('fixed', fixed() || modal() !== null)
2022-10-25 21:17:35 +00:00
document.body.classList.toggle(styles.fixed, fixed() && !modal())
if (!fixed() && !modal()) {
mainContent.style.marginTop = ''
window.scrollTo(0, windowScrollTop)
}
2022-10-25 15:36:32 +00:00
})
2022-09-09 11:53:35 +00:00
2022-09-14 21:07:47 +00:00
onMount(() => {
2022-09-15 18:18:07 +00:00
let scrollTop = window.scrollY
2022-09-14 21:07:47 +00:00
2022-09-15 18:18:07 +00:00
const handleScroll = () => {
setIsScrollingBottom(window.scrollY > scrollTop)
2022-09-15 19:41:05 +00:00
setIsScrolled(window.scrollY > 0)
2022-09-15 18:18:07 +00:00
scrollTop = window.scrollY
}
2022-09-14 21:07:47 +00:00
2022-09-15 18:18:07 +00:00
window.addEventListener('scroll', handleScroll, { passive: true })
onCleanup(() => {
window.removeEventListener('scroll', handleScroll)
})
})
2022-09-14 21:07:47 +00:00
2023-04-20 14:01:15 +00:00
const scrollToComments = (event, value) => {
event.preventDefault()
2023-04-17 10:31:20 +00:00
props.scrollToComments(value)
}
2022-09-09 11:53:35 +00:00
return (
2022-09-15 18:18:07 +00:00
<header
2022-09-28 21:08:14 +00:00
class={styles.mainHeader}
2022-09-15 18:18:07 +00:00
classList={{
2022-09-28 21:08:14 +00:00
[styles.headerFixed]: props.isHeaderFixed,
2022-09-29 20:10:00 +00:00
[styles.headerScrolledTop]: !getIsScrollingBottom() && getIsScrolled(),
[styles.headerScrolledBottom]:
(getIsScrollingBottom() && getIsScrolled() && !isProfilePopupVisible()) || isSharePopupVisible(),
2022-10-04 10:48:11 +00:00
[styles.headerWithTitle]: Boolean(props.title)
2022-09-15 18:18:07 +00:00
}}
>
2023-06-14 20:16:44 +00:00
<Modal variant="wide" name="auth" noPadding={true}>
2022-09-09 11:53:35 +00:00
<AuthModal />
</Modal>
2022-10-17 20:53:04 +00:00
<div class={clsx(styles.mainHeaderInner, 'wide-container')}>
<nav class={clsx('row', styles.headerInner, { ['fixed']: fixed() })}>
2023-06-28 20:18:43 +00:00
<div class={clsx('col-md-5 col-xl-4 col-auto', styles.mainLogo)}>
<a href={getPagePath(router, 'home')}>
2022-09-09 11:53:35 +00:00
<img src="/logo.svg" alt={t('Discours')} />
</a>
</div>
2023-06-28 20:18:43 +00:00
<div class={clsx('col offset-xl-1', styles.mainNavigationWrapper)}>
2022-10-03 21:44:21 +00:00
<Show when={props.title}>
<div class={styles.articleHeader}>{props.title}</div>
</Show>
2023-05-22 22:01:04 +00:00
<ul class={clsx('view-switcher', styles.mainNavigation)} classList={{ fixed: fixed() }}>
2022-09-15 19:41:05 +00:00
<For each={resources}>
2022-09-22 09:37:49 +00:00
{(r) => (
2023-05-22 22:01:04 +00:00
<li classList={{ 'view-switcher__item--selected': r.route === page().route }}>
2023-02-10 01:19:20 +00:00
<a href={getPagePath(router, r.route)}>{r.name}</a>
2022-09-15 19:41:05 +00:00
</li>
)}
</For>
</ul>
</div>
<HeaderAuth setIsProfilePopupVisible={setIsProfilePopupVisible} />
<Show when={props.title}>
2023-05-24 21:51:47 +00:00
<div class={clsx(styles.articleControls, 'col-auto')}>
<SharePopup
title={props.title}
imageUrl={props.cover}
2023-01-31 13:58:28 +00:00
shareUrl={getShareUrl()}
description={getDescription(props.articleBody)}
onVisibilityChange={(isVisible) => {
setIsSharePopupVisible(isVisible)
}}
containerCssClass={styles.control}
2022-12-04 15:10:27 +00:00
trigger={<Icon name="share-outline" class={styles.icon} />}
/>
2023-04-20 14:01:15 +00:00
<div onClick={(event) => scrollToComments(event, true)} class={styles.control}>
<Icon name="comments-outline" class={styles.icon} />
2023-04-17 10:31:20 +00:00
</div>
2022-11-27 06:33:05 +00:00
<a href={getPagePath(router, 'create')} class={styles.control}>
2022-12-04 15:10:27 +00:00
<Icon name="pencil-outline" class={styles.icon} />
</a>
<a href="#" class={styles.control} onClick={(event) => event.preventDefault()}>
<Icon name="bookmark" class={styles.icon} />
</a>
2022-09-09 11:53:35 +00:00
</div>
</Show>
2023-03-10 17:42:48 +00:00
<div class={clsx(styles.burgerContainer, 'col-auto')}>
2022-09-29 10:08:22 +00:00
<div class={styles.burger} classList={{ fixed: fixed() }} onClick={toggleFixed}>
2022-09-09 11:53:35 +00:00
<div />
</div>
</div>
</nav>
2023-02-09 22:39:52 +00:00
<Snackbar />
2022-09-09 11:53:35 +00:00
</div>
</header>
)
}