isInteractiveRole.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _ariaQuery = require("aria-query");
  7. var _jsxAstUtils = require("jsx-ast-utils");
  8. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  9. var _arrayPrototype = _interopRequireDefault(require("array.prototype.flatmap"));
  10. function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
  11. var roles = _ariaQuery.roles.keys();
  12. var interactiveRoles = roles.filter(function (name) {
  13. return !_ariaQuery.roles.get(name)["abstract"] && _ariaQuery.roles.get(name).superClass.some(function (klasses) {
  14. return (0, _arrayIncludes["default"])(klasses, 'widget');
  15. });
  16. });
  17. // 'toolbar' does not descend from widget, but it does support
  18. // aria-activedescendant, thus in practice we treat it as a widget.
  19. interactiveRoles.push('toolbar');
  20. /**
  21. * Returns boolean indicating whether the given element has a role
  22. * that is associated with an interactive component. Used when an element
  23. * has a dynamic handler on it and we need to discern whether or not
  24. * its intention is to be interacted with in the DOM.
  25. *
  26. * isInteractiveRole is a Logical Disjunction:
  27. * https://en.wikipedia.org/wiki/Logical_disjunction
  28. * The JSX element does not have a tagName or it has a tagName and a role
  29. * attribute with a value in the set of non-interactive roles.
  30. */
  31. var isInteractiveRole = function isInteractiveRole(tagName, attributes) {
  32. var value = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
  33. // 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 = (0, _arrayPrototype["default"])(normalizedValues, function (name) {
  42. return (0, _arrayIncludes["default"])(roles, name) ? [name] : [];
  43. });
  44. if (validRoles.length > 0) {
  45. // The first role value is a series takes precedence.
  46. isInteractive = (0, _arrayIncludes["default"])(interactiveRoles, validRoles[0]);
  47. }
  48. return isInteractive;
  49. };
  50. var _default = exports["default"] = isInteractiveRole;
  51. module.exports = exports.default;