Skip to content

Commit e9f7c9c

Browse files
committed
fix(desktop): preserve thread focus during layout changes
1 parent c7415bf commit e9f7c9c

7 files changed

Lines changed: 38 additions & 35 deletions

File tree

‎desktop/src/features/channels/ui/FocusThreadDrawer.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
THREAD_FOCUS_DRAWER_TRAVEL_PX,
66
THREAD_FOCUS_SLIVER_WIDTH_PX,
77
} from "@/features/channels/lib/threadFocusLayout";
8+
import { getThreadViewMode } from "@/features/channels/lib/threadViewModePreference";
89
import { cn } from "@/shared/lib/cn";
910

1011
type FocusThreadDrawerProps = {
@@ -167,7 +168,11 @@ export function FocusThreadDrawer({
167168
return () => {
168169
const previousFocus = previousFocusRef.current;
169170
requestAnimationFrame(() => {
170-
previousFocus?.focus({ preventScroll: true });
171+
// A real dismissal keeps focus mode selected; a presentation switch
172+
// has already selected split mode and owns focus inside the new panel.
173+
if (getThreadViewMode() === "focus") {
174+
previousFocus?.focus({ preventScroll: true });
175+
}
171176
});
172177
};
173178
}, []);

‎desktop/src/features/channels/ui/useThreadViewModeSwitch.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ export function useThreadViewModeSwitch({
5757
setLayoutScrollTargetId(anchorId);
5858
onModeChange?.(mode);
5959
setThreadViewMode(mode);
60-
if (restoreFocus) {
60+
requestAnimationFrame(() => {
6161
requestAnimationFrame(() => {
62-
requestAnimationFrame(() => {
63-
document
64-
.querySelector<HTMLElement>(
65-
'[data-testid="thread-view-mode-toggle"]',
66-
)
67-
?.focus({ preventScroll: true });
68-
});
62+
document
63+
.querySelector<HTMLElement>(
64+
restoreFocus
65+
? '[data-testid="thread-view-mode-toggle"]'
66+
: '[data-testid="message-thread-body"]',
67+
)
68+
?.focus({ preventScroll: true });
6969
});
70-
}
70+
});
7171
},
7272
[onModeChange],
7373
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ export function MessageThreadPanel({
526526
data-buzz-conversation-scroll
527527
data-testid="message-thread-body"
528528
onScroll={onScroll}
529+
tabIndex={-1}
529530
ref={threadBodyRef}
530531
>
531532
<div
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { isInteractiveSidebarTarget } from "./sidebarBackgroundTarget.ts";
4+
import { isSidebarBackgroundTarget } from "./sidebarBackgroundTarget.ts";
55

6-
test("non-DOM event targets are sidebar background", () => {
7-
assert.equal(isInteractiveSidebarTarget(null), false);
8-
assert.equal(isInteractiveSidebarTarget({}), false);
6+
test("non-DOM event targets are not sidebar background", () => {
7+
assert.equal(isSidebarBackgroundTarget(null), false);
8+
assert.equal(isSidebarBackgroundTarget({}), false);
99
});
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
const INTERACTIVE_SIDEBAR_TARGET_SELECTOR = [
2-
"a",
3-
"button",
4-
"input",
5-
"select",
6-
"textarea",
7-
"[contenteditable='true']",
8-
"[role='button']",
9-
"[role='link']",
10-
"[role='menuitem']",
11-
"[role='option']",
12-
"[tabindex]:not([tabindex='-1'])",
13-
].join(",");
1+
const SIDEBAR_BACKGROUND_ATTRIBUTE = "data-sidebar-background";
142

15-
/** Whether a sidebar click belongs to a control rather than its background. */
16-
export function isInteractiveSidebarTarget(
17-
target: EventTarget | null,
18-
): boolean {
3+
/** Whether a sidebar click landed directly on an opted-in blank surface. */
4+
export function isSidebarBackgroundTarget(target: EventTarget | null): boolean {
195
const element =
206
typeof Element !== "undefined" && target instanceof Element
217
? target
228
: typeof Node !== "undefined" && target instanceof Node
239
? target.parentElement
2410
: null;
25-
return Boolean(element?.closest(INTERACTIVE_SIDEBAR_TARGET_SELECTOR) ?? null);
11+
return element?.hasAttribute(SIDEBAR_BACKGROUND_ATTRIBUTE) ?? false;
2612
}

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from "@/features/sidebar/lib/channelSortPreference";
2222
import { useChannelSortPreference } from "@/features/sidebar/lib/useChannelSortPreference";
2323
import { useSidebarScrollLock } from "@/features/sidebar/lib/useSidebarScrollLock";
24-
import { isInteractiveSidebarTarget } from "@/features/sidebar/lib/sidebarBackgroundTarget";
24+
import { isSidebarBackgroundTarget } from "@/features/sidebar/lib/sidebarBackgroundTarget";
2525
import { useUnreadOverflow } from "@/features/sidebar/lib/useUnreadOverflow";
2626
import {
2727
CreateSectionDialog,
@@ -554,14 +554,15 @@ export function AppSidebar({
554554
collapsible="offcanvas"
555555
data-testid="app-sidebar"
556556
onClick={(event) => {
557-
if (!isInteractiveSidebarTarget(event.target)) {
557+
if (isSidebarBackgroundTarget(event.target)) {
558558
onBackgroundClick?.();
559559
}
560560
}}
561561
variant="sidebar"
562562
>
563563
<div
564564
className="relative flex min-h-0 flex-1 flex-col overflow-hidden"
565+
data-sidebar-background
565566
data-testid="app-sidebar-scroll-anchor"
566567
>
567568
<AppSidebarPinnedHeader
@@ -580,6 +581,7 @@ export function AppSidebar({
580581

581582
<div
582583
className="relative flex min-h-0 flex-1 flex-col"
584+
data-sidebar-background
583585
data-testid="sidebar-channel-content"
584586
>
585587
{unreadAboveCount > 0 ? (
@@ -593,10 +595,12 @@ export function AppSidebar({
593595

594596
<SidebarContent
595597
className="buzz-sidebar-scrollbar overscroll-none"
598+
data-sidebar-background
596599
ref={scrollRef}
597600
>
598601
<div
599602
className="flex w-full flex-col gap-2 px-[3px]"
603+
data-sidebar-background
600604
data-testid="sidebar-scroll-content"
601605
>
602606
<AppSidebarPrimaryMenu

‎desktop/tests/e2e/thread-focus-mode.spec.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ test("focus and split preserve reading context and interaction ownership", async
199199
await expect(channel).not.toHaveAttribute("inert", "");
200200
await expectChannelHeaderUnobscured(page);
201201
await expect(page.getByRole("tooltip")).toHaveCount(0);
202-
await expect(page.getByTestId("thread-view-mode-toggle")).not.toBeFocused();
202+
await expect(page.getByTestId("message-thread-body")).toBeFocused();
203+
await expect(summary).not.toBeFocused();
203204
await expect(
204205
body.locator(`[data-message-id="${anchorId}"]`),
205206
).toBeInViewport();
@@ -241,6 +242,12 @@ test("focus and split preserve reading context and interaction ownership", async
241242

242243
await summary.click();
243244
await expect(drawer).toBeVisible();
245+
const profileCard = page.getByTestId("sidebar-profile-card");
246+
await profileCard.click({ position: { x: 8, y: 8 } });
247+
await expect(page.getByTestId("profile-popover")).toBeVisible();
248+
await expect(drawer).toBeVisible();
249+
await profileCard.click({ position: { x: 8, y: 8 } });
250+
await expect(page.getByTestId("profile-popover")).toHaveCount(0);
244251
await page
245252
.getByTestId("app-sidebar-scroll-anchor")
246253
.evaluate((element) => (element as HTMLElement).click());

0 commit comments

Comments
 (0)