@data-slot/toggle

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

Headless toggle button component for vanilla JavaScript. Accessible, unstyled, tiny.

原始使用文档
文档目录14 节
  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. Behavior
  14. License

@data-slot/toggle

Headless toggle button component for vanilla JavaScript. Accessible, unstyled, tiny.

Installation

npm install @data-slot/toggle

Quick Start

<button data-slot="toggle">Bold</button>
<button data-slot="toggle" data-default-pressed>Italic</button>

<script type="module">
  import { create } from "@data-slot/toggle";

  const controllers = create();
</script>

API

Initialization

create(scope?)

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

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

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

createToggle(root, options?)

Create a controller for a specific element.

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

const toggle = createToggle(element, {
  defaultPressed: false,
  disabled: false,
  onPressedChange: (pressed) => console.log(pressed),
});

Slots

Runtime Slots

  • toggle - The button itself. Toggles its pressed state and exposes aria-pressed and data-state for accessibility and styling; no child slots are needed.

Markup

<button data-slot="toggle">Label</button>

The toggle is a simple single-element component. Always use a native <button> element—keyboard support (Enter/Space) and focus handling are only guaranteed with <button>. Non-button elements (e.g., <div>, <span>) are not recommended and would require manual keyboard handling.

Data Attributes

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

AttributeTypeDefaultDescription
data-default-pressedbooleanfalseInitial pressed state
data-disabledbooleanfalseDisabled state
<!-- Initially pressed toggle -->
<button data-slot="toggle" data-default-pressed>Bold</button>

Options

OptionTypeDefaultDescription
defaultPressedbooleanfalseInitial pressed state
disabledbooleanfalseDisabled state
onPressedChange(pressed: boolean) => voidundefinedCallback when state changes

Controller

Method/PropertyDescription
toggle()Toggle the pressed state (ignores disabled)
press()Set pressed to true (ignores disabled)
release()Set pressed to false (ignores disabled)
pressedCurrent pressed state (readonly boolean)
destroy()Cleanup all event listeners

Note: Controller methods always work, even when disabled. This allows programmatic control regardless of user interaction state. If you need to check disabled state before calling controller methods, check the element's attributes yourself.

Events

Outbound Events

Listen for changes via custom events:

element.addEventListener("toggle:change", (e) => {
  console.log("Pressed:", e.detail.pressed);
});

Inbound Events

Control the toggle via events (ignored when disabled):

EventDetailDescription
toggle:set{ value: boolean }Set pressed state programmatically
// Set to specific state
element.dispatchEvent(
  new CustomEvent("toggle:set", { detail: { value: true } })
);

Deprecated Shapes

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

// Deprecated: boolean detail
element.dispatchEvent(
  new CustomEvent("toggle:set", { detail: true })
);

// Deprecated: { pressed } shape
element.dispatchEvent(
  new CustomEvent("toggle:set", { detail: { pressed: true } })
);

Use { value: boolean } instead.

Styling

State Attributes

The component sets these attributes for styling:

  • aria-pressed="true|false" - ARIA state
  • data-state="on|off" - CSS styling hook

Basic Styling

/* Unpressed state */
[data-slot="toggle"] {
  background: #e5e7eb;
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
}

/* Pressed state */
[data-slot="toggle"][data-state="on"] {
  background: #3b82f6;
  color: white;
}

/* Or use aria-pressed */
[data-slot="toggle"][aria-pressed="true"] {
  background: #3b82f6;
  color: white;
}

/* Disabled state */
[data-slot="toggle"][aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

Tailwind Example

<button
  data-slot="toggle"
  class="px-4 py-2 bg-gray-200 data-[state=on]:bg-blue-500 data-[state=on]:text-white aria-disabled:opacity-50"
>
  Bold
</button>

Keyboard Navigation

The toggle uses a native <button> element, so keyboard support is automatic:

KeyAction
EnterToggle state
SpaceToggle state

Accessibility

The component automatically handles:

  • aria-pressed state on the button
  • Native disabled and aria-disabled when disabled (for buttons)
  • type="button" to prevent form submission

Behavior

Disabled Behavior

When disabled (via disabled option, data-disabled attribute, native disabled attribute, or aria-disabled="true"):

InputBlocked?
Click / Enter / SpaceYes
toggle:set eventYes
Controller methods (toggle(), press(), release())No

For <button> elements, the disabled option sets both the native disabled attribute and aria-disabled="true". For other elements, only aria-disabled is set.

License

MIT

版本与发布记录

@data-slot/toggle 共发布 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 的常见问题

@data-slot/toggle 是什么?

Headless toggle button component for vanilla JavaScript. Accessible, unstyled, tiny.

如何安装 @data-slot/toggle?

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

@data-slot/toggle 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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