{"id":24617417,"url":"https://github.com/accessible-ui/toggle-button","last_synced_at":"2026-04-15T08:33:01.614Z","repository":{"id":40794973,"uuid":"230652886","full_name":"accessible-ui/toggle-button","owner":"accessible-ui","description":"🅰 An accessible two-state button that can be either off (not pressed) or on (pressed)","archived":false,"fork":false,"pushed_at":"2023-01-06T02:23:10.000Z","size":2853,"stargazers_count":1,"open_issues_count":16,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T18:07:00.112Z","etag":null,"topics":["a11y","a11y-button","a11y-toggle","accessibility","accessible","accessible-react","accessible-toggle","aria","aria-button","aria-toggle","react"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/accessibletoggle-button-example-s1cuy","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:08:46.000Z","updated_at":"2020-08-10T22:01:10.000Z","dependencies_parsed_at":"2023-02-05T03:01:23.174Z","dependency_job_id":null,"html_url":"https://github.com/accessible-ui/toggle-button","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Ftoggle-button","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Ftoggle-button/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Ftoggle-button/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/accessible-ui%2Ftoggle-button/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/accessible-ui","download_url":"https://codeload.github.com/accessible-ui/toggle-button/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244101305,"owners_count":20398309,"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","a11y-toggle","accessibility","accessible","accessible-react","accessible-toggle","aria","aria-button","aria-toggle","react"],"created_at":"2025-01-24T23:24:12.218Z","updated_at":"2026-04-15T08:32:56.546Z","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;ToggleButton\u0026gt;\n  \u003c/h1\u003e\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://bundlephobia.com/result?p=@accessible/toggle-button\"\u003e\n    \u003cimg alt=\"Bundlephobia\" src=\"https://img.shields.io/bundlephobia/minzip/@accessible/toggle-button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n  \u003ca aria-label=\"Types\" href=\"https://www.npmjs.com/package/@accessible/toggle-button\"\u003e\n    \u003cimg alt=\"Types\" src=\"https://img.shields.io/npm/types/@accessible/toggle-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/toggle-button\"\u003e\n    \u003cimg alt=\"Code coverage\" src=\"https://img.shields.io/codecov/c/gh/accessible-ui/toggle-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/toggle-button\"\u003e\n    \u003cimg alt=\"Build status\" src=\"https://img.shields.io/travis/accessible-ui/toggle-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/toggle-button\"\u003e\n    \u003cimg alt=\"NPM Version\" src=\"https://img.shields.io/npm/v/@accessible/toggle-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/toggle-button?style=for-the-badge\u0026labelColor=24292e\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cpre align=\"center\"\u003enpm i @accessible/toggle-button\u003c/pre\u003e\n\u003chr\u003e\n\nAn accessible two-state button that can be either off (not pressed) or on (pressed). Common use cases\nare toolbar buttons like Bold, Italic, and Underline. In addition to following the\n[accessibility guidelines here](https://www.w3.org/TR/wai-aria-practices/#button), this component\nprovides interop between real `\u003cbutton\u003e` elements and faux `\u003cdiv\u003e`, `\u003ca\u003e`, `\u003cspan\u003e`, et. al. buttons.\n\n## Quick Start\n\n[Check out the example on **CodeSandbox**](https://codesandbox.io/s/accessibletoggle-button-example-s1cuy)\n\n```jsx harmony\nimport * as React from 'react'\nimport {ToggleButton, useA11yToggleButton} from '@accessible/toggle-button'\n\nconst Component = () =\u003e {\n  const [muted, setMuted] = React.useState(false)\n  return (\n    \u003cToggleButton active={muted} onChange={setMuted}\u003e\n      \u003cspan\u003e{muted ? 'Unmute' : 'Mute'}\u003c/span\u003e\n    \u003c/ToggleButton\u003e\n  )\n}\n\nconst ComponentWithHook = () =\u003e {\n  const ref = React.useRef(null)\n  const [muted, setMuted] = React.useState(false)\n  const a11yProps = useA11yToggleButton(ref, {\n    active: muted,\n    onChange: setMuted,\n  })\n\n  return (\n    \u003cbutton ref={ref} {...a11yProps}\u003e\n      \u003cspan\u003e{muted ? 'Unmute' : 'Mute'}\u003c/span\u003e\n    \u003c/button\u003e\n  )\n}\n```\n\n## API\n\n### useA11yToggleButton(target, options?)\n\nA React hook for creating a headless a11y toggle button to the\n[W3C accessibility standard](https://www.w3.org/TR/wai-aria-practices/#button). In addition\nto providing accessibility props to your component, this hook will add events for interoperability\nbetween actual `\u003cbutton\u003e` elements and fake ones e.g. `\u003ca\u003e` and `\u003cdiv\u003e` to the `target` element.\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| options  | [`UseA11yToggleButtonOptions`](#usea11ytogglebuttonoptions) | Yes       | The component you want to turn into a button that handles focus and `space`, `enter` keydown events. |\n\n#### UseA11yToggleButtonOptions\n\n```ts\nexport interface UseA11yToggleButtonOptions {\n  /**\n   * Creates a controlled hook where the active value always matches this one.\n   */\n  active?: boolean\n  /**\n   * Sets the default active state of the button for uncontrolled hooks.\n   * @default false\n   */\n  defaultActive?: boolean\n  /**\n   * This callback is invoked any time the active state of the\n   * toggle button changes\n   */\n  onChange?: (active: boolean) =\u003e void\n  /**\n   * Adds a click event to your button\n   */\n  onClick?: (event: MouseEvent) =\u003e any\n}\n```\n\n#### Returns\n\n```ts\ninterface ReturnValue {\n  readonly 'aria-pressed': boolean\n  readonly role: 'button'\n  readonly tabIndex: 0\n}\n```\n\n### \u0026lt;ToggleButton\u0026gt;\n\nThis component clones its child component and adds accessibility roles for pressed/unpressed\nstate buttons. It also creates context so its active state is accessible from its children.\n\n#### Props\n\n| Prop          | Type                        | Default | Required? | Description                                                                            |\n| ------------- | --------------------------- | ------- | --------- | -------------------------------------------------------------------------------------- |\n| active        | `string`                    |         | No        | Creates a controlled component where the active value always matches this one.         |\n| defaultActive | `string`                    | `false` | No        | Sets the default active state of the button.                                           |\n| activeClass   | `string`                    |         | No        | Adds this class name to its child component when the button is in a active state.      |\n| inactiveClass | `string`                    |         | No        | Adds this class name to its child component when the button is in an inactive state.   |\n| activeStyle   | `React.CSSProperties`       |         | No        | Adds this style object to its child component when the button is in a active state.    |\n| inactiveStyle | `React.CSSProperties`       |         | No        | Adds this style object to its child component when the button is in an inactive state. |\n| onChange      | `(active: boolean) =\u003e void` |         | No        | This callback is invoked any time the active state changes.                            |\n| children      | `React.ReactElement`        |         | Yes       | This is the element you want to turn into a ToggleButton.                              |\n\n## LICENSE\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faccessible-ui%2Ftoggle-button","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faccessible-ui%2Ftoggle-button","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faccessible-ui%2Ftoggle-button/lists"}