AI模态框生成器。

描述您需要的对话框,MeDo会生成整套结构 — 头部、可滚动主体和操作底栏,还包含焦点锁定、关闭时焦点还原、body滚动锁定,以及移动端底部弹层变体。

模态框
模态框

模态框最难的部分是它背后的页面

模态框看起来只是一个带阴影的居中盒子,这正是它被反复做坏的原因。视觉层面微不足道,真正困难的部分完全在组件之外。打开对话框必须冻结下方文档、捕获键盘焦点以免 Tab 游走到隐藏内容、对辅助技术隐藏页面其余部分,然后在关闭时把焦点精准还给打开它的那个元素。漏掉最后一步,键盘用户就会被无声地送回文档顶部,完全不知身处何处。把对话框和它的焦点契约一起生成,意味着这台状态机在首次渲染时就是正确的,而不是等到QA之后再打补丁。

6 个模板

可以生成的 模态框 模板

每个模板都是真实的提示词,而不是截图。复制到任意 AI 编辑器,或在 MeDo 中运行,直接拿到 React + Tailwind 代码。

BasicHeader + Footer

Composable Dialog With Focus Management

The base every other dialog extends. The visual layer takes minutes; the focus contract is the part worth generating rather than hand-writing.

Create a modal dialog with three sizes — sm 400px, md 560px, lg 760px — centered over a black backdrop at 50 percent opacity, 16px radius, soft shadow. Compose it from Header, Body and Footer slots: the header holds a title and an icon-only close button with an aria-label, the body scrolls independently at max-height 70vh, and the footer right-aligns Cancel next to a primary action. On open, move focus to the first interactive element, trap Tab and Shift+Tab inside the dialog, and lock body scroll by compensating for the scrollbar width so the page does not shift. Set role="dialog", aria-modal="true" and aria-labelledby pointing at the title, and restore focus to the trigger on close.

在 MeDo 中尝试
ConfirmationDestructive Confirm

Non-Dismissible Delete Dialog

The one dialog that must ignore Escape and backdrop clicks. A stray click outside the box should never be the gesture that deletes an account.

Create a destructive confirmation dialog: a warning icon, a heading naming the exact resource, a body stating what will be lost and whether it is recoverable, and a footer with Cancel on the left and a red Confirm on the right. Ignore both Escape and backdrop clicks so the action requires a deliberate button press. Move initial focus to Cancel rather than Confirm, so a stray Enter keypress cancels instead of destroying. Use role="alertdialog" with aria-describedby on the body text. Add an optional type-to-confirm mode requiring the resource name to be typed before Confirm enables, with the comparison case-sensitive and trimmed.

在 MeDo 中尝试
FormForm Dialog

Scrolling Form With Sticky Actions

A long form in a dialog needs a body that scrolls under a fixed header and footer, plus an unsaved-changes guard. Losing typed input to a misclick is unforgivable.

Create a form dialog with a fixed header, a scrolling body and a sticky footer holding Cancel and Save. Keep the header and footer outside the scroll container so the actions stay reachable in a long form. Add a dirty-state guard: when any field has changed, intercept Escape and backdrop clicks and show a nested confirm-discard step inside the same dialog rather than opening a second overlay. On validation failure, move focus to the first invalid field and scroll it into view inside the body. Set aria-busy while saving, disable Save without removing it from the tab order, and close only after the request resolves.

在 MeDo 中尝试
MobileBottom Sheet

Sheet That Slides Up on Phones

A centered card on a phone leaves the buttons floating mid-screen, away from the thumb. Same component, different layout under 640px.

Create a dialog that renders as a centered card above 640px and as a bottom sheet below it: full width, rounded top corners only, sliding up from the bottom edge with a 250ms ease-out transform. Add a drag handle at the top and support swipe-to-dismiss with a velocity threshold, falling back to the close button for keyboard and assistive technology. Cap the sheet height at 90vh with the body scrolling inside, and use dynamic viewport units so the mobile browser chrome does not clip the footer. Keep focus trapping and scroll lock identical in both layouts, and disable the slide animation under prefers-reduced-motion.

