Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(ui): scroll only the tab row when ViewTabs reveals its active tab (#2498)
Raghav Chamadiyabe07f49b702b
diff --git a/packages/ui/__tests__/decisions/decision-review-lanes.test.tsx b/packages/ui/__tests__/decisions/decision-review-lanes.test.tsxindex b107382bb..397143455 100644--- a/packages/ui/__tests__/decisions/decision-review-lanes.test.tsx+++ b/packages/ui/__tests__/decisions/decision-review-lanes.test.tsx@@ -1,4 +1,4 @@-import { beforeAll, describe, expect, it, vi } from "vitest";+import { describe, expect, it, vi } from "vitest";import { fireEvent, render, screen, within } from "@testing-library/react";import {DECISION_LANES,@@ -7,11 +7,6 @@ import {} from "../../src/decisions/decision-review-lanes";import type { DecisionRecord } from "@repowise-dev/types/decisions";-// jsdom has no scrollIntoView; `ViewTabs` keeps the active tab in view on mount.-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-function record(overrides: Partial<DecisionRecord> = {}): DecisionRecord {return {id: "d1",diff --git a/packages/ui/__tests__/files/file-decisions-tab.test.tsx b/packages/ui/__tests__/files/file-decisions-tab.test.tsxindex 15c826cff..0abcf54b4 100644--- a/packages/ui/__tests__/files/file-decisions-tab.test.tsx+++ b/packages/ui/__tests__/files/file-decisions-tab.test.tsx@@ -1,4 +1,4 @@-import { describe, it, expect, vi, beforeAll } from "vitest";+import { describe, it, expect, vi } from "vitest";import { render, screen, fireEvent } from "@testing-library/react";import { FileDecisionsTab } from "../../src/files/file-decisions-tab.js";import { FilePage } from "../../src/files/file-page.js";@@ -7,11 +7,6 @@ import { buildFilePanels } from "../../src/files/file-page-panels.js";import { fileTabsFor, type FilePageTab } from "../../src/files/file-page-tabs.js";import type { FileDetailResponse, GoverningDecisionRef } from "@repowise-dev/types/files";-// jsdom has no scrollIntoView; `ViewTabs` keeps the active tab in view on mount.-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-function makeDecision(id: string, title: string, status: string): GoverningDecisionRef {return { id, title, status };}diff --git a/packages/ui/__tests__/health/context-tabs.test.tsx b/packages/ui/__tests__/health/context-tabs.test.tsxindex 51dee0c0a..f9ad2798a 100644--- a/packages/ui/__tests__/health/context-tabs.test.tsx+++ b/packages/ui/__tests__/health/context-tabs.test.tsx@@ -9,16 +9,11 @@* total exists only for a server that predates context scoping and sends* no facets.context at all.*/-import { beforeAll, describe, expect, it, vi } from "vitest";+import { describe, expect, it } from "vitest";import { render, screen } from "@testing-library/react";import { ContextTabs } from "../../src/health/performance/filters.js";import type { PerformanceFacets } from "@repowise-dev/types/health";-// jsdom has no layout engine; ViewTabs' active-tab scroll is a no-op here.-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-const scopedCounts = [{ value: "production", total: 22 },{ value: "tooling", total: 5 },diff --git a/packages/ui/__tests__/health/performance-view.test.tsx b/packages/ui/__tests__/health/performance-view.test.tsxindex d9ddd48a6..bfbe570ba 100644--- a/packages/ui/__tests__/health/performance-view.test.tsx+++ b/packages/ui/__tests__/health/performance-view.test.tsx@@ -1,5 +1,5 @@import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";-import { beforeAll, describe, expect, it, vi } from "vitest";+import { describe, expect, it, vi } from "vitest";import type { RefactoringPlan } from "@repowise-dev/types/refactoring";import { PerformanceView } from "../../src/health/performance-view";@@ -12,10 +12,6 @@ import {sibling,} from "./fixtures/performance";-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-const rows = () => screen.findAllByRole("listitem");const openFirstRow = async () => {const [first] = await rows();diff --git a/packages/ui/__tests__/savings/savings-surface.test.tsx b/packages/ui/__tests__/savings/savings-surface.test.tsxindex b999468ee..b7b810142 100644--- a/packages/ui/__tests__/savings/savings-surface.test.tsx+++ b/packages/ui/__tests__/savings/savings-surface.test.tsx@@ -1,4 +1,4 @@-import { describe, it, expect, beforeAll, vi } from "vitest";+import { describe, it, expect, vi } from "vitest";import { fireEvent, render, screen, within } from "@testing-library/react";import { SavingsLede } from "../../src/savings/savings-lede";@@ -19,11 +19,6 @@ import {} from "../../src/savings/savings-methodology";import { surfaceLabel, type SavingsView, type SpendView } from "../../src/savings/types";-// jsdom has no scrollIntoView; `ViewTabs` keeps the active tab in view on mount.-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-function makeSavings(overrides: Partial<SavingsView> = {}): SavingsView {return {available: true,diff --git a/packages/ui/__tests__/shared/view-tabs.test.tsx b/packages/ui/__tests__/shared/view-tabs.test.tsxindex 52fc3dada..c74e34741 100644--- a/packages/ui/__tests__/shared/view-tabs.test.tsx+++ b/packages/ui/__tests__/shared/view-tabs.test.tsx@@ -1,12 +1,7 @@-import { describe, it, expect, vi, beforeAll } from "vitest";+import { afterEach, describe, it, expect, vi } from "vitest";import { render, screen, fireEvent } from "@testing-library/react";import { ViewTabs } from "../../src/shared/view-tabs.js";-// jsdom has no layout engine, so the active-tab scroll-into-view is a no-op here.-beforeAll(() => {- Element.prototype.scrollIntoView = vi.fn();-});-describe("ViewTabs", () => {it("renders tab labels and reports selection", () => {const onValueChange = vi.fn();@@ -51,4 +46,66 @@ describe("ViewTabs", () => {const plainTab = screen.getByRole("tab", { name: "Explore" });expect(plainTab.querySelector("svg")).toBeNull();});++ describe("keeping the active tab in view", () => {+ // jsdom has no layout, so give the row and its tabs a geometry: tabs 100px+ // wide side by side in a 150px row.+ const restore: (() => void)[] = [];+ function stub<K extends keyof HTMLElement>(key: K, get: (el: HTMLElement) => number) {+ const original = Object.getOwnPropertyDescriptor(HTMLElement.prototype, key);+ Object.defineProperty(HTMLElement.prototype, key, {+ configurable: true,+ get(this: HTMLElement) {+ return get(this);+ },+ });+ restore.push(() => {+ if (original) Object.defineProperty(HTMLElement.prototype, key, original);+ });+ }+ afterEach(() => {+ while (restore.length) restore.pop()!();+ });++ it("scrolls the tab row, not the page", () => {+ const scrolled = new WeakMap<Element, number>();+ const original = Object.getOwnPropertyDescriptor(Element.prototype, "scrollLeft");+ Object.defineProperty(Element.prototype, "scrollLeft", {+ configurable: true,+ get(this: Element) {+ return scrolled.get(this) ?? 0;+ },+ set(this: Element, v: number) {+ scrolled.set(this, v);+ },+ });+ restore.push(() => {+ if (original) Object.defineProperty(Element.prototype, "scrollLeft", original);+ });+ stub("offsetWidth", (el) => (el.getAttribute("role") === "tab" ? 100 : 0));+ stub("offsetLeft", (el) =>+ el.getAttribute("role") === "tab" ? [...el.parentElement!.children].indexOf(el) * 100 : 0,+ );+ stub("clientWidth", (el) => (el.getAttribute("role") === "tablist" ? 150 : 0));+ const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => {});++ render(+ <ViewTabs+ tabs={[+ { id: "a", label: "A" },+ { id: "b", label: "B" },+ { id: "c", label: "C" },+ ]}+ value="c"+ onValueChange={() => {}}+ />,+ );++ // Tab C spans 200 to 300, so the 150px row scrolls to 150 to show it.+ expect(screen.getByRole("tablist").scrollLeft).toBe(150);+ expect(scrollTo).not.toHaveBeenCalled();+ expect(window.scrollY).toBe(0);+ scrollTo.mockRestore();+ });+ });});diff --git a/packages/ui/src/shared/view-tabs.tsx b/packages/ui/src/shared/view-tabs.tsxindex 2507812cc..70af08b53 100644--- a/packages/ui/src/shared/view-tabs.tsx+++ b/packages/ui/src/shared/view-tabs.tsx@@ -61,10 +61,18 @@ export function ViewTabs({const panelId = ownsPanel ? `${baseId}-panel` : externalPanelId;const tabRefs = React.useRef<Record<string, HTMLButtonElement | null>>({});- // Keep the active tab in view even though the scrollbar is hidden — it can- // otherwise scroll off-screen with no way to reveal it.+ // Keep the active tab in view, since the row's scrollbar is hidden. Only the+ // row scrolls: moving the page here would pull a tab row below the fold into+ // view on load.React.useEffect(() => {- tabRefs.current[value]?.scrollIntoView({ inline: "nearest", block: "nearest" });+ const tab = tabRefs.current[value];+ const row = tab?.parentElement;+ if (!tab || !row) return;+ const left = tab.offsetLeft - row.offsetLeft;+ if (left < row.scrollLeft) row.scrollLeft = left;+ else if (left + tab.offsetWidth > row.scrollLeft + row.clientWidth) {+ row.scrollLeft = left + tab.offsetWidth - row.clientWidth;+ }}, [value]);// Left/right arrow keys move selection (and focus) between tabs; Home/Enddiff --git a/packages/web/src/app/repos/[id]/costs/page.test.tsx b/packages/web/src/app/repos/[id]/costs/page.test.tsxindex 04bfe51c0..55a2d1921 100644--- a/packages/web/src/app/repos/[id]/costs/page.test.tsx+++ b/packages/web/src/app/repos/[id]/costs/page.test.tsx@@ -13,7 +13,7 @@import React from "react";import { SWRConfig } from "swr";import { cleanup, render, screen, waitFor, fireEvent } from "@testing-library/react";-import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { CostSummary, Savings } from "@/lib/api/costs";@@ -50,12 +50,6 @@ vi.mock("next/link", () => ({import CostsPage from "./page";import { ACCOUNTING_METHOD_VERSION } from "@repowise-dev/ui/savings";-beforeAll(() => {- // jsdom implements no layout, so `ViewTabs` scrolling its active tab into- // view on mount throws rather than doing nothing.- Element.prototype.scrollIntoView = vi.fn();-});-/** A report with something in it. Every figure is a wire field; the page does* no arithmetic, so the values only have to be distinguishable. */function makeSavings(overrides: Partial<Savings> = {}): Savings {