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; max-width: 16px;
} }
} }
.cursorPointer {
cursor: pointer;
}

View File

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

View File

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