-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemed-link.tsx
More file actions
75 lines (73 loc) · 2 KB
/
themed-link.tsx
File metadata and controls
75 lines (73 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Link, useRouter } from 'expo-router';
import * as Linking from 'expo-linking';
import type { Href } from 'expo-router';
import { GestureResponderEvent, Platform, PressableProps } from 'react-native';
import { ThemedButton } from './themed-button';
import { ThemedTextType } from './themed-text';
import { ThemedColorNames } from '@/hooks/use-theme-colors';
const openBrowserAsync =
Platform.isTV && Platform.OS === 'ios'
? null
: // eslint-disable-next-line @typescript-eslint/no-require-imports
require('expo-web-browser').openBrowserAsync;
/**
* Themed button component to open a URL that can be either a web URL or a route in the app.
*/
export function ThemedLink({
href,
children,
className,
textType,
...props
}: PressableProps & {
href: string;
children: string;
className?: string;
textType?: ThemedTextType | undefined;
}) {
const router = useRouter();
if (
Platform.OS === 'web' &&
typeof href === 'string' &&
href.startsWith('http')
) {
return (
<Link target="_blank" href={href as Href} asChild>
<ThemedButton
className={className}
textType={textType ?? ThemedTextType.small}
textColor={ThemedColorNames.red}
{...props}
>
{children}
</ThemedButton>
</Link>
);
}
return (
<ThemedButton
className={className}
textType={textType ?? ThemedTextType.small}
textColor={ThemedColorNames.red}
onPress={async (event: GestureResponderEvent) => {
if (typeof href === 'string' && !href.startsWith('http')) {
router.navigate(href as Href);
return;
}
if (openBrowserAsync) {
event.preventDefault();
await openBrowserAsync(href);
return;
}
try {
await Linking.openURL(href).catch((reason) => alert(`${reason}`));
} catch (reason) {
alert(`${reason}`);
}
}}
{...props}
>
{children}
</ThemedButton>
);
}