-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathisNonInteractiveRole.js
64 lines (56 loc) · 2.13 KB
/
isNonInteractiveRole.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @flow
*/
import {
dom,
roles as rolesMap,
} from 'aria-query';
import type { Node } from 'ast-types-flow';
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
import includes from 'array-includes';
import flatMap from 'array.prototype.flatmap';
const roles = [...rolesMap.keys()];
const nonInteractiveRoles = roles.filter((name) => (
!rolesMap.get(name).abstract
&& !rolesMap.get(name).superClass.some((klasses) => includes(klasses, 'widget') || includes(klasses, 'window'))
));
/**
* Returns boolean indicating whether the given element has a role
* that is associated with a non-interactive component. Non-interactive roles
* include `listitem`, `article`, or `dialog`. These are roles that indicate
* for the most part containers.
*
* Elements with these roles should not respond or handle user interactions.
* For example, an `onClick` handler should not be assigned to an element with
* the role `listitem`. An element inside the `listitem`, like a button or a
* link, should handle the click.
*
* This utility returns true for elements that are assigned a non-interactive
* role. It will return false for elements that do not have a role. So whereas
* a `div` might be considered non-interactive, for the purpose of this utility,
* it is considered neither interactive nor non-interactive -- a determination
* cannot be made in this case and false is returned.
*/
const isNonInteractiveRole = (
tagName: string,
attributes: Array<Node>,
): boolean => {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
if (!dom.has(tagName)) {
return false;
}
const role = getLiteralPropValue(getProp(attributes, 'role'));
let isNonInteractive = false;
const normalizedValues = String(role).toLowerCase().split(' ');
const validRoles = flatMap(
normalizedValues,
(name: string) => (includes(roles, name) ? [name] : []),
);
if (validRoles.length > 0) {
// The first role value is a series takes precedence.
isNonInteractive = includes(nonInteractiveRoles, validRoles[0]);
}
return isNonInteractive;
};
export default isNonInteractiveRole;