¥Dialog
覆盖在主窗口或其他对话框窗口上的窗口,使下方内容呈现为非活动状态。
import * as React from "react";import { Dialog } from "radix-ui";import { Cross2Icon } from "@radix-ui/react-icons";import "./styles.css";const DialogDemo = () => (<Dialog.Root><Dialog.Trigger asChild><button className="Button violet">Edit profile</button></Dialog.Trigger><Dialog.Portal><Dialog.Overlay className="DialogOverlay" /><Dialog.Content className="DialogContent"><Dialog.Title className="DialogTitle">Edit profile</Dialog.Title><Dialog.Description className="DialogDescription">Make changes to your profile here. Click save when you're done.</Dialog.Description><fieldset className="Fieldset"><label className="Label" htmlFor="name">Name</label><input className="Input" id="name" defaultValue="Pedro Duarte" /></fieldset><fieldset className="Fieldset"><label className="Label" htmlFor="username">Username</label><input className="Input" id="username" defaultValue="@peduarte" /></fieldset><div style={{ display: "flex", marginTop: 25, justifyContent: "flex-end" }} ><Dialog.Close asChild><button className="Button green">Save changes</button></Dialog.Close></div><Dialog.Close asChild><button className="IconButton" aria-label="Close"><Cross2Icon /></button></Dialog.Close></Dialog.Content></Dialog.Portal></Dialog.Root>);export default DialogDemo;
Supports modal and non-modal modes.
Focus is automatically trapped within modal.
Can be controlled or uncontrolled.
Manages screen reader announcements with Title
and Description
components.
Esc closes the component automatically.
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入所有部分并将它们组合在一起。
¥Import all parts and piece them together.
¥API Reference
¥Root
包含对话框的所有部分。
¥Contains all the parts of a dialog.
¥Trigger
打开对话框的按钮。
¥The button that opens the dialog.
¥Portal
使用时,将叠加层和内容部分传送到 body
中。
¥When used, portals your overlay and content parts into the body
.
¥Overlay
对话框打开时,覆盖视图非活动部分的图层。
¥A layer that covers the inert portion of the view when the dialog is open.
¥Content
包含在打开的对话框中呈现的内容。
¥Contains content to be rendered in the open dialog.
¥Close
关闭对话框的按钮。
¥The button that closes the dialog.
¥Title
对话框打开时会显示可访问的标题。
¥An accessible title to be announced when the dialog is opened.
如果你想隐藏标题,请将其封装在我们的 视觉隐藏 实用程序中,例如 <VisuallyHidden asChild>
。
¥If you want to hide the title, wrap it inside our Visually Hidden utility like this <VisuallyHidden asChild>
.
¥Description
一个可选的可访问描述,在对话框打开时显示。
¥An optional accessible description to be announced when the dialog is opened.
如果你想隐藏描述,请将其封装在我们的 视觉隐藏 实用程序中,例如 <VisuallyHidden asChild>
。如果你想完全删除描述,请删除此部分并将 aria-describedby={undefined}
传递给 Dialog.Content
。
¥If you want to hide the description, wrap it inside our Visually Hidden utility like this <VisuallyHidden asChild>
. If you want to remove the description entirely, remove this part and pass aria-describedby={undefined}
to Dialog.Content
.
¥Examples
¥Close after asynchronous form submission
使用受控属性,在异步操作完成后以编程方式关闭对话框。
¥Use the controlled props to programmatically close the Dialog after an async operation has completed.
¥Scrollable overlay
将内容移至叠加层内,以渲染带有溢出的对话框。
¥Move the content inside the overlay to render a dialog with overflow.
¥Custom portal container
自定义对话框的入口元素。
¥Customise the element that your dialog portals into.
¥Accessibility
¥Adheres to the Dialog WAI-ARIA design pattern.
¥Keyboard Interactions
¥Custom APIs
通过将原始部分抽象到你自己的组件中来创建你自己的 API。
¥Create your own API by abstracting the primitive parts into your own component.
¥Abstract the overlay and the close button
此示例抽象了 Dialog.Overlay
和 Dialog.Close
部分。
¥This example abstracts the Dialog.Overlay
and Dialog.Close
parts.
¥Usage
¥Implementation