Skip to content

Commit 9d83502

Browse files
hystsgradio-pr-bot
andauthored
Make dropdowns accessible to screen readers (combobox ARIA pattern) (#13542)
* Make dropdowns accessible to screen readers The dropdown text input used role="listbox", an invalid ARIA role for a text field, and had no aria-activedescendant. Screen readers got contradictory semantics and never announced options during keyboard navigation, so the list could not be used. Switch the input to the WAI-ARIA combobox pattern: role="combobox", aria-activedescendant pointing at the active option while the list is open, and aria-autocomplete describing the filter behavior. The popup (ul role="listbox", li role="option") was already correct. Apply the same fix to the multiselect variant, which had no combobox semantics at all, and pass it a listbox_id so its options have stable ids. Update the unit test that asserted the old listbox role and the e2e specs that located the input by that role. * add changeset --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 1d19f2b commit 9d83502

6 files changed

Lines changed: 77 additions & 11 deletions

File tree

‎.changeset/empty-teams-smash.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/dropdown": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Make dropdowns accessible to screen readers (combobox ARIA pattern)

‎js/dropdown/dropdown.test.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,11 @@ describe("Single-select: get_data / set_data", () => {
758758
describe("Single-select: Accessibility", () => {
759759
afterEach(() => cleanup());
760760

761-
test("input exposes the listbox role", async () => {
761+
test("input exposes the combobox role", async () => {
762762
const { getByLabelText } = await render(Dropdown, single_select_props);
763763

764764
const input = getByLabelText("Dropdown") as HTMLInputElement;
765-
expect(input).toHaveAttribute("role", "listbox");
765+
expect(input).toHaveAttribute("role", "combobox");
766766
});
767767

768768
test("aria-controls points at the options listbox", async () => {
@@ -781,6 +781,25 @@ describe("Single-select: Accessibility", () => {
781781
expect(listbox).toBeTruthy();
782782
expect(listbox).toHaveAttribute("role", "listbox");
783783
});
784+
785+
test("aria-activedescendant tracks the active option while navigating", async () => {
786+
const { getByLabelText } = await render(Dropdown, {
787+
...single_select_props,
788+
value: null
789+
});
790+
791+
const input = getByLabelText("Dropdown") as HTMLInputElement;
792+
await input.focus();
793+
expect(input).not.toHaveAttribute("aria-activedescendant");
794+
795+
await event.keyboard("{ArrowDown}");
796+
const active_id = input.getAttribute("aria-activedescendant");
797+
expect(active_id).toBeTruthy();
798+
799+
const active_option = document.getElementById(active_id as string);
800+
expect(active_option).toHaveAttribute("role", "option");
801+
expect(active_option).toHaveAttribute("aria-label", "apple");
802+
});
784803
});
785804

786805
describe("Single-select: Dynamic choices", () => {
@@ -945,6 +964,28 @@ describe("Multiselect: Rendering", () => {
945964
});
946965
});
947966

967+
describe("Multiselect: Accessibility", () => {
968+
afterEach(() => cleanup());
969+
970+
test("input is a combobox wired to the options listbox", async () => {
971+
const { container } = await render(Dropdown, multiselect_props);
972+
973+
const input = container.querySelector("input") as HTMLInputElement;
974+
expect(input).toHaveAttribute("role", "combobox");
975+
976+
await input.focus();
977+
978+
const controls = input.getAttribute("aria-controls");
979+
const listbox = document.getElementById(controls as string);
980+
expect(listbox).toHaveAttribute("role", "listbox");
981+
982+
const active_id = input.getAttribute("aria-activedescendant");
983+
const active_option = document.getElementById(active_id as string);
984+
expect(active_option).toHaveAttribute("role", "option");
985+
expect(active_option).toHaveAttribute("aria-label", "apple");
986+
});
987+
});
988+
948989
describe("Multiselect: Options display", () => {
949990
afterEach(() => cleanup());
950991

‎js/dropdown/shared/Dropdown.svelte‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@
233233
<div class="wrap-inner" class:show_options>
234234
<div class="secondary-wrap">
235235
<input
236-
role="listbox"
236+
role="combobox"
237237
aria-controls={listbox_id}
238238
aria-expanded={show_options}
239+
aria-activedescendant={show_options && active_index !== null
240+
? `${listbox_id}-option-${active_index}`
241+
: undefined}
242+
aria-autocomplete={filterable ? "list" : "none"}
239243
aria-label={label}
240244
class="border-none"
241245
class:subdued={!choices_names.includes(input_text) &&

‎js/dropdown/shared/Multiselect.svelte‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
let filter_input: HTMLElement;
1515
let input_text = $state("");
1616
let label = $derived(gradio.shared.label || "Multiselect");
17+
18+
const uid = $props.id();
19+
const listbox_id = `${uid}-options`;
1720
let buttons = $derived(gradio.props.buttons);
1821
1922
// Translate the display side only; values stay raw for event payloads.
@@ -242,6 +245,14 @@
242245
{/each}
243246
<div class="secondary-wrap">
244247
<input
248+
role="combobox"
249+
aria-controls={listbox_id}
250+
aria-expanded={show_options}
251+
aria-activedescendant={show_options && active_index !== null
252+
? `${listbox_id}-option-${active_index}`
253+
: undefined}
254+
aria-autocomplete={gradio.props.filterable ? "list" : "none"}
255+
aria-label={label}
245256
class="border-none"
246257
class:subdued={(!choices_names.includes(input_text) &&
247258
!gradio.props.allow_custom_value) ||
@@ -290,6 +301,7 @@
290301
{disabled}
291302
{selected_indices}
292303
{active_index}
304+
{listbox_id}
293305
remember_scroll={true}
294306
onchange={handle_option_selected}
295307
/>

‎js/spa/test/change_vs_input.spec.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,18 @@ test("Radio change and input events work correctly", async ({ page }) => {
9696
});
9797

9898
test("Dropdown change and input events work correctly", async ({ page }) => {
99-
await page.getByRole("listbox", { name: "DD Input" }).click();
99+
await page.getByRole("combobox", { name: "DD Input" }).click();
100100
await page.getByRole("option", { name: "b" }).click();
101101

102102
await expect(page.getByLabel("Change counter")).toHaveValue("1");
103103

104104
await expect(
105-
page.getByRole("listbox", { name: "Dropdown Input Event", exact: true })
105+
page.getByRole("combobox", { name: "Dropdown Input Event", exact: true })
106106
).toHaveValue("b");
107107
await expect(
108-
page.getByRole("listbox", { name: "Dropdown Change Event x2", exact: true })
108+
page.getByRole("combobox", {
109+
name: "Dropdown Change Event x2",
110+
exact: true
111+
})
109112
).toHaveValue("b");
110113
});

‎js/spa/test/dropdown_custom_value.spec.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test.describe("Dropdown with allow_custom_value=True", () => {
44
test("selecting an option updates the textbox with the value, not the name", async ({
55
page
66
}) => {
7-
const dropdown = page.getByRole("listbox", { name: "Dropdown" });
7+
const dropdown = page.getByRole("combobox", { name: "Dropdown" });
88
const output = page.getByLabel("Output");
99

1010
await dropdown.click();
@@ -16,7 +16,7 @@ test.describe("Dropdown with allow_custom_value=True", () => {
1616
test("blurring dropdown after selecting an option does not change the value", async ({
1717
page
1818
}) => {
19-
const dropdown = page.getByRole("listbox", { name: "Dropdown" });
19+
const dropdown = page.getByRole("combobox", { name: "Dropdown" });
2020
const output = page.getByLabel("Output");
2121

2222
await dropdown.click();
@@ -33,7 +33,7 @@ test.describe("Dropdown with allow_custom_value=True", () => {
3333
test("typing a custom value and pressing Enter updates the textbox", async ({
3434
page
3535
}) => {
36-
const dropdown = page.getByRole("listbox", { name: "Dropdown" });
36+
const dropdown = page.getByRole("combobox", { name: "Dropdown" });
3737
const output = page.getByLabel("Output");
3838

3939
// Focus the dropdown and type a custom value
@@ -48,7 +48,7 @@ test.describe("Dropdown with allow_custom_value=True", () => {
4848
test("typing a custom value and blurring updates the textbox", async ({
4949
page
5050
}) => {
51-
const dropdown = page.getByRole("listbox", { name: "Dropdown" });
51+
const dropdown = page.getByRole("combobox", { name: "Dropdown" });
5252
const output = page.getByLabel("Output");
5353

5454
await dropdown.click();
@@ -61,7 +61,7 @@ test.describe("Dropdown with allow_custom_value=True", () => {
6161
test("selecting an option after typing a custom value works correctly", async ({
6262
page
6363
}) => {
64-
const dropdown = page.getByRole("listbox", { name: "Dropdown" });
64+
const dropdown = page.getByRole("combobox", { name: "Dropdown" });
6565
const output = page.getByLabel("Output");
6666

6767
await dropdown.click();

0 commit comments

Comments
 (0)