Files
mindfulness/client/components/ExternalLink.tsx
2026-01-28 22:54:21 +08:00

27 lines
846 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Link } from 'expo-router';
import * as WebBrowser from 'expo-web-browser';
import React from 'react';
import { Platform } from 'react-native';
export function ExternalLink(
props: Omit<React.ComponentProps<typeof Link>, 'href'> & { href: string }
) {
return (
<Link
target="_blank"
{...props}
// expo-router 的 Link 类型会限制 href主要用于站内路由
// 这里明确把 href 视为外部链接(字符串),并由 onPress 接管打开逻辑
href={props.href as any}
onPress={(e) => {
if (Platform.OS !== 'web') {
// Prevent the default behavior of linking to the default browser on native.
e.preventDefault();
// Open the link in an in-app browser.
WebBrowser.openBrowserAsync(props.href);
}
}}
/>
);
}