@aria-ui/utils

中文文档
0.1.8发布于 2026/10/21MITTypeScriptES Module构建来源已验证近 30 天下载 11.9万

Utility functions for aria-ui

原始使用文档
文档目录10 节
  1. Installation
  2. API overview
  3. ARIA reflection hooks
  4. Collections
  5. Pointer and keyboard interaction
  6. Identity and attributes
  7. Feature detection
  8. Re-exports
  9. Example: a minimal listbox item
  10. License

@aria-ui/utils

Reusable hooks and helpers for building accessible Web Components on top of @aria-ui/core.

This package provides the building blocks that @aria-ui/elements uses internally — ARIA attribute reflection, collection navigation, pointer/keyboard interaction, presence and transition tracking, and more. You can use it to author your own custom elements that follow the same patterns.

Installation

npm install @aria-ui/core @aria-ui/utils
# or
pnpm add @aria-ui/core @aria-ui/utils

API overview

ARIA reflection hooks

Each hook subscribes to a getter and writes the result to the corresponding ARIA attribute on the host element. They are tiny effects, but using them keeps your setup functions declarative and consistent.

HookReflects to
useAriaDisabled(host, get)aria-disabled
useAriaExpanded(host, get)aria-expanded
useAriaSelected(host, get)aria-selected
useAriaControls(host, get)aria-controls
useAriaDescribedBy(host, get)aria-describedby
useAriaMultiselectable(host, get)aria-multiselectable
useAriaOrientation(host, get)aria-orientation
useAriaActivedescendant(host, get)aria-activedescendant
useAriaHasPopup(host, get)aria-haspopup

Plus low-level helpers getAriaHasPopup(element) and setAriaHasPopup(element, value).

import { useAriaDisabled, useAriaExpanded } from '@aria-ui/utils'

function setupTrigger(host, props) {
  useAriaDisabled(host, () => props.disabled.get())
  useAriaExpanded(host, () => props.open.get())
}

Collections

Collection helpers let you build composite widgets — listboxes, menus, comboboxes — where a parent element coordinates a set of item elements.

ExportDescription
CollectionAn ordered set of items keyed by value, sorted by DOM position.
getCollectionItemValue(element)Read the value of a collection item.
createCollectionStore()Create a store with getCollection(), getHighlightedValue(), setHighlightedValue(), first(), etc. Designed to be embedded inside your component's own store.
setupCollectionItem(host, props, getStore, getVisible?)Register an item element with its parent collection store. Handles registration, ID assignment, and the data-active attribute.
handleCollectionNavigation(event, collection, getHighlighted, setHighlighted, orientation, loop?)Process a keydown event for ArrowUp/Down/Left/Right/Home/End and update the highlighted value. Returns true if the event was handled.

Pointer and keyboard interaction

HookDescription
useEventListener(host, type, listener, options?)Add a DOM event listener that is automatically removed when the host disconnects.
useGlobalEventListener(host, type, listener)Add a window event listener tied to the host's connected lifecycle, deduplicated across hosts.
usePress(host, onPress)Unified press handler that fires for mouse, touch, and keyboard (Enter/Space) activation.
useHover(host, { openDelay, closeDelay, onOpen, onClose })Hover tracking with configurable open/close delays. Used by popovers and tooltips.
usePresence(host, present)Track whether the host should be present in the DOM, with hooks for unmount-after-animation behavior.
useTransitionStatus(host, present)Track CSS transition status ('entering', 'entered', 'exiting', 'exited') for animating overlays.
useDisabledMountTransitionStyle(host)Disable CSS transitions during the initial mount, to avoid a flash on first paint.
createDelayedToggle({ openDelay, closeDelay })Standalone delayed open/close state machine.

Identity and attributes

HookDescription
useElementId(host)Assign a stable, unique id to the host element if it doesn't already have one. Returns the id. Useful for aria-activedescendant and aria-controls.
useAttribute(host, name, get)Reflect a value to an arbitrary HTML attribute.
useDataState(host, get)Reflect a state string to data-state.

Feature detection

ExportDescription
FeatureDetectionPublic feature detection helpers.
FeatureDetectionInternalsInternal feature detection helpers.

Re-exports

For convenience, this package also re-exports getNearestOverflowAncestor from @zag-js/dom-query.

Example: a minimal listbox item

import {
  defineCustomElement,
  defineProps,
  onMount,
  type HostElement,
  type State,
} from '@aria-ui/core'
import { setupCollectionItem, useAriaSelected, useEventListener } from '@aria-ui/utils'

import { MyListboxContext } from './my-listbox-context'

interface ItemProps {
  value: string
}

const itemProps = defineProps<ItemProps>({
  value: { default: '', attribute: 'value', type: 'string' },
})

function setupItem(host: HostElement, props: State<ItemProps>) {
  onMount(host, () => {
    host.role = 'option'
  })

  const getStore = MyListboxContext.consume(host)

  setupCollectionItem(host, props, getStore)

  useAriaSelected(host, () => {
    return getStore()?.selectedValues.get().includes(props.value.get()) ?? false
  })

  useEventListener(host, 'click', () => {
    getStore()?.toggle(props.value.get())
  })
}

export class MyItemElement extends defineCustomElement(setupItem, itemProps) {}

License

MIT

版本与发布记录

@aria-ui/utils 共发布 19 个版本,首次发布于 2026/03/29;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.8latest2026/10/2117 天前
0.1.72026/07/044 个月前
0.1.62026/05/155 个月前
0.1.52026/04/146 个月前
0.1.42026/04/146 个月前
0.1.32026/04/136 个月前

关于 @aria-ui/utils 的常见问题

@aria-ui/utils 是什么?

Utility functions for aria-ui

如何安装 @aria-ui/utils?

在项目目录运行 npm install @aria-ui/utils,或使用 pnpm add / yarn add / bun add。

@aria-ui/utils 支持 TypeScript 吗?

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

@aria-ui/utils 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aria-ui/utils 可以商用吗?

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

@aria-ui/utils 的下载量有多少?

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

@aria-ui/utils 还在维护吗?

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