isInteractiveRole.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 interactiveRoles = 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. }); // 'toolbar' does not descend from widget, but it does support
  19. // aria-activedescendant, thus in practice we treat it as a widget.
  20. interactiveRoles.push('toolbar');
  21. /**
  22. * Returns boolean indicating whether the given element has a role
  23. * that is associated with an interactive component. Used when an element
  24. * has a dynamic handler on it and we need to discern whether or not
  25. * its intention is to be interacted with in the DOM.
  26. *
  27. * isInteractiveRole is a Logical Disjunction:
  28. * https://en.wikipedia.org/wiki/Logical_disjunction
  29. * The JSX element does not have a tagName or it has a tagName and a role
  30. * attribute with a value in the set of non-interactive roles.
  31. */
  32. var isInteractiveRole = function isInteractiveRole(tagName, attributes) {
  33. var value = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role')); // If value is undefined, then the role attribute will be dropped in the DOM.
  34. // If value is null, then getLiteralAttributeValue is telling us that the
  35. // value isn't in the form of a literal
  36. if (value === undefined || value === null) {
  37. return false;
  38. }
  39. var isInteractive = false;
  40. var normalizedValues = String(value).toLowerCase().split(' ');
  41. var validRoles = normalizedValues.reduce(function (accumulator, name) {
  42. if ((0, _arrayIncludes["default"])(roles, name)) {
  43. accumulator.push(name);
  44. }
  45. return accumulator;
  46. }, []);
  47. if (validRoles.length > 0) {
  48. // The first role value is a series takes precedence.
  49. isInteractive = (0, _arrayIncludes["default"])(interactiveRoles, validRoles[0]);
  50. }
  51. return isInteractive;
  52. };
  53. var _default = isInteractiveRole;
  54. exports["default"] = _default;