Files
rabbit/src/components/utils/SafeLink.tsx
Shusui MOYATANI 3348bba012 update
2023-03-22 12:45:32 +09:00

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;