2022-11-13 19:35:57 +00:00
|
|
|
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'
|
2022-11-13 19:35:57 +00:00
|
|
|
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'
|
2022-11-13 19:35:57 +00:00
|
|
|
import { HeaderAuth } from './HeaderAuth'
|
2023-01-31 13:58:28 +00:00
|
|
|
import { getShareUrl, SharePopup } from '../Article/SharePopup'
|
2023-01-30 10:39:36 +00:00
|
|
|
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
|
2023-01-30 10:39:36 +00:00
|
|
|
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)
|
2022-10-28 09:10:14 +00:00
|
|
|
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-11-13 19:35:57 +00:00
|
|
|
|
2022-10-25 15:36:32 +00:00
|
|
|
const toggleFixed = () => setFixed((oldFixed) => !oldFixed)
|
2022-09-09 11:53:35 +00:00
|
|
|
// effects
|
2022-11-04 13:05:50 +00:00
|
|
|
|
|
|
|
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
|
2022-11-04 13:05:50 +00:00
|
|
|
|
|
|
|
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())
|
2022-11-04 13:05:50 +00:00
|
|
|
|
|
|
|
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(),
|
2022-10-28 09:10:14 +00:00
|
|
|
[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
|
|
|
}}
|
|
|
|
>
|
2022-11-27 05:49:48 +00:00
|
|
|
<Modal variant="wide" name="auth">
|
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')}>
|
2022-09-28 21:08:14 +00:00
|
|
|
<nav class={clsx(styles.headerInner, 'row')} classList={{ fixed: fixed() }}>
|
2023-03-10 17:42:48 +00:00
|
|
|
<div class={clsx(styles.mainLogo, 'col-auto col-md-4')}>
|
2022-11-15 16:16:31 +00:00
|
|
|
<a href={getPagePath(router, 'home')}>
|
2022-09-09 11:53:35 +00:00
|
|
|
<img src="/logo.svg" alt={t('Discours')} />
|
|
|
|
</a>
|
|
|
|
</div>
|
2023-03-10 17:42:48 +00:00
|
|
|
<div class={clsx(styles.mainNavigationWrapper, 'col-auto offset-md-1')}>
|
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>
|
2022-09-28 21:08:14 +00:00
|
|
|
<li class={styles.headerSearch}>
|
2022-09-18 15:31:48 +00:00
|
|
|
<a href="#">
|
2023-02-17 09:21:02 +00:00
|
|
|
<Icon name="search" class={styles.icon} />
|
2022-09-18 15:31:48 +00:00
|
|
|
{t('Search')}
|
|
|
|
</a>
|
|
|
|
</li>
|
2022-09-15 19:41:05 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
2022-11-13 19:35:57 +00:00
|
|
|
<HeaderAuth setIsProfilePopupVisible={setIsProfilePopupVisible} />
|
|
|
|
<Show when={props.title}>
|
|
|
|
<div class={styles.articleControls}>
|
|
|
|
<SharePopup
|
2023-01-30 10:39:36 +00:00
|
|
|
title={props.title}
|
|
|
|
imageUrl={props.cover}
|
2023-01-31 13:58:28 +00:00
|
|
|
shareUrl={getShareUrl()}
|
2023-01-30 10:39:36 +00:00
|
|
|
description={getDescription(props.articleBody)}
|
2022-11-13 19:35:57 +00:00
|
|
|
onVisibilityChange={(isVisible) => {
|
|
|
|
setIsSharePopupVisible(isVisible)
|
|
|
|
}}
|
|
|
|
containerCssClass={styles.control}
|
2022-12-04 15:10:27 +00:00
|
|
|
trigger={<Icon name="share-outline" class={styles.icon} />}
|
2022-11-13 19:35:57 +00:00
|
|
|
/>
|
2023-04-20 14:01:15 +00:00
|
|
|
<div onClick={(event) => scrollToComments(event, true)} class={styles.control}>
|
2022-11-13 19:35:57 +00:00
|
|
|
<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} />
|
2022-11-13 19:35:57 +00:00
|
|
|
</a>
|
2022-09-09 11:53:35 +00:00
|
|
|
</div>
|
2022-11-13 19:35:57 +00:00
|
|
|
</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>
|
|
|
|
)
|
|
|
|
}
|