mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 06:24:25 +01:00
29 lines
638 B
TypeScript
29 lines
638 B
TypeScript
import { Component, Show, JSX } from 'solid-js';
|
|
|
|
type SafeLinkProps = {
|
|
class?: string;
|
|
href: string;
|
|
children?: JSX.Element;
|
|
};
|
|
|
|
const SafeLink: Component<SafeLinkProps> = (props) => {
|
|
const isSafe = () => {
|
|
try {
|
|
const url = new URL(props.href.toString());
|
|
return url.protocol === 'https:' || url.protocol === 'http:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Show when={isSafe()} fallback={props.href}>
|
|
<a class={props.class} href={props.href} target="_blank" rel="noreferrer noopener">
|
|
{props.children ?? props.href}
|
|
</a>
|
|
</Show>
|
|
);
|
|
};
|
|
|
|
export default SafeLink;
|