Skip to content
Lenso UI

Alert 警告

向用户展示重要消息与通知,并提供状态指示

用法

import { Alert } from '@lenso/ui';

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (e385ac202b2cdb94b1bf6fa76d32c31c8259cc5e), Apache-2.0.import { Alert, Button, CloseButton, Spinner } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";import { s } from "../card/display.stylex";
export function Basic() {  return (    <div {...stylex.props(s.alertStack)}>      <Alert>        <Alert.Indicator />        <Alert.Content>          <Alert.Title>New features available</Alert.Title>          <Alert.Description>            Check out our latest updates including dark mode support and improved accessibility            features.          </Alert.Description>        </Alert.Content>      </Alert>      <Alert status="accent">        <Alert.Indicator />        <Alert.Content>          <Alert.Title>Update available</Alert.Title>          <Alert.Description>            A new version of the application is available. Please refresh to get the latest features            and bug fixes.          </Alert.Description>          <Button xstyle={s.mobile2} size="sm" variant="primary">            Refresh          </Button>        </Alert.Content>        <Button xstyle={s.desktopBlock} size="sm" variant="primary">          Refresh        </Button>      </Alert>      <Alert status="danger">        <Alert.Indicator />        <Alert.Content>          <Alert.Title>Unable to connect to server</Alert.Title>          <Alert.Description>            We're experiencing connection issues. Please try the following:            <ul {...stylex.props(s.list)}>              <li>Check your internet connection</li>              <li {...stylex.props(s.space1)}>Refresh the page</li>              <li {...stylex.props(s.space1)}>Clear your browser cache</li>            </ul>          </Alert.Description>          <Button xstyle={s.mobile2} size="sm" variant="danger">            Retry          </Button>        </Alert.Content>        <Button xstyle={s.desktopBlock} size="sm" variant="danger">          Retry        </Button>      </Alert>      <Alert status="success">        <Alert.Indicator />        <Alert.Content>          <Alert.Title>Profile updated successfully</Alert.Title>        </Alert.Content>        <CloseButton />      </Alert>      <Alert status="accent">        <Alert.Indicator>          <Spinner size="sm" />        </Alert.Indicator>        <Alert.Content>          <Alert.Title>Processing your request</Alert.Title>          <Alert.Description>            Please wait while we sync your data. This may take a few moments.          </Alert.Description>        </Alert.Content>      </Alert>      <Alert status="warning">        <Alert.Indicator />        <Alert.Content>          <Alert.Title>Scheduled maintenance</Alert.Title>          <Alert.Description>            Our services will be unavailable on Sunday, March 15th from 2:00 AM to 6:00 AM UTC for            scheduled maintenance.          </Alert.Description>        </Alert.Content>      </Alert>    </div>  );}

Local adaptation source above. Derived from HeroUI v3.2.6 source.

组件结构

import { Alert } from '@lenso/ui';
export default () => (  <Alert>    <Alert.Indicator />    <Alert.Content>      <Alert.Title />      <Alert.Description />    </Alert.Content>  </Alert>)

自定义样式

Tailwind CSS

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (e385ac202b2cdb94b1bf6fa76d32c31c8259cc5e), Apache-2.0.import { Alert, Button, CloseButton } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";import { s } from "../card/display.stylex";
const styles = stylex.create({  alert: {    position: "relative",    overflow: "hidden",    borderRadius: 12,    borderWidth: 1,    borderStyle: "solid",    borderColor: {      default: "color-mix(in oklab, var(--warning) 20%, transparent)",      ':is([data-theme="dark"] *)': "color-mix(in oklab, var(--warning) 30%, transparent)",    },    backgroundImage: {      default:        "linear-gradient(to bottom right in oklab, color-mix(in oklab, var(--warning) 10%, transparent), var(--surface), var(--surface-secondary))",      ':is([data-theme="dark"] *)':        "linear-gradient(to bottom right in oklab, color-mix(in oklab, var(--warning) 15%, transparent), var(--surface), color-mix(in oklab, var(--warning) 5%, transparent))",    },    boxShadow: "0 1px 2px 0 rgb(0 0 0 / 0.05)",  },  glow: {    pointerEvents: "none",    position: "absolute",    top: -32,    right: -32,    width: 112,    height: 112,    borderRadius: 9999,    backgroundColor: {      default: "color-mix(in oklab, var(--warning) 15%, transparent)",      ':is([data-theme="dark"] *)': "color-mix(in oklab, var(--warning) 25%, transparent)",    },    filter: "blur(40px)",  },});export function CustomStyles() {  return (    <div {...stylex.props(s.maxXl)}>      <Alert xstyle={styles.alert} status="warning">        <div aria-hidden="true" {...stylex.props(styles.glow)} />        <Alert.Indicator xstyle={[s.relative, s.warning]} />        <Alert.Content xstyle={s.relative}>          <Alert.Title>Payment method expires soon</Alert.Title>          <Alert.Description>            Your Visa ending in 4242 expires on March 28. Update billing to avoid interrupting your            Pro subscription.          </Alert.Description>          <Button xstyle={s.mobile3} size="sm" variant="tertiary">            Update billing          </Button>        </Alert.Content>        <Button xstyle={s.desktopInline} size="sm" variant="tertiary">          Update billing        </Button>        <CloseButton xstyle={s.relative} />      </Alert>    </div>  );}

Local adaptation source above. Derived from HeroUI v3.2.6 source.

全局 CSS

要自定义 Alert 的组件类,可使用 @layer components 指令。 了解更多。

@layer components {  .alert {    @apply rounded-2xl shadow-lg;  }
  .alert__title {    @apply font-bold text-lg;  }
  .alert--danger {    @apply border-l-4 border-red-600;  }}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

CSS 类

Alert 使用以下 CSS 类(查看源码样式):

基础类 [!toc]

  • .alert - Alert 根容器
  • .alert__indicator - 图标/指示器容器
  • .alert__content - 包裹标题与说明的内容容器
  • .alert__title - Alert 标题文本
  • .alert__description - Alert 说明文本

状态变体类 [!toc]

  • .alert--default - 默认灰色状态
  • .alert--accent - 强调蓝色状态
  • .alert--success - 成功绿色状态
  • .alert--warning - 警告黄/橙色状态
  • .alert--danger - 危险红色状态

交互状态

Alert 主要用于信息展示,基础组件本身通常没有交互状态;但它可以包含按钮或关闭按钮等交互元素。

API 参考

Alert

Prop类型默认值描述
status"default" | "accent" | "success" | "warning" | "danger""default"Alert 的视觉状态
classNamestring-附加的 CSS 类
childrenReactNode-Alert 内容

Alert.Indicator

Prop类型默认值描述
classNamestring-附加的 CSS 类
childrenReactNode-自定义指示图标(默认显示状态图标)

Alert.Content

Prop类型默认值描述
classNamestring-附加的 CSS 类
childrenReactNode-内容(通常为 Title 与 Description)

Alert.Title

Prop类型默认值描述
classNamestring-附加的 CSS 类
childrenReactNode-Alert 标题文本

Alert.Description

Prop类型默认值描述
classNamestring-附加的 CSS 类
childrenReactNode-Alert 说明文本

相关组件