isInteractiveRole.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 _arrayPrototype = _interopRequireDefault(require("array.prototype.flatmap"));
  12. var roles = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
  13. var interactiveRoles = roles.filter(function (name) {
  14. return !_ariaQuery.roles.get(name)["abstract"] && _ariaQuery.roles.get(name).superClass.some(function (klasses) {
  15. return (0, _arrayIncludes["default"])(klasses, 'widget');
  16. });
  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'));
  34. // If value is undefined, then the role attribute will be dropped in the DOM.
  35. // If value is null, then getLiteralAttributeValue is telling us that the
  36. // value isn't in the form of a literal
  37. if (value === undefined || value === null) {
  38. return false;
  39. }
  40. var isInteractive = false;
  41. var normalizedValues = String(value).toLowerCase().split(' ');
  42. var validRoles = (0, _arrayPrototype["default"])(normalizedValues, function (name) {
  43. return (0, _arrayIncludes["default"])(roles, name) ? [name] : [];
  44. });
  45. if (validRoles.length > 0) {
  46. // The first role value is a series takes precedence.
  47. isInteractive = (0, _arrayIncludes["default"])(interactiveRoles, validRoles[0]);
  48. }
  49. return isInteractive;
  50. };
  51. var _default = exports["default"] = isInteractiveRole;
  52. module.exports = exports.default;