这是警报标题
import { Alert } from "@chakra-ui/react"
const Demo = () => {
return (
<Alert.Root status="info" title="This is the alert title">
<Alert.Indicator />
<Alert.Title>This is the alert title</Alert.Title>
</Alert.Root>
)
}
用法
import { Alert } from "@chakra-ui/react"
<Alert.Root>
<Alert.Indicator />
<Alert.Content>
<Alert.Title />
<Alert.Description />
</Alert.Content>
</Alert.Root>
信息
如果您偏好封闭式组件组合,请查看下面的代码片段。示例
描述
渲染 Alert.Description
组件以提供警报的额外上下文。
无效字段
您的表单存在一些错误。请修正后重试。
import { Alert } from "@chakra-ui/react"
const Demo = () => {
return (
<Alert.Root status="error">
<Alert.Indicator />
<Alert.Content>
<Alert.Title>Invalid Fields</Alert.Title>
<Alert.Description>
Your form has some errors. Please fix them and try again.
</Alert.Description>
</Alert.Content>
</Alert.Root>
)
}
状态
通过传递 status
属性来更改警报的状态。这将影响使用的颜色方案和图标。警报支持 error
、success
、warning
和 info
状态。
处理您的请求时出错
Chakra 将于8月30日上线。准备好了吗!
您的账户似乎即将到期,立即升级
数据已上传至服务器。开始吧!
import { Alert, Stack } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="4" width="full">
<Alert.Root status="error">
<Alert.Indicator />
<Alert.Title>There was an error processing your request</Alert.Title>
</Alert.Root>
<Alert.Root status="info">
<Alert.Indicator />
<Alert.Title>
Chakra is going live on August 30th. Get ready!
</Alert.Title>
</Alert.Root>
<Alert.Root status="warning">
<Alert.Indicator />
<Alert.Title>
Seems your account is about expire, upgrade now
</Alert.Title>
</Alert.Root>
<Alert.Root status="success">
<Alert.Indicator />
<Alert.Title>Data uploaded to the server. Fire on!</Alert.Title>
</Alert.Root>
</Stack>
)
}
变体
使用 variant
属性更改警报的视觉样式。值可以是 subtle
、solid
、outline
数据已上传至服务器。开始吧!
数据已上传至服务器。开始吧!
数据已上传至服务器。开始吧!
import { Alert, Stack } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="4">
<Alert.Root status="success" variant="subtle">
<Alert.Indicator />
<Alert.Title>Data uploaded to the server. Fire on!</Alert.Title>
</Alert.Root>
<Alert.Root status="success" variant="solid">
<Alert.Indicator />
<Alert.Title>Data uploaded to the server. Fire on!</Alert.Title>
</Alert.Root>
<Alert.Root status="success" variant="surface">
<Alert.Indicator />
<Alert.Title>Data uploaded to the server. Fire on!</Alert.Title>
</Alert.Root>
</Stack>
)
}
带关闭按钮
这是一个如何将 Alert
与关闭按钮组合的示例。
成功!
您的申请已收到。我们将在未来48小时内审核您的申请并回复。
import { Alert, CloseButton } from "@chakra-ui/react"
const Demo = () => {
return (
<Alert.Root>
<Alert.Indicator />
<Alert.Content>
<Alert.Title>Success!</Alert.Title>
<Alert.Description>
Your application has been received. We will review your application
and respond within the next 48 hours.
</Alert.Description>
</Alert.Content>
<CloseButton pos="relative" top="-2" insetEnd="-2" />
</Alert.Root>
)
}
带加载指示器
这是一个如何将 Alert
与加载指示器组合的示例。
我们正在加载一些内容
import { Alert, Spinner } from "@chakra-ui/react"
const Demo = () => {
return (
<Alert.Root
borderStartWidth="3px"
borderStartColor="colorPalette.600"
title="We are loading something"
>
<Alert.Indicator>
<Spinner size="sm" />
</Alert.Indicator>
<Alert.Title>We are loading something</Alert.Title>
</Alert.Root>
)
}
自定义图标
使用 icon
属性将自定义图标传递给警报。这将覆盖警报状态的默认图标。
提交此表单将删除您的账户
import { Alert } from "@chakra-ui/react"
import { LuAlarmClockPlus } from "react-icons/lu"
const Demo = () => {
return (
<Alert.Root status="warning">
<Alert.Indicator>
<LuAlarmClockPlus />
</Alert.Indicator>
<Alert.Title>Submitting this form will delete your account</Alert.Title>
</Alert.Root>
)
}
颜色调色板覆盖
默认的颜色调色板是从 status
属性推断的。要覆盖颜色调色板,请传递 colorPalette
属性。
这是一个信息警报,但显示为青色
import { Alert } from "@chakra-ui/react"
const Demo = () => {
return (
<Alert.Root status="info" colorPalette="teal">
<Alert.Indicator />
<Alert.Title>This is an info alert but shown as teal</Alert.Title>
</Alert.Root>
)
}
定制
您可以使用样式属性来样式化 Alert
组件。
黑色星期五特卖 (八折优惠)
升级您的计划以获得特卖访问权限。
注意:黑色星期五特卖 (八折优惠)
import { Alert, Link, Stack } from "@chakra-ui/react"
import { LuPercent } from "react-icons/lu"
const Demo = () => {
return (
<Stack gap="4">
<Alert.Root title="Success" status="success">
<Alert.Indicator>
<LuPercent />
</Alert.Indicator>
<Alert.Content color="fg">
<Alert.Title>Black Friday Sale (20% off)</Alert.Title>
<Alert.Description>
Upgrade your plan to get access to the sale.
</Alert.Description>
</Alert.Content>
<Link alignSelf="center" fontWeight="medium">
Upgrade
</Link>
</Alert.Root>
<Alert.Root
size="sm"
borderStartWidth="3px"
borderStartColor="colorPalette.solid"
alignItems="center"
title="Success"
status="success"
>
<LuPercent />
<Alert.Title textStyle="sm">
Heads up: Black Friday Sale (20% off)
</Alert.Title>
</Alert.Root>
</Stack>
)
}
封闭组件
以下是如何为封闭组件组合设置 Alert
。
import { Alert as ChakraAlert } from "@chakra-ui/react"
import * as React from "react"
export interface AlertProps extends Omit<ChakraAlert.RootProps, "title"> {
startElement?: React.ReactNode
endElement?: React.ReactNode
title?: React.ReactNode
icon?: React.ReactElement
}
export const AlertClosedComponent = React.forwardRef<
HTMLDivElement,
AlertProps
>(function Alert(props, ref) {
const { title, children, icon, startElement, endElement, ...rest } = props
return (
<ChakraAlert.Root ref={ref} {...rest}>
{startElement || <ChakraAlert.Indicator>{icon}</ChakraAlert.Indicator>}
{children ? (
<ChakraAlert.Content>
<ChakraAlert.Title>{title}</ChakraAlert.Title>
<ChakraAlert.Description>{children}</ChakraAlert.Description>
</ChakraAlert.Content>
) : (
<ChakraAlert.Title flex="1">{title}</ChakraAlert.Title>
)}
{endElement}
</ChakraAlert.Root>
)
})
如果您想自动将封闭组件添加到您的项目中,请运行此命令
npx @chakra-ui/cli snippet add alert
属性
根
属性 | 默认值 | 类型 |
---|---|---|
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' 组件的颜色调色板 |
status | 'info' | 'info' | 'warning' | 'success' | 'error' | 'neutral' 组件的状态 |
variant | 'subtle' | 'subtle' | 'surface' | 'outline' | 'solid' 组件的变体 |
size | 'md' | 'sm' | 'md' | 'lg' 组件的大小 |
inline | 'true' | 'false' 组件的内联状态 |