Skip to content

Commit 2014bd8

Browse files
author
Carl
committed
Keep identity actions reachable and preserve moved focus
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent 5c5832f commit 2014bd8

5 files changed

Lines changed: 405 additions & 213 deletions

File tree

‎src/shared/design-system/DESIGN.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,13 @@ PreviewCard may expose one supplemental action through `actionRef`, such as copy
378378
an identity's full npub. Action previews open without a delay so immediate Tab
379379
navigation reaches the action. They remain non-modal and never take focus on hover. Tab
380380
from the trigger reaches the action; Shift+Tab returns to the trigger; forward Tab
381-
continues after the trigger. Escape dismisses the preview before restoring focus.
381+
continues after the trigger. Escape dismisses the preview before restoring focus,
382+
but never pulls focus back if the user moved it during exit. Closing previews
383+
are no longer Tab destinations.
382384
The positioned portal owns its layer above dialogs. An optional content anchor
383-
keeps previews near compact identity content inside wider actionable rows.
385+
keeps previews near compact identity content inside wider actionable rows. Identity
386+
previews prefer above that content (with Base UI collision handling), leaving
387+
the hovered row’s trailing action unobstructed.
384388

385389
## Menu row corners
386390

‎src/shared/design-system/ui/PreviewCard.tsx‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function PreviewCard({
5050
const triggerRef = useRef<HTMLAnchorElement>(null);
5151
const popupRef = useRef<HTMLDivElement>(null);
5252
const focusedTrigger = useRef<HTMLElement | null>(null);
53-
const returnFocusOnClose = useRef(false);
53+
const closingPopup = useRef<HTMLDivElement | null>(null);
5454
const restoreFocus = () => {
5555
const trigger = triggerRef.current;
5656
if (!trigger) return;
@@ -73,17 +73,26 @@ export function PreviewCard({
7373
open={open}
7474
onOpenChange={(next, details) => {
7575
onOpenChange?.(next, details);
76-
returnFocusOnClose.current =
76+
if (details.isCanceled) return;
77+
closingPopup.current =
7778
!next &&
78-
!details.isCanceled &&
7979
details.reason === "escape-key" &&
80-
!!popupRef.current?.contains(document.activeElement);
80+
popupRef.current?.contains(document.activeElement)
81+
? popupRef.current
82+
: null;
8183
}}
8284
onOpenChangeComplete={(next) => {
83-
if (!next && returnFocusOnClose.current) {
84-
returnFocusOnClose.current = false;
85+
if (next) return;
86+
// Retain the closing element through unmount, as finalFocus.ts does.
87+
// Escape grants return-focus ownership only until the user moves it.
88+
const popup = closingPopup.current;
89+
closingPopup.current = null;
90+
const active = popup?.ownerDocument.activeElement ?? null;
91+
if (
92+
popup &&
93+
(active === popup.ownerDocument.body || popup.contains(active))
94+
)
8595
restoreFocus();
86-
}
8796
}}
8897
>
8998
<BasePreviewCard.Trigger
@@ -99,7 +108,7 @@ export function PreviewCard({
99108
(link || actionRef) &&
100109
event.key === "Tab" &&
101110
!event.shiftKey &&
102-
popupRef.current
111+
popupRef.current?.hasAttribute("data-open")
103112
) {
104113
event.preventDefault();
105114
(actionRef?.current ?? popupRef.current).focus();

‎src/shared/identity/IdentityRow.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function IdentityRow({
8080
id={previewId}
8181
open={open}
8282
onOpenChange={setOpen}
83-
side="right"
83+
side="top"
8484
anchor={anchor}
8585
actionRef={copyButton}
8686
aria-label={`${name} identity`}

‎tests/browser/channel-members-focus.fixture.tsx‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import "../../src/shared/styles/globals.css";
1818
const viewer = keypair();
1919
const relay = keypair();
2020
const person = keypair();
21+
const candidates = new URLSearchParams(location.search).has("multiple")
22+
? [1, 2, 3].map((index) => ({
23+
key: keypair(),
24+
name: `Morgan Field Tester ${index}`,
25+
}))
26+
: [{ key: person, name: "Morgan" }];
27+
const candidateProfiles = candidates.map(({ key, name }) =>
28+
profile(key, { name }),
29+
);
30+
const additions: string[] = [];
2131
const channelId = "11111111-1111-4111-8111-111111111111";
2232
const members = [viewer.pubkey];
2333
let clock = 1700000000;
@@ -37,8 +47,7 @@ const { session } = createRelaySession(
3747
media: () => undefined,
3848
readAgentLibrary: async () => ({ definitions: [], identities: [] }),
3949
query: async (filters) => {
40-
if (filters.some((filter) => filter.search))
41-
return [profile(person, { name: "Morgan" })];
50+
if (filters.some((filter) => filter.search)) return candidateProfiles;
4251
return [
4352
roster(relay, channelId, members, clock),
4453
signed(relay, {
@@ -52,18 +61,21 @@ const { session } = createRelaySession(
5261
],
5362
}),
5463
profile(viewer, { name: "Carl" }),
55-
profile(person, { name: "Morgan" }),
64+
...candidateProfiles,
5665
].filter((event) =>
5766
filters.some((filter) => matchesEvent(event, filter)),
5867
);
5968
},
6069
writer: {
6170
kinds: [9000],
6271
sign: async (template) => signed(viewer, template),
63-
publish: async () => {
72+
publish: async (event) => {
73+
const key = event.tags.find(([tag]) => tag === "p")?.[1];
74+
if (!key) throw new Error("Missing added identity");
75+
additions.push(key);
6476
publishStarted();
6577
await held;
66-
members.push(person.pubkey);
78+
members.push(key);
6779
clock++;
6880
},
6981
},
@@ -94,5 +106,5 @@ function Fixture() {
94106
createRoot(root).render(<Fixture />);
95107
// The test controls when confirmation arrives; no relay or member is contacted.
96108
Object.assign(window, {
97-
focusFixture: { published, confirm: () => releasePublish() },
109+
focusFixture: { published, additions, confirm: () => releasePublish() },
98110
});

0 commit comments

Comments
 (0)