Mobile Popup 弹出层
在当前页面上方显示带遮罩的滑入面板,可用于底部抽屉、侧边面板和临时内容。
何时使用
- 需要承载一段自定义内容并暂时阻断背景操作时使用
Popup。 - 只需要确认、取消语义时优先使用
Dialog;一组动作使用ActionSheet。 - 轻量的短消息使用
Toast,不要用 Popup 模拟 Toast。 open指针由页面状态持有,Popup 只负责展示和关闭反馈。
在线体验
真实 gallery 展示底部 Popup 和 Dialog;可切换主题、明暗模式并打开浮层。
基本用法
Popup 的内容通过 Body 构建。遮罩默认显示,点击遮罩或按 Esc 可以关闭。用 CloseOnMask(false) 禁止点击遮罩关闭。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
"github.com/tangoui/tango/ui/mobile"
)
var open bool
func page(c *ui.Context) {
if mobile.Button(c, "打开面板").Primary().Clicked() {
open = true
}
mobile.Popup(c, &open).
A11yLabel("设置面板").
CloseOnMask(true).
Body(func() {
ui.Column(c, func() {
ui.Text(c, "面板内容")
mobile.Button(c, "完成").Block().Clicked()
}, layout.Pad(16))
})
}常见用法
设置位置和圆角
Position 支持底部、顶部、左右物理边和逻辑起止边;底部面板可打开 Handle。
func bottom(c *ui.Context, open *bool) {
mobile.Popup(c, open).
Position(mobile.PosBottom).
Handle(true).
Round(true).
Body(func() { ui.Text(c, "底部内容") })
}侧边面板
侧边 Popup 适合筛选或导航内容,逻辑位置用 PosStart、PosEnd 可随 RTL 自动适配。
func side(c *ui.Context, open *bool) {
mobile.Popup(c, open).
Position(mobile.PosEnd).
Layout(layout.W(300)).
Body(func() { mobile.List(c, "筛选").Body(func() { mobile.Cell(c, "全部") }) })
}自定义遮罩行为
关闭遮罩后,必须提供明确的关闭控件或由业务状态关闭;Closed 可观察本帧关闭事件。
func explicitClose(c *ui.Context, open *bool) {
popup := mobile.Popup(c, open).Mask(true).CloseOnMask(false).Body(func() {
if mobile.Button(c, "关闭").Clicked() {
*open = false
}
})
if popup.Closed() {
ui.Text(c, "面板已关闭")
}
}升级为 Dialog 或 ActionSheet
同一页面的浮层按交互语义选组件,而不是把所有内容都放进 Popup。
func semanticOverlay(c *ui.Context, dialogOpen, actionsOpen *bool) {
mobile.Dialog(c, dialogOpen, "删除确认").Content("删除后无法恢复").Confirm("删除", "取消").Show()
mobile.ActionSheet(c, actionsOpen, []mobile.Action{{Text: "编辑"}, {Text: "删除", Danger: true}}).Show()
}主题与样式
Popup 的遮罩、面板背景、圆角和拖拽把手读取移动主题令牌。shadcn 风格使用带把手的底部 sheet 语义;Ant Mobile 和 Vant 保持各自的面板间距。自定义背景可用 Bg,但优先使用 mobile.ThemeOf(c) 的颜色。
注意事项
open 必须是稳定状态,不能在每帧声明为 false。遮罩与键盘焦点由 Popup 处理;关闭时清理业务状态或异步任务。为无标题的面板设置 A11yLabel,为危险动作使用 Dialog 的确认语义。RTL 下 PosStart、PosEnd 是逻辑边,PosLeft、PosRight 是物理边。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Mobile · ActionSheet 动作面板
gallery 演示 MobileActionsheet 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
Mobile · 展示:Swiper / Skeleton / Empty / Result / Toast
gallery 演示 MobileDisplay 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
mobile.Popup
func Popup(c *ui.Context, open *bool) *PopupWPopup creates a popup bound to *open (bottom sheet by default); set options, then build the content with Body. Tapping the mask or pressing Escape closes it. In the shadcn style a bottom popup is a Drawer: an outlined sheet with rounded top corners and a grab handle that can be dragged down to close it.
ConfigProvider defaults ("mobile.Popup"): "position" ("bottom", "top", "center", "left", "right", "start", "end"), "round", "mask", "closeOnMaskClick", "handle" (bool).
返回 *PopupW,可链式调用的方法见 PopupW 方法。
mobile.ActionSheet
func ActionSheet(c *ui.Context, open *bool, actions []Action) *ActionSheetWActionSheet opens a bottom action menu bound to *open. It renders when Show is called (after options) and closes after a selection. In the shadcn style the actions sit in an outlined rounded card floating above the bottom edge, the cancel button in a second card below.
ConfigProvider defaults ("mobile.ActionSheet"): "cancelText" (string, shows the cancel button), "closeOnMaskClick" (bool).
返回 *ActionSheetW,可链式调用的方法见 ActionSheetW 方法。
mobile.Dialog
func Dialog(c *ui.Context, open *bool, title string) *DialogWDialog creates a modal dialog bound to *open. By default it is an alert with one acknowledge button; Confirm adds a cancel button. Call Show (or a query) after the options.
In the shadcn style the dialog follows AlertDialog on small screens: an outlined card with a centred title, a muted message and full-width stacked buttons (solid confirm above an outline cancel).
ConfigProvider defaults ("mobile.Dialog"): "closeOnMaskClick" (bool), "confirmText" (string, the acknowledge / OK text), "cancelText" (string, used by Confirm when it gets "").
返回 *DialogW,可链式调用的方法见 DialogW 方法。
mobile.Toast
func Toast(c *ui.Context, show *bool, msg string) *ToastWToast shows msg while *show is true and clears it after the duration (2 s; 0 keeps it). Call Show after the options. In the shadcn style it is a Sonner toast: an outlined popover card with the icon inline, shown at the bottom by default.
ConfigProvider defaults ("mobile.Toast"): "duration" (time.Duration or ms), "position" ("top", "center", "bottom").
返回 *ToastW,可链式调用的方法见 ToastW 方法。
mobile.ShowToast
func ShowToast(c *ui.Context, msg string, o ...ToastOptions)ShowToast shows msg as a toast from event-handling code (replacing the current one). It is drawn by the enclosing Provider.
PopupW 方法
mobile.PopupW:PopupW is a sliding panel over a dimmed mask.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the panel (a dialog for screen readers; name it after its content). | A11yLabel(s string) *PopupW |
Bg | Bg sets the panel background. | Bg(col paintColor) *PopupW |
Body | Body builds the panel content (only while open or animating out). | Body(fn func()) *PopupW |
CloseOnMask | CloseOnMask toggles closing on a mask tap (default on). | CloseOnMask(on bool) *PopupW |
Closed | Closed reports that the user dismissed the popup this frame. | Closed() bool |
Handle | Handle toggles the drawer grab handle of a bottom popup (shadcn default on): a bar on top of the sheet; dragging it down closes the popup. | Handle(on bool) *PopupW |
Layout | Layout applies layout options to the panel. | Layout(opts ...layout.Option) *PopupW |
Mask | Mask toggles the dimmed backdrop (default on). | Mask(on bool) *PopupW |
Position | Position sets where the panel appears. | Position(p Position) *PopupW |
Round | Round toggles rounded corners on the inner edge. | Round(on bool) *PopupW |
ActionSheetW 方法
mobile.ActionSheetW:ActionSheetW is a bottom menu of actions.
| 方法 | 说明 | 签名 |
|---|---|---|
Cancel | Cancel adds a separated cancel button ("" uses the localized "取消"). | Cancel(s string) *ActionSheetW |
Cancelled | Cancelled reports a tap on the cancel button or the mask this frame. | Cancelled() bool |
Extra | Extra shows a caption above the actions. | Extra(s string) *ActionSheetW |
Selected | Selected returns the index of the action tapped this frame. | Selected() (int, bool) |
Show | Show renders the sheet. | Show() *ActionSheetW |
DialogW 方法
mobile.DialogW:DialogW is a centred modal with a title, message and buttons.
| 方法 | 说明 | 签名 |
|---|---|---|
Cancelled | Cancelled reports a cancel this frame (button or mask). | Cancelled() bool |
CloseOnMask | CloseOnMask lets a mask tap dismiss the dialog (cancel). | CloseOnMask() *DialogW |
Confirm | Confirm turns the dialog into a confirm box with OK / Cancel buttons ("" keeps the localized texts). | Confirm(ok, cancel string) *DialogW |
Confirmed | Confirmed reports a tap on the OK / acknowledge button this frame. | Confirmed() bool |
Content | Content sets the message. | Content(s string) *DialogW |
Custom | Custom builds custom content under the message. | Custom(fn func()) *DialogW |
OKText | OKText sets the acknowledge button text of an alert. | OKText(s string) *DialogW |
PanelID | PanelID returns the ID of the dialog panel (0 while closed). | PanelID() ui.ID |
Show | Show renders the dialog. | Show() *DialogW |
ToastW 方法
mobile.ToastW:ToastW is a short centred message.
| 方法 | 说明 | 签名 |
|---|---|---|
Duration | Duration sets the auto-hide delay (0 = stay). | Duration(d time.Duration) *ToastW |
Icon | Icon adds an icon: "success", "fail", "loading". | Icon(kind string) *ToastW |
Position | Position sets the vertical placement (PosTop, PosCenter, PosBottom). | Position(p Position) *ToastW |
Show | Show renders the toast. | Show() *ToastW |
Action 参数类型
mobile.Action:Action is one entry of an ActionSheet.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Text | string | |
Description | string | |
Danger | bool | |
Bold | bool | |
Disabled | bool |
ToastOptions 参数类型
mobile.ToastOptions:ToastOptions configure ShowToast.
| 字段 | 说明 | 类型 |
|---|---|---|
Icon | "success", "fail", "loading" or "" | string |
Duration | default 2 s | time.Duration |
Position | default PosCenter | Position |