diff --git a/src/bundled/channels/ChannelLifecycleActions.test.tsx b/src/bundled/channels/ChannelLifecycleActions.test.tsx
index ce8814b44f..3d4eb6f8d9 100644
--- a/src/bundled/channels/ChannelLifecycleActions.test.tsx
+++ b/src/bundled/channels/ChannelLifecycleActions.test.tsx
@@ -121,6 +121,35 @@ it("replaces Archive with Unarchive when the live channel is archived", () => {
screen.getByRole("button", { name: "Unarchive channel" }),
).toBeVisible();
});
+it("keeps focus in the pane when a role change retires the focused action, unless it moved", async () => {
+ const host = session();
+ const pane = (role: ChannelSummary["viewerRole"]) => (
+ <>
+
+
+ >
+ );
+ const view = render(pane("admin"));
+ screen.getByRole("button", { name: "Archive channel" }).focus();
+ view.rerender(pane("member"));
+ expect(screen.queryByRole("button", { name: "Archive channel" })).toBeNull();
+ await vi.waitFor(() =>
+ expect(screen.getByRole("button", { name: "Settings tab" })).toHaveFocus(),
+ );
+ view.rerender(pane("admin"));
+ screen.getByRole("button", { name: "Archive channel" }).focus();
+ screen.getByRole("textbox", { name: "Elsewhere" }).focus();
+ view.rerender(pane("member"));
+ await Promise.resolve();
+ expect(screen.getByRole("textbox", { name: "Elsewhere" })).toHaveFocus();
+});
it("explains an unsupported host without a lookup or write", () => {
const host = session(false);
render(
diff --git a/src/bundled/channels/ChannelLifecycleActions.tsx b/src/bundled/channels/ChannelLifecycleActions.tsx
index e8e991c976..f545ba5e3b 100644
--- a/src/bundled/channels/ChannelLifecycleActions.tsx
+++ b/src/bundled/channels/ChannelLifecycleActions.tsx
@@ -1,3 +1,4 @@
+import { useCallback } from "react";
import type { ChannelSummary } from "../../features/relay/contracts";
import type { ChannelLifecycleAction } from "../../features/relay/channel-lifecycle-protocol";
import { Button } from "../../shared/design-system/ui/Button";
@@ -6,7 +7,10 @@ import {
SignOutIcon,
TrashIcon,
} from "../../shared/design-system/icons";
-import type { LifecycleNoun } from "./ChannelLifecycleDialog";
+import {
+ lifecycleFocusFallback,
+ type LifecycleNoun,
+} from "./ChannelLifecycleDialog";
import { useLifecycleOffer, type LifecycleSession } from "./useLifecycleOffer";
/** The archived footer uses the same live permissions as Settings. */
@@ -48,18 +52,37 @@ export function ChannelLifecycleActions({
noun?: LifecycleNoun;
}) {
const permissions = useLifecycleOffer(session, channel);
+ // A refreshed or live role can retire the focused action. Ref cleanup runs
+ // before its button detaches, while it still owns focus; keep focus in the
+ // pane unless something else took it meanwhile.
+ const retire = useCallback((node: HTMLButtonElement | null) => {
+ if (!node) return;
+ return () => {
+ const document = node.ownerDocument;
+ if (document.activeElement !== node) return;
+ const fallback = lifecycleFocusFallback(node);
+ queueMicrotask(() => {
+ if (!node.isConnected && document.activeElement === document.body)
+ fallback?.focus({ preventScroll: true });
+ });
+ };
+ }, []);
if (!session.channelLifecycle.available)
return
Channel actions unavailable on this connection
;
return (
<>
{permissions?.canLeave && (
-