Skip to content
TypeScript

perf(web): panel presence keeps its retained value in state (#20459)

Julius Marmingea17781f63822
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index f97b38bfb83..5a16279017f 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -34,4 +34,5 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/state/sourceControlActions.ts",
"apps/web/src/components/settings/ProjectFaviconPickerDialog.tsx",
"apps/web/src/components/settings/ThemeEditorPanel.tsx",
+ "apps/web/src/panelAnimations.ts",
] as const;
diff --git a/apps/web/src/panelAnimations.ts b/apps/web/src/panelAnimations.ts
index e917fbca386..efbdfa526d5 100644
--- a/apps/web/src/panelAnimations.ts
+++ b/apps/web/src/panelAnimations.ts
@@ -1,4 +1,4 @@
-import { createContext, useContext, useEffect, useRef, useState } from "react";
+import { createContext, useContext, useEffect, useState } from "react";
import { type PanelAnimationDurationMs } from "@t3tools/contracts/settings";
import { useMediaQuery } from "./hooks/useMediaQuery";
@@ -45,30 +45,23 @@ export function usePanelPresence<T>(
durationMs: PanelAnimationDurationMs,
): { present: boolean; value: T | null } {
const [present, setPresent] = useState(open);
- const retainedRef = useRef<{ scopeKey: string | null; value: T | null } | null>(
+ const [retained, setRetained] = useState<{ scopeKey: string | null; value: T | null } | null>(
open ? { scopeKey, value } : null,
);
+ if (open && (retained?.scopeKey !== scopeKey || retained.value !== value)) {
+ setRetained({ scopeKey, value });
+ }
- useEffect(() => {
- if (open) retainedRef.current = { scopeKey, value };
- }, [open, scopeKey, value]);
+ if (open && !present) setPresent(true);
+ if (!open && !animated && present) setPresent(false);
useEffect(() => {
- if (open) {
- setPresent(true);
- return;
- }
- if (!animated) {
- setPresent(false);
- return;
- }
-
+ if (open || !animated) return;
const timeout = window.setTimeout(() => setPresent(false), durationMs);
return () => window.clearTimeout(timeout);
}, [animated, durationMs, open]);
- const retainedValue =
- retainedRef.current?.scopeKey === scopeKey ? retainedRef.current.value : null;
- const visible = open || (animated && present && retainedRef.current?.scopeKey === scopeKey);
+ const retainedValue = retained?.scopeKey === scopeKey ? retained.value : null;
+ const visible = open || (animated && present && retained?.scopeKey === scopeKey);
return { present: visible, value: open ? value : visible ? retainedValue : null };
}