Keel v0.5.1 中文 GitHub

Accordion

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/accordion.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"
)

func init() {
	registerSection("accordion", "display", func() core.Widget {
		txt := func(s string) el.View { return el.ViewFunc(func(*el.Context) el.Element { return el.Text(s) }) }
		name := kit.Input(demoText("Display name", "显示名称")).Placeholder(demoText("Input is preserved when collapsed", "收起后保留输入内容"))
		one := kit.Accordion().Add(demoText("Profile", "个人资料"), name).Add(demoText("Notification settings", "通知设置"), kit.Switch(demoText("Receive notifications", "接收通知"), true)).Add(demoText("Unavailable", "暂不可用"), txt(demoText("Disabled item", "禁用项目")))
		one.Heading(0, el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().Row().Gap(8).Child(kit.Icon(kit.IconUser).Size(16).Render(cx), el.Text(demoText("Profile", "个人资料")).Bold(), kit.Tag(demoText("Editable", "可编辑")).Render(cx))
		}))
		one.SetValue(0)
		one.SetItemDisabled(2, true)
		faq := kit.Accordion().Multiple().
			Add(demoText("How do I use the keyboard?", "如何用键盘操作?"), txt(demoText("Tab focuses a heading; ↑ ↓ changes headings; Enter or Space expands; Home / End jumps to the first or last heading.", "Tab 聚焦标题,↑ ↓ 切换标题,Enter 或空格展开,Home / End 跳到首尾。"))).
			Add(demoText("Can multiple items be expanded?", "可以同时展开多项吗?"), txt(demoText("Multiple is enabled, so two items can be expanded at once.", "这个面板启用了 Multiple,可以同时展开两项。")))
		small := kit.Accordion().Size(kit.AccordionSizeSmall).Bordered(false).
			Add(demoText("Small · Borderless", "Small · 无边框"), txt(demoText("Hide the outer border and section dividers; keep the background and rounded corners.", "隐藏外框和分节线,保留背景与圆角。"))).
			Add(demoText("Second section", "第二节"), txt(demoText("Changing size preserves the expanded state.", "尺寸切换不改变展开状态。")))
		small.SetValue(0)
		large := kit.Accordion().Size(kit.AccordionSizeLarge).Add(demoText("Large", "Large · 大尺寸"), txt(demoText("The heading, arrow, spacing, and body adjust together.", "标题、箭头、间距与正文一起调整。")))
		large.SetValue(0)
		tiny := kit.Accordion().Size(kit.AccordionSizeXSmall).Add(demoText("XSmall · Compact", "XSmall · 紧凑"), txt(demoText("Suitable for information-dense panels.", "适用于信息密集的面板。")))
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(16).W(el.Dp(480)).MaxW(el.Full).Child(one.Render(cx), faq.Render(cx), small.Render(cx), large.Render(cx), tiny.Render(cx))
		}))
	})
}

Expandable sectioned panels. By default, only one section is expanded at the same time, and multiple sections can be expanded after Multiple().

faq := kit.Accordion().Add("How do I request a refund?", answer1).Add("When will the payment arrive?", answer2).Multiple()
  • Titles can get focus: ↑ ↓ Home End Move between titles and skip disabled sections; press Enter or space to expand or collapse.
  • Value() returns the sequence number of the expanded section; SetValue(i...) does not trigger a callback, and only the first one is retained in single section mode; SetItemDisabled(i, bool).

Agent: container role group; each title is disclosure, value is expanded / collapsed; expanded content is listed separately.

Verify: go run ./examples/components -section accordion, add -theme dark to check the dark theme.

Heading(i, view) Customize the display title of item i. The original title is still used as the accessibility name. Trigger(i) and Content(i) can be entered into custom layouts separately, each rendered once; do not render the entire Accordion at the same time. Arrow keys will skip titles that are not rendered, disabled, or in a disabled container.

SetDisabled disables the entire group, SetItemDisabled disables both the item's title and expanded content, but retains the expanded state. Expand/collapse has 180ms animation, continuous switching reverses from the current height; reduced motion switches immediately. When folding, the input status is retained. If the focus is in the text, it will return to the title. The text cannot be continued during the folding process.

Bordered(false) hides the outer frame and section dividers, retains the background, rounded corners, and expanded state; the outline is off by default, and Bordered(true) explicitly enables it. Size supports AccordionSizeXSmall, AccordionSizeSmall, AccordionSizeMedium (default), AccordionSizeLarge, and uniformly adjusts the inherited font size of titles, arrows, spacing, and text. The default file retains the original font size inheritance; the font size explicitly set for the custom title or body takes precedence.