Conditional render header links (#231)

This commit is contained in:
Ilya Y 2023-09-25 09:13:47 +03:00 committed by GitHub
parent 8a71cb41ea
commit f741ab9af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 44 deletions

View File

@ -98,7 +98,3 @@
max-width: 16px;
}
}
.cursorPointer {
cursor: pointer;
}

View File

@ -32,7 +32,7 @@ export const Userpic = (props: Props) => {
[styles.big]: props.isBig,
[styles.authorsList]: props.isAuthorsList,
[styles.feedMode]: props.isFeedMode,
[styles.cursorPointer]: props.onClick
['cursorPointer']: props.onClick
})}
onClick={props.onClick}
>

View File

@ -22,6 +22,7 @@ import { useSession } from '../../../context/session'
import styles from './Header.module.scss'
import { apiClient } from '../../../utils/apiClient'
import { RANDOM_TOPICS_COUNT } from '../../Views/Home'
import { Link } from './Link'
type Props = {
title?: string
@ -135,7 +136,7 @@ export const Header = (props: Props) => {
clearTimeout(timer)
}
const hideSubnavigation = (ev, time = 500) => {
const hideSubnavigation = (event, time = 500) => {
timer = setTimeout(() => {
toggleSubnavigation(false)
}, time)
@ -193,44 +194,32 @@ export const Header = (props: Props) => {
{t('zine')}
</a>
</li>
<li classList={{ 'view-switcher__item--selected': page().route.startsWith('feed') }}>
<a
classList={{ [styles.mainNavigationItemActive]: isFeedVisible() }}
onMouseOver={() => toggleSubnavigation(true, setIsFeedVisible)}
onMouseOut={hideSubnavigation}
href={getPagePath(router, 'feed')}
>
{t('feed')}
</a>
</li>
<li classList={{ 'view-switcher__item--selected': page().route === 'topics' }}>
<a
classList={{ [styles.mainNavigationItemActive]: isTopicsVisible() }}
onMouseOver={() => toggleSubnavigation(true, setIsTopicsVisible)}
onMouseOut={hideSubnavigation}
href={getPagePath(router, 'topics')}
>
{t('topics')}
</a>
</li>
<li classList={{ 'view-switcher__item--selected': page().route === 'authors' }}>
<a
onMouseOver={(ev) => hideSubnavigation(ev, 0)}
onMouseOut={(ev) => hideSubnavigation(ev, 0)}
href={getPagePath(router, 'authors')}
>
{t('community')}
</a>
</li>
<li>
<a
classList={{ [styles.mainNavigationItemActive]: isKnowledgeBaseVisible() }}
onMouseOver={() => toggleSubnavigation(true, setIsKnowledgeBaseVisible)}
onMouseOut={hideSubnavigation}
>
{t('Knowledge base')}
</a>
</li>
<Link
onMouseOver={() => toggleSubnavigation(true, setIsFeedVisible)}
onMouseOut={() => hideSubnavigation}
routeName="feed"
active={isFeedVisible()}
body={t('feed')}
/>
<Link
onMouseOver={() => toggleSubnavigation(true, setIsTopicsVisible)}
onMouseOut={() => hideSubnavigation}
routeName="topics"
active={isFeedVisible()}
body={t('topics')}
/>
<Link
onMouseOver={(event) => hideSubnavigation(event, 0)}
onMouseOut={(event) => hideSubnavigation(event, 0)}
routeName="authors"
body={t('authors')}
/>
<Link
onMouseOver={() => toggleSubnavigation(true, setIsKnowledgeBaseVisible)}
onMouseOut={() => hideSubnavigation}
body={t('Knowledge base')}
active={isKnowledgeBaseVisible()}
/>
</ul>
<div class={styles.mainNavigationMobile}>

View File

@ -0,0 +1,34 @@
import styles from './Header.module.scss'
import { router, ROUTES, useRouter } from '../../../stores/router'
import { clsx } from 'clsx'
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
import { getPagePath } from '@nanostores/router'
type Props = {
onMouseOver: (event?: MouseEvent, time?: number) => void
onMouseOut: (event?: MouseEvent, time?: number) => void
routeName?: keyof typeof ROUTES
body: string
active?: boolean
}
export const Link = (props: Props) => {
const { page } = useRouter()
const isSelected = page().route === props.routeName
return (
<li classList={{ 'view-switcher__item--selected': isSelected }}>
<ConditionalWrapper
condition={!isSelected && Boolean(props.routeName)}
wrapper={(children) => <a href={getPagePath(router, props.routeName)}>{children}</a>}
>
<span
class={clsx('cursorPointer', { [styles.mainNavigationItemActive]: props.active })}
onMouseOver={props.onMouseOver}
onMouseOut={props.onMouseOut}
>
{props.body}
</span>
</ConditionalWrapper>
</li>
)
}

View File

@ -987,3 +987,7 @@ iframe {
font-weight: bold;
line-height: 1.5;
}
.cursorPointer {
cursor: pointer;
}