Skip to content

Commit c065897

Browse files
gilgtmcodexclaude
committed
fix(solo): run a row menu action after its menu closes
A contributed Solo row action used to run on click, while its menu was still open. The menu then returned focus to the row, which took focus from the dialog or composer that the action had just opened. Now the row menu runs the chosen action once its close completes. It first puts focus on the row, so the action can move focus from there. The action is dropped if the menu reopens, the row unmounts or rebinds, or the user moves focus elsewhere before the close completes. Escape and outside dismissal still return focus without running anything. Co-authored-by: Codex <[email protected]> Co-authored-by: Claude <[email protected]> Signed-off-by: Gil Birman <[email protected]>
1 parent d3c1d91 commit c065897

2 files changed

Lines changed: 519 additions & 14 deletions

File tree

‎src/features/channel-navigation/SoloDestination.tsx‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type RefObject,
55
type FocusEvent,
66
createContext,
7+
useCallback,
78
useContext,
89
useLayoutEffect,
910
useRef,
@@ -67,7 +68,9 @@ type ConversationState = Readonly<{
6768
open(): void;
6869
canRename: boolean;
6970
rename(): boolean;
70-
focusTarget(): HTMLElement | false;
71+
finalFocus(): HTMLElement | false;
72+
closeMenu(popup: HTMLElement | null): void;
73+
openMenu(): void;
7174
}>;
7275
const ConversationContext = createContext<ConversationState | undefined>(
7376
undefined,
@@ -79,6 +82,15 @@ export function RegionConversation(props: SoloConversationProps) {
7982
const handoff = useChannelNavigation();
8083
const root = useRef<HTMLDivElement>(null);
8184
const restoreRename = useRef(false);
85+
const afterClose = useRef<() => void>(undefined);
86+
const chosen = useRef(false);
87+
const chosenItem = useRef<HTMLElement>(null);
88+
const openMenu = useCallback(() => {
89+
afterClose.current = undefined;
90+
chosen.current = false;
91+
chosenItem.current = null;
92+
}, []);
93+
useLayoutEffect(() => openMenu, [openMenu]);
8294
const channel = context?.region.channels.find(
8395
(channel) => channel.id === props.channelId,
8496
);
@@ -126,15 +138,19 @@ export function RegionConversation(props: SoloConversationProps) {
126138
{props.menuActions.slice(0, 6).map((action) => (
127139
<MenuItem
128140
key={action.id}
129-
onClick={() => {
130-
if (!active()) return;
131-
try {
132-
void Promise.resolve(action.onSelect()).catch(() => {
133-
if (active()) setError("The action could not run.");
134-
});
135-
} catch {
136-
setError("The action could not run.");
137-
}
141+
onClick={(event) => {
142+
chosen.current = true;
143+
chosenItem.current = event.currentTarget;
144+
afterClose.current = () => {
145+
if (!active()) return;
146+
try {
147+
void Promise.resolve(action.onSelect()).catch(() => {
148+
if (active()) setError("The action could not run.");
149+
});
150+
} catch {
151+
setError("The action could not run.");
152+
}
153+
};
138154
}}
139155
>
140156
<MenuIcon>{action.icon}</MenuIcon>
@@ -166,7 +182,23 @@ export function RegionConversation(props: SoloConversationProps) {
166182
setDialog("rename");
167183
return true;
168184
},
169-
focusTarget,
185+
finalFocus: () => (chosen.current ? false : focusTarget()),
186+
openMenu,
187+
closeMenu(popup) {
188+
if (!popup?.contains(chosenItem.current)) return;
189+
const action = afterClose.current;
190+
afterClose.current = undefined;
191+
if (!action || !root.current?.isConnected || !active()) return;
192+
const current = document.activeElement;
193+
if (
194+
current !== document.body &&
195+
current !== focusTarget() &&
196+
!popup?.contains(current)
197+
)
198+
return;
199+
(focusTarget() || undefined)?.focus({ preventScroll: true });
200+
action();
201+
},
170202
};
171203
return (
172204
<div
@@ -251,16 +283,21 @@ function ConversationMenu({ children }: { children: ReactNode }) {
251283
const state = useContext(ConversationContext);
252284
const [open, setOpen] = useState(false);
253285
const [anchor, setAnchor] = useState<HTMLElement>();
286+
const popup = useRef<HTMLDivElement>(null);
254287
if (!state) return null;
255288
if (state.channel.cached)
256289
return <div className={styles.menu}>{children}</div>;
257290
return (
258291
<ContextMenuRoot
259292
open={open}
260293
onOpenChange={(next) => {
294+
if (next) state.openMenu();
261295
setOpen(next);
262296
if (!next) setAnchor(undefined);
263297
}}
298+
onOpenChangeComplete={(next) => {
299+
if (!next) state.closeMenu(popup.current);
300+
}}
264301
>
265302
<ContextMenuTrigger
266303
render={<div className={styles.menu} />}
@@ -271,6 +308,7 @@ function ConversationMenu({ children }: { children: ReactNode }) {
271308
) {
272309
event.preventDefault();
273310
event.stopPropagation();
311+
state.openMenu();
274312
setAnchor(event.currentTarget);
275313
setOpen(true);
276314
}
@@ -281,7 +319,10 @@ function ConversationMenu({ children }: { children: ReactNode }) {
281319
<MenuPopup
282320
aria-label={`Actions for ${state.channel.name}`}
283321
anchor={anchor}
284-
finalFocus={state.focusTarget}
322+
ref={(element) => {
323+
if (element) popup.current = element;
324+
}}
325+
finalFocus={state.finalFocus}
285326
>
286327
{state.menu}
287328
</MenuPopup>
@@ -291,9 +332,17 @@ function ConversationMenu({ children }: { children: ReactNode }) {
291332

292333
function ConversationActions() {
293334
const state = useContext(ConversationContext);
335+
const popup = useRef<HTMLDivElement>(null);
294336
if (!state || state.channel.cached) return null;
295337
return (
296-
<MenuRoot>
338+
<MenuRoot
339+
onOpenChange={(next) => {
340+
if (next) state.openMenu();
341+
}}
342+
onOpenChangeComplete={(next) => {
343+
if (!next) state.closeMenu(popup.current);
344+
}}
345+
>
297346
<MenuTrigger
298347
render={(props) => (
299348
<IconButton
@@ -309,7 +358,10 @@ function ConversationActions() {
309358
<MenuPopup
310359
aria-label={`Actions for ${state.channel.name}`}
311360
align="end"
312-
finalFocus={state.focusTarget}
361+
ref={(element) => {
362+
if (element) popup.current = element;
363+
}}
364+
finalFocus={state.finalFocus}
313365
>
314366
{state.menu}
315367
</MenuPopup>

0 commit comments

Comments
 (0)