# @accessible/button

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

Latest version **2.0.2** (published 2020-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @accessible/button
pnpm add @accessible/button
yarn add @accessible/button
bun add @accessible/button
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2020-08-10 |
| First published | 2019-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 75.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react component, button, accessible button, a11y button, react a11y button, react accessible button, react button, react button component, a11y, react aria button, aria, aria button, a11y button hook, usea11ybutton, use-a11y-button |

## Links

- npm: https://www.npmjs.com/package/@accessible/button
- Repository: https://github.com/accessible-ui/button
- Homepage: https://github.com/accessible-ui/button#readme
- Issues: https://github.com/accessible-ui/button/issues
- npm.io page: https://npm.io/package/@accessible/button

## Dependencies (3)

- [@react-hook/event](https://npm.io/package/@react-hook/event.md) ^1.2.2
- [@accessible/use-key](https://npm.io/package/@accessible/use-key.md) ^1.0.2
- [@react-hook/merged-ref](https://npm.io/package/@react-hook/merged-ref.md) ^1.3.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2020-08-10
- 2.0.1 — 2020-07-08
- 2.0.0 — 2020-07-08
- 1.1.1 — 2020-05-30
- 1.1.0 — 2020-05-29
- 1.0.4 — 2020-05-25
- 1.0.3 — 2019-12-31
- 1.0.2 — 2019-12-28
- 1.0.1 — 2019-12-28
- 1.0.0 — 2019-12-28

## README

<hr>
<div align="center">
  <h1 align="center">
    &lt;Button&gt;
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@accessible/button">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@accessible/button?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@accessible/button">
    <img alt="Types" src="https://img.shields.io/npm/types/@accessible/button?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Code coverage report" href="https://codecov.io/gh/accessible-ui/button">
    <img alt="Code coverage" src="https://img.shields.io/codecov/c/gh/accessible-ui/button?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.org/accessible-ui/button">
    <img alt="Build status" src="https://img.shields.io/travis/accessible-ui/button?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@accessible/button">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@accessible/button?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@accessible/button?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @accessible/button</pre>
<hr>

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](https://codesandbox.io/s/accessiblebutton-example-spjh2)

```jsx harmony
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

### &lt;Button&gt;

#### Props

| Prop     | Type                 | Default     | Required? | Description                                                                                          |
| -------- | -------------------- | ----------- | --------- | ---------------------------------------------------------------------------------------------------- |
| children | `React.ReactElement` | `undefined` | Yes       | The 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.

```jsx harmony
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

| Argument | Type                                                       | Required? | Description                                                                                          |
| -------- | ---------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------- |
| target   | <code>React.RefObject&lt;T&gt; &#124; T &#124; null</code> | Yes       | A React ref or HTML element                                                                          |  |
| children | `React.ReactElement`                                       | Yes       | The component you want to turn into a button that handles focus and `space`, `enter` keydown events. |

#### Returns

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

## LICENSE

MIT

---
_Source: https://npm.io/package/@accessible/button · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
