|
1 | | -# react-accessible-dropdown-menu-hook |
| 1 | +# React Accessible Dropdown Menu Hook |
2 | 2 |
|
3 | 3 | [](https://greenkeeper.io/) |
4 | 4 |
|
5 | | -Effortlessly create fully accessible dropdown menus in React |
| 5 | +This Hook handles all the accessibility logic when building a dropdown menu, dropdown button, etc., and leaves the design completely up to you. [View the demo.](http://sparksuite.github.io/react-accessible-dropdown-menu-hook) |
| 6 | + |
| 7 | +## Getting started |
| 8 | + |
| 9 | +Install with Yarn or npm: |
| 10 | + |
| 11 | +``` |
| 12 | +yarn add react-accessible-dropdown-menu-hook |
| 13 | +``` |
| 14 | + |
| 15 | +``` |
| 16 | +npm install react-accessible-dropdown-menu-hook |
| 17 | +``` |
| 18 | + |
| 19 | +Import the Hook: |
| 20 | + |
| 21 | +```tsx |
| 22 | +import useDropdownMenu from 'react-accessible-dropdown-menu-hook'; |
| 23 | +``` |
| 24 | + |
| 25 | +Call the Hook, telling it how many items your menu will have. |
| 26 | + |
| 27 | +```tsx |
| 28 | +const [buttonProps, itemProps, isOpen] = useDropdownMenu(numberOfItems); |
| 29 | +``` |
| 30 | + |
| 31 | +Spread the `buttonProps` onto a button: |
| 32 | + |
| 33 | +```tsx |
| 34 | +<button {...buttonProps}>Example</button> |
| 35 | +``` |
| 36 | + |
| 37 | +Create the menu with the `role='menu'` property and spread `itemProps[x]` onto each item: |
| 38 | + |
| 39 | +```tsx |
| 40 | +<div className={isOpen ? 'visible' : ''} role='menu'> |
| 41 | + <a {...itemProps[0]} href='https://example.com'>Regular link</a> |
| 42 | + <a {...itemProps[1]} onClick={handleClick}>With click handler</a> |
| 43 | +</div> |
| 44 | +``` |
| 45 | + |
| 46 | +Done! |
| 47 | + |
| 48 | +## Accessibility notes |
| 49 | +Our team carefully studied and adhered to [Web Content Accessibility Guidelines 2.1](https://www.w3.org/WAI/standards-guidelines/wcag/) and [WAI-ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices/) when designing this Hook. Here are some facets of accessibility that are handled automatically: |
| 50 | + |
| 51 | +- Strict adherence to the best practices for menus ([WAI-ARIA: 3.15](https://www.w3.org/TR/wai-aria-practices/#menu)) |
| 52 | +- Strict adherence to the best practices for menu buttons ([WAI-ARIA: 3.16](https://www.w3.org/TR/wai-aria-practices/#menubutton)) |
| 53 | +- Full keyboard accessibility ([WCAG: 2.1](https://www.w3.org/WAI/WCAG21/quickref/#keyboard-accessible)) |
| 54 | +- Use of ARIA properties and roles to establish relationships amongst elements ([WCAG: 1.3.1](https://www.w3.org/WAI/WCAG21/quickref/#info-and-relationships)) |
| 55 | +- Use of roles to identify the purpose of different parts of the menu ([WCAG: 1.3.6](https://www.w3.org/WAI/WCAG21/quickref/#identify-purpose)) |
| 56 | +- Focusable components receive focus in an order that preserves meaning and operability ([WCAG: 2.4.3](https://www.w3.org/WAI/WCAG21/quickref/#focus-order)) |
| 57 | +- Appears and operates in predictable ways ([WCAG: 3.2](https://www.w3.org/WAI/WCAG21/quickref/#predictable)) |
| 58 | + |
| 59 | +For more details, see [this comment](https://github.com/sparksuite/react-accessible-dropdown-menu-hook/issues/8#issuecomment-567568103). |
0 commit comments