mirror of https://github.com/msgbyte/tailchat
refactor: nav and sidebar active status
parent
f466ba8a44
commit
a75ea92087
@ -1,21 +1,38 @@
|
|||||||
import type { ClassValue } from 'clsx';
|
import type { ClassValue } from 'clsx';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { useLocation } from 'react-router';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
export const NavbarNavItem: React.FC<{
|
export const NavbarNavItem: React.FC<{
|
||||||
className?: ClassValue;
|
className?: ClassValue;
|
||||||
|
to?: string;
|
||||||
onClick?: () => void;
|
onClick?: () => void;
|
||||||
}> = React.memo((props) => {
|
}> = React.memo((props) => {
|
||||||
return (
|
const { className, to } = props;
|
||||||
|
const location = useLocation();
|
||||||
|
const isActive = typeof to === 'string' && location.pathname.startsWith(to);
|
||||||
|
|
||||||
|
const inner = (
|
||||||
<div
|
<div
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'w-12 h-12 hover:rounded-lg transition-all rounded-1/2 cursor-pointer flex items-center justify-center overflow-hidden',
|
'w-12 h-12 hover:rounded-lg transition-all cursor-pointer flex items-center justify-center overflow-hidden',
|
||||||
props.className
|
className,
|
||||||
|
{
|
||||||
|
'rounded-1/2': !isActive,
|
||||||
|
'rounded-lg': isActive,
|
||||||
|
}
|
||||||
)}
|
)}
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
>
|
>
|
||||||
{props.children}
|
{props.children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (typeof to === 'string') {
|
||||||
|
return <Link to={to}>{inner}</Link>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner;
|
||||||
});
|
});
|
||||||
NavbarNavItem.displayName = 'NavbarNavItem';
|
NavbarNavItem.displayName = 'NavbarNavItem';
|
||||||
|
|||||||
Loading…
Reference in New Issue