Feature/toggle mobile header (#281)

- toggle language
- toggle menu on current link
This commit is contained in:
Ilya Y 2023-10-25 18:04:23 +03:00 committed by GitHub
parent ea1de20466
commit b85c05a8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -12,7 +12,7 @@ import { Icon } from '../../_shared/Icon'
import type { Topic } from '../../../graphql/types.gen' import type { Topic } from '../../../graphql/types.gen'
import { useModalStore } from '../../../stores/ui' import { useModalStore } from '../../../stores/ui'
import { router, useRouter } from '../../../stores/router' import { router, ROUTES, useRouter } from '../../../stores/router'
import { getDescription } from '../../../utils/meta' import { getDescription } from '../../../utils/meta'
@ -38,11 +38,14 @@ type HeaderSearchParams = {
source?: string source?: string
} }
const handleSwitchLanguage = (event) => {
location.href = `${location.href}${location.href.includes('?') ? '&' : '?'}lng=${event.target.value}`
}
export const Header = (props: Props) => { export const Header = (props: Props) => {
const { t, lang } = useLocalize() const { t, lang } = useLocalize()
const { modal } = useModalStore() const { modal } = useModalStore()
const { page } = useRouter()
const { const {
actions: { requireAuthentication } actions: { requireAuthentication }
} = useSession() } = useSession()
@ -59,7 +62,6 @@ export const Header = (props: Props) => {
const [isTopicsVisible, setIsTopicsVisible] = createSignal(false) const [isTopicsVisible, setIsTopicsVisible] = createSignal(false)
const [isZineVisible, setIsZineVisible] = createSignal(false) const [isZineVisible, setIsZineVisible] = createSignal(false)
const [isFeedVisible, setIsFeedVisible] = createSignal(false) const [isFeedVisible, setIsFeedVisible] = createSignal(false)
const toggleFixed = () => setFixed((oldFixed) => !oldFixed) const toggleFixed = () => setFixed((oldFixed) => !oldFixed)
const tag = (topic: Topic) => const tag = (topic: Topic) =>
@ -148,6 +150,15 @@ export const Header = (props: Props) => {
setRandomTopics(topics) setRandomTopics(topics)
}) })
const handleToggleMenuByLink = (event: MouseEvent, route: keyof typeof ROUTES) => {
if (!fixed()) {
return
}
event.preventDefault()
if (page().route === route) {
toggleFixed()
}
}
return ( return (
<header <header
class={styles.mainHeader} class={styles.mainHeader}
@ -196,6 +207,7 @@ export const Header = (props: Props) => {
routeName="home" routeName="home"
active={isZineVisible()} active={isZineVisible()}
body={t('journal')} body={t('journal')}
onClick={(event) => handleToggleMenuByLink(event, 'home')}
/> />
<Link <Link
onMouseOver={() => toggleSubnavigation(true, setIsFeedVisible)} onMouseOver={() => toggleSubnavigation(true, setIsFeedVisible)}
@ -203,6 +215,7 @@ export const Header = (props: Props) => {
routeName="feed" routeName="feed"
active={isFeedVisible()} active={isFeedVisible()}
body={t('feed')} body={t('feed')}
onClick={(event) => handleToggleMenuByLink(event, 'feed')}
/> />
<Link <Link
onMouseOver={() => toggleSubnavigation(true, setIsTopicsVisible)} onMouseOver={() => toggleSubnavigation(true, setIsTopicsVisible)}
@ -210,12 +223,14 @@ export const Header = (props: Props) => {
routeName="topics" routeName="topics"
active={isTopicsVisible()} active={isTopicsVisible()}
body={t('topics')} body={t('topics')}
onClick={(event) => handleToggleMenuByLink(event, 'topics')}
/> />
<Link <Link
onMouseOver={(event) => hideSubnavigation(event, 0)} onMouseOver={(event) => hideSubnavigation(event, 0)}
onMouseOut={(event) => hideSubnavigation(event, 0)} onMouseOut={(event) => hideSubnavigation(event, 0)}
routeName="authors" routeName="authors"
body={t('authors')} body={t('authors')}
onClick={(event) => handleToggleMenuByLink(event, 'authors')}
/> />
<Link <Link
onMouseOver={() => toggleSubnavigation(true, setIsKnowledgeBaseVisible)} onMouseOver={() => toggleSubnavigation(true, setIsKnowledgeBaseVisible)}
@ -223,6 +238,7 @@ export const Header = (props: Props) => {
routeName="guide" routeName="guide"
body={t('Knowledge base')} body={t('Knowledge base')}
active={isKnowledgeBaseVisible()} active={isKnowledgeBaseVisible()}
onClick={(event) => handleToggleMenuByLink(event, 'guide')}
/> />
</ul> </ul>
@ -283,8 +299,12 @@ export const Header = (props: Props) => {
<h4>{t('Newsletter')}</h4> <h4>{t('Newsletter')}</h4>
<Subscribe variant={'mobileSubscription'} /> <Subscribe variant={'mobileSubscription'} />
<h4>{t('Newsletter')}</h4> <h4>{t('Language')}</h4>
<select class={styles.languageSelectorMobile}> <select
class={styles.languageSelectorMobile}
onChange={handleSwitchLanguage}
value={lang()}
>
<option value="ru">🇷🇺 Русский</option> <option value="ru">🇷🇺 Русский</option>
<option value="en">🇬🇧 English</option> <option value="en">🇬🇧 English</option>
</select> </select>

View File

@ -10,13 +10,17 @@ type Props = {
routeName?: keyof typeof ROUTES routeName?: keyof typeof ROUTES
body: string body: string
active?: boolean active?: boolean
onClick?: (event: MouseEvent) => void
} }
export const Link = (props: Props) => { export const Link = (props: Props) => {
const { page } = useRouter() const { page } = useRouter()
const isSelected = page().route === props.routeName const isSelected = page().route === props.routeName
return ( return (
<li classList={{ 'view-switcher__item--selected': page().route === props.routeName }}> <li
onClick={props.onClick}
classList={{ 'view-switcher__item--selected': page().route === props.routeName }}
>
<ConditionalWrapper <ConditionalWrapper
condition={!isSelected && Boolean(props.routeName)} condition={!isSelected && Boolean(props.routeName)}
wrapper={(children) => <a href={getPagePath(router, props.routeName)}>{children}</a>} wrapper={(children) => <a href={getPagePath(router, props.routeName)}>{children}</a>}