Skip to content

Tabs 标签页 ​

在同一工作区切换内容面板,例如概览、事件、设置。用稳定的 Key 标识标签,用 Label 显示标题。

在线体验 ​

点击不同标签查看面板变化;聚焦后可以用方向键切换。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。

Tabs 标签页 · 在线体验真实 Go 组件
在独立页面打开 ↗

基本用法 ​

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

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

var active = "overview"

func page(c *ui.Context) {
    ui.Tabs(c, &active, []ui.TabItem{
        {Key: "overview", Label: "Overview"},
        {Key: "events", Label: "Events"},
        {Key: "settings", Label: "Settings"},
    }).Show()
    switch active {
    case "events":
        ui.Text(c, "Calls, events and streams")
    default:
        ui.Text(c, "Build your interface with TanGo")
    }
}

常用参数与方法 ​

参数 / 方法说明
active *string当前标签的 Key
items []ui.TabItem标签项列表
Card()卡片式标签
Placement(ui.Placement)标签栏的位置
Closable()允许关闭标签
Show()完成标签栏构建
Changed() bool读取当前帧是否切换标签

使用说明 ​

本文示例根据 active 渲染内容。也可以使用 Panel 回调组织面板;可关闭标签的应用需要维护自己的标签列表。

更多场景 ​

官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。

Tabs 标签页 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

实现与更多示例 ​

全部示例代码 ​

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

Tabs 标签页

gallery 演示 Tabs 的 Render 函数,源码 page_nav.go#L19。

加载中…

Tabs 竖向溢出、边框卡片与附加内容

gallery 演示 TabsLayout 的具名 Render 函数,源码 page_shad2c_tabs.go#L23。

加载中…

Tabs 可编辑、位置与溢出

gallery 演示 TabsMore 的具名 Render 函数,源码 page_p2c_nav.go#L61。

加载中…

Tabs 尺寸、指示条、等分与切换拦截

gallery 演示 TabsAlign 的 Render 函数,源码 page_b2h.go#L45。

加载中…

Tabs 拖拽排序

gallery 演示 TabsDraggable 的具名 Render 函数,源码 page_dock.go#L106。

加载中…

完整 API ​

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

入口函数 ​

ui.Tabs ​

go
func Tabs(c *Context, active *string, items []TabItem) *TabsW

Tabs adds a tab bar bound to the active key; render the panel yourself based on *active (or with Panel). Call Show after options.

ConfigProvider.Defaults("Tabs", ...) keys: "type" ("line" | "card" | "editable-card" | "border-card"), "size" (ControlSize), "centered" (bool), "tabPlacement" (Placement or Ant v6 "top" | "bottom" | "start" | "end"; the deprecated keys "placement" and "tabPosition" take "top" | "bottom" | "left" | "right" too), "stretch" (bool), "tabBarGutter" (px), "variant" ("default" | "line", the shadcn TabsList variant).

active may be nil (the Tabs keep the key themselves). When *active is not a key of items, the tab at the last active index (clamped; the first tab at first) becomes active, like Ant's Tabs when the active key goes away; Changed does not report that fallback.

Under shadcn/ui every combination is drawn as the segmented TabsList (muted track, background pill with shadow-sm): vertical placements stack the triggers, Closable adds a small x to each trigger, OnAdd a ghost + button after the list, Card switches to an outline track and BorderCard puts the list and panel in a bordered card.

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

TabsW 方法 ​

ui.TabsW:TabsW is a tab bar: line or card style, editable cards (close and add buttons), top / bottom / left / right placement, centered tabs and a scrolling tab list with a "more" dropdown for the tabs that do not fit.

