isNonInteractiveRole.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 nonInteractiveRoles = _ariaQuery.roles.keys().filter(function (name) {
  12. return !_ariaQuery.roles.get(name)["abstract"] && !_ariaQuery.roles.get(name).superClass.some(function (klasses) {
  13. return (0, _arrayIncludes["default"])(klasses, 'widget');
  14. });
  15. });
  16. /**
  17. * Returns boolean indicating whether the given element has a role
  18. * that is associated with a non-interactive component. Non-interactive roles
  19. * include `listitem`, `article`, or `dialog`. These are roles that indicate
  20. * for the most part containers.
  21. *
  22. * Elements with these roles should not respond or handle user interactions.
  23. * For example, an `onClick` handler should not be assigned to an element with
  24. * the role `listitem`. An element inside the `listitem`, like a button or a
  25. * link, should handle the click.
  26. *
  27. * This utility returns true for elements that are assigned a non-interactive
  28. * role. It will return false for elements that do not have a role. So whereas
  29. * a `div` might be considered non-interactive, for the purpose of this utility,
  30. * it is considered neither interactive nor non-interactive -- a determination
  31. * cannot be made in this case and false is returned.
  32. */
  33. var isNonInteractiveRole = function isNonInteractiveRole(tagName, attributes) {
  34. // Do not test higher level JSX components, as we do not know what
  35. // low-level DOM element this maps to.
  36. if (!_ariaQuery.dom.has(tagName)) {
  37. return false;
  38. }
  39. var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(attributes, 'role'));
  40. var isNonInteractive = false;
  41. var normalizedValues = String(role).toLowerCase().split(' ');
  42. var validRoles = (0, _arrayPrototype["default"])(normalizedValues, function (name) {
  43. return _ariaQuery.roles.has(name) ? [name] : [];
  44. });
  45. if (validRoles.length > 0) {
  46. // The first role value is a series takes precedence.
  47. isNonInteractive = (0, _arrayIncludes["default"])(nonInteractiveRoles, validRoles[0]);
  48. }
  49. return isNonInteractive;
  50. };
  51. var _default = exports["default"] = isNonInteractiveRole;
  52. module.exports = exports.default;