pleroma/packages/pl-fe/src/components/sidebar-navigation-link.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-02-06 10:01:03 -08:00
import clsx from 'clsx';
2022-03-21 11:09:01 -07:00
import React from 'react';
import { NavLink } from 'react-router-dom';
import { useSettings } from 'soapbox/hooks';
import { Icon, Text } from './ui';
2022-03-21 11:09:01 -07:00
interface ISidebarNavigationLink {
/** Notification count, if any. */
count?: number;
2022-11-03 09:13:45 -07:00
/** Optional max to cap count (ie: N+) */
countMax?: number;
/** URL to an SVG icon. */
icon: string;
/** URL to an SVG icon for active state. */
activeIcon?: string;
/** Link label. */
text: React.ReactNode;
/** Route to an internal page. */
to?: string;
/** Callback when the link is clicked. */
onClick?: React.EventHandler<React.MouseEvent>;
2022-03-21 11:09:01 -07:00
}
/** Desktop sidebar navigation link. */
const SidebarNavigationLink = React.forwardRef((props: ISidebarNavigationLink, ref: React.ForwardedRef<HTMLAnchorElement>): JSX.Element => {
const { icon, activeIcon, text, to = '', count, countMax, onClick } = props;
2022-03-21 11:09:01 -07:00
const isActive = location.pathname === to;
const { demetricator } = useSettings();
const handleClick: React.EventHandler<React.MouseEvent> = (e) => {
if (onClick) {
onClick(e);
e.preventDefault();
e.stopPropagation();
}
};
2022-03-21 11:09:01 -07:00
return (
<NavLink
exact
to={to}
ref={ref}
onClick={handleClick}
2023-02-06 10:01:03 -08:00
className={clsx({
'flex items-center py-2 text-sm font-semibold space-x-4 rtl:space-x-reverse transition-colors duration-200': true,
'text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200': !isActive,
'text-gray-900 dark:text-white': isActive,
2022-03-21 11:09:01 -07:00
})}
>
<span
className={clsx({
'relative rounded-lg inline-flex p-2.5 transition-colors duration-200': true,
'bg-primary-50 hover:bg-primary-100 dark:bg-slate-700 dark:hover:bg-slate-600 black:bg-gray-900 black:hover:bg-gray-800': !isActive,
'bg-primary-600': isActive,
})}
>
<Icon
src={(isActive && activeIcon) || icon}
count={demetricator ? undefined : count}
countMax={countMax}
2023-02-06 10:01:03 -08:00
className={clsx('h-5 w-5', {
'text-primary-700 dark:text-white': !isActive,
'text-white': isActive,
})}
/>
2022-03-21 11:09:01 -07:00
</span>
<Text weight='semibold' theme='inherit'>{text}</Text>
2022-03-21 11:09:01 -07:00
</NavLink>
);
});
2022-03-21 11:09:01 -07:00
export { SidebarNavigationLink as default };