Skip to content

Commit 20aaed8

Browse files
flambohclaude
authored andcommitted
fix(web): pane tabs keep their icon and PR state when you switch tabs (#29080)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9975b36 commit 20aaed8

10 files changed

Lines changed: 492 additions & 356 deletions

‎apps/web/src/components/PaneNotch.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { cn } from "../lib/utils";
1515
export const PANE_TAB_CLASS =
1616
"inline-flex h-7 min-w-0 max-w-80 items-center gap-1.75 rounded-control px-2.5 text-control text-strong-foreground outline-none [-webkit-app-region:no-drag] focus-visible:inset-ring-2 focus-visible:inset-ring-ring";
1717

18+
export const PANE_TAB_TITLE_CLASS = "max-w-68 truncate";
19+
export const PANE_INACTIVE_TAB_TITLE_CLASS = "max-w-40 truncate";
20+
1821
/** The thread tab opens its action menu while keeping the notch's tab geometry. */
1922
export function PaneTabButton({ className, render, ...props }: useRender.ComponentProps<"button">) {
2023
return useRender({
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { useState, type ReactNode } from "react";
2+
import { expect, it } from "vite-plus/test";
3+
4+
import { render } from "../../test/render";
5+
import {
6+
PanelTabActions,
7+
PanelTabSlotsContext,
8+
usePanelTabActionsHost,
9+
} from "./PanelTabBreadcrumb";
10+
11+
function TabBar({ children }: { children: ReactNode }) {
12+
const { hasActions, registerActions } = usePanelTabActionsHost();
13+
const [actions, setActions] = useState<HTMLDivElement | null>(null);
14+
return (
15+
<>
16+
{hasActions ? <div ref={setActions} data-testid="actions-notch" /> : null}
17+
<PanelTabSlotsContext value={{ breadcrumb: null, actions, registerActions }}>
18+
{children}
19+
</PanelTabSlotsContext>
20+
</>
21+
);
22+
}
23+
24+
function Surface({ actions }: { actions: readonly string[] }) {
25+
return actions.map((label) => (
26+
<PanelTabActions key={label}>
27+
<button type="button">{label}</button>
28+
</PanelTabActions>
29+
));
30+
}
31+
32+
it("draws the actions notch only while a surface provides actions", async () => {
33+
await using screen = await render(
34+
<TabBar>
35+
<Surface actions={[]} />
36+
</TabBar>,
37+
{ router: false },
38+
);
39+
const notch = screen.getByTestId("actions-notch");
40+
await expect.element(notch).not.toBeInTheDocument();
41+
42+
await screen.rerender(
43+
<TabBar>
44+
<Surface actions={["Wrap", "Refresh"]} />
45+
</TabBar>,
46+
);
47+
await expect.element(notch.getByRole("button", { name: "Wrap" })).toBeVisible();
48+
await expect.element(notch.getByRole("button", { name: "Refresh" })).toBeVisible();
49+
50+
await screen.rerender(
51+
<TabBar>
52+
<Surface actions={["Refresh"]} />
53+
</TabBar>,
54+
);
55+
await expect.element(notch.getByRole("button", { name: "Refresh" })).toBeVisible();
56+
57+
await screen.rerender(
58+
<TabBar>
59+
<Surface actions={[]} />
60+
</TabBar>,
61+
);
62+
await expect.element(notch).not.toBeInTheDocument();
63+
});

‎apps/web/src/components/PanelTabBreadcrumb.tsx‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChevronRightIcon } from "lucide-react";
2-
import { createContext, useContext, type ReactNode } from "react";
2+
import { createContext, useContext, useLayoutEffect, useState, type ReactNode } from "react";
33
import { createPortal } from "react-dom";
44

55
/**
@@ -10,6 +10,7 @@ import { createPortal } from "react-dom";
1010
export interface PanelTabSlots {
1111
readonly breadcrumb: HTMLElement | null;
1212
readonly actions: HTMLElement | null;
13+
readonly registerActions: () => () => void;
1314
}
1415

1516
export const PanelTabSlotsContext = createContext<PanelTabSlots | null>(null);
@@ -22,7 +23,25 @@ export function useInPanelTabBar(): boolean {
2223
return useContext(PanelTabSlotsContext) !== null;
2324
}
2425

25-
function PanelTabPortal({ slot, children }: { slot: keyof PanelTabSlots; children: ReactNode }) {
26+
export function usePanelTabActionsHost(): {
27+
hasActions: boolean;
28+
registerActions: () => () => void;
29+
} {
30+
const [count, setCount] = useState(0);
31+
const registerActions = () => {
32+
setCount((current) => current + 1);
33+
return () => setCount((current) => current - 1);
34+
};
35+
return { hasActions: count > 0, registerActions };
36+
}
37+
38+
function PanelTabPortal({
39+
slot,
40+
children,
41+
}: {
42+
slot: "breadcrumb" | "actions";
43+
children: ReactNode;
44+
}) {
2645
const slots = useContext(PanelTabSlotsContext);
2746
if (!slots) return children;
2847
const element = slots[slot];
@@ -49,5 +68,8 @@ export function PanelTabBreadcrumb({ children }: { children: ReactNode }) {
4968
* bar they render in place.
5069
*/
5170
export function PanelTabActions({ children }: { children: ReactNode }) {
71+
const registerActions = useContext(PanelTabSlotsContext)?.registerActions;
72+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Tell the tab bar before paint, so its actions notch never draws empty.
73+
useLayoutEffect(() => registerActions?.(), [registerActions]);
5274
return <PanelTabPortal slot="actions">{children}</PanelTabPortal>;
5375
}

‎apps/web/src/components/RightPanelTabs.test.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { EnvironmentId, type ThreadPullRequestLink } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import {
5-
resolvePullRequestTabLink,
6-
surfaceShortcutActionForKey,
7-
surfaceShortcutTargetsTypingContext,
8-
} from "./RightPanelTabs";
4+
import { surfaceShortcutActionForKey, surfaceShortcutTargetsTypingContext } from "./RightPanelTabs";
5+
import { resolvePullRequestTabLink } from "./SurfaceTabIcon";
96

107
function shortcutEvent(
118
key: string,

0 commit comments

Comments
 (0)