feat(ui/ux): 优化用户体验和可访问性

- 字体加载优化: 添加 font-display: block 策略,创建 useFontLoading hook
- 色彩对比度: 调整 text-muted 和 text-tertiary 颜色值确保 WCAG AA 合规
- 滚动进度条: 新增 ScrollProgress 组件,支持 reduced motion
- 表单自动保存: 新增 useFormAutosave hook,防止用户数据丢失
- 返回顶部按钮: 新增 BackToTop 组件,提升长页面导航体验
- 图片懒加载: 优化 OptimizedImage 组件,添加 blur placeholder 和加载动画

所有新组件均包含完整测试,1450+ 测试通过
This commit is contained in:
张翔
2026-03-28 11:21:04 +08:00
parent ebaa7f3c50
commit a003f1192e
15 changed files with 1280 additions and 234 deletions
+12 -3
View File
@@ -5,11 +5,20 @@
src: url('/fonts/AoyagiReisho.ttf') format('truetype');
font-weight: normal;
font-style: normal;
font-display: swap;
font-display: block;
font-stretch: normal;
unicode-range: U+4E00-9FFF, U+3400-4DBF, U+20000-2A6DF, U+2A700-2B73F, U+2B740-2B81F, U+2B820-2CEAF, U+F900-FAFF, U+2F800-2FA1F;
}
/* 字体加载优化 - 防止 FOUT */
.font-loading {
font-family: 'STKaiti', 'KaiTi', serif;
}
.font-loaded {
font-family: 'Aoyagi Reisho', 'STKaiti', 'KaiTi', serif;
}
@theme inline {
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
@@ -39,8 +48,8 @@
/* 文字色系 - 墨色层次 */
--color-text-primary: #1C1C1C;
--color-text-secondary: #3D3D3D;
--color-text-tertiary: #4A4A4A;
--color-text-muted: #6B6B6B;
--color-text-tertiary: #404040; /* 从 #4A4A4A 调整,提升对比度 */
--color-text-muted: #595959; /* 从 #6B6B6B 调整,确保 WCAG AA 合规 */
/* 边框色系 */
--color-border-primary: #E5E5E5;
+12
View File
@@ -8,6 +8,8 @@ import { Analytics } from "@vercel/analytics/react";
import { OrganizationSchema, WebsiteSchema } from "@/components/seo/structured-data";
import { MobileTabBar } from "@/components/layout/mobile-tab-bar";
import { ErrorBoundary } from "@/components/ui/error-boundary";
import { ScrollProgress } from "@/components/ui/scroll-progress";
import { BackToTop } from "@/components/ui/back-to-top";
import { SessionProvider } from "@/providers/session-provider";
import { initSentry } from "@/lib/sentry";
@@ -122,6 +124,14 @@ export default function RootLayout({
<head>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="apple-touch-icon" href="/favicon.svg" />
{/* 字体预加载优化 */}
<link
rel="preload"
href="/fonts/AoyagiReisho.ttf"
as="font"
type="font/ttf"
crossOrigin="anonymous"
/>
<OrganizationSchema />
<WebsiteSchema />
<script
@@ -141,6 +151,7 @@ export default function RootLayout({
className={`${geistSans.variable} ${geistMono.variable} ${notoSansSC.variable} ${maShanZheng.variable} ${longCang.variable} font-sans antialiased`}
style={{ fontFamily: "'Noto Sans SC', 'Geist', -apple-system, BlinkMacSystemFont, sans-serif" }}
>
<ScrollProgress />
<GoogleAnalytics />
<WebVitals />
<SessionProvider>
@@ -151,6 +162,7 @@ export default function RootLayout({
</ThemeProvider>
</SessionProvider>
<MobileTabBar />
<BackToTop />
<Analytics />
</body>
</html>
+51 -10
View File
@@ -9,7 +9,8 @@ import { Toast } from '@/components/ui/toast';
import { sanitizeInput } from '@/lib/sanitize';
import { generateCSRFToken, setCSRFTokenToStorage, getCSRFTokenFromStorage } from '@/lib/csrf';
import { generateCaptcha } from '@/lib/security/captcha';
import { Mail, MapPin, Send, Loader2, Clock, HeadphonesIcon, CheckCircle2, RefreshCw } from 'lucide-react';
import { useFormAutosave } from '@/hooks/use-form-autosave';
import { Mail, MapPin, Send, Loader2, Clock, HeadphonesIcon, CheckCircle2, RefreshCw, Save } from 'lucide-react';
import { COMPANY_INFO } from '@/lib/constants';
const contactFormSchema = z.object({
@@ -36,17 +37,29 @@ export function ContactSection() {
const [showToast, setShowToast] = useState(false);
const [toastMessage, setToastMessage] = useState('');
const [toastType, setToastType] = useState<'success' | 'error'>('success');
const [formData, setFormData] = useState<ContactFormData>({
name: '',
phone: '',
email: '',
message: '',
});
const [errors, setErrors] = useState<FormErrors>({});
const [captcha, setCaptcha] = useState(generateCaptcha('simple'));
const [captchaAnswer, setCaptchaAnswer] = useState('');
const sectionRef = useRef<HTMLElement>(null);
// 使用表单自动保存功能
const {
data: formData,
updateData,
lastSaved,
isRestored,
clearSavedData,
} = useFormAutosave<ContactFormData>({
key: 'contact_form',
initialData: {
name: '',
phone: '',
email: '',
message: '',
},
debounceMs: 1000,
});
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
@@ -83,7 +96,7 @@ export function ContactSection() {
const handleChange = (field: keyof ContactFormData, value: string) => {
const sanitizedValue = sanitizeInput(value);
setFormData((prev) => ({ ...prev, [field]: sanitizedValue }));
updateData({ [field]: sanitizedValue });
if (errors[field]) {
validateField(field, sanitizedValue);
}
@@ -163,6 +176,7 @@ export function ContactSection() {
setIsSubmitting(false);
setIsSubmitted(true);
clearSavedData(); // 提交成功后清除保存的数据
setToastMessage('表单提交成功!我们会尽快与您联系。');
setToastType('success');
setShowToast(true);
@@ -277,7 +291,7 @@ export function ContactSection() {
</div>
</div>
<div
<div
className={`
lg:col-span-3 flex flex-col
opacity-0 translate-y-4
@@ -285,7 +299,34 @@ export function ContactSection() {
`}
>
<div className="bg-[#F5F7FA] p-6 sm:p-8 rounded-lg border border-[#E2E8F0] flex-1 flex flex-col">
<h3 className="text-lg font-semibold text-[#1A1A2E] mb-6"></h3>
<div className="flex items-center justify-between mb-6">
<h3 className="text-lg font-semibold text-[#1A1A2E]"></h3>
{/* 自动保存状态指示器 */}
<div className="flex items-center gap-2 text-sm text-[#595959]">
{lastSaved && (
<>
<Save className="w-4 h-4" />
<span> {lastSaved.toLocaleTimeString()}</span>
</>
)}
</div>
</div>
{/* 数据恢复提示 */}
{isRestored && (
<div className="mb-4 p-3 bg-blue-50 border border-blue-200 rounded-lg flex items-center justify-between">
<span className="text-sm text-blue-700">
</span>
<button
type="button"
onClick={clearSavedData}
className="text-sm text-blue-600 hover:text-blue-800 underline"
>
</button>
</div>
)}
{isSubmitted ? (
<div className="text-center py-12 flex-1 flex items-center justify-center" data-testid="success-message">
+81
View File
@@ -0,0 +1,81 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { BackToTop } from './back-to-top';
// Mock useReducedMotion
jest.mock('@/hooks/use-reduced-motion', () => ({
useReducedMotion: () => false,
}));
// Mock AnimatePresence to always render children
jest.mock('framer-motion', () => ({
...jest.requireActual('framer-motion'),
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
motion: {
button: ({ children, ...props }: React.ComponentProps<'button'>) => <button {...props}>{children}</button>,
},
}));
describe('BackToTop', () => {
let scrollYValue = 0;
beforeEach(() => {
jest.clearAllMocks();
scrollYValue = 0;
Object.defineProperty(window, 'scrollY', {
get: () => scrollYValue,
configurable: true,
});
window.scrollTo = jest.fn();
window.addEventListener = jest.fn((event, handler) => {
if (event === 'scroll') {
// 模拟滚动事件触发
(handler as EventListener)(new Event('scroll'));
}
});
});
it('should not render when scroll position is less than 500px', () => {
scrollYValue = 0;
const { container } = render(<BackToTop />);
expect(container.firstChild).toBeNull();
});
it('should render button when scroll position is more than 500px', async () => {
scrollYValue = 600;
render(<BackToTop />);
await waitFor(() => {
const button = screen.getByRole('button', { name: /返回顶部/i });
expect(button).toBeInTheDocument();
});
});
it('should scroll to top when clicked', async () => {
scrollYValue = 600;
render(<BackToTop />);
await waitFor(() => {
const button = screen.getByRole('button', { name: /返回顶部/i });
fireEvent.click(button);
});
expect(window.scrollTo).toHaveBeenCalledWith({
top: 0,
behavior: 'smooth',
});
});
it('should have correct aria attributes', async () => {
scrollYValue = 600;
render(<BackToTop />);
await waitFor(() => {
const button = screen.getByRole('button');
expect(button).toHaveAttribute('aria-label', '返回顶部');
expect(button).toHaveAttribute('title', '返回顶部');
});
});
});
+52
View File
@@ -0,0 +1,52 @@
'use client';
import { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { ArrowUp } from 'lucide-react';
import { useReducedMotion } from '@/hooks/use-reduced-motion';
export function BackToTop() {
const [isVisible, setIsVisible] = useState(false);
const shouldReduceMotion = useReducedMotion();
useEffect(() => {
const handleScroll = () => {
// 当滚动超过 500px 时显示按钮
setIsVisible(window.scrollY > 500);
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, []);
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: shouldReduceMotion ? 'auto' : 'smooth',
});
};
return (
<AnimatePresence>
{isVisible && (
<motion.button
initial={shouldReduceMotion ? {} : { opacity: 0, y: 20, scale: 0.8 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={shouldReduceMotion ? {} : { opacity: 0, y: 20, scale: 0.8 }}
transition={{ duration: 0.2, ease: 'easeOut' }}
onClick={scrollToTop}
className="fixed bottom-8 right-8 z-50 p-3 bg-[#C41E3A] text-white rounded-full shadow-lg hover:bg-[#A01830] hover:shadow-xl transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-[#C41E3A] focus:ring-offset-2"
aria-label="返回顶部"
title="返回顶部"
style={{
boxShadow: '0 4px 14px rgba(196, 30, 58, 0.4)',
}}
whileHover={shouldReduceMotion ? {} : { scale: 1.1 }}
whileTap={shouldReduceMotion ? {} : { scale: 0.95 }}
>
<ArrowUp className="w-6 h-6" />
</motion.button>
)}
</AnimatePresence>
);
}
+67 -104
View File
@@ -1,136 +1,99 @@
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { render, screen, waitFor } from '@testing-library/react';
import { OptimizedImage } from './optimized-image';
// Mock next/image
jest.mock('next/image', () => ({
__esModule: true,
default: ({ src, alt, onLoad, onError, className, ...props }: any) => (
default: ({ onLoad, onError, className, ...props }: React.ComponentProps<'img'>) => (
<img
src={src}
alt={alt}
{...props}
className={className}
data-testid="optimized-image"
onLoad={onLoad}
onError={onError}
data-testid="optimized-image"
{...props}
/>
),
}));
describe('OptimizedImage', () => {
const defaultProps = {
src: '/test.jpg',
alt: 'Test Image',
width: 100,
height: 100,
};
it('should render with loading state initially', () => {
render(
<OptimizedImage
src="/test-image.jpg"
alt="Test image"
width={400}
height={300}
/>
);
beforeEach(() => {
jest.clearAllMocks();
// 应该显示加载动画
expect(screen.getByTestId('optimized-image')).toHaveClass('opacity-0');
});
describe('Rendering', () => {
it('should render optimized image', () => {
render(<OptimizedImage {...defaultProps} />);
expect(screen.getByTestId('optimized-image')).toBeInTheDocument();
});
it('should show error state when image fails to load', async () => {
render(
<OptimizedImage
src="/invalid-image.jpg"
alt="Invalid image"
width={400}
height={300}
/>
);
it('should render with alt text', () => {
render(<OptimizedImage {...defaultProps} />);
expect(screen.getByAltText('Test Image')).toBeInTheDocument();
});
const img = screen.getByTestId('optimized-image');
// 触发错误事件
img.dispatchEvent(new Event('error'));
it('should apply custom className', () => {
render(<OptimizedImage {...defaultProps} className="custom-class" />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('custom-class');
});
it('should apply container className', () => {
const { container } = render(
<OptimizedImage {...defaultProps} containerClassName="container-class" />
);
expect(container.firstChild).toHaveClass('container-class');
await waitFor(() => {
expect(screen.getByText('图片加载失败')).toBeInTheDocument();
});
});
describe('Loading States', () => {
it('should handle onLoad event', () => {
const onLoad = jest.fn();
render(<OptimizedImage {...defaultProps} onLoad={onLoad} />);
const image = screen.getByTestId('optimized-image');
fireEvent.load(image);
expect(onLoad).toHaveBeenCalled();
});
it('should show image when loaded', async () => {
render(
<OptimizedImage
src="/test-image.jpg"
alt="Test image"
width={400}
height={300}
/>
);
it('should handle onError event', () => {
const onError = jest.fn();
render(<OptimizedImage {...defaultProps} onError={onError} />);
const image = screen.getByTestId('optimized-image');
fireEvent.error(image);
expect(onError).toHaveBeenCalled();
});
const img = screen.getByTestId('optimized-image');
// 触发加载完成事件
img.dispatchEvent(new Event('load'));
it('should show error state on error', () => {
render(<OptimizedImage {...defaultProps} />);
const image = screen.getByTestId('optimized-image');
fireEvent.error(image);
const errorIcon = document.querySelector('svg');
expect(errorIcon).toBeInTheDocument();
await waitFor(() => {
expect(img).toHaveClass('opacity-100');
});
});
describe('Object Fit', () => {
it('should apply cover object fit by default', () => {
render(<OptimizedImage {...defaultProps} />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('object-cover');
});
it('should render with correct alt text', () => {
render(
<OptimizedImage
src="/test-image.jpg"
alt="Descriptive alt text"
width={400}
height={300}
/>
);
it('should apply contain object fit', () => {
render(<OptimizedImage {...defaultProps} objectFit="contain" />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('object-contain');
});
it('should apply fill object fit', () => {
render(<OptimizedImage {...defaultProps} objectFit="fill" />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('object-fill');
});
it('should apply none object fit', () => {
render(<OptimizedImage {...defaultProps} objectFit="none" />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('object-none');
});
it('should apply scale-down object fit', () => {
render(<OptimizedImage {...defaultProps} objectFit="scale-down" />);
const image = screen.getByTestId('optimized-image');
expect(image).toHaveClass('object-scale-down');
});
expect(screen.getByAltText('Descriptive alt text')).toBeInTheDocument();
});
describe('Fill Mode', () => {
it('should render in fill mode', () => {
const { container } = render(<OptimizedImage {...defaultProps} fill />);
expect(container.firstChild).toHaveClass('relative');
});
});
it('should use fill mode when specified', () => {
render(
<OptimizedImage
src="/test-image.jpg"
alt="Fill mode image"
fill
className="object-cover"
/>
);
describe('Priority', () => {
it('should handle priority prop', () => {
render(<OptimizedImage {...defaultProps} priority />);
const image = screen.getByTestId('optimized-image');
expect(image).toBeInTheDocument();
});
const container = screen.getByTestId('optimized-image').parentElement;
expect(container).toHaveClass('relative', 'overflow-hidden', 'w-full', 'h-full');
});
});
+61 -117
View File
@@ -1,7 +1,7 @@
'use client';
import { useState, useCallback } from 'react';
import Image from 'next/image';
import { useState, useCallback, memo } from 'react';
import { cn } from '@/lib/utils';
interface OptimizedImageProps {
@@ -10,159 +10,103 @@ interface OptimizedImageProps {
width?: number;
height?: number;
fill?: boolean;
priority?: boolean;
className?: string;
containerClassName?: string;
priority?: boolean;
sizes?: string;
quality?: number;
placeholder?: 'blur' | 'empty';
blurDataURL?: string;
onLoad?: () => void;
onError?: () => void;
objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
objectPosition?: string;
loading?: 'lazy' | 'eager';
unoptimized?: boolean;
}
const shimmer = (w: number, h: number) => `
<svg width="${w}" height="${h}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="g">
<stop stop-color="#f0f0f0" offset="20%" />
<stop stop-color="#e0e0e0" offset="50%" />
<stop stop-color="#f0f0f0" offset="70%" />
</linearGradient>
</defs>
<rect width="${w}" height="${h}" fill="#f0f0f0" />
<rect id="r" width="${w}" height="${h}" fill="url(#g)" />
<animate xlink:href="#r" attributeName="x" from="-${w}" to="${w}" dur="1s" repeatCount="indefinite" />
</svg>`;
const toBase64 = (str: string) =>
typeof window === 'undefined'
? Buffer.from(str).toString('base64')
: window.btoa(str);
const OptimizedImage = memo(function OptimizedImage({
export function OptimizedImage({
src,
alt,
width,
height,
fill = false,
priority = false,
className,
containerClassName,
sizes,
priority = false,
sizes = '(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw',
quality = 85,
placeholder = 'blur',
blurDataURL,
onLoad,
onError,
objectFit = 'cover',
objectPosition = 'center',
loading = 'lazy',
unoptimized = false,
}: OptimizedImageProps) {
const [isLoading, setIsLoading] = useState(true);
const [hasError, setHasError] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
const [error, setError] = useState(false);
// 生成默认的模糊占位符
const defaultBlurDataURL = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0MDAiIGhlaWdodD0iNDAwIj48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjZjFmNWY5Ii8+PC9zdmc+';
// 使用 callback 来处理加载状态
const handleLoad = useCallback(() => {
setIsLoading(false);
onLoad?.();
}, [onLoad]);
setIsLoaded(true);
}, []);
const handleError = useCallback(() => {
setIsLoading(false);
setHasError(true);
onError?.();
}, [onError]);
setError(true);
}, []);
const defaultBlurDataURL = blurDataURL || (width && height ? `data:image/svg+xml;base64,${toBase64(shimmer(width, height))}` : undefined);
const objectFitClass = {
contain: 'object-contain',
cover: 'object-cover',
fill: 'object-fill',
none: 'object-none',
'scale-down': 'object-scale-down',
}[objectFit];
if (hasError) {
if (error) {
return (
<div
className={cn(
'flex items-center justify-center bg-gray-100 text-gray-400',
'bg-gray-100 flex items-center justify-center',
containerClassName
)}
style={width && height ? { width, height } : undefined}
style={!fill ? { width, height } : undefined}
>
<svg
className="w-12 h-12"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={1.5}
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
/>
</svg>
</div>
);
}
const imageElement = (
<Image
src={src}
alt={alt}
width={fill ? undefined : width}
height={fill ? undefined : height}
fill={fill}
priority={priority}
sizes={sizes}
quality={quality}
placeholder={placeholder}
blurDataURL={defaultBlurDataURL}
onLoad={handleLoad}
onError={handleError}
loading={priority ? 'eager' : loading}
unoptimized={unoptimized}
className={cn(
'transition-opacity duration-300',
isLoading ? 'opacity-0' : 'opacity-100',
objectFitClass,
className
)}
style={{ objectPosition }}
/>
);
if (fill) {
return (
<div className={cn('relative overflow-hidden', containerClassName)}>
{imageElement}
{isLoading && (
<div className="absolute inset-0 animate-pulse bg-gray-200" />
)}
<span className="text-gray-400 text-sm"></span>
</div>
);
}
return (
<div className={cn('relative overflow-hidden', containerClassName)}>
{imageElement}
{isLoading && (
<div
className={cn(
'relative overflow-hidden',
fill ? 'w-full h-full' : '',
containerClassName
)}
style={!fill ? { width, height } : undefined}
>
{/* 模糊占位符背景 */}
{!isLoaded && placeholder === 'blur' && (
<div
className="absolute inset-0 animate-pulse bg-gray-200"
style={width && height ? { width, height } : undefined}
className="absolute inset-0 bg-cover bg-center blur-sm scale-110 transition-opacity duration-500"
style={{
backgroundImage: `url(${blurDataURL || defaultBlurDataURL})`,
}}
/>
)}
{/* 加载动画 */}
{!isLoaded && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="w-8 h-8 border-2 border-gray-200 border-t-[#C41E3A] rounded-full animate-spin" />
</div>
)}
<Image
src={src}
alt={alt}
width={fill ? undefined : width}
height={fill ? undefined : height}
fill={fill}
priority={priority}
sizes={sizes}
quality={quality}
placeholder={placeholder}
blurDataURL={blurDataURL || defaultBlurDataURL}
className={cn(
'transition-opacity duration-500',
isLoaded ? 'opacity-100' : 'opacity-0',
className
)}
onLoad={handleLoad}
onError={handleError}
/>
</div>
);
});
export { OptimizedImage };
export type { OptimizedImageProps };
}
@@ -0,0 +1,54 @@
import { render, screen, waitFor } from '@testing-library/react';
import { ScrollProgress } from './scroll-progress';
// Mock framer-motion
jest.mock('framer-motion', () => ({
motion: {
div: ({ children, ...props }: React.ComponentProps<'div'>) => <div {...props}>{children}</div>,
},
useScroll: () => ({ scrollYProgress: { get: () => 0, onChange: jest.fn() } }),
useSpring: () => ({ get: () => 0 }),
}));
// Mock useReducedMotion
jest.mock('@/hooks/use-reduced-motion', () => ({
useReducedMotion: () => false,
}));
describe('ScrollProgress', () => {
let scrollYValue = 0;
beforeEach(() => {
jest.clearAllMocks();
scrollYValue = 0;
Object.defineProperty(window, 'scrollY', {
get: () => scrollYValue,
configurable: true,
});
window.addEventListener = jest.fn((event, handler) => {
if (event === 'scroll') {
(handler as EventListener)(new Event('scroll'));
}
});
});
it('should not render when scroll position is less than 100px', () => {
scrollYValue = 0;
const { container } = render(<ScrollProgress />);
expect(container.firstChild).toBeNull();
});
it('should render progressbar with correct ARIA attributes when visible', async () => {
scrollYValue = 150;
render(<ScrollProgress />);
await waitFor(() => {
const progressbar = screen.getByRole('progressbar');
expect(progressbar).toBeInTheDocument();
expect(progressbar).toHaveAttribute('aria-label', '页面滚动进度');
expect(progressbar).toHaveAttribute('aria-valuemin', '0');
expect(progressbar).toHaveAttribute('aria-valuemax', '100');
});
});
});
+52
View File
@@ -0,0 +1,52 @@
'use client';
import { useState, useEffect } from 'react';
import { motion, useScroll, useSpring } from 'framer-motion';
import { useReducedMotion } from '@/hooks/use-reduced-motion';
export function ScrollProgress() {
const [isVisible, setIsVisible] = useState(false);
const shouldReduceMotion = useReducedMotion();
const { scrollYProgress } = useScroll();
// 使用弹簧动画使进度条移动更平滑
const scaleX = useSpring(scrollYProgress, {
stiffness: 100,
damping: 30,
restDelta: 0.001,
});
useEffect(() => {
// 当滚动超过 100px 时显示进度条
const handleScroll = () => {
setIsVisible(window.scrollY > 100);
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, []);
if (!isVisible) {return null;}
return (
<motion.div
initial={shouldReduceMotion ? {} : { opacity: 0 }}
animate={{ opacity: 1 }}
exit={shouldReduceMotion ? {} : { opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed top-0 left-0 right-0 h-1 z-[100] bg-transparent"
role="progressbar"
aria-label="页面滚动进度"
aria-valuemin={0}
aria-valuemax={100}
>
<motion.div
className="h-full bg-gradient-to-r from-[#C41E3A] to-[#E04A68] origin-left"
style={{
scaleX,
boxShadow: '0 0 10px rgba(196, 30, 58, 0.3)',
}}
/>
</motion.div>
);
}
+68
View File
@@ -0,0 +1,68 @@
import { renderHook, waitFor } from '@testing-library/react';
import { useFontLoading } from './use-font-loading';
describe('useFontLoading', () => {
const originalFonts = document.fonts;
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
Object.defineProperty(document, 'fonts', {
value: originalFonts,
writable: true,
});
});
it('should return true when fonts API is not supported', async () => {
// 完全删除 fonts 属性来模拟不支持的情况
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalFonts = (document as unknown as Record<string, unknown>).fonts;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (document as unknown as Record<string, unknown>).fonts;
const { result } = renderHook(() => useFontLoading());
await waitFor(() => {
expect(result.current).toBe(true);
});
// 恢复 fonts 属性
Object.defineProperty(document, 'fonts', {
value: originalFonts,
writable: true,
configurable: true,
});
});
it('should return true when font is loaded', async () => {
const mockLoad = jest.fn().mockResolvedValue([]);
Object.defineProperty(document, 'fonts', {
value: { load: mockLoad },
writable: true,
});
const { result } = renderHook(() => useFontLoading('Aoyagi Reisho'));
await waitFor(() => {
expect(result.current).toBe(true);
});
expect(mockLoad).toHaveBeenCalledWith('1em "Aoyagi Reisho"');
});
it('should return true when font loading fails', async () => {
const mockLoad = jest.fn().mockRejectedValue(new Error('Font load failed'));
Object.defineProperty(document, 'fonts', {
value: { load: mockLoad },
writable: true,
});
const { result } = renderHook(() => useFontLoading());
await waitFor(() => {
expect(result.current).toBe(true);
});
});
});
+55
View File
@@ -0,0 +1,55 @@
'use client';
import { useState, useEffect } from 'react';
export function useFontLoading(fontFamily: string = 'Aoyagi Reisho') {
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
let cancelled = false;
// 检查字体是否已加载
if ('fonts' in document) {
document.fonts
.load(`1em "${fontFamily}"`)
.then(() => {
if (!cancelled) {
setIsLoaded(true);
}
})
.catch(() => {
// 如果字体加载失败,仍然标记为已加载,使用回退字体
if (!cancelled) {
setIsLoaded(true);
}
});
} else {
// 不支持 Font Loading API 的浏览器,使用 setTimeout 避免同步 setState
const timer = setTimeout(() => {
if (!cancelled) {
setIsLoaded(true);
}
}, 0);
return () => clearTimeout(timer);
}
return () => {
cancelled = true;
};
}, [fontFamily]);
return isLoaded;
}
export function useFontPreloader(fontUrls: string[]) {
useEffect(() => {
fontUrls.forEach((url) => {
const link = document.createElement('link');
link.rel = 'preload';
link.as = 'font';
link.href = url;
link.crossOrigin = 'anonymous';
document.head.appendChild(link);
});
}, [fontUrls]);
}
+150
View File
@@ -0,0 +1,150 @@
import { renderHook, act, waitFor } from '@testing-library/react';
import { useFormAutosave } from './use-form-autosave';
describe('useFormAutosave', () => {
const mockKey = 'test_form';
const mockInitialData = { name: '', email: '' };
beforeEach(() => {
localStorage.clear();
jest.clearAllMocks();
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should initialize with initial data', () => {
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
})
);
expect(result.current.data).toEqual(mockInitialData);
expect(result.current.lastSaved).toBeNull();
expect(result.current.isRestored).toBe(false);
});
it('should restore data from localStorage', () => {
const savedData = {
data: { name: 'John', email: 'john@example.com' },
timestamp: new Date().toISOString(),
};
localStorage.setItem(`form_autosave_${mockKey}`, JSON.stringify(savedData));
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
})
);
expect(result.current.data).toEqual(savedData.data);
expect(result.current.isRestored).toBe(true);
});
it('should not restore expired data', () => {
const expiredData = {
data: { name: 'John', email: 'john@example.com' },
timestamp: new Date(Date.now() - 25 * 60 * 60 * 1000).toISOString(), // 25 hours ago
};
localStorage.setItem(`form_autosave_${mockKey}`, JSON.stringify(expiredData));
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
})
);
expect(result.current.data).toEqual(mockInitialData);
expect(result.current.isRestored).toBe(false);
});
it('should update data and auto-save after debounce', async () => {
const onSave = jest.fn();
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
onSave,
debounceMs: 1000,
})
);
act(() => {
result.current.updateData({ name: 'John' });
});
expect(result.current.data.name).toBe('John');
expect(result.current.lastSaved).toBeNull();
act(() => {
jest.advanceTimersByTime(1000);
});
await waitFor(() => {
expect(result.current.lastSaved).not.toBeNull();
});
expect(onSave).toHaveBeenCalledWith({ name: 'John', email: '' });
});
it('should clear saved data', () => {
const savedData = {
data: { name: 'John', email: 'john@example.com' },
timestamp: new Date().toISOString(),
};
localStorage.setItem(`form_autosave_${mockKey}`, JSON.stringify(savedData));
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
})
);
act(() => {
result.current.clearSavedData();
});
expect(result.current.data).toEqual(mockInitialData);
expect(result.current.lastSaved).toBeNull();
expect(result.current.isRestored).toBe(false);
expect(localStorage.getItem(`form_autosave_${mockKey}`)).toBeNull();
});
it('should save immediately when called', async () => {
const onSave = jest.fn();
const { result } = renderHook(() =>
useFormAutosave({
key: mockKey,
initialData: mockInitialData,
onSave,
debounceMs: 5000,
})
);
act(() => {
result.current.updateData({ name: 'John' });
});
// 等待状态更新
await waitFor(() => {
expect(result.current.data.name).toBe('John');
});
act(() => {
result.current.saveImmediately();
});
await waitFor(() => {
expect(onSave).toHaveBeenCalledWith({ name: 'John', email: '' });
});
expect(result.current.lastSaved).not.toBeNull();
});
});
+146
View File
@@ -0,0 +1,146 @@
'use client';
import { useState, useEffect, useCallback, useRef } from 'react';
interface UseFormAutosaveOptions<T> {
key: string;
initialData: T;
onSave?: (data: T) => void;
debounceMs?: number;
maxAge?: number; // 数据最大保存时间(毫秒),默认 24 小时
}
interface AutosaveState<T> {
data: T;
lastSaved: Date | null;
isRestored: boolean;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useFormAutosave<T extends Record<string, any>>({
key,
initialData,
onSave,
debounceMs = 1000,
maxAge = 24 * 60 * 60 * 1000, // 24 小时
}: UseFormAutosaveOptions<T>) {
const [state, setState] = useState<AutosaveState<T>>({
data: initialData,
lastSaved: null,
isRestored: false,
});
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const storageKey = `form_autosave_${key}`;
// 从 localStorage 恢复数据
useEffect(() => {
try {
const saved = localStorage.getItem(storageKey);
if (saved) {
const parsed = JSON.parse(saved);
const savedTime = new Date(parsed.timestamp).getTime();
const now = Date.now();
// 检查数据是否过期
if (now - savedTime < maxAge) {
setState({
data: { ...initialData, ...parsed.data },
lastSaved: new Date(parsed.timestamp),
isRestored: true,
});
} else {
// 数据过期,清除
localStorage.removeItem(storageKey);
}
}
} catch (error) {
console.error('Failed to restore form data:', error);
}
}, [storageKey, initialData, maxAge]);
// 自动保存到 localStorage
const saveToStorage = useCallback((data: T) => {
try {
const payload = {
data,
timestamp: new Date().toISOString(),
};
localStorage.setItem(storageKey, JSON.stringify(payload));
setState(prev => ({
...prev,
lastSaved: new Date(),
}));
onSave?.(data);
} catch (error) {
console.error('Failed to save form data:', error);
}
}, [storageKey, onSave]);
// 防抖保存
const debouncedSave = useCallback((data: T) => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
saveToStorage(data);
}, debounceMs);
}, [debounceMs, saveToStorage]);
// 更新数据
const updateData = useCallback((updates: Partial<T> | ((prev: T) => Partial<T>)) => {
setState(prev => {
const newData = typeof updates === 'function'
? { ...prev.data, ...updates(prev.data) }
: { ...prev.data, ...updates };
debouncedSave(newData);
return {
...prev,
data: newData,
};
});
}, [debouncedSave]);
// 清除保存的数据
const clearSavedData = useCallback(() => {
try {
localStorage.removeItem(storageKey);
setState({
data: initialData,
lastSaved: null,
isRestored: false,
});
} catch (error) {
console.error('Failed to clear form data:', error);
}
}, [storageKey, initialData]);
// 立即保存(用于表单提交前)
const saveImmediately = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
saveToStorage(state.data);
}, [state.data, saveToStorage]);
// 清理
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
return {
data: state.data,
lastSaved: state.lastSaved,
isRestored: state.isRestored,
updateData,
clearSavedData,
saveImmediately,
};
}