@accessible/button

中文文档
2.0.2发布于 2020/08/11MITTypeScriptESM + CJS近 30 天下载 5964

An accessible button component for React that provides interop between real elements and fake ones, e.g.

原始使用文档
文档目录6 节
  1. Why does this exist?
  2. Quick Start
  3. API
  4. <Button>
  5. useA11yButton(target, onClick)
  6. LICENSE

<Button>

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i @accessible/button

An accessible button component for React that provides interop between real <button> elements and fake ones, e.g. <div role='button'>. To do so, this component attaches the onClick handler from its child component to the keyboard events for space and enter. It also adds role='button' and tabIndex={0} properties, though this behavior can be overridden by providing those props to the child component e.g. <Button><div tabIndex={-1}></Button>.

Why does this exist?

In designing accessible libraries, we just don't know if our users are going to do the right thing i.e. using a <button> for buttons, rather than a <div>, <span>, or <a>. This component provides interoperability between <button> elements and those faux button elements.

Quick Start

Check out the example on CodeSandbox

import {Button, useA11yButton} from '@accessible/button'

const Component = () => (
  // Adds `space` and `enter` keydown handlers to the div,
  // also adds role='button' and tabIndex='0', both
  // of which can be overridden by providing those
  // props on your <div>
  <Button>
    <div onClick={console.log} />
  </Button>
  // <div role='button' tabindex='0'/>
)

const WithHook = () => {
  const ref = React.useRef(null)
  const a11yProps = useA11yButton(ref, (event) => {
    // This is your `onClick` handler
    console.log('Clicked', event)
  })
  return <button {...a11yProps} ref={ref} />
}

API

<Button>

Props

PropTypeDefaultRequired?Description
childrenReact.ReactElementundefinedYesThe component you want to turn into a button that handles focus and space, enter keydown events.

useA11yButton(target, onClick)

A React hook for adding a11y properties and button/role=button interop to elements.

const Button = () => {
  const ref = React.useRef(null)
  const a11yProps = useA11yButton(ref, (event) => {
    // This is your `onClick` handler
    console.log('Clicked', event)
  })
  return <div {...a11yProps} ref={ref} />
}

Arguments

ArgumentTypeRequired?Description
targetReact.RefObject<T> | T | nullYesA React ref or HTML element
childrenReact.ReactElementYesThe component you want to turn into a button that handles focus and space, enter keydown events.

Returns

{
    readonly role: "button";
    readonly tabIndex: 0;
}

LICENSE

MIT

版本与发布记录

@accessible/button 共发布 10 个版本,首次发布于 2019/12/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2020/08/116 年前
2.0.12020/07/086 年前
2.0.02020/07/086 年前
1.1.12020/05/306 年前
1.1.02020/05/296 年前
1.0.42020/05/266 年前

关于 @accessible/button 的常见问题

@accessible/button 是什么?

An accessible button component for React that provides interop between real elements and fake ones, e.g.

如何安装 @accessible/button?

在项目目录运行 npm install @accessible/button,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@accessible/button 支持 TypeScript 吗?

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

@accessible/button 支持 ESM 还是 CommonJS?

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

@accessible/button 可以商用吗?

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

@accessible/button 的下载量有多少?

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

@accessible/button 还在维护吗?

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