@data-slot/tabs

中文文档
1.1.3发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 6855

Headless tabs component for vanilla JavaScript. Accessible, unstyled, tiny.

原始使用文档
文档目录13 节
  1. Installation
  2. Quick Start
  3. API
  4. Initialization
  5. Slots
  6. Data Attributes
  7. Options
  8. Controller
  9. Events
  10. Styling
  11. Keyboard Navigation
  12. Accessibility
  13. License

@data-slot/tabs

Headless tabs component for vanilla JavaScript. Accessible, unstyled, tiny.

Installation

npm install @data-slot/tabs

Quick Start

<div data-slot="tabs" data-default-value="one">
  <div data-slot="tabs-list">
    <button data-slot="tabs-trigger" data-value="one">Tab One</button>
    <button data-slot="tabs-trigger" data-value="two">Tab Two</button>
    <button data-slot="tabs-trigger" data-value="three">Tab Three</button>
    <div data-slot="tabs-indicator"></div>
  </div>
  <div data-slot="tabs-content" data-value="one">Content One</div>
  <div data-slot="tabs-content" data-value="two">Content Two</div>
  <div data-slot="tabs-content" data-value="three">Content Three</div>
</div>

<script type="module">
  import { create } from "@data-slot/tabs";
  
  const controllers = create();
</script>

API

Initialization

create(scope?)

Auto-discover and bind all tabs instances in a scope (defaults to document).

import { create } from "@data-slot/tabs";

const controllers = create(); // Returns TabsController[]

createTabs(root, options?)

Create a controller for a specific element.

import { createTabs } from "@data-slot/tabs";

const tabs = createTabs(element, {
  defaultValue: "one",
  orientation: "horizontal",
  onValueChange: (value) => console.log(value),
});

Slots

Runtime Slots

  • tabs - Root element that manages the selected tab and activation mode.
  • tabs-list - Required tab-list container that receives tablist semantics and orientation.
  • tabs-trigger - Tab button identified by data-value; receives selected state and keyboard navigation. At least one trigger is required.
  • tabs-content - Panel matched to its trigger by data-value; receives tabpanel semantics and is shown when selected.
  • tabs-indicator - Optional animated highlight whose position and size follow the selected trigger.

Markup

<div data-slot="tabs" data-default-value="initial-tab">
  <div data-slot="tabs-list">
    <button data-slot="tabs-trigger" data-value="unique-id">Label</button>
    <!-- Optional animated indicator -->
    <div data-slot="tabs-indicator"></div>
  </div>
  <div data-slot="tabs-content" data-value="unique-id">Panel content</div>
</div>

Data Attributes

Options can also be set via data attributes on the root element. JS options take precedence.

AttributeTypeDefaultDescription
data-default-valuestringfirst enabled tabInitial selected tab
data-orientationstring"horizontal"Tab orientation: horizontal, vertical
data-activation-modestring"auto"Activation mode: auto, manual
<!-- Vertical tabs with manual activation -->
<div data-slot="tabs" data-orientation="vertical" data-activation-mode="manual">
  ...
</div>

Options

OptionTypeDefaultDescription
defaultValuestringFirst enabled trigger's valueInitial selected tab
orientation"horizontal" | "vertical""horizontal"Tab orientation for keyboard nav
activationMode"auto" | "manual""auto"How tabs are activated with keyboard
onValueChange(value: string) => voidundefinedCallback when selected tab changes

Controller

Method/PropertyDescription
select(value)Select a tab by value
valueCurrently selected value (readonly string)
updateIndicator()Recalculate indicator position after layout changes
destroy()Cleanup all event listeners

Events

Outbound Events

Listen for changes via custom events:

element.addEventListener("tabs:change", (e) => {
  console.log("Selected tab:", e.detail.value);
});

Inbound Events

Control the tabs via events:

EventDetailDescription
tabs:set{ value: string }Select a tab programmatically
// Select a tab
element.dispatchEvent(
  new CustomEvent("tabs:set", { detail: { value: "two" } })
);

Deprecated Events

The following event is deprecated and will be removed in v1.0:

// Deprecated: tabs:select event
element.dispatchEvent(
  new CustomEvent("tabs:select", { detail: { value: "two" } })
);

// Deprecated: string detail
element.dispatchEvent(
  new CustomEvent("tabs:select", { detail: "two" })
);

Use tabs:set with { value: string } instead.

