{"id":24911415,"url":"https://github.com/accessible-ui/button","last_synced_at":"2025-06-22T05:35:20.487Z","repository":{"id":36839976,"uuid":"230652062","full_name":"accessible-ui/button","owner":"accessible-ui","description":"🅰 An accessible button component for React that provides interop between real \u003cbutton\u003e elements and fake ones, e.g. \u003cdiv role='button'\u003e","archived":false,"fork":false,"pushed_at":"2023-01-06T02:23:07.000Z","size":2862,"stargazers_count":1,"open_issues_count":16,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T19:47:31.208Z","etag":null,"topics":["a11y","a11y-button","accessibility","accessible","accessible-button","accessible-react","aria","aria-button","react"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/accessiblebutton-example-spjh2","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/accessible-ui.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-28T19:01:54.000Z","updated_at":"2020-08-10T21:42:23.000Z","dependencies_parsed_at":"2023-01-17T05:37:12.966Z","dependency_job_id":null,"html_url":"https://github.com/accessible-ui/button","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Fbutton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Fbutton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Fbutton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Fbutton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/accessible-ui","download_url":"https://codeload.github.com/accessible-ui/button/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245960814,"owners_count":20700781,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["a11y","a11y-button","accessibility","accessible","accessible-button","accessible-react","aria","aria-button","react"],"created_at":"2025-02-02T04:20:03.886Z","updated_at":"2025-03-28T03:14:32.777Z","avatar_url":"https://github.com/accessible-ui.png","language":"TypeScript","readme":"\u003chr\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003e\n    \u0026lt;Button\u0026gt;\n  \u003c/h1\u003e\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://bundlephobia.com/result?p=@accessible/button\"\u003e\n    \u003cimg alt=\"Bundlephobia\" src=\"https://img.shields.io/bundlephobia/minzip/@accessible/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"Types\" href=\"https://www.npmjs.com/package/@accessible/button\"\u003e\n    \u003cimg alt=\"Types\" src=\"https://img.shields.io/npm/types/@accessible/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"Code coverage report\" href=\"https://codecov.io/gh/accessible-ui/button\"\u003e\n    \u003cimg alt=\"Code coverage\" src=\"https://img.shields.io/codecov/c/gh/accessible-ui/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"Build status\" href=\"https://travis-ci.org/accessible-ui/button\"\u003e\n    \u003cimg alt=\"Build status\" src=\"https://img.shields.io/travis/accessible-ui/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"NPM version\" href=\"https://www.npmjs.com/package/@accessible/button\"\u003e\n    \u003cimg alt=\"NPM Version\" src=\"https://img.shields.io/npm/v/@accessible/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"License\" href=\"https://jaredlunde.mit-license.org/\"\u003e\n    \u003cimg alt=\"MIT License\" src=\"https://img.shields.io/npm/l/@accessible/button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cpre align=\"center\"\u003enpm i @accessible/button\u003c/pre\u003e\n\u003chr\u003e\n\nAn accessible button component for React that provides interop between real `\u003cbutton\u003e` elements and fake ones, e.g. `\u003cdiv role='button'\u003e`.\nTo do so, this component attaches the `onClick` handler from its child component to the keyboard\nevents for `space` and `enter`. It also adds `role='button'` and `tabIndex={0}` properties, though\nthis behavior can be overridden by providing those props to the child component e.g. `\u003cButton\u003e\u003cdiv tabIndex={-1}\u003e\u003c/Button\u003e`.\n\n## Why does this exist?\n\nIn designing accessible libraries, we just don't know if our users are going to do the right thing\ni.e. using a `\u003cbutton\u003e` for buttons, rather than a `\u003cdiv\u003e`, `\u003cspan\u003e`, or `\u003ca\u003e`. This component\nprovides interoperability between `\u003cbutton\u003e` elements and those faux button elements.\n\n## Quick Start\n\n[Check out the example on CodeSandbox](https://codesandbox.io/s/accessiblebutton-example-spjh2)\n\n```jsx harmony\nimport {Button, useA11yButton} from '@accessible/button'\n\nconst Component = () =\u003e (\n  // Adds `space` and `enter` keydown handlers to the div,\n  // also adds role='button' and tabIndex='0', both\n  // of which can be overridden by providing those\n  // props on your \u003cdiv\u003e\n  \u003cButton\u003e\n    \u003cdiv onClick={console.log} /\u003e\n  \u003c/Button\u003e\n  // \u003cdiv role='button' tabindex='0'/\u003e\n)\n\nconst WithHook = () =\u003e {\n  const ref = React.useRef(null)\n  const a11yProps = useA11yButton(ref, (event) =\u003e {\n    // This is your `onClick` handler\n    console.log('Clicked', event)\n  })\n  return \u003cbutton {...a11yProps} ref={ref} /\u003e\n}\n```\n\n## API\n\n### \u0026lt;Button\u0026gt;\n\n#### Props\n\n| Prop     | Type                 | Default     | Required? | Description                                                                                          |\n| -------- | -------------------- | ----------- | --------- | ---------------------------------------------------------------------------------------------------- |\n| children | `React.ReactElement` | `undefined` | Yes       | The component you want to turn into a button that handles focus and `space`, `enter` keydown events. |\n\n### useA11yButton(target, onClick)\n\nA React hook for adding a11y properties and button/role=button interop to elements.\n\n```jsx harmony\nconst Button = () =\u003e {\n  const ref = React.useRef(null)\n  const a11yProps = useA11yButton(ref, (event) =\u003e {\n    // This is your `onClick` handler\n    console.log('Clicked', event)\n  })\n  return \u003cdiv {...a11yProps} ref={ref} /\u003e\n}\n```\n\n#### Arguments\n\n| Argument | Type                                                       | Required? | Description                                                                                          |\n| -------- | ---------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------- |\n| target   | \u003ccode\u003eReact.RefObject\u0026lt;T\u0026gt; \u0026#124; T \u0026#124; null\u003c/code\u003e | Yes       | A React ref or HTML element                                                                          |  |\n| children | `React.ReactElement`                                       | Yes       | The component you want to turn into a button that handles focus and `space`, `enter` keydown events. |\n\n#### Returns\n\n```ts\n{\n    readonly role: \"button\";\n    readonly tabIndex: 0;\n}\n```\n\n## LICENSE\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faccessible-ui%2Fbutton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faccessible-ui%2Fbutton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faccessible-ui%2Fbutton/lists"}