27 lines
846 B
TypeScript
27 lines
846 B
TypeScript
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);
|
||
}
|
||
}}
|
||
/>
|
||
);
|
||
}
|