{"id":25619679,"url":"https://github.com/danoc/clickable-box","last_synced_at":"2025-04-09T12:08:35.171Z","repository":{"id":38272812,"uuid":"157494448","full_name":"danoc/clickable-box","owner":"danoc","description":"React component to add `onClick` to HTML elements without sacrificing accessibility.","archived":false,"fork":false,"pushed_at":"2023-10-19T21:42:57.000Z","size":1427,"stargazers_count":106,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T11:07:25.503Z","etag":null,"topics":["accessibility","component","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/danoc.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-14T05:12:33.000Z","updated_at":"2023-10-13T06:18:12.000Z","dependencies_parsed_at":"2023-01-21T22:16:22.240Z","dependency_job_id":null,"html_url":"https://github.com/danoc/clickable-box","commit_stats":{"total_commits":200,"total_committers":7,"mean_commits":"28.571428571428573","dds":"0.18000000000000005","last_synced_commit":"98da37e916526fe4f243c7029c3645e2aa2d8117"},"previous_names":["danoc/unbutton"],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danoc%2Fclickable-box","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danoc%2Fclickable-box/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danoc%2Fclickable-box/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danoc%2Fclickable-box/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danoc","download_url":"https://codeload.github.com/danoc/clickable-box/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"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":["accessibility","component","react"],"created_at":"2025-02-22T06:19:31.284Z","updated_at":"2025-04-09T12:08:35.154Z","avatar_url":"https://github.com/danoc.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📦 ClickableBox\n\n[![NPM version](https://badgen.net/npm/v/clickable-box)](https://www.npmjs.com/package/clickable-box) [![Test build status](https://github.com/danoc/clickable-box/workflows/Test/badge.svg)](https://github.com/danoc/clickable-box/actions?query=workflow%3ATest) [![Bundle size](https://badgen.net/bundlephobia/min/clickable-box?label=size)](https://bundlephobia.com/result?p=clickable-box) [![Bundle size](https://badgen.net/bundlephobia/minzip/clickable-box?label=gzip%20size)](https://bundlephobia.com/result?p=clickable-box)\n\n\u003e React component to add `onClick` to HTML elements without sacrificing accessibility.\n\n## What is this?\n\nIt's [very hard](https://www.scottohara.me/blog/2018/10/03/unbutton-buttons.html) to remove all styles from HTML `button` elements. It's also hard to create clickable `div`s that are accessible. This can cause developers to ship inaccessible UI.\n\nThe `ClickableBox` React component accepts an `onClick` prop and an element to render. It returns the element with the `onClick` as well as the attributes and event listeners needed to make it as accessible as a `button`.\n\n## Install\n\nYou can install `ClickableBox` with npm, Yarn, or pnpm.\n\n```bash\nnpm install clickable-box\nyarn add clickable-box\npnpm install clickable-box\n```\n\n## Usage\n\nHere's how to use `ClickableBox` to make a clickable SVG:\n\n```jsx\n// import ClickableBox from 'clickable-box';\n\n\u003cClickableBox\n  onClick={this.closeModal}\n  aria-label=\"Close modal\"\n  className=\"icon-button\"\n\u003e\n  \u003cCloseIcon /\u003e\n\u003c/ClickableBox\u003e\n```\n\n`ClickableBox` will return a `span` that looks like this:\n\n```js\n\u003cspan\n  // Make the element clickable\n  onClick={this.closeModal}\n  // Make the element navigable by keyboard\n  tabIndex={0}\n  // Call `this.closeModal` if the user presses either the\n  // enter or space key while the element is in focus\n  onKeyDown={...}\n  // Tell screen readers that the element is a button\n  role=\"button\"\n  // All other props are passed through to the element\n  aria-label=\"Close modal\"\n  className=\"icon-button\"\n\u003e\n  \u003cCloseIcon /\u003e\n\u003c/span\u003e\n```\n\nThe resulting HTML is accessible for users navigating by screen readers, keyboard, and mouse/touch.\n\n## Props\n\nThere are a few props that are built into `ClickableBox`:\n\n| prop       | type                                             | description                                                  |\n| ---------- | ------------------------------------------------ | ------------------------------------------------------------ |\n| `onClick`  | `function` \\| defaults to: `undefined`           | The action to perform when the element is pressed            |\n| `is`       | `string`, `React.Element` \\| defaults to: `span` | The element to render                                        |\n| `disabled` | `boolean` \\| defaults to: `false`                | Makes element non-interactive, even if `onClick` is provided |\n| `ref`      | `React.Ref`                                      | Provides access to the React element                         |\n\nYou can pass any custom prop as well. This component will forward those props to the rendered element.\n\n## When should you use this?\n\n- You're building a button that looks like plain text.\n- You're building a button that has content spanning multiple columns or rows.\n- You're making a clickable SVG icon.\n\n## When shouldn't you use this?\n\n- You're linking to another page: Use an `a` tag with an `href` instead. The anchor tag is semantically correct, allows users to preview the URL, open it in a new tab, and copy the link to their clipboard.\n- You're using this as a submit button in a form. (It's possible, but [there's a quirk](https://github.com/danoc/clickable-box/issues/4).)\n- You're building a button that [looks like a button](https://getbootstrap.com/docs/4.0/components/buttons/#examples): This is fairly easy to build as a `button` element with CSS.\n- You think it'd be easier to simply style a `button`: This is a good sign that you should use a `button` element instead.\n\n## FAQs\n\n**How can I style this with `cursor: pointer`?**\n\n`ClickableBox` accepts all props including `className` and `style` prop. If you prefer, you can add the cursor style globally with this CSS:\n\n```css\n/* Targets all instances of `ClickableBox` */\n[role=\"button\"] {\n  cursor: pointer;\n}\n```\n\n**What are accessibility best practices for `ClickableBox`?**\n\n- Pass `aria-label` to `ClickableBox` if `children` contains an SVG and no descriptive text. The value of `aria-label` should describe the action that will happen if the button is interacted with. It will be announced to users navigating with screen readers.\n- You shouldn't use `ClickableBox` within an anchor tag or another button. You also shouldn't use an `a` or `button` in the `children` prop.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanoc%2Fclickable-box","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanoc%2Fclickable-box","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanoc%2Fclickable-box/lists"}