Skip to content

Drawer 抽屉 ​

从窗口边缘滑出的浮层面板,用于在不离开当前页面的情况下查看详情、填写较长的表单或做一组设置。

何时使用 ​

  • 需要一个比对话框更大的区域,并且希望用户还能看到页面的上下文,例如记录详情、编辑表单、筛选面板、设置面板。
  • 抽屉里还需要进入下一层(例如详情里再看子记录)时,可以嵌套抽屉,子抽屉打开会把父抽屉推开。
  • 只是一句确认或一个短表单,用 Modal 对话框 或 Confirm 确认框;需要常驻页面、可拖动分隔的侧栏,用布局组件而不是抽屉。

在线体验 ​

Drawer 抽屉 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
	"github.com/tangoui/tango/ui"
)

var open bool

func page(c *ui.Context) {
	if ui.Button(c, "打开抽屉").Primary().Clicked() {
		open = true
	}
	ui.Drawer(c, &open, "基础抽屉").Body(func() {
		ui.Text(c, "抽屉内容……")
		ui.Text(c, "支持任意组件。")
	})
}

ui.Drawer(c, &open, title) 在 open 为 true 时从右侧(RTL 下从左侧)滑出。标题栏关闭按钮、点击遮罩和 Esc 默认会把 open 设为 false,本帧 Closed() 返回 true。页脚的“完成”等自定义按钮可直接将 open 设为 false。

常见用法 ​

方向与尺寸 ​

Placement 选择滑出的边:ui.DrawerEnd(默认,LTR 为右侧)、ui.DrawerStart(LTR 为左侧)、ui.DrawerTop、ui.DrawerBottom;ui.DrawerRight / ui.DrawerLeft 是不随 RTL 镜像的物理方向。Size(px) 设置宽度(左右方向)或高度(上下方向),默认 Ant Design 378、Element Plus 窗口的 30%;Large() 是 736 的预设。

go
import (
	"github.com/tangoui/tango/ui"
	"github.com/tangoui/tango/ui/layout"
)

var left, top bool

func page(c *ui.Context) {
	ui.Row(c, func() {
		if ui.Button(c, "左侧").Clicked() {
			left = true
		}
		if ui.Button(c, "顶部").Clicked() {
			top = true
		}
	}, layout.Gap(8))
	ui.Drawer(c, &left, "左侧抽屉").Placement(ui.DrawerStart).Body(func() { ui.Text(c, "从左侧滑出") })
	ui.Drawer(c, &top, "顶部抽屉").Placement(ui.DrawerTop).Size(200).Body(func() { ui.Text(c, "从顶部滑出") })
}

标题栏操作与页脚 ​

Extra(fn) 在标题栏末端放置操作按钮,Footer(fn) 在底部放置固定的页脚(内容区单独滚动)。页脚按钮的逻辑由你处理,例如校验通过后关闭抽屉。

go
import (
	"github.com/tangoui/tango/ui"
)

var (
	editOpen bool
	title    string
)

func page(c *ui.Context) {
	if ui.Button(c, "编辑文章").Clicked() {
		editOpen = true
	}
	ui.Drawer(c, &editOpen, "编辑文章").Size(480).
		Extra(func() {
			ui.Button(c, "预览").Small()
		}).
		Footer(func() {
			if ui.Button(c, "取消").Clicked() {
				editOpen = false
			}
			if ui.Button(c, "保存").Primary().Clicked() && title != "" {
				editOpen = false
			}
		}).
		Body(func() {
			ui.Input(c, &title).Placeholder("文章标题")
		})
}

多层嵌套 ​

在一个抽屉的 Body 里再打开 Drawer,子抽屉滑出时父抽屉向外推开,层次清楚;关闭子抽屉后父抽屉回到原位。Esc 只关闭最上层的抽屉。

在父抽屉上用 Push(px) 调整推出距离,默认为 180px,Push(0) 关闭推出效果。

go
import (
	"github.com/tangoui/tango/ui"
)

