2017-04-22 04:05:35 +10:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
|
|
|
import classNames from 'classnames';
|
2024-03-12 00:35:23 +11:00
|
|
|
import { useRouteMatch, NavLink } from 'react-router-dom';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
2023-05-09 11:11:56 +10:00
|
|
|
import { Icon } from 'mastodon/components/icon';
|
2016-12-13 00:27:52 +11:00
|
|
|
|
2024-03-12 00:35:23 +11:00
|
|
|
const ColumnLink = ({ icon, activeIcon, iconComponent, activeIconComponent, text, to, href, method, badge, transparent, ...other }) => {
|
|
|
|
const match = useRouteMatch(to);
|
2022-10-10 00:55:32 +11:00
|
|
|
const className = classNames('column-link', { 'column-link--transparent': transparent });
|
2018-01-21 23:20:20 +11:00
|
|
|
const badgeElement = typeof badge !== 'undefined' ? <span className='column-link__badge'>{badge}</span> : null;
|
2023-10-25 04:45:08 +11:00
|
|
|
const iconElement = (typeof icon === 'string' || iconComponent) ? <Icon id={icon} icon={iconComponent} className='column-link__icon' /> : icon;
|
2024-03-12 00:35:23 +11:00
|
|
|
const activeIconElement = activeIcon ?? (activeIconComponent ? <Icon id={icon} icon={activeIconComponent} className='column-link__icon' /> : iconElement);
|
|
|
|
const active = match?.isExact;
|
2018-01-21 23:20:20 +11:00
|
|
|
|
2016-12-13 00:27:52 +11:00
|
|
|
if (href) {
|
|
|
|
return (
|
2022-10-10 00:55:32 +11:00
|
|
|
<a href={href} className={className} data-method={method} title={text} {...other}>
|
2024-03-12 00:35:23 +11:00
|
|
|
{active ? activeIconElement : iconElement}
|
2022-10-10 15:51:16 +11:00
|
|
|
<span>{text}</span>
|
2018-01-21 23:20:20 +11:00
|
|
|
{badgeElement}
|
2016-12-13 00:27:52 +11:00
|
|
|
</a>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2024-03-12 00:35:23 +11:00
|
|
|
<NavLink to={to} className={className} title={text} exact {...other}>
|
|
|
|
{active ? activeIconElement : iconElement}
|
2022-10-10 15:51:16 +11:00
|
|
|
<span>{text}</span>
|
2018-01-21 23:20:20 +11:00
|
|
|
{badgeElement}
|
2022-10-10 00:55:32 +11:00
|
|
|
</NavLink>
|
2016-12-13 00:27:52 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ColumnLink.propTypes = {
|
2022-10-10 00:55:32 +11:00
|
|
|
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
|
2023-10-25 04:45:08 +11:00
|
|
|
iconComponent: PropTypes.func,
|
2024-03-12 00:35:23 +11:00
|
|
|
activeIcon: PropTypes.node,
|
|
|
|
activeIconComponent: PropTypes.func,
|
2017-04-22 04:05:35 +10:00
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
to: PropTypes.string,
|
|
|
|
href: PropTypes.string,
|
|
|
|
method: PropTypes.string,
|
2018-01-21 23:20:20 +11:00
|
|
|
badge: PropTypes.node,
|
2022-10-10 00:55:32 +11:00
|
|
|
transparent: PropTypes.bool,
|
2016-12-13 00:27:52 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ColumnLink;
|