fix: 素材骨架改为 CSS 动画,避免构建预渲染访问 window

This commit is contained in:
chenpei 2026-08-17 12:04:35 +08:00
parent 32610b5527
commit 91b617bc2e

View File

@ -1,12 +1,8 @@
'use client'
import React, { useRef } from 'react'
import gsap from 'gsap'
import { useGSAP } from '@gsap/react'
import React from 'react'
import MaterialCard, { Material } from './MaterialCard'
gsap.registerPlugin(useGSAP)
// 用户会话类型
interface UserSession {
id: string
@ -49,33 +45,8 @@ interface MaterialGridProps {
}
function MaterialGridSkeleton() {
const rootRef = useRef<HTMLDivElement>(null)
useGSAP(() => {
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return
const motion = gsap.matchMedia()
motion.add('(prefers-reduced-motion: no-preference)', () => {
gsap.fromTo(
'[data-skeleton-block]',
{ opacity: 0.42 },
{
opacity: 0.92,
duration: 1.15,
ease: 'sine.inOut',
yoyo: true,
repeat: -1,
stagger: { each: 0.08, from: 'start' },
},
)
})
return () => motion.revert()
}, { scope: rootRef })
return (
<div
ref={rootRef}
className="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"
aria-busy="true"
aria-live="polite"
@ -86,13 +57,13 @@ function MaterialGridSkeleton() {
key={index}
className="overflow-hidden rounded-[14px] border border-[#e7e8ea] bg-white"
>
<div data-skeleton-block className="h-[400px] bg-[#eceef0]" />
<div className="h-[400px] animate-pulse bg-[#eceef0]" />
<div className="space-y-3 px-4 py-4">
<div data-skeleton-block className="h-4 w-3/4 bg-[#eceef0]" />
<div data-skeleton-block className="h-3 w-1/2 bg-[#f2f3f5]" />
<div className="h-4 w-3/4 animate-pulse bg-[#eceef0]" />
<div className="h-3 w-1/2 animate-pulse bg-[#f2f3f5]" />
<div className="flex items-center justify-between pt-1">
<div data-skeleton-block className="h-3 w-20 bg-[#f2f3f5]" />
<div data-skeleton-block className="h-3 w-12 bg-[#f2f3f5]" />
<div className="h-3 w-20 animate-pulse bg-[#f2f3f5]" />
<div className="h-3 w-12 animate-pulse bg-[#f2f3f5]" />
</div>
</div>
</article>