TypeScript
fix(web): settings search reaches rows in collapsed sections (#18903)
Guillermo Casanovaafa42e6c2df3
diff --git a/apps/web/src/components/settings/settingsLayout.browser.test.tsx b/apps/web/src/components/settings/settingsLayout.browser.test.tsxindex 565fcb278c0..b44b0cb56bf 100644--- a/apps/web/src/components/settings/settingsLayout.browser.test.tsx+++ b/apps/web/src/components/settings/settingsLayout.browser.test.tsx@@ -62,6 +62,99 @@ describe("settings search targets", () => {expect(target.classList.contains(PULSE)).toBe(false);});+ it.each(["before", "after", "without animation"] as const)(+ "fully reveals the row when the opening style clears: %s",+ async (phase) => {+ // Keep scrolling synchronous while the panel height transitions independently.+ reduceMotion(true);+ document.body.innerHTML = `+ <div id="viewport" style="height: 200px; overflow: auto; overflow-anchor: none">+ <div id="panel" data-slot="collapsible-panel" data-open data-starting-style+ style="height: 0; overflow: hidden; transition: height 200ms linear">+ <div style="height: 500px"></div>+ <div id="legacy-sidebar" tabindex="-1" style="height: 40px">Legacy sidebar</div>+ <div style="height: 60px"></div>+ </div>+ </div>`;+ const viewport = document.getElementById("viewport")!;+ const panel = document.getElementById("panel")!;+ const target = document.getElementById("legacy-sidebar")!;++ void panel.offsetHeight;+ const animated = phase !== "without animation";+ if (animated) {+ panel.style.height = "600px";+ } else {+ panel.style.transition = "none";+ }+ const transition = panel.getAnimations()[0];+ if (animated) {+ expect(transition).toBeDefined();+ transition!.pause();+ transition!.currentTime = phase === "before" ? 0 : 100;+ if (phase === "after") panel.removeAttribute("data-starting-style");+ }++ expect(scrollToSettingsTarget("legacy-sidebar")).toBe(true);+ expect(document.activeElement).toBe(target);+ const opened = animated+ ? new Promise<void>((resolve) => {+ panel.addEventListener("transitionend", () => resolve(), { once: true });+ })+ : Promise.resolve();+ panel.style.height = "600px";+ panel.removeAttribute("data-starting-style");+ transition?.finish();+ await opened;++ const bounds = viewport.getBoundingClientRect();+ const row = target.getBoundingClientRect();+ expect(row.top).toBeGreaterThanOrEqual(bounds.top);+ expect(row.bottom).toBeLessThanOrEqual(bounds.bottom);+ expect(document.activeElement).toBe(target);+ },+ );++ it("keeps the newer search destination visible when an earlier panel finishes opening", async () => {+ reduceMotion(true);+ document.body.innerHTML = `+ <div id="viewport" style="height: 200px; overflow: auto; overflow-anchor: none">+ <div id="new-target" tabindex="-1" style="height: 40px">New target</div>+ <div style="height: 1000px"></div>+ <div id="panel" data-slot="collapsible-panel" data-open data-starting-style+ style="height: 0; overflow: hidden; transition: height 200ms linear">+ <div style="height: 500px"></div>+ <div id="legacy-sidebar" tabindex="-1" style="height: 40px">Legacy sidebar</div>+ <div style="height: 60px"></div>+ </div>+ </div>`;+ const panel = document.getElementById("panel")!;+ void panel.offsetHeight;+ panel.style.height = "600px";+ const transition = panel.getAnimations()[0]!;+ transition.pause();+ transition.currentTime = 100;++ scrollToSettingsTarget("legacy-sidebar");+ panel.removeAttribute("data-starting-style");+ scrollToSettingsTarget("new-target");+ const opened = new Promise<void>((resolve) => {+ panel.addEventListener("transitionend", () => resolve(), { once: true });+ });+ transition.finish();+ await opened;++ const viewport = document.getElementById("viewport")!;+ const target = document.getElementById("new-target")!;+ expect(target.getBoundingClientRect().top).toBeGreaterThanOrEqual(+ viewport.getBoundingClientRect().top,+ );+ expect(target.getBoundingClientRect().bottom).toBeLessThanOrEqual(+ viewport.getBoundingClientRect().bottom,+ );+ expect(document.activeElement).toBe(target);+ });+it("leaves not-yet-mounted destinations to their mount lifecycle", () => {expect(scrollToSettingsTarget("archive")).toBe(false);});diff --git a/apps/web/src/components/settings/settingsLayout.tsx b/apps/web/src/components/settings/settingsLayout.tsxindex 81f10bddbb3..3d6abb71f3f 100644--- a/apps/web/src/components/settings/settingsLayout.tsx+++ b/apps/web/src/components/settings/settingsLayout.tsx@@ -75,7 +75,10 @@ function SettingsSearchTargetProvider({return <SettingsSearchTargetContext value={value}>{children}</SettingsSearchTargetContext>;}+let pendingTargetScroll: AbortController | undefined;+function scrollAndFocusSettingsTarget(target: HTMLElement, highlight = true): void {+ pendingTargetScroll?.abort();const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;const markedScrollTarget =typeof target.querySelector === "function"@@ -87,10 +90,60 @@ function scrollAndFocusSettingsTarget(target: HTMLElement, highlight = true): vo? (target.firstElementChild as HTMLElement): target);- scrollTarget.scrollIntoView({- behavior: prefersReducedMotion ? "auto" : "smooth",- block: "center",- });+ const scroll = () =>+ scrollTarget.scrollIntoView({+ behavior: prefersReducedMotion ? "auto" : "smooth",+ block: "center",+ });+ scroll();+ const panel = target.parentElement?.closest<HTMLElement>(+ '[data-slot="collapsible-panel"][data-open]',+ );+ const heightTransition = () =>+ panel+ ?.getAnimations()+ .find(+ (animation) =>+ animation instanceof CSSTransition && animation.transitionProperty === "height",+ );+ if (panel && (panel.hasAttribute("data-starting-style") || heightTransition())) {+ const controller = new AbortController();+ pendingTargetScroll = controller;+ const { signal } = controller;+ const finishScroll = () => {+ if (signal.aborted) return;+ controller.abort();+ if (+ panel.hasAttribute("data-open") &&+ target.isConnected &&+ document.activeElement === target+ ) {+ scroll();+ }+ };+ const waitForHeight = () => {+ const transition = heightTransition();+ if (transition) {+ void transition.finished.then(finishScroll, () => controller.abort());+ } else {+ finishScroll();+ }+ };+ // Base UI clears the starting style before the height transition finishes.+ if (panel.hasAttribute("data-starting-style")) {+ const observer = new MutationObserver(() => {+ if (!panel.hasAttribute("data-starting-style")) {+ observer.disconnect();+ waitForHeight();+ }+ });+ observer.observe(panel, { attributes: true, attributeFilter: ["data-starting-style"] });+ signal.addEventListener("abort", () => observer.disconnect(), { once: true });+ } else {+ waitForHeight();+ }+ target.addEventListener("blur", () => controller.abort(), { once: true, signal });+ }target.focus({ preventScroll: true });target.classList.remove("settings-search-target-pulse");if (!highlight || prefersReducedMotion) return;