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

# Alert

反馈`@rockshin/tao-ui`

[代码演示](#代码演示)[API](#api)

用于展示带有明确语义状态的重要上下文反馈信息。

## 何时使用

页面需要持续展示某个状态、风险或操作结果时使用 Alert；短暂通知应使用 Sonner。

**推荐**

- 根据消息严重程度选择对应的语义类型。
- 标题保持简短，将补充信息放入说明。
- 需要快速扫读时开启状态图标。

**避免**

- 避免在同一视图堆叠大量 Alert。
- 不要用 Alert 承载短暂反馈，改用 toast。

## 代码演示


### 基础用法
```tsx preview
import { Alert, Stack } from '@rockshin/tao-ui';

function AlertExample() {
  return (
    <Stack gap="md">
      <Alert type="success" title="Changes saved" showIcon />
      <Alert
        type="info"
        title="New version available"
        description="Refresh when you are ready."
        showIcon
      />
      <Alert type="warning" title="Subscription expires soon" showIcon />
      <Alert type="error" title="Could not save changes" showIcon />
    </Stack>
  );
}

export default AlertExample;
```

### 说明与图标
```tsx preview
import { Alert, Stack } from '@rockshin/tao-ui';

function DetailedAlerts() {
  return (
    <Stack gap="md">
      <Alert
        type="success"
        title="Deployment complete"
        description="Production is running version 0.0.25."
        showIcon
      />
      <Alert
        type="info"
        title="Update available"
        description="Refresh when you are ready."
        showIcon
      />
      <Alert
        type="warning"
        title="Quota almost reached"
        description="Storage is 92% full."
        showIcon
      />
      <Alert
        type="error"
        title="Build failed"
        description="Fix the type errors and run again."
        showIcon
      />
    </Stack>
  );
}

export default DetailedAlerts;
```

### 可关闭提示与操作
```tsx preview
import { Alert, Button, Stack } from '@rockshin/tao-ui';

function ActionAlerts() {
  return (
    <Stack gap="md">
      <Alert type="info" title="Closable message" closable showIcon />
      <Alert
        type="warning"
        title="Payment method expires soon"
        description="Update it before the next billing date."
        showIcon
        action={
          <Button size="small" variant="secondary">
            Update
          </Button>
        }
      />
      <Alert
        type="error"
        title="Connection lost"
        action={<Button size="small">Retry</Button>}
        closable
        showIcon
      />
    </Stack>
  );
}

export default ActionAlerts;
```

### 标题与详细说明
用 `description` 补充原因和恢复方式，让标题保持简短。
```tsx preview
import { Alert, Stack } from '@rockshin/tao-ui';

export default function AlertWithDescription() {
  return (
    <Stack gap="md">
      <Alert
        type="success"
        title="Success"
        description="Your changes have been saved successfully."
      />
      <Alert
        type="info"
        title="Info"
        description="A new version is available. Refresh to update."
      />
      <Alert
        type="warning"
        title="Warning"
        description="Your subscription expires in 3 days."
      />
      <Alert
        type="error"
        title="Error"
        description="Something went wrong while processing your request."
      />
    </Stack>
  );
}
```

### 状态图标
`showIcon` 为成功、信息、警告和错误提供对应的语义图标。
```tsx preview
import { Alert, Stack } from '@rockshin/tao-ui';

export default function AlertWithIcon() {
  return (
    <Stack gap="md">
      <Alert type="success" title="Success" showIcon />
      <Alert type="info" title="Info" showIcon />
      <Alert type="warning" title="Warning" showIcon />
      <Alert type="error" title="Error" showIcon />
      <Alert
        type="success"
        title="Success"
        description="Icons grow larger when a description is present."
        showIcon
      />
      <Alert
        type="warning"
        title="Custom Icon"
        showIcon
        icon={<span>🔥</span>}
      />
    </Stack>
  );
}
```

### 行内操作
通过 `action` 放置与提示相关的操作，操作结果应在应用中明确反馈。
```tsx preview
import { useState } from 'react';
import { Alert, Button, Stack } from '@rockshin/tao-ui';
export default function Example() {
  const [updated, setUpdated] = useState(false);
  const [retried, setRetried] = useState(false);
  return (
    <Stack>
      <Alert
        type={updated ? 'success' : 'info'}
        title={updated ? 'Demo update applied' : 'New version available'}
        showIcon
        action={
          <Button
            size="small"
            disabled={updated}
            onClick={() => setUpdated(true)}
          >
            Update
          </Button>
        }
      />
      <Alert
        type={retried ? 'success' : 'error'}
        title={retried ? 'Demo retry completed' : 'Request failed'}
        description="These actions update local example state."
        showIcon
        action={
          <Button
            size="small"
            disabled={retried}
            onClick={() => setRetried(true)}
          >
            Retry
          </Button>
        }
      />
    </Stack>
  );
}
```

## API


### [#](#alert)Alert



| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| className | 节点类名 | `string | undefined` | `-` |
| style | 节点样式 | `CSSProperties | undefined` | `-` |

