Skip to content

Commit 4e82c4c

Browse files
authored
Improve a11y attributes and support for id props in dropdown components (#2257)
## Summary: - ActionMenu: Adds new optional props: `id` and `dropdownId`. If not provided, they are auto-generated. Ids are applied and the dropdownId is now used for the `aria-controls` attribute for the opener - SingleSelect and MultiSelect: Adds optional `dropdownId` prop. If `id` or `dropdownId` props are not provided, ids for these will be auto-generated. The opener's `aria-controls` attribute is set to the dropdown id - Make sure default and custom openers have all the correct attributes (`aria-haspopup`, `aria-expanded`). This addresses warnings in the Storybook a11y add on Issue: WB-1714 ## Test plan: - Review documentation ActionMenu, SingleSelect, and MultiSelect - `id` and `dropdownId` props - Docs for `With Custom Opener` example - Confirm in ActionMenu, SingleSelect, and MultiSelect: - When the `id` and `dropdownId` props are not set, there is an auto-generated id for each of these on the opener and the dropdown. The opener should also have `aria-controls` set to the dropdown id - When the `id` and `dropdownId` props are set, they should be the id on the opener and the dropdown elements. The opener should also have `aria-controls` set to the provided `dropdownId` - The opener should have the `aria-haspopup` and `aria-expanded` attributes set - All of the above should apply also when a custom opener is used Author: beaesguerra Reviewers: jandrade Required Reviewers: Approved By: jandrade Checks: ✅ codecov/project, ✅ Lint (ubuntu-latest, 20.x), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ gerald, ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Lint (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ⏭️ Publish npm snapshot, ⏭️ Chromatic - Skip on Release PR (changesets), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️ dependabot Pull Request URL: #2257
1 parent f099cf8 commit 4e82c4c

13 files changed

+1210
-189
lines changed

.changeset/ninety-dogs-push.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@khanacademy/wonder-blocks-dropdown": minor
3+
---
4+
5+
Improves support for providing ids to the opener and dropdown elements. These ids are auto-generated if they are not provided.
6+
7+
Also applies attributes to elements automatically for improved accessibility (`aria-controls`, `aria-haspopup`, `aria-expanded`).
8+
9+
- `ActionMenu`
10+
- Adds new `dropdownId` and `id` props. If these are not provided, these ids will be generated automatically
11+
- `aria-controls` is set to the dropdown id for both default and custom openers
12+
- Ensure `aria-haspopup` and `aria-expanded` attributes are set on both default and custom openers
13+
- `SingleSelect` and `MultiSelect`
14+
- Adds new `dropdownId` prop. If this is not provided, an id will be generated automatically
15+
- If the `id` prop is not provided, an id for the component is now generated automatically
16+
- `aria-controls` is set to the dropdown id for both default and custom openers
17+
- Ensure `id`, `aria-haspopup` and `aria-expanded` attributes are set on both default and custom openers

__docs__/wonder-blocks-dropdown/action-menu.stories.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ export const Controlled: StoryComponentType = {
358358
*
359359
* **Note:** If you need to use a custom ID for testing the opener, make sure to
360360
* pass the testId prop inside the opener component/element.
361+
*
362+
* **Accessibility:** When a custom opener is used, the following attributes are
363+
* added automatically: `aria-expanded`, `aria-haspopup`, and `aria-controls`.
361364
*/
362365

363366
export const CustomOpener: StoryComponentType = {
@@ -375,6 +378,7 @@ export const CustomOpener: StoryComponentType = {
375378
hovered && styles.hovered,
376379
pressed && styles.pressed,
377380
]}
381+
role="button"
378382
>
379383
{text}
380384
</LabelLarge>

__docs__/wonder-blocks-dropdown/base-select.argtypes.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,22 @@ const argTypes: ArgTypes = {
117117
control: {type: "text"},
118118
description: `Unique identifier attached to the field control. If used,
119119
we need to guarantee that the ID is unique within everything
120-
rendered on a page. Used to match \`<label>\` with \`<button>\`
121-
elements for screenreaders.`,
120+
rendered on a page. If one is not provided, one is auto-generated.
121+
Used to match \`<label>\` with \`<button>\` elements for
122+
screenreaders.`,
123+
table: {
124+
type: {summary: "string"},
125+
},
126+
type: {name: "string", required: false},
127+
},
128+
129+
dropdownId: {
130+
control: {type: "text"},
131+
description: `Unique identifier attached to the dropdown. If used,
132+
we need to guarantee that the ID is unique within everything
133+
rendered on a page. If one is not provided, one is auto-generated.
134+
It is used for the opener's \`aria-controls\` attribute for
135+
screenreaders.`,
122136
table: {
123137
type: {summary: "string"},
124138
},

__docs__/wonder-blocks-dropdown/multi-select.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ export const VirtualizedFilterable: StoryComponentType = {
502502
*
503503
* **Note:** If you need to use a custom ID for testing the opener, make sure to
504504
* pass the testId prop inside the opener component/element.
505+
*
506+
* **Accessibility:** When a custom opener is used, the following attributes are
507+
* added automatically: `aria-expanded`, `aria-haspopup`, and `aria-controls`.
505508
*/
506509
export const CustomOpener: StoryComponentType = {
507510
render: Template,

__docs__/wonder-blocks-dropdown/single-select.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@ export const DropdownInModal: StoryComponentType = {
660660
*
661661
* **Note:** If you need to use a custom ID for testing the opener, make sure to
662662
* pass the testId prop inside the opener component/element.
663+
*
664+
* **Accessibility:** When a custom opener is used, the following attributes are
665+
* added automatically: `aria-expanded`, `aria-haspopup`, and `aria-controls`.
663666
*/
664667
export const CustomOpener: StoryComponentType = {
665668
render: Template,

0 commit comments

Comments
 (0)