在 MeDo 中尝试
PlatformNative Dialog

HTML dialog With showModal

The browser already ships focus trapping, top-layer stacking and Escape handling. Choose this and you delete most of the code the custom overlay needed.

Create a modal built on the native HTML dialog element opened with showModal(), so the browser supplies focus trapping, Escape handling and top-layer stacking with no z-index management. Style the backdrop through the ::backdrop pseudo-element. Add the parts the platform does not give you: body scroll lock on open, an entry and exit animation using allow-discrete and the transition-behavior property, and a close-on-backdrop handler that compares the click target to the dialog element rather than its children. Listen for the close event to restore focus and read returnValue, and use autofocus on the element that should receive focus first.

在 MeDo 中尝试
OverlayCommand Palette

Cmd+K Search Dialog

A modal whose focus stays in the input while arrow keys drive a list elsewhere. Roving focus would break typing, so this one runs on aria-activedescendant.

Create a command palette dialog opened by Cmd+K or Ctrl+K: a search input at the top with results grouped by section below, positioned near the top of the viewport rather than vertically centered. Keep DOM focus in the input at all times and move the visual selection with aria-activedescendant so arrow keys navigate the list without stealing focus from typing. Support Enter to run the highlighted command, Escape to close with focus restored to the previously active element, and a scroll-into-view on the highlighted row. Debounce the query, render an empty state naming what was searched for, and announce the result count through a polite live region.

在 MeDo 中尝试

如何自定义你的 模态框

说明哪些对话框不允许被关闭

Escape 与遮罩点击对信息类对话框是正确的默认行为,但在破坏性确认和含未保存输入的多步表单中却是隐患。指明哪些变体应同时忽略两者,这样盒子外的误点就不会删掉一个账户。

要焦点还原,而不只是焦点锁定

大多数提示词只做到了锁定,却忘了回程。明确要求关闭时焦点回到触发元素,并且对话框在打开时保存该引用,键盘用户就能从离开的位置精确继续。

按断点选择居中还是底部弹层

手机上居中的对话框会让操作按钮悬在屏幕中部,远离拇指。请要求在 640px 以下使用底部弹层,让主要操作触手可及,而在更宽的视口保持居中布局。

永远不要在模态框中再打开模态框

第二个对话框叠在第一个之上会破坏焦点锁定、让遮罩叠加两层,还会让 Escape 关闭哪一层变得含糊。请改为在同一个对话框内使用分步内容,让浮层始终是只有一个焦点归属的单一层级。

谁在使用 模态框 组件

SaaS产品

用于取消订阅的不可关闭确认对话框,主体中再次显示套餐名称,并配红色"确认"按钮;同时搭配一个更轻量、可关闭的邀请团队成员对话框。

电商店铺

快速查看对话框,内含商品图片、尺码选择器和"加入购物车"按钮;在手机上渲染为底部弹层,让尺码选项与购物车按钮都留在拇指可达范围内。

仪表盘或管理工具

批量删除确认框会写明确切的记录数量,并要求输入资源名称后"确认"按钮才可用;再配一个编辑对话框,其中的长表单在固定头部与底栏之间滚动。

内容或媒体网站

用于查看原图的灯箱对话框,支持方向键切换、Escape 关闭,并将焦点还给打开它的缩略图,使图库中的浏览位置不会丢失。

不同网站的 模态框 方案

同一个 模态框 组件,针对它所在网站的类型做调整。

01 / 06

SaaS Product Dialogs

Two classes of dialog with opposite dismissal rules: invite-a-teammate should close on Escape and backdrop click, cancel-my-subscription should close on neither. Decide that per variant rather than globally, because a single dismissal policy across a product means either annoying dialogs or destroyed data.

模态框 的风格与变体

挑选与页面其余部分匹配的变体,然后生成它。

01 / 06

Centered Dialog

The default above 640px: a fixed-width card centered over a dimmed backdrop. Cap the height at around 70vh with the body scrolling internally, because a dialog taller than the viewport pushes its own footer out of reach and there is no page scroll left to recover it.

