Skip to content

Commit

Permalink
Merge pull request #128 from GovTechSG/dropdown-a11y
Browse files Browse the repository at this point in the history
WIP: Implement a11y for Dropdown
  • Loading branch information
clukhei authored Oct 3, 2023
2 parents d18c619 + 59efc9e commit 0ff0cf6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/base/dropdown-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { property, state } from "lit/decorators.js";
import { Ref, createRef } from "lit/directives/ref.js";
import mergeDeep from "../utils/mergeDeep";
import SgdsElement from "./sgds-element";
import generateId from "../utils/generateId";

const ARROW_DOWN = "ArrowDown";
const ARROW_UP = "ArrowUp";
Expand All @@ -29,6 +30,9 @@ export class DropdownElement extends SgdsElement {
/** @internal */
protected bsDropdown: BsDropdown = null;

/** @internal Unique id generated for the dropdown menu */
protected dropdownMenuId: string = generateId("dropdown-menu", "ul");

/** @internal Controls auto-flipping of menu */
@property({ type: Boolean, state: true })
protected noFlip = false;
Expand Down
6 changes: 5 additions & 1 deletion src/components/ComboBox/sgds-combo-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ export class SgdsComboBox extends ScopedElementsMixin(DropdownListElement) {
?readonly=${this.readonly}
.value=${this.value}
@sgds-input=${this._handleInputChange}
role="combobox"
aria-expanded=${this.menuIsOpen}
aria-autocomplete="list"
aria-controls=${this.dropdownMenuId}
>
</sgds-input>
<div class="form-control-icon">
<slot name="icon"></slot>
</div>
<ul class="dropdown-menu" part="menu">
<ul id=${this.dropdownMenuId} class="dropdown-menu" part="menu" tabindex="-1">
${this.filteredMenuList.map(
item => html`<button class="dropdown-item" @click=${this._handleSelectChange}>${item}</button>`
)}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Datepicker/sgds-datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ export class SgdsDatepicker extends ScopedElementsMixin(DropdownElement) {
readonly
?required=${this.required}
?disabled=${this.disabled}
role="combobox"
aria-haspopup="dialog"
aria-controls=${this.dropdownMenuId}
></sgds-input>
<button
?disabled=${this.disabled}
Expand All @@ -344,8 +347,9 @@ export class SgdsDatepicker extends ScopedElementsMixin(DropdownElement) {
${svgEl}
</button>
<ul
id=${this.dropdownMenuId}
class="sgds datepicker dropdown-menu"
role="menu"
role="dialog"
part="menu"
@click=${(event: MouseEvent) => event.stopPropagation()}
>
Expand Down
2 changes: 2 additions & 0 deletions src/components/Dropdown/sgds-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ export class SgdsDropdown extends ScopedElementsMixin(DropdownListElement) {
return html`
<div>
<sgds-button
role="button"
variant="outline-${this.variant}"
?disabled=${this.disabled}
aria-expanded="${this.menuIsOpen}"
aria-haspopup="menu"
${ref(this.myDropdown)}
@click=${() => this.toggleMenu()}
id=${this.togglerId}
Expand Down
2 changes: 2 additions & 0 deletions test/dropdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ describe("sgds-dropdown", () => {
el,
` <div>
<sgds-button
role="button"
aria-expanded="false"
aria-haspopup="menu"
variant="outline-secondary"
>
<svg
Expand Down

0 comments on commit 0ff0cf6

Please sign in to comment.