webapp/src/components/Nav/Header/Link.tsx

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-09-25 06:13:47 +00:00
import { clsx } from 'clsx'
2024-06-24 17:50:27 +00:00
import { ROUTES } from '../../../config/routes'
2023-09-25 06:13:47 +00:00
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
2024-06-24 17:50:27 +00:00
import { A, useMatch } from '@solidjs/router'
import { createMemo } from 'solid-js'
import styles from './Header.module.scss'
2023-09-25 06:13:47 +00:00
type Props = {
onMouseOver: (event?: MouseEvent, time?: number) => void
onMouseOut: (event?: MouseEvent, time?: number) => void
routeName?: keyof typeof ROUTES
body: string
active?: boolean
onClick?: (event: MouseEvent) => void
2023-09-25 06:13:47 +00:00
}
export const Link = (props: Props) => {
2024-06-24 17:50:27 +00:00
const matchRoute = useMatch(() => props.routeName || '')
const isSelected = createMemo(() => Boolean(matchRoute()))
2023-09-25 06:13:47 +00:00
return (
2024-06-24 17:50:27 +00:00
<li onClick={props.onClick} classList={{ 'view-switcher__item--selected': isSelected() }}>
2023-09-25 06:13:47 +00:00
<ConditionalWrapper
condition={!isSelected && Boolean(props.routeName)}
2024-06-24 17:50:27 +00:00
wrapper={(children) => <A href={props.routeName || ''}>{children}</A>}
2023-09-25 06:13:47 +00:00
>
<span
2023-09-29 06:46:15 +00:00
class={clsx('cursorPointer linkReplacement', { [styles.mainNavigationItemActive]: props.active })}
2023-09-25 06:13:47 +00:00
onMouseOver={props.onMouseOver}
onMouseOut={props.onMouseOut}
>
{props.body}
</span>
</ConditionalWrapper>
</li>
)
}