getActiveElement.js.flow 933 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule getActiveElement
  8. * @typechecks
  9. */
  10. /* eslint-disable fb-www/typeof-undefined */
  11. /**
  12. * Same as document.activeElement but wraps in a try-catch block. In IE it is
  13. * not safe to call document.activeElement if there is nothing focused.
  14. *
  15. * The activeElement will be null only if the document or document body is not
  16. * yet defined.
  17. *
  18. * @param {?DOMDocument} doc Defaults to current document.
  19. * @return {?DOMElement}
  20. */
  21. function getActiveElement(doc) /*?DOMElement*/{
  22. doc = doc || (typeof document !== 'undefined' ? document : undefined);
  23. if (typeof doc === 'undefined') {
  24. return null;
  25. }
  26. try {
  27. return doc.activeElement || doc.body;
  28. } catch (e) {
  29. return doc.body;
  30. }
  31. }
  32. module.exports = getActiveElement;