Просмотр исходного кода

fix: smooth layout reveal - disable grid transitions until settled

- Use double requestAnimationFrame to wait for correct layout to paint
- Disable react-grid-item transitions during initialization
- Fade in with CSS animation once layout is in correct position
- Prevents cards from visibly shrinking on mobile
- Prevents flash of empty content on desktop

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rriecc 9 месяцев назад
Родитель
Сommit
fbab6e6291
3 измененных файлов с 28 добавлено и 7 удалено
  1. 3 3
      components/home-client.tsx
  2. 11 4
      hooks/useWindowWidth.ts
  3. 14 0
      styles/globals.css

+ 3 - 3
components/home-client.tsx

@@ -84,7 +84,7 @@ export default function HomeClient({
   chatbotUrl,
   paperUrl,
 }: HomeClientProps) {
-  const { width, ready } = useWindowWidth();
+  const { width, settled } = useWindowWidth();
   const [tabSelected, setTabSelected] = useState("all");
   const [, startTransition] = useTransition();
   const router = useRouter();
@@ -128,8 +128,8 @@ export default function HomeClient({
       <Responsive
         breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
         className={cn(
-          "layout w-full h-full transition-opacity duration-300",
-          ready ? "opacity-100" : "opacity-0",
+          "layout w-full h-full",
+          settled ? "layout-settled" : "layout-initializing",
         )}
         cols={{ lg: 4, md: 4, sm: 2, xs: 2, xxs: 2 }}
         layouts={layouts[tabSelected]}

+ 11 - 4
hooks/useWindowWidth.ts

@@ -3,24 +3,31 @@ import { useState, useEffect } from "react";
 // Default to 1280 (max container width) for SSR so server renders meaningful HTML
 const SSR_DEFAULT_WIDTH = 1280;
 
-function useWindowWidth(): { width: number; ready: boolean } {
+function useWindowWidth(): { width: number; settled: boolean } {
   const [width, setWidth] = useState<number>(SSR_DEFAULT_WIDTH);
-  const [ready, setReady] = useState(false);
+  const [settled, setSettled] = useState(false);
 
   useEffect(() => {
     const handleResize = () => {
       setWidth(Math.min(window.innerWidth, 1280));
     };
 
+    // Set correct width immediately
     handleResize();
-    setReady(true);
+
+    // Wait for the layout to paint with the correct width, then reveal
+    requestAnimationFrame(() => {
+      requestAnimationFrame(() => {
+        setSettled(true);
+      });
+    });
 
     window.addEventListener("resize", handleResize);
 
     return () => window.removeEventListener("resize", handleResize);
   }, []);
 
-  return { width, ready };
+  return { width, settled };
 }
 
 export default useWindowWidth;

+ 14 - 0
styles/globals.css

@@ -39,6 +39,20 @@
   transition-duration: 400ms;
 }
 
+/* Hide grid and disable item transitions until layout settles with correct width */
+.layout-initializing {
+  opacity: 0;
+}
+
+.layout-initializing .react-grid-item {
+  transition: none !important;
+}
+
+/* Fade in smoothly once layout is correct */
+.layout-settled {
+  animation: fade 0.4s ease-out;
+}
+
 .react-grid-item > .react-resizable-handle {
   display: none;
 }