-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
next-ui: add active item for sidebar
Signed-off-by: Login Victor <batazor@evrone.com>
- Loading branch information
Showing
3 changed files
with
147 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useRouter } from 'next/router' | ||
import ListItemIcon, { ListItemIconProps } from "@mui/material/ListItemIcon"; | ||
import React, { useState, useEffect, ReactElement, Children } from 'react' | ||
import { UrlObject } from 'url'; | ||
|
||
declare type Url = string | UrlObject; | ||
|
||
type ActiveLinkProps = ListItemIconProps & { | ||
href: Url; | ||
children: ReactElement | ||
activeClassName?: string | ||
} | ||
|
||
const ActiveLink = ({ | ||
children, | ||
activeClassName, | ||
...props | ||
}: ActiveLinkProps) => { | ||
const { asPath, isReady } = useRouter() | ||
|
||
const child = Children.only(children) | ||
const childClassName = child.props.className || '' | ||
const [className, setClassName] = useState(childClassName) | ||
|
||
useEffect(() => { | ||
// Check if the router fields are updated client-side | ||
if (isReady) { | ||
// Dynamic route will be matched via props.as | ||
// Static route will be matched via props.href | ||
const linkPathname = new URL( | ||
props.href as string, | ||
location.href | ||
).pathname | ||
|
||
// Using URL().pathname to get rid of query and hash | ||
const activePathname = new URL(asPath, location.href).pathname | ||
|
||
const newClassName = | ||
linkPathname === activePathname | ||
? `${childClassName} ${activeClassName}`.trim() | ||
: childClassName | ||
|
||
if (newClassName !== className) { | ||
setClassName(newClassName) | ||
} | ||
} | ||
}, [ | ||
asPath, | ||
isReady, | ||
props.href, | ||
childClassName, | ||
activeClassName, | ||
setClassName, | ||
className, | ||
]) | ||
|
||
return ( | ||
<ListItemIcon {...props}> | ||
{React.cloneElement(child, { | ||
className: className || null, | ||
})} | ||
</ListItemIcon> | ||
) | ||
} | ||
|
||
export default ActiveLink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useRouter } from 'next/router' | ||
import Link, { LinkProps } from 'next/link' | ||
import React, { useState, useEffect, ReactElement, Children } from 'react' | ||
|
||
type ActiveLinkProps = LinkProps & { | ||
children: ReactElement | ||
activeClassName?: string | ||
} | ||
|
||
const ActiveLink = ({ | ||
children, | ||
activeClassName, | ||
...props | ||
}: ActiveLinkProps) => { | ||
const { asPath, isReady } = useRouter() | ||
|
||
const child = Children.only(children) | ||
const childClassName = child.props.className || '' | ||
const [className, setClassName] = useState(childClassName) | ||
|
||
useEffect(() => { | ||
// Check if the router fields are updated client-side | ||
if (isReady) { | ||
// Dynamic route will be matched via props.as | ||
// Static route will be matched via props.href | ||
const linkPathname = new URL( | ||
(props.as || props.href) as string, | ||
location.href | ||
).pathname | ||
|
||
// Using URL().pathname to get rid of query and hash | ||
const activePathname = new URL(asPath, location.href).pathname | ||
|
||
const newClassName = | ||
linkPathname === activePathname | ||
? `${childClassName} ${activeClassName}`.trim() | ||
: childClassName | ||
|
||
if (newClassName !== className) { | ||
setClassName(newClassName) | ||
} | ||
} | ||
}, [ | ||
asPath, | ||
isReady, | ||
props.as, | ||
props.href, | ||
childClassName, | ||
activeClassName, | ||
setClassName, | ||
className, | ||
]) | ||
|
||
return ( | ||
<Link {...props}> | ||
{React.cloneElement(child, { | ||
className: className || null, | ||
})} | ||
</Link> | ||
) | ||
} | ||
|
||
export default ActiveLink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters