Keel v0.5.1 中文 GitHub

Tabs

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/tabs.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 (
	"fmt"
	"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("tabs", "navigation", func() core.Widget {
		tabs := kit.Tabs().
			Add(demoText("General", "基本"), kit.Input(demoText("Name", "名称")).Placeholder(demoText("Content is preserved after switching", "切换后内容保留"))).
			Add(demoText("Notifications", "通知"), kit.Switch(demoText("Email reminders", "邮件提醒"), true)).
			Add(demoText("About", "关于"), el.ViewFunc(func(*el.Context) el.Element {
				return el.Text(demoText("Keel component examples 1.0", "Keel 组件示例 1.0"))
			}))
		tabs.Closable(tabs.Remove).Reorderable(nil).Trailing(kit.Button(demoText("Add", "新增"), func() { tabs.Add(demoText("New tag", "新标签"), kit.Input(demoText("Content", "内容"))) }).Variant(kit.ButtonGhost))
		variants := []*kit.TabsView{}
		for _, variant := range []kit.TabsVariant{kit.TabsUnderline, kit.TabsPill, kit.TabsOutline, kit.TabsSegmented} {
			variants = append(variants, kit.Tabs().Variant(variant).AddItem(kit.TabItem{Title: demoText("Overview", "概览"), Icon: kit.IconHome}).AddItem(kit.TabItem{Title: demoText("Message", "消息"), Icon: kit.IconInbox, Content: el.ViewFunc(func(*el.Context) el.Element {
				return el.Div().Row().Gap(theme.SpaceSm).Child(el.Text(demoText("Message", "消息")), el.Text("3").Bold())
			})}).AddItem(kit.TabItem{Title: demoText("Disabled", "已禁用"), Icon: kit.IconLock, Disabled: true}))
		}
		scrolling := kit.Tabs().Scrollable(true).MaxWidth(100).Variant(kit.TabsPill)
		for i := range 12 {
			scrolling.Add(fmt.Sprintf(demoText("Document %02d", "文档 %02d"), i+1), nil)
		}
		scrolling.Trailing(kit.Button(demoText("Last item", "最后一项"), func() { scrolling.ScrollTo(scrolling.Len() - 1) }).Compact(true))
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			out := el.Div().P(24).Gap(theme.SpaceXl).W(el.Dp(460)).MaxW(el.Full).Child(tabs.Render(cx))
			for i, v := range variants {
				out.Child(el.Text([]string{demoText("Underline", "下划线"), demoText("Pill", "胶囊"), demoText("Tinted", "着色"), demoText("Segmented", "分段")}[i]).TextColor(theme.Muted), v.Render(cx))
			}
			out.Child(el.Text(demoText("Horizontal scrolling · Labels limited to 100dp", "横向滚动 · 标签宽度上限 100dp")).TextColor(theme.Muted), scrolling.Render(cx))
			return out
		}))
	})
}

Tabs with underline, pill, tinted, and segmented appearances. Every variant is borderless.

tabs := kit.Tabs().Add("General", basicForm).Add("Notifications", notifySettings).OnChange(onTab)
  • Variant(TabsUnderline/TabsPill/TabsOutline/TabsSegmented) selects the appearance and keeps underline as the default. TabsOutline is retained as the compatibility name for a borderless tinted tab. Colors switch with the theme.
  • AddItem(TabItem{Title, Page, Icon, Content, Disabled}) Add an icon or rich label. Content replaces the visible title and should be a display element; Title is still used for the Agent name and overflow menu. Rich tags are laid out within a limited width, and text truncation requires the content to set MaxLines by itself.
  • SetItem(i, item) updates the entry and preserves stable identity; SetItemDisabled(i, bool) disables a single entry. Disabled items cannot be clicked, closed, or dragged, and arrow keys and overflow menus skip them. Disable the current item to automatically select the next available item; when all are disabled, the current page is retained, the tab does not enter Tab navigation, and the page content remains available. Restore selection after re-enabling an entry. Program modification does not trigger OnChange; SetValue does not select disabled items.
  • Only render the current page. Each page is a View held by the application itself, and the state remains after switching.
  • The Tab key focuses on the current tab, ← → switches, cycles through the beginning and end, and Home / End jumps to the beginning and end.
  • When the labels cannot be placed, the extra ones will be put into the "More" menu at the end. The currently selected label will always remain in the column, and you can continue to use the arrow keys to operate it; long titles will be truncated. Like Toolbar, tabs should be placed in a position with width constraints.
  • Closable(fn) adds a close button to each label, fn(i) determines the meaning of close, and usually passes tabs.Remove directly. The close button is next to the label but not inside the label, so clicking it will not switch to this label first.
  • Reorderable(fn) allows dragging the visible labels to rearrange them and submitting after releasing the pointer; canceling the drag does not change the order. Move(from, to) can programmatically rearrange any label without triggering a callback; the page and editing status follow the stable identity, and the selected page remains unchanged. Dragging does not automatically open the overflow menu.
  • Delete calls the close callback when the tab is focused. After closing the current page, the focus moves to the remaining adjacent tabs; closing other pages keeps the current page.
  • Leading(el.View) / Trailing(el.View) places fixed left and right areas; Size(dp) sets the label height, default 40, minimum 24. SetDisabled Disables tabs, close buttons, dragging, and the current page.
  • MaxWidth(dp) limits the width of a single label area (including icons and padding, excluding the close button next to it); 0 cancels the upper limit, and negative and non-finite values are ignored. Text is automatically omitted by default, and rich content is truncated by itself.
  • Scrollable(true) Changed the tab bar to horizontal scrolling, all tabs are kept in the track, and are no longer included in the overflow menu; Fixed Leading/Trailing not to participate in scrolling. Automatically scroll into the viewport after the selected item changes, and the keyboard focus will be transferred after the scrolling is completed. Switching back to false restores the original overflow menu behavior.
  • ScrollTo(i) requests to display an item without changing the selected item or triggering a callback; it can be called before the first layout and requests to follow the label identity. ScrollState(cx) returns the horizontal offset, viewport width, and content width of the most recent drawing, in dp. Manual scrolling will not be forced back by the currently selected item.
  • Value() / SetValue(i) (no callback is triggered), Remove(i), Len().

Agent: The label bar is tablist, each label is tab, selected represents the current label; the current page is tabpanel named after the label title.

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