Skip to content

Commit b3310c2

Browse files
loganjLarry
andcommitted
fix(desktop): keep portaled popover clicks from stealing To-field focus
Recipient-inspection popovers portal their content to the document body but still bubble click events through React's tree to the new-DM To-field div, whose onClick focused the search input. Focusing that input dismisses the popover via focus-outside, so ordinary clicks on the nested Copy npub button detached the popover mid-click and never copied. The field's onClick now only acts on clicks whose event target is a DOM descendant of the field itself; portaled popover clicks keep their own focus. Clicks physically within the field — including the To label — still focus the input and open the recipient picker. The clipboard regressions this unblocks pin both PubKey variants through the mock bridge to the real browser clipboard: the new-DM recipient verification flow (full variant: pointer copy, inspection survives the copy, inner/outer Escape with the recipient retained, and a natural keyboard Space-then-Enter path) and the agent-access owner hint (compact variant), each expecting the exact canonical npub of the identity shown — never the raw hex the popover also lists, and never a truncation. The static PubKey suite header now names those E2E owners of the clipboard interaction. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent 5c20712 commit b3310c2

4 files changed

Lines changed: 111 additions & 4 deletions

File tree

‎desktop/src/features/messages/ui/NewMessageScreen.tsx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,22 @@ export function NewMessageScreen() {
328328
<div
329329
className="group/to-field flex min-h-9 min-w-0 flex-1 cursor-text flex-wrap items-center gap-1.5 py-1"
330330
data-testid="new-message-to-field"
331-
onClick={() => {
331+
onClick={(event) => {
332+
// Portaled popovers (recipient inspection and its nested key
333+
// copy) still bubble through React's tree to this handler,
334+
// but their event targets are not DOM descendants of the
335+
// field. Those clicks belong to the popover's own controls
336+
// — they must not steal focus into the search input (which
337+
// dismisses the popover via focus-outside) or reopen the
338+
// picker. Only clicks physically within the recipient field
339+
// focus its input.
340+
const { currentTarget, target } = event;
341+
if (
342+
!(target instanceof Node) ||
343+
!currentTarget.contains(target)
344+
) {
345+
return;
346+
}
332347
setIsRecipientPickerOpen(true);
333348
searchInputRef.current?.focus({ preventScroll: true });
334349
}}

‎desktop/src/shared/ui/PubKey.test.mjs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
* rendered text per variant, and that an unencodable identity — including
77
* degenerate-length hex and short-payload npubs whose npubEncode outputs
88
* carry valid checksums — renders the neutral label with no copy affordance,
9-
* never a fake npub. Static rendering does not exercise the clipboard
10-
* write behind the copy affordance or the popover the widget can open;
11-
* those interactions are outside this suite's scope.
9+
* never a fake npub. The clipboard write behind the copy affordance and the
10+
* popover the widget opens are real-bridge interactions owned by the E2E
11+
* regressions: the full variant's copy is pinned by the new-DM recipient
12+
* verification flow (tests/e2e/pubkey-display-screenshots.spec.ts) and the
13+
* compact variant's by the agent-access owner hint
14+
* (tests/e2e/agent-access-warning.spec.ts); both drive CopyRow through the
15+
* mock bridge into the actual browser clipboard.
1216
*/
1317
import assert from "node:assert/strict";
1418
import { after, afterEach, before, test } from "node:test";

‎desktop/tests/e2e/agent-access-warning.spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, test } from "@playwright/test";
2+
import { npubEncode } from "nostr-tools/nip19";
23

34
import { waitForAnimations } from "../helpers/animations";
45
import { installMockBridge, TEST_IDENTITIES } from "../helpers/bridge";
@@ -123,6 +124,31 @@ test("open agent access explains the available access before save", async ({
123124
.getByRole("dialog", { name: "Manage agent access" })
124125
.screenshot({ path: `${SHOTS}/selected-people-warning.png` });
125126

127+
// Compact-variant clipboard regression (D1a): the owner hint's compact
128+
// PubKey must expand to and copy the viewer's complete canonical npub —
129+
// the truncated trigger is only a recognition aid. The real bridge writes
130+
// the browser clipboard and the poll reads it back.
131+
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
132+
await page
133+
.getByTestId("agent-respond-to")
134+
.getByRole("button", { name: "Show full public key" })
135+
.click();
136+
const copyNpubButton = page.getByRole("button", { name: "Copy npub" });
137+
await copyNpubButton.click();
138+
await expect
139+
.poll(() => page.evaluate(() => navigator.clipboard.readText()))
140+
.toBe(npubEncode("deadbeef".repeat(8)));
141+
await expect(
142+
page.locator("[data-sonner-toast]").filter({ hasText: "npub copied" }),
143+
).toBeVisible();
144+
// Dismiss just the key popover: the access dialog stays open for the
145+
// remaining mode assertions below.
146+
await page.keyboard.press("Escape");
147+
await expect(copyNpubButton).toHaveCount(0);
148+
await expect(
149+
page.getByRole("dialog", { name: "Manage agent access" }),
150+
).toBeVisible();
151+
126152
// Only me shares nothing, so the warning goes away entirely.
127153
await accessSelect.selectOption("owner-only");
128154
await expect(warning).toHaveCount(0);

‎desktop/tests/e2e/pubkey-display-screenshots.spec.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, test } from "@playwright/test";
2+
import { npubEncode } from "nostr-tools/nip19";
23

34
import {
45
installMockBridge,
@@ -198,8 +199,59 @@ test("selected new-DM recipient can be verified again through search", async ({
198199
await page.getByTestId("new-message-page").screenshot({
199200
path: `${SHOTS}/new-dm-selected-recipient-key.png`,
200201
});
202+
203+
// Full-variant clipboard regression (D1a): the nested copy affordances
204+
// must write the recipient's complete canonical npub through the real
205+
// bridge — never the legacy raw hex the popover also shows, and never a
206+
// truncation. The evidence screenshot above is captured first, so this
207+
// interaction leaves it untouched.
208+
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
209+
const copyPublicKeyTrigger = charliePubkey.getByRole("button", {
210+
name: "Copy public key",
211+
});
212+
await copyPublicKeyTrigger.click();
213+
const copyNpubButton = page.getByRole("button", { name: "Copy npub" });
214+
await copyNpubButton.click();
215+
await expect
216+
.poll(() => page.evaluate(() => navigator.clipboard.readText()))
217+
.toBe(npubEncode(TEST_IDENTITIES.charlie.pubkey));
218+
await expect(
219+
page.locator("[data-sonner-toast]").filter({ hasText: "npub copied" }),
220+
).toBeVisible();
221+
// Copying is not a dismissal: the nested affordance and the inspection
222+
// popover it lives in both survive the copy.
223+
await expect(copyNpubButton).toBeVisible();
224+
await expect(charlieKeyPopover).toBeVisible();
225+
226+
// The inner Escape closes only the nested key popover — the inspection
227+
// stays open.
228+
await page.keyboard.press("Escape");
229+
await expect(copyNpubButton).toHaveCount(0);
230+
await expect(charlieKeyPopover).toBeVisible();
231+
232+
// Keyboard path: after the inner Escape focus returns naturally to the
233+
// full-key trigger; Space reopens the popover, whose auto-focus lands on
234+
// Copy npub, and Enter activates it. The sentinel proves this keyboard
235+
// copy rewrites the clipboard rather than inheriting the value above.
236+
await expect(copyPublicKeyTrigger).toBeFocused();
237+
await page.evaluate(() =>
238+
navigator.clipboard.writeText("keyboard-copy-sentinel"),
239+
);
240+
await page.keyboard.press("Space");
241+
await expect(copyNpubButton).toBeVisible();
242+
await expect(copyNpubButton).toBeFocused();
243+
await page.keyboard.press("Enter");
244+
await expect
245+
.poll(() => page.evaluate(() => navigator.clipboard.readText()))
246+
.toBe(npubEncode(TEST_IDENTITIES.charlie.pubkey));
247+
248+
// Close the reopened nested popover so the inspection popover owns the
249+
// final Escape; the recipient itself survives both dismissals.
250+
await page.keyboard.press("Escape");
251+
await expect(copyNpubButton).toHaveCount(0);
201252
await page.keyboard.press("Escape");
202253
await expect(charlieKeyPopover).toHaveCount(0);
254+
await expect(charlieChip).toBeVisible();
203255

204256
await search.fill("charlie");
205257
await expect(charlieResult).toBeVisible();
@@ -230,6 +282,16 @@ test("selected new-DM recipient can be verified again through search", async ({
230282
await page.getByTestId("new-message-page").screenshot({
231283
path: `${SHOTS}/new-dm-selected-recipient.png`,
232284
});
285+
286+
// The To-field guard ignores popover clicks that bubble into the field;
287+
// a click on the label itself — a physical descendant of the field — must
288+
// still focus the input and open the recipient picker.
289+
await page
290+
.getByTestId("new-message-to-field")
291+
.getByText("To:", { exact: true })
292+
.click();
293+
await expect(search).toBeFocused();
294+
await expect(page.getByTestId("new-message-recipient-popover")).toBeVisible();
233295
});
234296

235297
test("member removal confirm shows the full npub inline", async ({ page }) => {

0 commit comments

Comments
 (0)