menu refactoring
This commit is contained in:
1 parent
8a41a895fb
commit
b07def3eee
2 files changed
+40
-17
No files matched your search
@@ -0,0 +1,31 @@
|
|||||||
|
import cx from 'classnames'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
import { ActiveLink } from './ActiveLink'
|
||||||
|
|
||||||
|
export type MenuItemProps = {
|
||||||
|
href: string
|
||||||
|
layout: 'desktop' | 'mobile'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MenuItem: React.FC<MenuItemProps> = props => {
|
||||||
|
const router = useRouter()
|
||||||
|
const current = router.asPath === props.href
|
||||||
|
const href = current ? null : props.href
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li
|
||||||
|
className={cx('menuItem', props.layout, {
|
||||||
|
selected: current,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<ActiveLink
|
||||||
|
href={href}
|
||||||
|
activeStyle={{
|
||||||
|
fontWeight: 'bold'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</ActiveLink>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,30 +1,22 @@
|
|||||||
import './styles.css'
|
import './styles.css'
|
||||||
|
|
||||||
import cx from 'classnames'
|
import cx from 'classnames'
|
||||||
import { ActiveLink } from './ActiveLink'
|
import { MenuItem } from './MenuItem'
|
||||||
|
|
||||||
export interface IMenuProps {
|
export type MenuProps = {
|
||||||
layout: 'desktop' | 'mobile'
|
layout: 'desktop' | 'mobile'
|
||||||
items: any[]
|
items: any[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Menu: React.FC<IMenuProps> = props => (
|
export const Menu: React.FC<MenuProps> = props => (
|
||||||
<div className={cx('menu', props.layout)}>
|
<menu className={cx('menu', props.layout)}>
|
||||||
{props.items.map((item, index) => (
|
{props.items.map((item, index) => (
|
||||||
<li key={index}
|
<MenuItem key={index}
|
||||||
className={cx('menuItem', props.layout, {
|
href={item.url}
|
||||||
selected: item.highlighted
|
layout={props.layout}
|
||||||
})}
|
|
||||||
>
|
|
||||||
<ActiveLink
|
|
||||||
href={!item.active ? item.url : null}
|
|
||||||
activeStyle={{
|
|
||||||
fontWeight: 'bold',
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{item.text}
|
{item.text}
|
||||||
</ActiveLink>
|
</MenuItem>
|
||||||
</li>
|
|
||||||
))}
|
))}
|
||||||
</div>
|
</menu>
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user