fix: header 控件区布局抖动清零(CLS 修复)

- ModeToggle:旧实现在 isClient 前完全不渲染(SSR 无按钮 → hydrate 后
  凭空多出 40px)。改为恒渲染固定 40px 按钮:挂载前显示 Sun 占位图标,
  挂载后切换当前主题图标(动画保留)
- UserMenu:isPending 时渲染 24px 小圆(与终态 135px 差距大)。改为
  与未登录终态同尺寸骨架:Sign in 透明文字(带 muted 底色块)+
  Sign Up 半透明胶囊,右缘与终态精确对齐,占位色跟随各站主题
- SSR HTML 验证:首帧即含 Toggle theme 占位按钮 / Select language /
  UserMenu aria-hidden 骨架(curl 实证)
- 逐帧截图验收:commit 时刻与 hydrate 后 header 右上角布局一致;
  Language/Toggle x 坐标全程稳定
This commit is contained in:
AI Directory Platform
2026-09-10 09:29:17 +00:00
parent c50b5a6c5e
commit e382f2d4c4
5 changed files with 62 additions and 59 deletions
+47 -49
View File
@@ -2,10 +2,16 @@
import * as React from "react"; import * as React from "react";
import { useTheme } from "next-themes"; import { useTheme } from "next-themes";
import { MoonIcon } from "lucide-react"; import { MoonIcon, SunMedium } from "lucide-react";
import { IconSunLow } from "@tabler/icons-react"; import { IconSunLow } from "@tabler/icons-react";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
/**
* # 组件 ModeToggle
* 说明:深浅模式切换按钮。按钮**恒渲染**(固定 40px)——挂载前显示 Sun 占位图标
* (next-themes 挂载前主题未知,图标以占位代替),挂载后切换为当前主题图标。
* 消除旧实现(isClient 前不渲染)导致的 header 宽度抖动(CLS)。
*/
export function ModeToggle() { export function ModeToggle() {
const { theme, setTheme } = useTheme(); const { theme, setTheme } = useTheme();
@@ -15,56 +21,48 @@ export function ModeToggle() {
setIsClient(true); setIsClient(true);
}, []); }, []);
return ( // 挂载前:固定占位图标(与真实按钮同尺寸,无布局位移)
isClient && ( if (!isClient) {
return (
<button <button
onClick={() => { aria-label="Toggle theme"
theme === "dark" ? setTheme("light") : setTheme("dark"); className="w-10 h-10 flex hover:bg-hover rounded-lg items-center justify-center outline-none focus:outline-none active:outline-none overflow-hidden"
}}
className="w-10 h-10 flex hover:bg-hover rounded-lg items-center justify-center outline-none focus:ring-0 focus:outline-none active:ring-0 active:outline-none overflow-hidden"
> >
{theme === "light" && ( <SunMedium className="h-4 w-4 flex-shrink-0 text-muted-foreground" />
<motion.div
key={theme}
initial={{
x: 40,
opacity: 0,
}}
animate={{
x: 0,
opacity: 1,
}}
transition={{
duration: 0.3,
ease: "easeOut",
}}
>
<IconSunLow className="h-4 w-4 flex-shrink-0 text-muted-foreground" />
</motion.div>
)}
{theme === "dark" && (
<motion.div
key={theme}
initial={{
x: 40,
opacity: 0,
}}
animate={{
x: 0,
opacity: 1,
}}
transition={{
ease: "easeOut",
duration: 0.3,
}}
>
<MoonIcon className="h-4 w-4 flex-shrink-0 text-muted-foreground" />
</motion.div>
)}
<span className="sr-only">Toggle theme</span>
</button> </button>
) );
}
return (
<button
onClick={() => {
theme === "dark" ? setTheme("light") : setTheme("dark");
}}
className="w-10 h-10 flex hover:bg-hover rounded-lg items-center justify-center outline-none focus:ring-0 focus:outline-none active:ring-0 active:outline-none overflow-hidden"
>
{theme === "light" && (
<motion.div
key={theme}
initial={{ x: 40, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ duration: 0.3, ease: "easeOut" }}
>
<IconSunLow className="h-4 w-4 flex-shrink-0 text-muted-foreground" />
</motion.div>
)}
{theme === "dark" && (
<motion.div
key={theme}
initial={{ x: 40, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ ease: "easeOut", duration: 0.3 }}
>
<MoonIcon className="h-4 w-4 flex-shrink-0 text-muted-foreground" />
</motion.div>
)}
<span className="sr-only">Toggle theme</span>
</button>
); );
} }
@@ -35,8 +35,16 @@ export function UserMenu() {
}, [session.data?.user?.id]); }, [session.data?.user?.id]);
if (session.isPending) { if (session.isPending) {
// 占位与"未登录"终态同尺寸(Sign in 文字 + Sign Up 按钮),消除宽度抖动
return ( return (
<div className="h-6 w-6 rounded-full bg-muted animate-pulse" /> <div className="flex items-center gap-2" aria-hidden>
<span className="relative inline-block h-5 w-12 rounded bg-muted-foreground/25 overflow-hidden select-none">
<span className="absolute inset-0 text-transparent">{t('common.actions.signIn')}</span>
</span>
<span className="bg-primary/40 text-primary-foreground text-sm px-4 py-1.5 rounded-full select-none">
<span className="text-transparent">{t('common.actions.signUp')}</span>
</span>
</div>
); );
} }
+2 -3
View File
@@ -15,7 +15,7 @@
"moduleResolution": "bundler", "moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "react-jsx", "jsx": "preserve",
"incremental": true, "incremental": true,
"plugins": [ "plugins": [
{ {
@@ -55,8 +55,7 @@
"next-env.d.ts", "next-env.d.ts",
"**/*.ts", "**/*.ts",
"**/*.tsx", "**/*.tsx",
".next/types/**/*.ts", ".next/types/**/*.ts"
".next/dev/types/**/*.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules"
+2 -3
View File
@@ -15,7 +15,7 @@
"moduleResolution": "bundler", "moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "react-jsx", "jsx": "preserve",
"incremental": true, "incremental": true,
"plugins": [ "plugins": [
{ {
@@ -55,8 +55,7 @@
"next-env.d.ts", "next-env.d.ts",
"**/*.ts", "**/*.ts",
"**/*.tsx", "**/*.tsx",
".next/types/**/*.ts", ".next/types/**/*.ts"
".next/dev/types/**/*.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules"
+2 -3
View File
@@ -15,7 +15,7 @@
"moduleResolution": "bundler", "moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "react-jsx", "jsx": "preserve",
"incremental": true, "incremental": true,
"plugins": [ "plugins": [
{ {
@@ -55,8 +55,7 @@
"next-env.d.ts", "next-env.d.ts",
"**/*.ts", "**/*.ts",
"**/*.tsx", "**/*.tsx",
".next/types/**/*.ts", ".next/types/**/*.ts"
".next/dev/types/**/*.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules"