对话框

对齐目标:@astryxdesign/core v0.7.1 · Dialog

Modal Title
Dialog content goes here.

使用方法 #

Dialog(模态对话框)会在当前页面之上浮起一个独立的小窗口,并把背景内容锁住,用来让用户集中精力完成某个具体任务,比如填写表单、配置选项或编辑资料。

它基于浏览器原生的 <dialog> 元素构建,打开时会自动锁定网页背景、把光标焦点限制在窗口内部,关闭后还会贴心地把焦点还给刚才点开它的那个按钮。搭配 DialogHeader 一起使用,可以自带标题、副标题以及右上角的关闭叉号:

Vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Dialog, DialogHeader } from '@astryx-vue/core/Dialog'
import { ref } from 'vue'

const isOpen = ref(false)
</script>

<template>
  <Button label="分享项目" variant="secondary" @click="isOpen = true" />
  <Dialog :is-open="isOpen" @open-change="isOpen = $event">
    <DialogHeader title="分享项目" has-close-button @open-change="isOpen = $event" />
    <p>复制链接并发送给团队成员。</p>
  </Dialog>
</template>

什么时候用:

  • 需要打断用户当前操作,让他们专注完成一步独立任务(如输入资料、配置参数)时。

什么时候不要用:

  • 如果只是为了在删除、覆盖或撤销授权等不可逆操作前确认一下,请改用专门的 AlertDialog。
  • 如果只是一句轻量的成功反馈(如“保存成功”),不要弹对话框打断用户,请改用轻提示 useToast。
  • 如果流程很长很复杂、需要来回切换多个页面,请直接做成独立的新页面,不要在一个小弹窗里硬塞太多东西。
  • 不要在一个对话框里再弹出一个新的对话框。如有分步需求,请在同一个弹窗内部切换步骤。

解剖

部分说明
页眉标题、可选副标题和关闭按钮。打开时焦点落到标题上,标题通过 aria-labelledby 标出对话框。
正文主要内容:文字、表单、列表或任意布局。
页脚保存、取消或接受、拒绝这类操作按钮,靠尾部对齐。
遮罩对话框后面的半透明层,挡住页面交互。

最佳实践 #

指引实践
推荐选择正确的用途:info 用于可关闭的内容,form 用于防止误点遮罩关闭,required 用于必须由用户作出响应的场景。
推荐在头部添加清晰的标题,让用户立即了解对话框要他们做什么。
推荐含有输入内容的对话框使用 purpose="form"。这样用户不会因误点遮罩而丢失数据。
推荐对话框只处理单一任务。内容超出可容纳范围时,考虑改用整页。
避免用对话框展示简单消息。这类消息可以内联显示,或使用 Toast 通知。
避免在对话框中嵌套对话框。应把流程拆成单个对话框内的多个步骤。
避免为简单确认使用 fullscreen 变体。它适用于编辑器或长表单等复杂内容。

示例 #

对话框 — 自适应呈现

Edit profile
Dialog remains the default presentation. This example explicitly opts into a Bottom Sheet only at lg and below when the device has a coarse pointer and no hover. Pass the presentation prop to make tests or unusual environments deterministic. In Bottom Sheet presentation, purpose="form" blocks scrim clicks and swipe dismissal while preserving Escape. Use this opt-in only when that contract is acceptable; keep AlertDialog/destructive confirmations on Dialog unless a product deliberately chooses otherwise.

默认仍是对话框。触屏且宽度不超过 lg 时,同一份资料表单改为底部弹层。

对话框 — 确认

Delete project?
This will permanently delete "Marketing Dashboard" and all of its data. This action cannot be undone.

破坏性操作前先确认。页面上的预览一直留着,点按钮才会打开真正的对话框。

对话框 — 表单

Edit profile
Update your display name and bio

不离开当前页面就能改名字和简介。purpose 为 form 时保留 Escape,并忽略点击遮罩。

对话框 — 全屏