在 React、Next.js、Vue 与 Svelte 中使用 模态框

React、Next.js、Vue 3、SvelteKit、Astro 或纯 HTML —— 同一个 模态框,六种接入方式。

01 / 06

React Modal

Render through a portal so the dialog escapes any ancestor with overflow hidden or a transform, both of which would clip it. Store the trigger element on open and focus it again in the cleanup, which is the step most hand-rolled modals skip.

src/components/Modal.tsx

export function Modal({ open, onClose, children }) {
  const trigger = useRef(null)

  useEffect(() => {
    if (!open) return
    trigger.current = document.activeElement
    return () => trigger.current?.focus()
  }, [open])

  if (!open) return null
  return createPortal(
    <div role="dialog" aria-modal="true">{children}</div>,
    document.body
  )
}

如何把 模态框 加入项目

四个步骤,从挑选 模态框 模板到上线生产环境。

01~30s

Pick a Dialog Template

Scan the six templates and start from the interaction — a destructive confirm, a scrolling form, a mobile bottom sheet, or a Cmd+K palette.

02~10s

Copy the Prompt

Take the prompt into MeDo, Lovable, Bolt, v0 or Cursor. State whether this dialog may be dismissed by Escape and backdrop click before you run it, since that single decision separates a helpful overlay from one that deletes data on a misclick.

03~1min

Generate and Refine

You get React + Tailwind back with a live preview. Follow up in plain English — "make it a bottom sheet under 640px", "focus Cancel first", "build it on the native dialog element" — rather than rewiring the focus trap yourself.

04~2min

Test It With the Keyboard Only

Open it, Tab to the end and confirm focus cycles instead of escaping into the page, then close it and check focus returned to the trigger. Those two checks catch nearly every modal defect that ships.

关于 模态框 的常见问题

如何用AI生成模态框组件?

在一条提示词里描述布局、尺寸和关闭规则 — 例如"一个 560px 居中的对话框,主体可滚动,带取消和确认操作,并有一个忽略 Escape 的破坏性变体"。MeDo 生成的组件会包含焦点锁定、焦点还原、滚动锁定和对话框语义,所以你得到的是一个真正可用的浮层,而不是一个加了样式的 div。

什么是焦点锁定,模态框为什么需要它?

焦点锁定让 Tab 与 Shift+Tab 在对话框内部的可交互元素之间循环,而不是逃到下面的页面。没有它,键盘用户会从"确认"按钮继续 Tab 到看不见的链接,既让人困惑,也不符合 WCAG。焦点锁定应始终与焦点还原搭配,在对话框关闭时把焦点交还给触发元素。

点击遮罩时模态框应该关闭吗?

对信息类和低风险的对话框,应该 — 点击遮罩和按 Escape 是人们预期的关闭方式。对破坏性确认和含未保存输入的表单则不应该:盒子外的误点不该删除数据或丢弃已输入内容。任何不可撤销的操作都应要求明确点击按钮。

可以用原生 HTML <dialog> 元素替代自定义模态框吗?

通常可以。配合 showModal() 的原生 <dialog> 元素免费提供焦点锁定、Escape 处理、顶层叠放以及 ::backdrop 伪元素,无需管理 z-index。但你仍需自行锁定 body 滚动并实现动画,较旧的浏览器还需要降级方案。告诉 MeDo 你想用原生元素,它就会在其之上构建,而不是重新实现这些行为。

模态框在移动端应该如何表现?

大约在 640px 以下,底部弹层通常优于居中卡片:它把主要操作锚定在拇指附近,用整个宽度容纳表单字段,也符合用户已经熟悉的平台惯例。MeDo 可以在一个组件里生成两种布局,并在你指定的断点之间切换。

模态框需要 role="dialog" 和 aria-modal 吗?

需要,除非你使用原生 <dialog> 元素,它自带这些语义。自定义浮层需要 role="dialog"、aria-modal="true",以及指向可见标题的 aria-labelledby,或在没有可见标题时使用 aria-label。缺少 aria-modal,屏幕阅读器会继续把浮层背后的页面当作可浏览内容读出。