External Link
Link-role Button that validates HTTP/HTTPS URLs and opens them directly with React Native Linking.openURL.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add external-linkSource files
Expand a file to read the source the installer copies into your app.
components/ui/external-link/ExternalLink.tsx
import * as React from 'react'
import { Linking, type GestureResponderEvent } from 'react-native'
import { Button, type ButtonProps } from '@/components/ui/button'
export interface ExternalLinkProps extends ButtonProps {
/** External HTTP or HTTPS URL to hand off to the operating system. */
href: string
/**
* Called after `Linking.openURL` resolves.
* Indicates the OS accepted the open request, not that a browser loaded the page or returned successfully.
*/
onOpened?: (href: string) => void
/** Called when `href` is not a valid HTTP/HTTPS URL or `Linking.openURL` rejects. */
onError?: (error: Error) => void
}
// eslint-disable-next-line no-control-regex -- Reject control characters before handing a URL to the OS.
const INVALID_URL_CHARS = /[\u0000-\u001F\u007F\s]/
const HTTP_SCHEME_WITH_AUTHORITY = /^(https?):\/\/([^/?#]+)(.*)$/i
function parseHttpUrl(href: string): string | null {
const trimmed = href.trim()
if (!trimmed || INVALID_URL_CHARS.test(trimmed)) return null
const match = HTTP_SCHEME_WITH_AUTHORITY.exec(trimmed)
if (!match) return null
const normalized = `${match[1].toLowerCase()}://${match[2]}${match[3]}`
try {
const parsed = new URL(normalized)
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null
if (!parsed.hostname) return null
return normalized
} catch {
return null
}
}
/**
* Opens an external `http:` or `https:` URL directly via `Linking.openURL`
* without calling `canOpenURL`. For explicit external links only, not in-app routing.
*/
const ExternalLink = React.forwardRef<React.ComponentRef<typeof Button>, ExternalLinkProps>(
(
{
href,
onOpened,
onError,
onPress,
disabled,
loading = false,
variant = 'link',
accessibilityRole = 'link',
children = 'Open link',
...props
},
ref,
) => {
const [pending, setPending] = React.useState(false)
const mountedRef = React.useRef(true)
const pendingRef = React.useRef(false)
React.useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
async function handlePress(event: GestureResponderEvent) {
if (disabled || loading || pendingRef.current) return
onPress?.(event)
if (event?.defaultPrevented) return
const target = parseHttpUrl(href)
if (!target) {
onError?.(new Error('Invalid external link URL. Only http and https URLs are supported.'))
return
}
pendingRef.current = true
setPending(true)
let nativeError: Error | undefined
try {
await Linking.openURL(target)
} catch (error) {
nativeError = error instanceof Error ? error : new Error(String(error))
}
pendingRef.current = false
if (!mountedRef.current) return
setPending(false)
if (nativeError) {
onError?.(nativeError)
return
}
onOpened?.(target)
}
return (
<Button
ref={ref}
variant={variant}
disabled={disabled}
loading={loading || pending}
accessibilityRole={accessibilityRole}
onPress={handlePress}
{...props}
>
{children}
</Button>
)
},
)
ExternalLink.displayName = 'ExternalLink'
export { ExternalLink }
components/ui/external-link/index.ts
export { ExternalLink, type ExternalLinkProps } from './ExternalLink'