方法说明签名
ActiveTabFontWeightActiveTabFontWeight sets the active tab label's font weight (100–900).ActiveTabFontWeight(weight int) *TabsW
AddIconAddIcon replaces the + of the add button (Ant addIcon).AddIcon(ic *icon.Icon) *TabsW
AddedAdded reports a click on the add button this frame.Added() bool
BeforeLeaveBeforeLeave is asked before the active tab changes (Element before-leave): returning false keeps the current tab.BeforeLeave(fn func(newKey, oldKey string) bool) *TabsW
BorderCardBorderCard puts the bar and the panel built by Panel in a bordered card whose header strip holds the tabs (Element type="border-card").BorderCard() *TabsW
CardCard() *TabsW
CenteredCentered centers the tabs in the bar.Centered() *TabsW
ChangedChanged() bool
ClosableClosable makes an editable card bar (Ant type="editable-card", Element closable): every tab without NoClose gets a close button; read Closed.Closable() *TabsW
ClosedClosed returns the key of the tab closed this frame ("" when none).Closed() string
DraggableDraggable lets the user reorder the tabs by dragging them (the moved tab becomes active); read the new order with Reordered.Draggable() *TabsW
ExtraExtra(fn func()) *TabsW
ExtraLeftExtraLeft builds content at the start of the bar, before the tabs (Ant tabBarExtraContent.left); Extra is the right one.ExtraLeft(fn func()) *TabsW
HeightHeight fixes the height of a Left / Right Tabs (bar and panel); tabs that do not fit scroll and are listed in the more menu.Height(px float32) *TabsW
IndicatorIndicator sets the ink bar of line tabs (Ant v6 indicator): size is its length in px (0: the tab's extent; Element: the label's), align places it along the tab: "start", "center" (default) or "end".Indicator(size float32, align string) *TabsW
IndicatorThicknessIndicatorThickness sets the cross-axis thickness of a line-tab indicator in logical pixels.IndicatorThickness(px float32) *TabsW
MoreIconMoreIcon replaces the icon of the more button (Ant more.icon).MoreIcon(ic *icon.Icon) *TabsW
NoMarginNoMargin drops the gap between the bar and the content (for a bar that sits at the bottom edge of a header, as in PageContainer).NoMargin() *TabsW
OnAddOnAdd shows the add button of an editable card bar; fn runs when it is clicked (Added reports it as well).OnAdd(fn func()) *TabsW
OnTearOffOnTearOff makes the tabs draggable and lets a tab be dragged out of the window: released outside, fn runs with its key and the release point, and TornOff reports them.OnTearOff(fn func(key string, pos geom.Point)) *TabsW
PanelPanel builds the content of the active tab in a tab panel (exposed to screen readers as the tabpanel labelled by the active tab).Panel(fn func()) *Box
PlacementPlacement puts the bar at the Top (default), Bottom, Left or Right of the panel built by Panel.Placement(p Placement) *TabsW
RemoveIconRemoveIcon replaces the x of the close buttons (Ant removeIcon).RemoveIcon(ic *icon.Icon) *TabsW
ReorderedReordered returns the items in their new order after a drag or keyboard move this frame (nil when none).Reordered() []TabItem
ShowShow builds the tab bar.Show() *TabsW
SizeSize(s ControlSize) *TabsW
StretchStretch makes the tabs of a horizontal bar share its width (Element stretch); the bar no longer scrolls.Stretch() *TabsW
TabBarGutterTabBarGutter sets the space between two tabs (Ant tabBarGutter; the default comes from the theme: Ant 32 for line tabs, 2 for cards).TabBarGutter(px float32) *TabsW
TabBarStyleTabBarStyle applies layout options to the bar (padding, margin ...; Ant tabBarStyle).TabBarStyle(opts ...layout.Option) *TabsW
TabClickedTabClicked returns the key of the tab clicked this frame, also when it is already active ("" when none; Ant onTabClick, Element tab-click).TabClicked() string
TabItemHeightTabItemHeight sets the height of horizontal tabs in logical pixels.TabItemHeight(px float32) *TabsW
TabPlacementTabPlacement puts the bar by Ant v6 name: "top" (default), "bottom", "start" or "end" (logical sides: mirrored in RTL); "left" and "right" are accepted as their deprecated aliases.TabPlacement(p string) *TabsW
TornOffTornOff reports the tab torn off this frame and its release point (see OnTearOff).TornOff() (key string, pos geom.Point, ok bool)
VariantVariant picks the list look in every theme: "default" or "line" (line tabs: a transparent list with an underline under the active tab).Variant(v string) *TabsW

TabItem 参数类型 ​

ui.TabItem:TabItem is a tab.

字段说明类型
Keystring
Labelstring
Icon*icon.Icon
Disabledbool
NoCloseno close button on this tab of a Closable barbool

以 MIT 许可发布