Skip to content

Commit

Permalink
アクセシビリティの設定を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
oki07 committed Nov 23, 2024
1 parent 657c3b7 commit 2fa9ab4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/components/dropdownAction/sp-dropdown-action-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export class SpDropdownActionButton extends UbButton {
iconElement.size = "small";
this.buttonElement.insertBefore(iconElement, this.textElement.nextSibling);
}

setAriaHasPopup(value: string) {
this.buttonElement.setAttribute("aria-haspopup", value);
}

setAriaExpanded(value: string) {
this.buttonElement.setAttribute("aria-expanded", value);
}

setAriaControls(value: string) {
this.buttonElement.setAttribute("aria-controls", value);
}
}

declare global {
Expand Down
24 changes: 22 additions & 2 deletions src/components/dropdownAction/sp-dropdown-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function isValidPosition(value: string): value is Position {
return positions.some((position) => position === value);
}

function createMenuId() {
return `sp-dropdown-action-menu-${Math.random().toString(32).substring(2)}`;
}

const styles = new CSSStyleSheet();
styles.replaceSync(`${foundationStyle} ${dropdownActionStyle}`);

Expand All @@ -21,6 +25,8 @@ export class SpDropdownAction extends HTMLElement {
#buttonElement = document.createElement("sp-dropdown-action-button");
#menuElement = document.createElement("div");
#menuSlotElement = document.createElement("slot");

#menuId = createMenuId();

#open: boolean = false;
#disabled: boolean = false;
Expand Down Expand Up @@ -87,7 +93,7 @@ export class SpDropdownAction extends HTMLElement {
connectedCallback() {
this.#buttonElement.addEventListener(
"click",
this.#toggle.bind(this),
this.#toggleMenuVisibility.bind(this),
);

this.#baseElement.appendChild(this.#buttonElement);
Expand All @@ -103,6 +109,7 @@ export class SpDropdownAction extends HTMLElement {

this.shadowRoot?.appendChild(this.#baseElement);

this.#setupAccessibilityAttributes();
this.#syncMenuMinWidthWithButtonWidth();
}

Expand Down Expand Up @@ -132,12 +139,25 @@ export class SpDropdownAction extends HTMLElement {
}
}

#toggle() {
#toggleMenuVisibility() {
this.open = !this.open;
this.#updateAriaExpandedAttribute();
}

#hideMenu() {
this.open = false;
this.#updateAriaExpandedAttribute();
}

#setupAccessibilityAttributes() {
this.#buttonElement.setAriaHasPopup("true");
this.#buttonElement.setAriaControls(this.#menuId);
this.#menuElement.setAttribute("id", this.#menuId);
this.#updateAriaExpandedAttribute();
}

#updateAriaExpandedAttribute() {
this.#buttonElement.setAriaExpanded(this.open ? "true" : "false");
}

#syncMenuMinWidthWithButtonWidth() {
Expand Down

0 comments on commit 2fa9ab4

Please sign in to comment.