Skip to content
TypeScript

fix(mobile): Browser menu pastes the phone's clipboard into the page (#19648)

spiky02plateau6cf704beb5c9
diff --git a/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx b/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
index 4db00762add..e0660e11ed4 100644
--- a/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
+++ b/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
@@ -177,6 +177,7 @@ function BrowserPreviewScreen({
streaming={streaming}
ready={streaming && canControl}
onCommand={(input) => streamRef.current?.command(input)}
+ onPaste={() => streamRef.current?.paste()}
/>
{live ? (
<PreviewStreamWebView
@@ -213,12 +214,14 @@ function BrowserAddressBar({
ready,
streaming,
onCommand,
+ onPaste,
}: {
readonly environmentId: EnvironmentId;
readonly tab: PreviewSessionSnapshot;
readonly ready: boolean;
readonly streaming: boolean;
readonly onCommand: PreviewStreamRef["command"];
+ readonly onPaste: () => void;
}) {
const url = browserTabUrl(tab);
const [draft, setDraft] = useState<string | null>(null);
@@ -268,7 +271,12 @@ function BrowserAddressBar({
disabled={!ready}
onPress={() => onCommand({ type: "reload" })}
/>
- <BrowserTabMenu environmentId={environmentId} tab={tab} disabled={!streaming} />
+ <BrowserTabMenu
+ environmentId={environmentId}
+ tab={tab}
+ disabled={!streaming}
+ onPaste={ready ? onPaste : undefined}
+ />
</View>
);
}
diff --git a/apps/mobile/src/features/browser/BrowserTabMenu.tsx b/apps/mobile/src/features/browser/BrowserTabMenu.tsx
index 322c0768633..0ade61a7a1b 100644
--- a/apps/mobile/src/features/browser/BrowserTabMenu.tsx
+++ b/apps/mobile/src/features/browser/BrowserTabMenu.tsx
@@ -41,10 +41,12 @@ export function BrowserTabMenu({
environmentId,
tab,
disabled,
+ onPaste,
}: {
readonly environmentId: EnvironmentId;
readonly tab: PreviewSessionSnapshot;
readonly disabled: boolean;
+ readonly onPaste?: () => void;
}) {
const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change");
const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change");
@@ -64,6 +66,7 @@ export function BrowserTabMenu({
: "custom";
const actions: MenuAction[] = [
+ ...(onPaste ? [{ id: "paste", title: "Paste", image: "doc.on.clipboard" }] : []),
{ id: "hard-reload", title: "Hard reload", image: "arrow.clockwise.circle" },
{
id: "appearance",
@@ -112,6 +115,7 @@ export function BrowserTabMenu({
};
const onAction = (id: string) => {
+ if (id === "paste") return onPaste?.();
if (id === "hard-reload") return void run({ hardReload: true });
if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) });
if (id === "zoom:out") return void run({ zoomFactor: zoomTo(-1) });
diff --git a/apps/mobile/src/features/browser/PreviewStreamWebView.tsx b/apps/mobile/src/features/browser/PreviewStreamWebView.tsx
index 5737e45067e..1f436162594 100644
--- a/apps/mobile/src/features/browser/PreviewStreamWebView.tsx
+++ b/apps/mobile/src/features/browser/PreviewStreamWebView.tsx
@@ -39,6 +39,7 @@ import {
export interface PreviewStreamRef {
readonly command: (input: PreviewStreamInput) => void;
+ readonly paste: () => void;
readonly togglePictureInPicture: () => void;
}
@@ -271,6 +272,15 @@ function PreviewStreamDocumentView({
webView.current?.injectJavaScript(
`window.T3PreviewStream?.command(${JSON.stringify(input)}); true;`,
);
+ // Reads here rather than in the screen: a tab switch during the iOS paste prompt unmounts
+ // this view and nulls its WebView ref, so the text can never type into another tab.
+ const paste = () =>
+ void Clipboard.getStringAsync().then(
+ (text) => {
+ if (text) command({ type: "text", text });
+ },
+ () => Dialog.alert("Could not read the clipboard"),
+ );
const fail = (message: string) => {
if (!active.current || failed.current) return;
failed.current = true;
@@ -299,6 +309,7 @@ function PreviewStreamDocumentView({
);
useImperativeHandle(ref, () => ({
command,
+ paste,
togglePictureInPicture: () =>
webView.current?.injectJavaScript("window.T3PreviewStream?.pictureInPicture(); true;"),
}));