Overlay Avoidance
空間不足時,各類浮層元件的處理方式
浮層避讓的核心是先界定可用範圍,再動態計算位置:涵蓋選單翻轉與限高、對話框頭尾常駐、提示框八向換位與跟隨回差、避開置頂與置底固定列 4 條互動準則,並附可操作的示範。
提示框(Tooltip)、選單、對話框這類浮層元件,內容長度不一,用戶端與網頁視窗的尺寸也不固定——這是 AI 生成介面時極容易忽略的細節。浮層若只會朝單一固定方向展開,一旦觸發元素靠近視窗邊緣、位於長列表底部,或被捲到置頂列下方,浮層就會被視窗裁切,或遮住關鍵操作。穩妥的做法是先界定浮層的「可用範圍」:視窗四邊各內縮 16px,頁面內的非模態浮層再額外扣除置頂導覽列與置底固定列;同時透過 Portal 掛載到最上層,避免被父容器裁切,並在每次開啟、捲動、視窗縮放,以及浮層本身尺寸改變時,動態重新計算位置。
放不下就翻轉,太長就限高
選單預設在觸發元素下方 8px 處展開。下方空間不足、上方空間充裕時,應自動翻轉為向上展開;階層式子選單在右側放不下時,同樣翻到左側,展開動畫的起點(transform-origin)也要同步改到靠近觸發元素的一側。翻轉後若側向仍有超出,就沿著視窗邊緣平移回安全範圍內;若選單總高度仍超過這一側的可用高度,就把容器的最大高度限制在可用範圍內、改為內部捲動,並在截斷處加上漸隱效果,提示下方還有內容。此外,選單開啟後,若游標在選單外捲動頁面,代表使用者的視線已回到主畫面,選單應立即淡出收起。
- 適用情境:下拉選單、右鍵選單、階層式子選單、選擇器(Select)
- 參數:距視窗邊緣 16px,距觸發元素 8px,依「翻轉 → 平移 → 限高捲動」的順序處理;在選單外捲動時立即淡出
對話框頭尾常駐
對話框承載較長內容時,若直接依內容高度置中撐開,標題列與底部操作列會被推到視窗之外,使用者既看不清目前的脈絡,也找不到確認按鈕。應把對話框的最大高度限制在視窗的安全高度內,固定標題列與底部操作列,只讓中間的內容區捲動,並在內容捲到標題下方時浮現一條 1px 分隔線。若某個情境的內容經常很多,建議改用抽屜取代對話框——桌機版用從側邊滑出的抽屜,行動版直接用由下往上展開的全寬底部抽屜。
- 適用情境:長列表對話框、設定面板、表單對話框、條款確認
- 參數:上下各留 16px,標題列與底部操作列固定,內容捲動後標題下顯示 1px 分隔線;內容經常很長時改用側邊抽屜或行動版全寬抽屜
提示框換向與跟隨回差
提示框超出視窗邊緣時,應翻轉到另一個方向;若只是靠近側邊而局部超出,也可以把浮層沿邊緣平移回範圍內,並讓箭頭始終指向觸發元素的中心。帶箭頭的提示框可預設上、下、左、右與四個角共 8 個方位,依距離各邊緣的剩餘空間,動態選出最合適的一個。圖表中跟隨游標移動的氣泡,預設放在游標右下方,並完全讓開游標本身;靠近右側邊緣時翻到游標左側,往回移動時則要比換邊的位置再往內多移 24px,才換回右側,藉由「回差」避免游標停在邊界附近時,氣泡來回跳動。提示框、跟隨氣泡與抽屜該怎麼選,詳見 009 提示框、氣泡、抽屜與對話框怎麼選。
- 適用情境:圖示按鈕與頭像的提示框、圖表讀數氣泡、跟隨游標的資訊卡
- 參數:帶箭頭時在 8 個方位中動態選擇,平移時箭頭對準觸發元素中心;跟隨氣泡讓開游標,換回的回差 24px
避開置頂與置底固定列
任何非模態浮層——尤其是提示框、下拉選單與 Toast——都應避開固定在頂部與底部的元素(例如頂部導覽列、底部播放列、標籤列、聊天輸入框),計算可用範圍時,先扣除這些固定列占用的高度。Toast 應停在底部固定列上方,避免蓋住播放控制項或主要導覽;捲動時不會主動關閉的浮層,一旦觸發元素隨頁面捲到置頂導覽列下方,就應同步隱藏,而不是懸空壓在導覽列上。模態對話框帶有遮罩、會接管整個畫面的操作,因此可以覆蓋頂部與底部的固定列,可用範圍以整個視窗計算。
- 適用情境:置頂導覽列與置底列周圍的提示框、下拉選單,以及底部播放列/標籤列/輸入框上方的 Toast
- 參數:可用範圍扣除頂部與底部固定列,Toast 距底部列 16px,觸發元素被置頂列遮住時浮層同步隱藏
交給 AI 來做
將下方 Prompt 複製給 Claude Code、Codex、Cursor、WorkBuddy 等 AI 工具:它會先盤點專案現有的選單、氣泡、提示框、Toast 與對話框元件,找出被邊緣裁切與被固定列遮擋的問題,整理成表格;經你確認後,直接在元件層補上翻轉、平移、限高與避讓的邏輯。
Check the overlay components my product already has (menus and dropdowns, popovers, tooltips, toasts and dialogs), find where each one breaks at the extremes (by an edge of the window, low on a long page, under a sticky header, above a fixed bottom bar, with long content), and fix the cause in the component, so every place that uses it gets the fix. Diagnose first, and change only what I approve. Work inside my code and components, and do not add a dependency without asking. Every value below is a default to start from. 0. Diagnose - List each overlay component, where it is defined (my own, or a library: Floating UI, Radix, MUI, Ant Design), and how it is placed today: preferred side, offset, whether it flips, shifts or caps its height, what it measures against, whether it is rendered in a portal, and when it places itself again. - Find what stays fixed on screen (sticky headers, bottom bars) and any clipping or scrolling container between an overlay and the window. - Find the places where each one fails: the last rows of a long list, the right end of a header, inside a scrolling panel, a dialog with its longest content. Trace each failure to its cause in the component, not to the page it shows up on. 1. The room - Every overlay stays inside the window less 16px on each side. In the page, also less the sticky header and any fixed bottom bar; modal layers cover the bars. Render overlays in a portal. - Place it again on open, on scroll, on resize and when its own size changes, not once only. 2. Menus and dropdowns - 8px from the trigger. When the preferred side has no room, flip to the other side, then shift along it into the room. - When it is still too tall, cap it to the room and let it scroll inside. - When the page scrolls while the pointer is outside the menu, fade it out and close it. - With Floating UI: offset(8), flip, shift and size with padding 16, and autoUpdate. 3. Dialogs - No taller than the window less 16px. The title bar and the button bar stay put; only the content between them scrolls, with a 1px line where it passes under them. - Where the content is often long, suggest a side drawer on desktop and a full-width bottom sheet on phones, and ask me before changing. 4. Tooltips - By an edge, flip to the opposite side or shift along it; the arrow keeps pointing at the trigger. - With an arrow, choose among 8 placements (above, below, left, right and the four corners) by the room on each side. - A tooltip that follows the pointer sits clear of the cursor, moves to its other side by an edge, and comes back only once the pointer is 24px further in. 5. Fixed bars - No overlay, tooltips above all, covers a sticky header or a bottom bar. Toasts sit 16px above the bottom bar. An overlay whose trigger scrolls under the header hides with it. 6. Report and check - Before changing anything, give me one table, one row per component: | Component | Used in | Today | Cases that fail | Cause | Fix | Values |. Ask me which rows to change. - Afterwards, go back to every failing case and check that it opens whole, inside the room.


























