@accessible/button

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

React的可访问按钮组件,提供真实元素和假元素之间的接口,例如.

中文使用文档
文档目录6 节
  1. 这为什么存在?
  2. 快速启动
  3. API 密码
  4. 按钮
  5. 使用 A11y 按钮( 目标, 在 Click)
  6. 自由主义

按钮

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i @accessible/button

React 的可访问按钮组件, 提供真实之间的接口 <button> 元素和假元素,例如:. <div role='button'>。 。 。 . 为此,本部分附有: onClick 从子组件到键盘的处理器 用于 space 财务报告和审定财务报表 enter。还添加 role='button' 财务报告和审定财务报表 tabIndex={0} 属性,尽管 通过向儿童部分提供这些道具,可以推翻这种行为. <Button><div tabIndex={-1}></Button>。 。 。 .

这为什么存在?

在设计无障碍图书馆时,我们只是不知道我们的用户是否会做正确的事情 即使用 <button> 按钮,而不是一个 <div>, (中文(简体) ), <span>,或 <a>。本部分 提供互操作性 <button> 元素和那些软扣元素.

快速启动

查看 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 密码

按钮

道具

Prop类型默认需要吗?说明
儿童React.ReactElementundefined对您想要变成一个处理焦点和 space, (中文(简体) ), enter 键盘下的事件.

使用 A11y 按钮( 目标, 在 Click)

用于在元素中添加a11y属性和按钮/role=按钮间接的回放钩.

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} />
}

参数

参数类型需要吗?说明
目标React.RefObject<T> | T | null对反应 ref 或 HTML 元素
儿童React.ReactElement对您想要变成一个处理焦点和 space, (中文(简体) ), enter 键盘下的事件.

回返

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

自由主义

麻省理工学院

版本与发布记录

@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 是什么?

React的可访问按钮组件,提供真实元素和假元素之间的接口,例如.

如何安装 @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 和提交记录判断。