Skip to content

Skeleton 骨架屏

在数据加载前展示占位图形,减少用户等待焦虑。

基础用法

tsx
import { Skeleton } from '@airo-ui/react'

function Demo() {
  return (
    <>
      <Skeleton width="200px" height="16px" />
      <Skeleton width="100%" height="200px" />
    </>
  )
}

圆角

tsx
import { Skeleton } from '@airo-ui/react'

function Demo() {
  return (
    <>
      <Skeleton width="48px" height="48px" rounded />
      <Skeleton width="200px" height="16px" rounded />
    </>
  )
}

动画

tsx
import { Skeleton } from '@airo-ui/react'

function Demo() {
  return (
    <>
      <Skeleton width="200px" height="16px" animated />
      <Skeleton width="100%" height="100px" animated rounded />
    </>
  )
}

包裹内容

children 存在时,骨架屏作为加载态的包裹层,内容加载完成后自动显示。

tsx
import { useState, useEffect } from 'react'
import { Skeleton } from '@airo-ui/react'

function Demo() {
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    const timer = setTimeout(() => setLoading(false), 2000)
    return () => clearTimeout(timer)
  }, [])

  if (loading) {
    return <Skeleton width="300px" height="60px" animated rounded />
  }

  return (
    <Skeleton>
      <div style={{ padding: 16 }}>
        <h3>内容已加载</h3>
        <p>这是实际的内容区域</p>
      </div>
    </Skeleton>
  )
}

API

Props

名称类型默认值说明
widthstring | number宽度
heightstring | number高度
roundedbooleanfalse是否圆角
animatedbooleantrue是否显示动画
childrenReactNode子内容(加载完成后显示)

MIT License