Styling

Basic Styling

/* Hidden panels */
[data-slot="tabs-content"][hidden] {
  display: none;
}

/* Active trigger */
[data-slot="tabs-trigger"][aria-selected="true"] {
  font-weight: bold;
  border-bottom: 2px solid currentColor;
}

/* Or use data-state */
[data-slot="tabs-trigger"][data-state="active"] {
  color: blue;
}

[data-slot="tabs-trigger"][data-state="inactive"] {
  color: gray;
}

Panel Activation Direction

Panels receive data-activation-direction after tab changes (not on initial mount):

  • Horizontal: left, right
  • Vertical: up, down

Use it for directional content animations:

[data-slot="tabs-content"][data-activation-direction="right"] {
  animation: slide-in-from-right 200ms ease;
}

[data-slot="tabs-content"][data-activation-direction="left"] {
  animation: slide-in-from-left 200ms ease;
}

Animated Indicator

The indicator receives CSS variables for positioning:

[data-slot="tabs-indicator"] {
  position: absolute;
  left: var(--active-tab-left);
  width: var(--active-tab-width);
  height: 2px;
  background: currentColor;
  transition: left 0.2s, width 0.2s;
}

/* Vertical orientation */
[data-slot="tabs-list"][aria-orientation="vertical"] [data-slot="tabs-indicator"] {
  top: var(--active-tab-top);
  height: var(--active-tab-height);
  width: 2px;
}

CSS Variables

VariableDescription
--active-tab-leftLeft offset of active trigger
--active-tab-widthWidth of active trigger
--active-tab-topTop offset of active trigger
--active-tab-heightHeight of active trigger

Tailwind Example

<div data-slot="tabs">
  <div data-slot="tabs-list" class="relative flex border-b">
    <button 
      data-slot="tabs-trigger" 
      data-value="one"
      class="px-4 py-2 aria-selected:text-blue-600"
    >
      Tab One
    </button>
    <div 
      data-slot="tabs-indicator" 
      class="absolute bottom-0 h-0.5 bg-blue-600 transition-all"
      style="left: var(--active-tab-left); width: var(--active-tab-width)"
    ></div>
  </div>
  <div data-slot="tabs-content" data-value="one" class="p-4">
    Content
  </div>
</div>

Keyboard Navigation

The tables below describe activationMode: "auto" (the default). In "manual" mode, arrow keys, Home, and End only move focus; Enter or Space selects the focused tab. Disabled triggers are skipped.

Horizontal Orientation

KeyAction
ArrowLeftSelect previous tab
ArrowRightSelect next tab
HomeSelect first tab
EndSelect last tab

Vertical Orientation

KeyAction
ArrowUpSelect previous tab
ArrowDownSelect next tab
HomeSelect first tab
EndSelect last tab

Accessibility

The component automatically handles:

  • role="tablist" on list
  • role="tab" on triggers
  • role="tabpanel" on content
  • aria-orientation on list
  • aria-selected on triggers
  • aria-controls linking triggers to panels
  • aria-labelledby linking panels to triggers
  • tabindex management (only the selected enabled tab is in the tab order)

License

MIT

版本与发布记录

@data-slot/tabs 共发布 164 个版本,首次发布于 2026/01/09;过去 12 个月发布了 164 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.3latest2026/11/056 天前
1.1.22026/10/1428 天前
1.1.12026/10/051 个月前
1.1.02026/10/021 个月前
1.0.22026/09/271 个月前
1.0.12026/09/261 个月前

关于 @data-slot/tabs 的常见问题

@data-slot/tabs 是什么?

Headless tabs component for vanilla JavaScript. Accessible, unstyled, tiny.

如何安装 @data-slot/tabs?

在项目目录运行 npm install @data-slot/tabs,或使用 pnpm add / yarn add / bun add。

@data-slot/tabs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@data-slot/tabs 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@data-slot/tabs 可以商用吗?

@data-slot/tabs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@data-slot/tabs 的下载量有多少?

根据 npm 官方下载统计,@data-slot/tabs 在 2026/10/12 – 2026/11/10 这 30 天内被下载 6,855 次,日均约 229 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@data-slot/tabs 还在维护吗?

最新版本 1.1.3 发布于 2026/11/05(6 天前);至今共发布 164 个版本,其中过去 12 个月发布了 164 个;首次发布于 2026/01/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。