> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/components/loading.html

> For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/llms-full.txt.

# Loading 加载

提示内容正在加载或操作正在进行。


## 基础用法

在等待内容或操作完成时显示加载指示器。


basic.tsx

复制代码

```tsx lineNumbers file="../../src/examples/loading/basic.tsx"
import { Loading } from '@ifhover/leaf-ui';

export function LoadingBasic() {
  return <Loading />;
}
```

展开代码


## 示例

### 尺寸

通过 `size` 选择加载指示器的尺寸。


小中（默认）大

sizes.tsx

复制代码

```tsx lineNumbers file="../../src/examples/loading/sizes.tsx"
import { Flex, Grid, Loading, Text } from '@ifhover/leaf-ui';
import { Fragment } from 'react';

const sizes = [
  { value: 'sm', label: '小' },
  { value: 'md', label: '中（默认）' },
  { value: 'lg', label: '大' },
] as const;

export function LoadingSizes() {
  return (
    <Grid columns={{ xs: 1, sm: 2 }} gap={16} align="center">
      {sizes.map(({ value, label }) => (
        <Fragment key={value}>
          <Text tone="muted">{label}</Text>
          <Flex>
            <Loading size={value} />
          </Flex>
        </Fragment>
      ))}
    </Grid>
  );
}
```

展开代码

### 区域加载

传入 `children` 后覆盖内容区域，加载层可见时阻止子内容交互。通过 `spinning` 控制加载状态，使用 `tip` 补充提示。


结束加载

### 项目概览

在此查看项目进度。

编辑项目

正在读取项目

overlay.tsx

复制代码

```tsx lineNumbers file="../../src/examples/loading/overlay.tsx"
import { Button, Card, Flex, Loading, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function LoadingOverlay() {
  const [spinning, setSpinning] = useState(true);
  const [edited, setEdited] = useState(false);

  return (
    <Flex direction="column" gap="md">
      <Flex>
        <Button variant="outline" onClick={() => setSpinning((value) => !value)}>
          {spinning ? '结束加载' : '开始加载'}
        </Button>
      </Flex>
      <Loading spinning={spinning} tip="正在读取项目">
        <Card title="项目概览">
          <Flex direction="column" gap="md">
            <Text>在此查看项目进度。</Text>
            <Flex>
              <Button variant="outline" onClick={() => setEdited(true)}>
                编辑项目
              </Button>
            </Flex>
            {edited && <Text role="status">已打开项目编辑</Text>}
          </Flex>
        </Card>
      </Loading>
    </Flex>
  );
}
```

展开代码

### 延迟显示

经过 `delay` 毫秒后，若 `spinning` 仍为 `true`，才显示加载提示；在此之前结束加载则不显示。

`delay` 不会延迟请求，也不会在等待期间阻止内容交互。需要立即禁用的操作应单独处理。


开始加载

delay.tsx

复制代码

```tsx lineNumbers file="../../src/examples/loading/delay.tsx"
import { Button, Flex, Loading } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function LoadingDelay() {
  const [spinning, setSpinning] = useState(false);

  return (
    <Flex direction="column" gap="md" align="start">
      <Button onClick={() => setSpinning((value) => !value)}>
        {spinning ? '结束加载' : '开始加载'}
      </Button>
      <Loading spinning={spinning} delay={600} tip="正在加载" />
    </Flex>
  );
}
```

展开代码


## API

### Loading API

加载状态、提示内容与延迟显示的参数。

| 属性         | 类型                     | 默认值    | 说明          |
| ---------- | ---------------------- | ------ | ----------- |
| `spinning` | `boolean`              | `true` | 是否加载中       |
| `size`     | `'sm' \| 'md' \| 'lg'` | `'md'` | 图标尺寸        |
| `tip`      | `ReactNode`            | —      | 提示文字        |
| `delay`    | `number`               | `0`    | 延迟显示，单位毫秒   |
| `children` | `ReactNode`            | —      | 被加载遮罩覆盖的内容。 |
