getElementPosition.js.flow 741 B

123456789101112131415161718192021222324252627282930
  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 getElementPosition
  8. * @typechecks
  9. */
  10. const getElementRect = require('./getElementRect');
  11. /**
  12. * Gets an element's position in pixels relative to the viewport. The returned
  13. * object represents the position of the element's top left corner.
  14. *
  15. * @param {DOMElement} element
  16. * @return {object}
  17. */
  18. function getElementPosition(element) {
  19. const rect = getElementRect(element);
  20. return {
  21. x: rect.left,
  22. y: rect.top,
  23. width: rect.right - rect.left,
  24. height: rect.bottom - rect.top
  25. };
  26. }
  27. module.exports = getElementPosition;