TypeScript
fix(web): stop panel motion during navigation (#9766)
Julius Marmingecd713679bbe0
diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsxindex 3bef170bcf5..6769586f7fa 100644--- a/apps/web/src/components/AppSidebarLayout.tsx+++ b/apps/web/src/components/AppSidebarLayout.tsx@@ -15,7 +15,11 @@ import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings"import { cn, isMacPlatform } from "../lib/utils";import { primaryServerKeybindingsAtom } from "../state/server";import { useEnvironmentIdentificationMode, useLegacySidebarEnabled } from "../hooks/useSettings";-import { usePanelAnimationSettings } from "../panelAnimations";+import {+ PanelAnimationSuppressionProvider,+ usePanelAnimationSettings,+ usePanelNavigationSuppression,+} from "../panelAnimations";import LegacyThreadSidebar from "./LegacySidebar";import ThreadSidebar from "./Sidebar";import { SettingsSidebarNav } from "./settings/SettingsSidebarNav";@@ -145,6 +149,8 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {// Settings routes show the settings nav in place of whichever thread// sidebar is active.const pathname = useLocation({ select: (location) => location.pathname });+ const panelAnimationsSuppressed = usePanelNavigationSuppression(pathname);+ const routePanelAnimationsActive = panelAnimationsActive && !panelAnimationsSuppressed;const isOnSettings = pathname === "/settings" || pathname.startsWith("/settings/");const isMacosDesktop = isElectron && isMacPlatform(navigator.platform);const [sidebarWidth, setSidebarWidth] = useState(readInitialThreadSidebarWidth);@@ -213,42 +219,44 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {}, [navigate, pathname]);return (- <SidebarProvider- className="h-dvh! min-h-0!"- data-panel-animations={panelAnimationsActive ? "true" : "false"}- defaultOpen- style={sidebarProviderStyle}- >- <ProjectProjectionRetention />- <Sidebar- side="left"- collapsible="offcanvas"- data-app-sidebar=""- className="border-r border-sidebar-border bg-sidebar text-sidebar-foreground"- resizable={{- maxWidth: sidebarMaximumWidth,- minWidth: THREAD_SIDEBAR_MIN_WIDTH,- shouldAcceptWidth: ({ currentWidth, nextWidth, wrapper }) =>- nextWidth <= currentWidth ||- wrapper.clientWidth - nextWidth >= THREAD_MAIN_CONTENT_MIN_WIDTH,- storageKey: THREAD_SIDEBAR_WIDTH_STORAGE_KEY,- onResize: setSidebarWidth,- }}+ <PanelAnimationSuppressionProvider value={panelAnimationsSuppressed}>+ <SidebarProvider+ className="h-dvh! min-h-0!"+ data-panel-animations={routePanelAnimationsActive ? "true" : "false"}+ defaultOpen+ style={sidebarProviderStyle}>- {isOnSettings ? (- <>- <SidebarChromeHeader isElectron={isElectron} />- <SettingsSidebarNav pathname={pathname} />- </>- ) : legacySidebarEnabled ? (- <LegacyThreadSidebar />- ) : (- <ThreadSidebar />- )}- <SidebarRail onDoubleClick={resetSidebarWidth} />- </Sidebar>- {children}- <SidebarControl />- </SidebarProvider>+ <ProjectProjectionRetention />+ <Sidebar+ side="left"+ collapsible="offcanvas"+ data-app-sidebar=""+ className="border-r border-sidebar-border bg-sidebar text-sidebar-foreground"+ resizable={{+ maxWidth: sidebarMaximumWidth,+ minWidth: THREAD_SIDEBAR_MIN_WIDTH,+ shouldAcceptWidth: ({ currentWidth, nextWidth, wrapper }) =>+ nextWidth <= currentWidth ||+ wrapper.clientWidth - nextWidth >= THREAD_MAIN_CONTENT_MIN_WIDTH,+ storageKey: THREAD_SIDEBAR_WIDTH_STORAGE_KEY,+ onResize: setSidebarWidth,+ }}+ >+ {isOnSettings ? (+ <>+ <SidebarChromeHeader isElectron={isElectron} />+ <SettingsSidebarNav pathname={pathname} />+ </>+ ) : legacySidebarEnabled ? (+ <LegacyThreadSidebar />+ ) : (+ <ThreadSidebar />+ )}+ <SidebarRail onDoubleClick={resetSidebarWidth} />+ </Sidebar>+ {children}+ <SidebarControl />+ </SidebarProvider>+ </PanelAnimationSuppressionProvider>);}diff --git a/apps/web/src/panelAnimations.test.tsx b/apps/web/src/panelAnimations.test.tsxnew file mode 100644index 00000000000..2475425086f--- /dev/null+++ b/apps/web/src/panelAnimations.test.tsx@@ -0,0 +1,64 @@+import { act, useLayoutEffect } from "react";+import { create, type ReactTestRenderer } from "react-test-renderer";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++import { usePanelNavigationSuppression } from "./panelAnimations";++let renderer: ReactTestRenderer | null = null;+let pendingFrames: FrameRequestCallback[] = [];+let observed: boolean[] = [];++function SuppressionProbe({ navigationKey }: { navigationKey: string }) {+ const suppressed = usePanelNavigationSuppression(navigationKey);+ useLayoutEffect(() => {+ observed.push(suppressed);+ }, [suppressed]);+ return null;+}++beforeEach(() => {+ pendingFrames = [];+ observed = [];+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ vi.stubGlobal("window", {+ requestAnimationFrame: vi.fn((callback: FrameRequestCallback) => {+ pendingFrames.push(callback);+ return pendingFrames.length;+ }),+ cancelAnimationFrame: vi.fn(),+ });+});++afterEach(async () => {+ await act(() => renderer?.unmount());+ vi.unstubAllGlobals();+});++async function paintPendingFrame() {+ const callback = pendingFrames.shift();+ await act(() => callback?.(0));+}++describe("usePanelNavigationSuppression", () => {+ it("suppresses initial and navigated panel state until each route has painted", async () => {+ await act(() => {+ renderer = create(<SuppressionProbe navigationKey="/thread/one" />);+ });+ expect(observed.at(-1)).toBe(true);++ await paintPendingFrame();+ expect(observed.at(-1)).toBe(true);+ await paintPendingFrame();+ expect(observed.at(-1)).toBe(false);++ await act(() => {+ renderer?.update(<SuppressionProbe navigationKey="/thread/two" />);+ });+ expect(observed.at(-1)).toBe(true);++ await paintPendingFrame();+ expect(observed.at(-1)).toBe(true);+ await paintPendingFrame();+ expect(observed.at(-1)).toBe(false);+ });+});diff --git a/apps/web/src/panelAnimations.ts b/apps/web/src/panelAnimations.tsindex f606a3f930d..11dd597ec56 100644--- a/apps/web/src/panelAnimations.ts+++ b/apps/web/src/panelAnimations.ts@@ -1,9 +1,36 @@-import { useEffect, useRef, useState } from "react";+import { createContext, useContext, useEffect, useRef, useState } from "react";import { type PanelAnimationDurationMs } from "@t3tools/contracts/settings";import { useMediaQuery } from "./hooks/useMediaQuery";import { useClientSettings } from "./hooks/useSettings";+const PanelAnimationSuppressionContext = createContext(false);++export const PanelAnimationSuppressionProvider = PanelAnimationSuppressionContext.Provider;++/**+ * Suppresses panel motion for the first painted frame of an initial route or navigation.+ * State restored by a route must be visible immediately; later user actions can animate.+ */+export function usePanelNavigationSuppression(navigationKey: string): boolean {+ const [paintedNavigationKey, setPaintedNavigationKey] = useState<string | null>(null);+ const suppressed = paintedNavigationKey !== navigationKey;++ useEffect(() => {+ if (!suppressed) return;+ let releaseFrame = 0;+ const paintFrame = window.requestAnimationFrame(() => {+ releaseFrame = window.requestAnimationFrame(() => setPaintedNavigationKey(navigationKey));+ });+ return () => {+ window.cancelAnimationFrame(paintFrame);+ window.cancelAnimationFrame(releaseFrame);+ };+ }, [navigationKey, suppressed]);++ return suppressed;+}+export function observeResponsiveBreakpointFade(options: {target: HTMLElement;container: HTMLElement;@@ -47,7 +74,8 @@ export function usePanelAnimationSettings(): {} {const durationMs = useClientSettings((settings) => settings.panelAnimationDurationMs);const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");- return { active: durationMs > 0 && !prefersReducedMotion, durationMs };+ const suppressed = useContext(PanelAnimationSuppressionContext);+ return { active: durationMs > 0 && !prefersReducedMotion && !suppressed, durationMs };}/** Keeps closing panel content mounted until its opt-in transition ends. */