Collapse 折叠面板
把相关内容收进可展开、可收起的面板,并用调用方状态保存当前展开项。
何时使用
- FAQ、设置分组、详情摘要等需要按需展开的内容。
- 多个面板可同时打开时使用普通模式;需要一次只看一组内容时使用
Accordion()。 - 页面级导航或滚动目录不要用 Collapse,应使用 Anchor 锚点;互斥的视图切换使用 Tabs 标签页。
- 侧栏需要随窗口断点自动折叠时,使用 CollapsibleSider 可折叠侧栏。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
var openPanels = []string{"intro"}
func page(c *ui.Context) {
ui.Collapse(c, &openPanels, []ui.CollapseItem{
{Key: "intro", Label: "简介", Body: func() { ui.Text(c, "这里是简介内容") }},
{Key: "details", Label: "详情", Body: func() { ui.Text(c, "这里是详情内容") }},
}).Show()
}active 是展开面板 key 的切片指针。即时模式每帧都会根据它重绘,状态应放在页面状态或包级变量中,而不是在 page 函数里每次重新初始化。
常见用法
手风琴
Accordion() 限制最多一个面板展开,适合互斥的说明或步骤。
import "github.com/tangoui/tango/ui"
var accordionOpen = []string{"step-1"}
func page(c *ui.Context) {
ui.Collapse(c, &accordionOpen, []ui.CollapseItem{
{Key: "step-1", Label: "第一步", Body: func() { ui.Text(c, "准备数据") }},
{Key: "step-2", Label: "第二步", Body: func() { ui.Text(c, "提交数据") }},
}).Accordion().Show()
}禁用面板与额外操作
Disabled 禁止打开面板,Extra 在标题行尾渲染操作或状态;额外节点应避免触发与标题相冲突的动作。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
var open = []string{"settings"}
func page(c *ui.Context) {
ui.Collapse(c, &open, []ui.CollapseItem{
{Key: "settings", Label: "设置", Extra: func() { ui.Icon(c, icon.Setting).Size(14) }, Body: func() { ui.Text(c, "可编辑设置") }},
{Key: "locked", Label: "锁定内容", Disabled: true, Body: func() { ui.Text(c, "不可用") }},
}).Show()
}无边框与尺寸
Borderless()、Ghost() 可用于卡片内部或页面背景;Size(ui.Small) 用于紧凑区域,尺寸只影响标题区域的内边距。
import "github.com/tangoui/tango/ui"
var smallOpen = []string{"one"}
func page(c *ui.Context) {
ui.Collapse(c, &smallOpen, []ui.CollapseItem{
{Key: "one", Label: "紧凑面板", Body: func() { ui.Text(c, "内容") }},
}).Borderless().Size(ui.Small).Show()
}侧栏折叠
CollapsibleSider 将折叠状态绑定到 *bool,带底部触发条;Breakpoint 在窄窗口自动收起,菜单应复用同一个状态。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
var (
siderCollapsed bool
selected = "home"
menuItems = []ui.MenuItem{{Key: "home", Label: "首页"}, {Key: "settings", Label: "设置"}}
)
func page(c *ui.Context) {
ui.Layout(c, func() {
ui.CollapsibleSider(c, 220, &siderCollapsed).Breakpoint(ui.LG).Body(func() {
ui.Menu(c, menuItems, &selected).Collapsed(siderCollapsed).Show()
})
ui.Content(c, func() { ui.Text(c, "当前:" + selected) }, layout.Pad(24))
})
}主题与样式
Collapse 的边框、标题背景、标题内边距和箭头颜色来自主题组件令牌,三套主题会分别采用 Ant Design 的分组边框、Element Plus 的简洁标题和 shadcn 的卡片式分隔。需要统一默认间距时优先通过主题配置覆盖,不要在每个 Body 中重复画边框。
注意事项
active的 key 必须稳定且唯一;IsOpen(key)可读取当前状态,Changed()可检测本帧是否发生切换。- 面板内容仍会参与当前帧构建;很重的内容应拆分为更小的页面区域,避免在大量面板中重复创建大数据视图。
- 标题和额外操作应提供清晰文本;面板标题可用 Tab 聚焦并用 Enter 或空格切换。自定义图标按钮要设置
A11yLabel。 - RTL 作用域下箭头位置和方向会自动镜像;不要用固定的左右坐标覆盖它。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Collapse
func Collapse(c *Context, active *[]string, items []CollapseItem) *CollapseWCollapse starts a collapse group bound to the app's open panel keys. Configure with chained modifiers and finish with Show.
返回 *CollapseW,可链式调用的方法见 CollapseW 方法。
CollapseW 方法
ui.CollapseW:CollapseW is an accordion / collapse panel group.
| 方法 | 说明 | 签名 |
|---|---|---|
Accordion | Accordion keeps at most one panel open. | Accordion() *CollapseW |
BeforeCollapse | BeforeCollapse is asked before a panel opens or closes (Element before-collapse); returning false keeps the panel as it is. | BeforeCollapse(fn func(key string) bool) *CollapseW |
Borderless | Borderless removes the outer border (Ant bordered={false}). | Borderless() *CollapseW |
Changed | Changed reports a panel toggle in the last Show. | Changed() bool |
Collapsible | Collapsible sets which part of every header toggles its panel (antd collapsible): "" the whole header, "header" only the title and the arrow, "icon" only the arrow, "disabled" nothing. | Collapsible(mode string) *CollapseW |
ExpandIcon | ExpandIcon replaces the expand arrow (antd expandIcon, Element icon). | ExpandIcon(ic *icon.Icon) *CollapseW |
ExpandIconPlacement | ExpandIconPlacement places the expand arrow: "start" (before the label, Ant's default) or "end" (Element and shadcn's default); antd expandIconPlacement, Element expand-icon-position ("left" / "right" are accepted too). | ExpandIconPlacement(p string) *CollapseW |
Ghost | Ghost makes the panels transparent and borderless. | Ghost() *CollapseW |
IconEnd | IconEnd puts the expand arrow at the end of the header (Element's default). | IconEnd() *CollapseW |
IconStart | IconStart puts the expand arrow before the label (Ant's default). | IconStart() *CollapseW |
IsOpen | IsOpen reports whether key is open. | IsOpen(key string) bool |
Layout | Layout applies layout options to the group (call after Show). | Layout(o ...layout.Option) *CollapseW |
Show | Show builds the panels. | Show() *CollapseW |
Size | Size sets header padding (Small / Middle / Large). | Size(s ControlSize) *CollapseW |
CollapseItem 参数类型
ui.CollapseItem:CollapseItem is one panel of a Collapse.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Label | string | |
Extra | content at the inline end of the header | func() |
Body | func() | |
Disabled | bool | |
NoArrow | bool | |
Collapsible | Collapsible overrides the group's Collapsible for this panel: "header" (only the title and the arrow toggle), "icon" (only the arrow toggles) or "disabled"; "" uses the group's mode. | string |