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,螢幕閱讀器會繼續把浮層背後的頁面當成可瀏覽內容讀出。