1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/elements/emby-button/LinkButton.tsx

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-12-16 16:02:11 -05:00
import React, { AnchorHTMLAttributes, DetailedHTMLProps, MouseEvent, useCallback } from 'react';
import classNames from 'classnames';
import layoutManager from '../../components/layoutManager';
import shell from '../../scripts/shell';
2023-05-01 10:04:13 -04:00
import { appRouter } from '../../components/router/appRouter';
import { appHost } from '../../components/apphost';
import './emby-button.scss';
interface LinkButtonProps extends DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
2024-08-21 02:54:09 -04:00
> {
className?: string;
isAutoHideEnabled?: boolean;
href?: string;
target?: string;
}
const LinkButton: React.FC<LinkButtonProps> = ({
className,
isAutoHideEnabled,
href,
target,
2024-02-24 11:32:08 -08:00
onClick,
children,
...rest
}) => {
2022-12-16 16:02:11 -05:00
const onAnchorClick = useCallback((e: MouseEvent<HTMLAnchorElement>) => {
const url = href || '';
if (url !== '#') {
if (target) {
if (!appHost.supports('targetblank')) {
e.preventDefault();
shell.openUrl(url);
}
} else {
e.preventDefault();
2023-05-02 11:24:53 -04:00
appRouter.show(url)
.catch(err => {
console.error('[LinkButton] failed to show url', url, err);
});
}
} else {
e.preventDefault();
}
2024-02-24 11:32:08 -08:00
onClick?.(e);
}, [ href, target, onClick ]);
if (isAutoHideEnabled === true && !appHost.supports('externallinks')) {
return null;
}
2022-12-15 22:54:58 +03:00
const cssClass = classNames(
'emby-button',
className,
{ 'show-focus': layoutManager.tv }
);
return (
<a
className={cssClass}
href={href}
target={target}
onClick={onAnchorClick}
{...rest}
>
{children}
</a>
);
};
export default LinkButton;