Skip to content

Commit

Permalink
fix(button): provides context for AT users when used as reference ele…
Browse files Browse the repository at this point in the history
…ment for collapsible content (#7658)

**Related Issue:** #6033 

## Summary

Provides context of expanded for AT users when `calcite-button` is used
as reference element for collapsible content.
  • Loading branch information
anveshmekala authored Sep 29, 2023
1 parent 90b036b commit 50cb3a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/calcite-components/src/components/button/button.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,27 @@ describe("calcite-button", () => {
expect(button1.textContent.length).toBeLessThan(longText.length);
expect(button1.getAttribute("title")).toEqual(longText);
});

it("should set aria-expanded attribute on shadowDOM element when used as trigger", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-button id="test-button" label="Info">Info</calcite-button>
<calcite-popover
id="popover-content"
positioning="fixed"
heading="About this data"
reference-element="test-button"
>
<p>Information</p>
</calcite-popover>`);

const calciteButton = await page.find("calcite-button");
const button = await page.find("calcite-button >>> button");
expect(button.getAttribute("aria-expanded")).toBe("false");
expect(calciteButton.getAttribute("aria-expanded")).toBe("false");

await calciteButton.click();
await page.waitForChanges();
expect(button.getAttribute("aria-expanded")).toBe("true");
expect(calciteButton.getAttribute("aria-expanded")).toBe("true");
});
});
13 changes: 13 additions & 0 deletions packages/calcite-components/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { Appearance, FlipContext, Kind, Scale, Width } from "../interfaces";
import { ButtonMessages } from "./assets/button/t9n";
import { ButtonAlignment } from "./interfaces";
import { CSS } from "./resources";
import {
GlobalAttrComponent,
unwatchGlobalAttributes,
watchGlobalAttributes,
} from "../../utils/globalAttributes";

/** Passing a 'href' will render an anchor link, instead of a button. Role will be set to link, or button, depending on this. */
/** It is the consumers responsibility to add aria information, rel, target, for links, and any button attributes for form submission */
Expand All @@ -39,6 +44,7 @@ import { CSS } from "./resources";
})
export class Button
implements
GlobalAttrComponent,
LabelableComponent,
InteractiveComponent,
FormOwner,
Expand Down Expand Up @@ -175,6 +181,7 @@ export class Button
connectInteractive(this);
connectLocalized(this);
connectMessages(this);
watchGlobalAttributes(this, ["aria-expanded"]);
this.hasLoader = this.loading;
this.setupTextContentObserver();
connectLabel(this);
Expand All @@ -189,6 +196,7 @@ export class Button
disconnectMessages(this);
this.resizeObserver?.disconnect();
this.formEl = null;
unwatchGlobalAttributes(this);
}

async componentWillLoad(): Promise<void> {
Expand Down Expand Up @@ -268,6 +276,7 @@ export class Button
target={childElType === "a" && this.target}
title={this.tooltipText}
type={childElType === "button" && this.type}
{...this.globalAttributes}
>
{loaderNode}
{this.iconStart ? iconStartEl : null}
Expand Down Expand Up @@ -344,6 +353,10 @@ export class Button

resizeObserver = createObserver("resize", () => this.setTooltipText());

@State() globalAttributes = {
ariaExpanded: undefined,
};

//--------------------------------------------------------------------------
//
// Private Methods
Expand Down
4 changes: 2 additions & 2 deletions packages/calcite-components/src/utils/globalAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createObserver } from "./observers";

type AttributeObject = { [k: string]: any };
type AllowedGlobalAttribute = "lang" | "role";
const allowedGlobalAttributes = ["lang", "role"];
type AllowedGlobalAttribute = "lang" | "role" | "aria-expanded";
const allowedGlobalAttributes = ["lang", "role", "aria-expanded"];

const elementToComponentAndObserverOptionsMap = new Map<
HTMLElement,
Expand Down

0 comments on commit 50cb3a6

Please sign in to comment.