@data-slot/toggle-group

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

A group of toggle buttons with shared state, supporting single or multiple selection.

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

@data-slot/toggle-group

A group of toggle buttons with shared state, supporting single or multiple selection.

Installation

bun add @data-slot/toggle-group

Usage

Basic Markup

<!-- Single selection (default) -->
<div data-slot="toggle-group" data-default-value="center">
  <button data-slot="toggle-group-item" data-value="left">Left</button>
  <button data-slot="toggle-group-item" data-value="center">Center</button>
  <button data-slot="toggle-group-item" data-value="right">Right</button>
</div>

<!-- Multiple selection -->
<div data-slot="toggle-group" data-multiple data-default-value="bold italic">
  <button data-slot="toggle-group-item" data-value="bold">B</button>
  <button data-slot="toggle-group-item" data-value="italic">I</button>
  <button data-slot="toggle-group-item" data-value="underline">U</button>
</div>

JavaScript

import { create, createToggleGroup } from "@data-slot/toggle-group";

// Auto-discover and bind all toggle-group elements
const controllers = create();

// Or target a specific element
const group = createToggleGroup(element, {
  defaultValue: "center",
  multiple: false,
  orientation: "horizontal",
  loop: true,
  onValueChange: (value) => console.log(value),
});

// Programmatic control
group.setValue("left");
group.toggle("bold");
console.log(group.value); // ["left"]

// Cleanup
group.destroy();

API

Initialization

create(scope?)

Find and bind uninitialized [data-slot="toggle-group"] descendants of scope (defaults to document). Returns ToggleGroupController[] for newly bound roots. To initialize the scope element itself, use createToggleGroup.

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

const controllers = create();

createToggleGroup(root, options?)

Create a ToggleGroupController for one root element. JavaScript options take precedence over the corresponding data attributes. Calling this again for a bound root returns its existing controller; destroy it before rebinding with new options.

import { createToggleGroup } from "@data-slot/toggle-group";

const controller = createToggleGroup(element, {});

Slots

Runtime Slots

  • toggle-group - Root element that manages single or multiple selection and keyboard navigation between items.
  • toggle-group-item - Toggle button inside the group, identified by a unique data-value; receives aria-pressed and data-state as its selection changes.

Data Attributes

Root Element

AttributeDescription
data-slot="toggle-group"Required. Identifies the root element.
data-default-valueInitial selected value(s). Space-separated for multiple values.
data-multipleEnable multiple selection mode.
data-orientation"horizontal" or "vertical" for keyboard navigation.
data-loopWrap keyboard focus at the ends (default true); use "false" to stop at the ends.
data-disabledDisable the entire group.

Item Elements

AttributeDescription
data-slot="toggle-group-item"Required. Identifies an item.
data-valueRequired. The value associated with this item.
data-disabledDisable this specific item.

State Attributes (set by component)

AttributeValuesDescription
aria-pressed"true" | "false"Whether item is pressed.
data-state"on" | "off"Visual state for styling.
data-value (on root)Space-separated valuesCurrent selection.

Options

OptionTypeDefaultDescription
defaultValuestring | string[][]Initial selected value(s). For multiple values, use array or space-separated string.
multiplebooleanfalseAllow multiple selections.
orientation"horizontal" | "vertical""horizontal"Orientation for keyboard navigation.
loopbooleantrueWrap keyboard focus at ends.
disabledbooleanfalseDisable the entire group.
onValueChange(value: string[]) => void-Callback when selection changes.

Controller

Method/PropertyDescription
setValue(value: string | string[])Replace the selection. Strings are space-separated; unknown values are ignored and single mode keeps the first matching value.
toggle(value: string)Toggle one value; single mode clears the previous selection.
valueCurrent selection (readonly string[], including in single mode).
destroy()Remove listeners and release the root binding.

Controller methods work while the group is disabled. User interaction and toggle-group:set are blocked while disabled.

Events

Outbound Events

// Listen for changes
root.addEventListener("toggle-group:change", (e) => {
  console.log(e.detail.value); // string[]
});

Inbound Events

EventDetailDescription
toggle-group:set{ value: string | string[] }Set selection programmatically
// Set selection from outside
root.dispatchEvent(new CustomEvent("toggle-group:set", {
  detail: { value: "bold" }
}));

// Set multiple values
root.dispatchEvent(new CustomEvent("toggle-group:set", {
  detail: { value: ["bold", "italic"] }
}));

Note: Blocked when group is disabled.

Deprecated Shapes

The following shapes are deprecated and will be removed in v1.0:

// Deprecated: bare string
root.dispatchEvent(new CustomEvent("toggle-group:set", {
  detail: "bold"
}));

// Deprecated: bare array
root.dispatchEvent(new CustomEvent("toggle-group:set", {
  detail: ["bold", "italic"]
}));

Use { value: ... } instead.

Styling

/* Style pressed items */
[data-slot="toggle-group-item"][data-state="on"] {
  background: #333;
  color: white;
}

/* Style disabled items */
[data-slot="toggle-group-item"][aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

Keyboard Navigation

KeyAction
ArrowRight / ArrowDownMove to next item (based on orientation)
ArrowLeft / ArrowUpMove to previous item (based on orientation)
HomeMove to first item
EndMove to last item
Enter / SpaceToggle current item

Accessibility

  • Root has role="group"
  • Items have aria-pressed attribute
  • Disabled items have aria-disabled="true" and native disabled
  • Supports roving tabindex for keyboard navigation
  • Add aria-label or aria-labelledby to root for context

版本与发布记录

@data-slot/toggle-group 共发布 149 个版本,首次发布于 2026/02/02;过去 12 个月发布了 149 个版本。最新版本附带 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/toggle-group 的常见问题

@data-slot/toggle-group 是什么?

A group of toggle buttons with shared state, supporting single or multiple selection.

如何安装 @data-slot/toggle-group?

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

@data-slot/toggle-group 支持 TypeScript 吗?

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

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

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

@data-slot/toggle-group 可以商用吗?

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

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

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

@data-slot/toggle-group 还在维护吗?

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