Help & Documentation 5 articles · Last updated Apr 2026
Documentation
Everything you need to get started
Getting startedCreate your first project by clicking New Project in the sidebar. Choose a template or start from scratch.
Team membersInvite collaborators from Settings > Team. Each member can have Admin, Editor, or Viewer permissions.
BillingFree plans include up to 3 projects. Upgrade to Pro for unlimited projects and priority support.
API accessGenerate API keys from Settings > Developer. Rate limits are 1,000 requests per minute on free plans.
Data exportExport your data anytime from Settings > Data. Exports are available as CSV or JSON within 24 hours.

variant 为 fullscreen 时占满视口,适合文档这类长内容。

对话框 — 可滚动

Terms and Conditions
1. You agree to use the service only for lawful purposes and in compliance with all applicable laws and regulations in your jurisdiction.2. Your account credentials are your responsibility. Notify us immediately if you suspect unauthorized access to your account.3. We reserve the right to suspend accounts that violate these terms or engage in abusive behavior toward other users.4. Content you upload remains your property. You grant us a license to host and display it within the service.5. We may update these terms at any time. Continued use after changes constitutes acceptance of the updated terms.6. The service is provided as-is without warranties of any kind. We are not liable for data loss or service interruptions.7. You may cancel your account at any time. Your data will be deleted within 30 days of cancellation.8. Disputes will be resolved through binding arbitration in accordance with applicable regulations.9. You agree not to reverse-engineer, decompile, or disassemble any part of the service or its underlying technology.10. We may collect anonymized usage data to improve the service. Personal data is handled per our Privacy Policy.11. Third-party integrations are governed by their own terms. We are not responsible for third-party service outages.12. You are responsible for maintaining backups of your data. We provide export tools but do not guarantee data recovery.13. Commercial use requires a Business plan. Free accounts are limited to personal and non-commercial projects.14. We may introduce new features or discontinue existing ones with 30 days notice via email or in-app notification.15. Violation of these terms may result in immediate termination of your account without prior notice or refund.

maxHeight 把对话框压短,条款在内部滚动。

对话框 — 必须响应

Transfer project ownership
This action requires confirmation from the new owner
You are about to transfer "Marketing Dashboard" to Sarah Chen. Once accepted, you will lose admin access.

purpose 为 required 时,Escape 和遮罩都不能关掉。用户必须选择取消或转让。

无障碍 #

Dialog 基于原生 <dialog> 元素构建,模态行为、焦点限制和关闭后的焦点归还都由浏览器提供,因此不需要额外配置就能获得正确的无障碍表现。

对话框通过 showModal() 打开,打开期间页面其余部分不可交互。打开后,初始焦点落在第一个带有 data-autofocus 的元素上;如果没有元素带这个属性,焦点落在 DialogHeader 的标题上。

对话框的名称默认取自 DialogHeader 的标题,组件会把标题与对话框通过 aria-labelledby 关联起来。如果显式设置了 aria-label 或 aria-labelledby,则以你提供的名称为准。

Escape 与点击遮罩不会直接关闭对话框,它们只发出 openChange(false),由父组件决定是否关闭。关闭后,如果打开对话框之前的那个元素仍在页面中,焦点会回到它身上。

限制 #

下面两点决定了你能在对话框里放什么内容,在组合 Tooltip、Popover 或多步流程之前需要先了解。

  • Tooltip 与 Popover 会渲染到 body。在打开的 Dialog 内使用时,它们位于模态顶层之下,因此既看不到也无法操作;在它们获得对话框自身的层级之前,请不要把它们嵌套进对话框。
  • 对话框不可嵌套。需要多步流程时,请把流程拆分为同一个对话框内的步骤。

布局 #

标准变体通过 width 与 maxHeight 定尺寸,在窄屏上以间距令牌为边距夹取到视口内。position 设置静态偏移;逻辑属性 start 与 end 在 RTL 下自动镜像。