isNonInteractiveRole.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _ariaQuery = require("aria-query");
  9. var _jsxAstUtils = require("jsx-ast-utils");
  10. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  11. var roles = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
  12. var nonInteractiveRoles = roles.filter(function (name) {
  13. return !_ariaQuery.roles.get(name)["abstract"];
  14. }).filter(function (name) {
  15. return !_ariaQuery.roles.get(name).superClass.some(function (klasses) {
  16. return (0, _arrayIncludes["default"])(klasses, 'widget');
  17. });
  18. });
  19. /**
  20. * Returns boolean indicating whether the given element has a role
  21. * that is associated with a non-interactive component. Non-interactive roles
  22. * include `listitem`, `article`, or `dialog`. These are roles that indicate
  23. * for the most part containers.
  24. *
  25. * Elements with these roles should not respond or handle user interactions.
  26. * For example, an `onClick` handler should not be assigned to an element with
  27. * the role `listitem`. An element inside the `listitem`, like a button or a
  28. * link, should handle the click.
  29. *
  30. * This utility returns true for elements that are assigned a non-interactive
  31. * role. It will return false for elements that do not have a role. So whereas
  32. * a `div` might be considered non-interactive, for the purpose of this utility,
  33. * it is considered neither interactive nor non-interactive -- a determination
  34. * cannot be made in this case and false is returned.
  35. */
  36. var isNonInteractiveRole = function isNonInteractiveRole(tagName, attributes) {
  37. // Do not test higher level JSX components, as we do not know what
  38. // low-level DOM element this maps to.
  39. if (!_ariaQuery.dom.has(tagName)) {
  40. return false;
  41. }
  42. var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
  43. var isNonInteractive = false;
  44. var normalizedValues = String(role).toLowerCase().split(' ');
  45. var validRoles = normalizedValues.reduce(function (accumulator, name) {
  46. if ((0, _arrayIncludes["default"])(roles, name)) {
  47. accumulator.push(name);
  48. }
  49. return accumulator;
  50. }, []);
  51. if (validRoles.length > 0) {
  52. // The first role value is a series takes precedence.
  53. isNonInteractive = (0, _arrayIncludes["default"])(nonInteractiveRoles, validRoles[0]);
  54. }
  55. return isNonInteractive;
  56. };
  57. var _default = isNonInteractiveRole;
  58. exports["default"] = _default;