movie-web/src/components/text/Link.tsx

43 lines
1.1 KiB
TypeScript
Raw Normal View History

import { ReactNode } from "react";
2022-02-18 20:22:56 +01:00
import { Link as LinkRouter } from "react-router-dom";
interface ILinkPropsBase {
children?: ReactNode;
2022-02-18 20:22:56 +01:00
className?: string;
onClick?: () => void;
}
interface ILinkPropsExternal extends ILinkPropsBase {
url: string;
newTab?: boolean;
2022-02-18 20:22:56 +01:00
}
interface ILinkPropsInternal extends ILinkPropsBase {
to: string;
}
type LinkProps =
2022-02-18 20:22:56 +01:00
| ILinkPropsExternal
| ILinkPropsInternal
| ILinkPropsBase;
export function Link(props: LinkProps) {
const isExternal = !!(props as ILinkPropsExternal).url;
const isInternal = !!(props as ILinkPropsInternal).to;
const content = (
<span className="text-bink-600 hover:text-bink-700 cursor-pointer font-bold">
{props.children}
2022-02-18 20:22:56 +01:00
</span>
);
if (isExternal)
2022-02-25 22:03:12 +01:00
return <a target={(props as ILinkPropsExternal).newTab ? "_blank" : undefined} rel="noreferrer" href={(props as ILinkPropsExternal).url}>{content}</a>;
2022-03-06 13:43:32 +01:00
if (isInternal)
2022-02-18 20:22:56 +01:00
return (
<LinkRouter to={(props as ILinkPropsInternal).to}>{content}</LinkRouter>
);
return (
<span onClick={() => props.onClick && props.onClick()}>{content}</span>
);
}