var parent, child bool

func page(c *ui.Context) {
	if ui.Button(c, "订单详情").Primary().Clicked() {
		parent = true
	}
	ui.Drawer(c, &parent, "订单详情").Size(520).Body(func() {
		ui.Text(c, "订单号 20261113")
		if ui.Button(c, "查看物流").Clicked() {
			child = true
		}
		ui.Drawer(c, &child, "物流信息").Size(360).Body(func() {
			ui.Text(c, "已签收")
		})
	})
}

拖动边缘调整大小 ​

Resizable(true) 让面向页面的那条边可以拖动;这条边也是可聚焦的分隔条,方向键每次 10px(Shift 50px),Home / End 到上下限。SizeLimits(min, max) 限定范围,BindSize(&px) 绑定当前尺寸,便于持久化用户的选择;Resized() 报告本帧用户改变了尺寸。

go
import (
	"fmt"

	"github.com/tangoui/tango/ui"
)

var (
	open  bool
	width float32 = 420
)

func page(c *ui.Context) {
	if ui.Button(c, "可调整宽度的抽屉").Clicked() {
		open = true
	}
	ui.Text(c, fmt.Sprintf("当前宽度 %.0fpx", width)).Secondary()
	ui.Drawer(c, &open, "可调整宽度").Resizable(true).SizeLimits(280, 720).BindSize(&width).
		Body(func() { ui.Text(c, "拖动左侧边缘调整宽度。") })
}

遮罩与关闭方式 ​

Mask(false) 关闭遮罩,MaskClosable(false) 禁止点击遮罩关闭,Keyboard(false) 禁止 Esc 关闭。Closable(false) 隐藏关闭按钮。ClosablePlacement("start") 或 ClosablePlacement("end") 将它移到标题栏的逻辑起始边或结束边,Ant Design 默认在起始边,Element Plus 和 shadcn 默认在结束边。NoHeader() 隐藏整个标题栏,可在内容里另设关闭入口。

Loading(true) 在内容区显示骨架屏。Body 后可用 AfterOpenChanged() 判断打开或关闭动画是否在本帧结束,再从绑定的 open 读取状态。

go
import (
	"github.com/tangoui/tango/ui"
)

var filterOpen bool

func page(c *ui.Context) {
	if ui.Button(c, "筛选").Clicked() {
		filterOpen = true
	}
	ui.Drawer(c, &filterOpen, "筛选").MaskClosable(false).Body(func() {
		if ui.Button(c, "应用并关闭").Primary().Clicked() {
			filterOpen = false
		}
	})
}

主题与样式 ​

Ant DesignElement Plusshadcn
默认尺寸378px对应窗口边长的 30%384px,上限为对应窗口边长的 75%
标题16px 加粗,标题栏下有分隔线次要文字色、常规字重,无分隔线18px 半粗(600),无分隔线(对应 Sheet)
页脚上方有分隔线无分隔线无分隔线,四周 24 留白
边框无无面向页面的一侧有细边框

组件令牌在 theme.Components.Drawer(theme.DrawerToken):Bg、TitleColor、TitleFontSize、TitleFontWeight、HeaderPaddingTop / HeaderPaddingBottom / HeaderPaddingInline、HeaderBorderColor、Padding、BodyPaddingTop、FooterPaddingTop / FooterPaddingBlock / FooterPaddingInline、FooterBorderColor、CloseIconColor、BorderColor、MaskBg、Shadow,以及 Resizable 相关的 ResizeHandleSize、ResizeLineColor、ResizeLineSize。

ConfigProvider.Defaults("Drawer", ui.Props{...}) 支持 placement(ui.DrawerPlacement 或 "start" / "end" / "left" / "right" / "top" / "bottom",字符串 "left" 和 "right" 表示逻辑起止边)、size(数字或 "large")、width、closable、mask、maskClosable、keyboard 和 closablePlacement。

