MenuIterm selected but clicable mode added
This commit is contained in:
1 parent
74f785134f
commit
822f1a0535
3 files changed
+7
-26
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
import cx from 'classnames'
|
import cx from 'classnames'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { ActiveLink } from './ActiveLink'
|
import { ActiveLink } from './ActiveLink'
|
||||||
|
import { isPartOfPath } from 'src/lib/url'
|
||||||
|
|
||||||
export type MenuItemProps = {
|
export type MenuItemProps = {
|
||||||
href: string
|
href: string
|
||||||
@@ -8,14 +9,16 @@ export type MenuItemProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const MenuItem: React.FC<MenuItemProps> = props => {
|
export const MenuItem: React.FC<MenuItemProps> = props => {
|
||||||
|
const ignore = ['/']
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const current = router.asPath === props.href
|
const current = router.asPath === props.href
|
||||||
|
const selected = current || (!ignore.includes(props.href) && isPartOfPath(props.href, router.asPath))
|
||||||
const href = current ? null : props.href
|
const href = current ? null : props.href
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li
|
<li
|
||||||
className={cx('menuItem', props.layout, {
|
className={cx('menuItem', props.layout, {
|
||||||
selected: current,
|
selected,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<ActiveLink
|
<ActiveLink
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
function getPathWithNoTrailingSlash(path='') {
|
|
||||||
path = path.charAt(path.length - 1) === '/'
|
|
||||||
? path.substr(0, path.length - 1)
|
|
||||||
: path
|
|
||||||
return path.length === 0
|
|
||||||
? '/'
|
|
||||||
: path
|
|
||||||
}
|
|
||||||
|
|
||||||
function isEqualPath(a, b, ignoreTrailingSlash = false) {
|
|
||||||
return ignoreTrailingSlash
|
|
||||||
? getPathWithNoTrailingSlash(a) === getPathWithNoTrailingSlash(b)
|
|
||||||
: a === b
|
|
||||||
}
|
|
||||||
|
|
||||||
function isMatchPathPattern(pattern, path, ignoreTrailingSlash = false) {
|
|
||||||
const test = ignoreTrailingSlash
|
|
||||||
? getPathWithNoTrailingSlash(path)
|
|
||||||
: path
|
|
||||||
return pattern.test(test)
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getPathWithNoTrailingSlash = getPathWithNoTrailingSlash
|
|
||||||
exports.isMatchPathPattern = isMatchPathPattern
|
|
||||||
exports.isEqualPath = isEqualPath
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export function isPartOfPath(part: string, path: string): boolean {
|
||||||
|
return path.startsWith(part)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user