Collapsible
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/collapsible.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
"github.com/dyike/keel/ui/theme"
)
func init() {
registerSection("collapsible", "display", func() core.Widget {
field := kit.Input(demoText("Order number", "订单编号"))
field.SetValue("SO-1001")
details := kit.Collapsible(demoText("Order details", "订单详情"), field)
details.SetValue(true)
separate := kit.Collapsible(demoText("Advanced filters", "高级筛选"), kit.Input("").Placeholder(demoText("Customer name", "客户名称"))).Heading(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().Row().Gap(8).Child(kit.Icon(kit.IconSearch).Size(16).Render(cx), el.Text(demoText("Advanced filters", "高级筛选")).Bold(), kit.Tag(demoText("Optional", "可选")).Render(cx))
}))
return el.Root(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(16).Items(el.Stretch).Child(
el.Text("Collapsible").TextSize(24).Bold(), details.Render(cx),
el.Div().Rounded(8).Border(1, theme.Outline).Child(separate.Trigger().Render(cx), el.Text(demoText("Description text can appear between the trigger and content.", "触发器与内容之间可以放说明文字。")).Px(14).TextColor(theme.Muted), separate.Content().Render(cx)),
el.Text(demoText("Repeated clicks reverse the animation. Reduced motion expands immediately.", "快速重复点击可反转动画;减少动画设置下立即展开。")),
)
}))
})
}
The expanded/collapsed state of a single section of content can be used as a whole, or the trigger and content can be placed separately in the layout.
section := kit.Collapsible("Advanced filters", form)
section.SetValue(true)
// Default bordered panel: section.Render(cx)
row.Child(section.Trigger().Render(cx))
body.Child(section.Content().Render(cx))
Value / SetValue reads and sets the status, and the program settings do not call back; OnChange is called when the user switches. SetDisabled disables both independent triggers and content. The Trigger/Content of each instance is rendered once. Do not call the overall Render at the same time.
Heading(view) Customize the display content of the title. The label during construction is still used as the accessibility name. Place text, icons and other display content inside the title to avoid nesting another interactive control.
The trigger supports Tab, Enter / Space, the role is disclosure, and the value is expanded / collapsed. When the content being edited is closed, the focus returns to the trigger and the input state is retained. The content being folded cannot be operated.
Expand and collapse use 180ms height animation; repeated switching reverses from the current height, and the text maintains a natural layout without squeezing the text. Switch immediately when reduced motion, and the initial expanded state is also displayed directly.
Verify: go run ./examples/components -section collapsible, add -theme dark to check the dark theme.