注意事项 ​

  • 状态放在哪:open 和抽屉里的表单数据放在包级变量或结构体字段里;Resizable 选出的尺寸在抽屉存在期间由组件保存,需要跨会话保留时用 BindSize。
  • 标题即身份:抽屉的内部状态(动画、尺寸)由“作用域 + 标题”标识,打开期间不要改标题;同一作用域内不要有两个同名抽屉,必要时用 ui.Key 包住。
  • 焦点:带遮罩的抽屉打开时,焦点移入抽屉并留在其中,关闭后回到打开前的控件。Esc 默认关闭最上层抽屉。
  • RTL:DrawerEnd / DrawerStart 随方向镜像,需要固定物理边时用 DrawerRight / DrawerLeft。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Drawer 抽屉

gallery 演示 Drawer 的具名 Render 函数,源码 page_nav_feedback.go#L156。

加载中…

Drawer 关闭按钮位置与默认尺寸

gallery 演示 DrawerAlign 的 Render 函数,源码 page_b3k.go#L39。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Drawer ​

go
func Drawer(c *Context, open *bool, title string) *DrawerW

Drawer starts a drawer shown while *open; chain options, then Body. A Drawer opened inside another one's Body pushes the parent aside.

Besides the keys read in shad2c_defaults.go, ConfigProvider.Defaults( "Drawer", ...) reads "keyboard" (bool) and "closablePlacement" ("start" / "end").

返回 *DrawerW,可链式调用的方法见 DrawerW 方法。

DrawerW 方法 ​

ui.DrawerW:DrawerW is a panel sliding from a window edge: four placements, nested drawers push their parent aside, header extra, footer, sizes.

方法说明签名
AfterOpenChangedAfterOpenChanged reports, after Body, that the open or close slide finished this frame (antd afterOpenChange; read *open for the state).AfterOpenChanged() bool
BindSizeBindSize binds the size to *px: a positive value overrides Size, and a resize writes the new size back.BindSize(px *float32) *DrawerW
BodyBody builds the drawer content.Body(fn func()) *DrawerW
ClosableClosable(on bool) *DrawerW
ClosablePlacementClosablePlacement puts the close button "start" (before the title) or "end" (after the title and extra); default from the theme (Ant start, Element and shadcn end; antd closable.placement).ClosablePlacement(p string) *DrawerW
ClosedClosed reports close / mask / Escape this frame.Closed() bool
CurrentSizeCurrentSize returns the width / height used this frame (after Body).CurrentSize() float32
ExtraExtra(fn func()) *DrawerW
FooterFooter(fn func()) *DrawerW
KeyboardKeyboard sets whether Escape closes the drawer (default true; antd keyboard, Element close-on-press-escape).Keyboard(on bool) *DrawerW
LargeLarge uses the large preset (736).Large() *DrawerW
LoadingLoading shows an active Skeleton instead of the content (antd loading).Loading(on bool) *DrawerW
MaskMask(on bool) *DrawerW
MaskBlurMaskBlur sets the Gaussian blur radius of the canvas behind the mask, in logical pixels.MaskBlur(radius float32) *DrawerW
MaskClosableMaskClosable(on bool) *DrawerW
NoHeaderNoHeader() *DrawerW
PlacementPlacement selects the edge (DrawerEnd default: right, left in RTL).Placement(p DrawerPlacement) *DrawerW
PushPush sets how far a nested drawer pushes this one aside (default 180; 0 turns it off; antd push.distance).Push(px float32) *DrawerW
ResizableResizable lets the user drag the edge facing the page to change the width (left / right) or height (top / bottom).Resizable(on bool) *DrawerW
ResizedResized reports a size change by the user this frame.Resized() bool
SizeSize sets the width (left/right) or height (top/bottom); default from theme.DrawerToken: 378 (Ant), 30% of the window (Element), 384 but at most 75% (shadcn Sheet).Size(px float32) *DrawerW
SizeLimitsSizeLimits bounds a resizable drawer (px).SizeLimits(min, max float32) *DrawerW

相关链接 ​

以 MIT 许可发布