Skip to content

Commit

Permalink
support mailto links (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored Nov 24, 2023
1 parent df3c7d0 commit 66bbb04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/expo-router/src/global-state/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Linking from "expo-linking";
import { ResultState } from "../fork/getStateFromPath";
import { Href, resolveHref } from "../link/href";
import { resolve } from "../link/path";
import { hasUrlProtocolPrefix } from "../utils/url";
import { shouldLinkExternally } from "../utils/url";
import type { RouterStore } from "./router-store";

function assertIsReady(store: RouterStore) {
Expand Down Expand Up @@ -52,7 +52,7 @@ export function setParams(
}

export function linkTo(this: RouterStore, href: string, event?: string) {
if (hasUrlProtocolPrefix(href)) {
if (shouldLinkExternally(href)) {
Linking.openURL(href);
return;
}
Expand Down
16 changes: 16 additions & 0 deletions packages/expo-router/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@
export function hasUrlProtocolPrefix(href: string): boolean {
return /^[\w\d_+.-]+:\/\//.test(href);
}

export function isWellKnownUri(href: string): boolean {
// This is a hack and we should change this to work like the web in the future where we have full confidence in the
// ability to match URLs and send anything unmatched to the OS. The main difference between this and `hasUrlProtocolPrefix` is
// that we don't require `//`, e.g. `mailto:` is valid and common, and `mailto://bacon` is invalid.
return /^(https?|mailto|tel|sms|geo|maps|market|itmss?|itms-apps|content|file):/.test(
href
);
}

export function shouldLinkExternally(href: string): boolean {
// Cheap check first to avoid regex if the href is not a path fragment.
return (
!/^[./]/.test(href) && (hasUrlProtocolPrefix(href) || isWellKnownUri(href))
);
}

0 comments on commit 66bbb04

Please sign in to comment.