Skip to content

Commit

Permalink
feat: add an isDisabled function to identify whether elements are dis…
Browse files Browse the repository at this point in the history
…abled or not (#919)

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
  • Loading branch information
MatanBobi and eps1lon authored May 7, 2023
1 parent ab4e208 commit 215955e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-cycles-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dom-accessibility-api": minor
---

add an `isDisabled` function to check if elements are disabled or not
20 changes: 20 additions & 0 deletions sources/__tests__/is-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isDisabled } from "../is-disabled";
import { cleanup, renderIntoDocument } from "./helpers/test-utils";

describe("isDisabled", () => {
afterEach(cleanup);
test.each([
["<button data-test />", false],
['<button data-test aria-disabled="true" />', true],
['<button data-test disabled="true" />', true],
["<button data-test disabled />", true],
['<button data-test aria-disabled="false" />', false],
["<div data-test disabled />", false],
])("markup #%#", (markup, expectedIsDisabled) => {
const container = renderIntoDocument(markup);
expect(container).not.toBe(null);

const testNode = container.querySelector("[data-test]");
expect(isDisabled(testNode)).toBe(expectedIsDisabled);
});
});
1 change: 1 addition & 0 deletions sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { computeAccessibleDescription } from "./accessible-description";
export { computeAccessibleName } from "./accessible-name";
export { default as getRole } from "./getRole";
export * from "./is-inaccessible";
export { isDisabled } from "./is-disabled";
27 changes: 27 additions & 0 deletions sources/is-disabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getLocalName } from "./getRole";

const elementsSupportingDisabledAttribute = new Set([
"button",
"fieldset",
"input",
"optgroup",
"option",
"select",
"textarea",
]);

/**
* Check if an element is disabled
* https://www.w3.org/TR/html-aam-1.0/#html-attribute-state-and-property-mappings
* https://www.w3.org/TR/wai-aria-1.1/#aria-disabled
*
* @param element
* @returns {boolean} true if disabled, otherwise false
*/
export function isDisabled(element: Element): boolean {
const localName = getLocalName(element);
return elementsSupportingDisabledAttribute.has(localName) &&
element.hasAttribute("disabled")
? true
: element.getAttribute("aria-disabled") === "true";
}

0 comments on commit 215955e

Please sign in to comment.