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:
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user