2023-05-16 19:07:08 +00:00
|
|
|
import { actions, useAppBridge } from "@saleor/app-sdk/app-bridge";
|
|
|
|
import { Text, TextProps } from "@saleor/macaw-ui/next";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
export interface TextLinkProps extends TextProps {
|
|
|
|
href: string;
|
|
|
|
newTab?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BaseTextLink = (props: TextLinkProps) => {
|
|
|
|
return (
|
2023-05-17 16:53:49 +00:00
|
|
|
<Text target="_blank" as={"a"} textDecoration={"none"} rel="noopener noreferrer" {...props}>
|
2023-05-16 19:07:08 +00:00
|
|
|
<Text
|
2023-05-17 16:53:49 +00:00
|
|
|
transition={"ease"}
|
|
|
|
variant={"bodyStrong"}
|
2023-07-18 08:19:21 +00:00
|
|
|
size={props.size}
|
2023-05-17 16:53:49 +00:00
|
|
|
color={{
|
|
|
|
default: "text3Decorative",
|
|
|
|
hover: "text1Decorative",
|
|
|
|
}}
|
2023-05-16 19:07:08 +00:00
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</Text>
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TextLink = ({ href, newTab = false, children, ...props }: TextLinkProps) => {
|
|
|
|
const { appBridge } = useAppBridge();
|
|
|
|
const { push } = useRouter();
|
|
|
|
|
|
|
|
const onNewTabClick = (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (!appBridge) {
|
|
|
|
console.warn(
|
|
|
|
"App bridge is not initialized, TextLink cannot be used with external links without it."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
appBridge?.dispatch(
|
|
|
|
actions.Redirect({
|
|
|
|
to: href,
|
|
|
|
newContext: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (props.onClick) {
|
|
|
|
props.onClick(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onInternalClick = (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
push(href);
|
|
|
|
|
|
|
|
if (props.onClick) {
|
|
|
|
props.onClick(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (newTab) {
|
|
|
|
return (
|
|
|
|
<BaseTextLink href={href} onClick={onNewTabClick} {...props}>
|
|
|
|
{children}
|
|
|
|
</BaseTextLink>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<BaseTextLink href={href} onClick={onInternalClick} {...props}>
|
|
|
|
{children}
|
|
|
|
</BaseTextLink>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|