getViewportDimensions.js.flow 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 getViewportDimensions
  8. * @flow
  9. * @typechecks
  10. */
  11. type ViewportDimensions = { width: number; height: number; };
  12. function getViewportWidth(): number {
  13. let width;
  14. if (document.documentElement) {
  15. width = document.documentElement.clientWidth;
  16. }
  17. if (!width && document.body) {
  18. width = document.body.clientWidth;
  19. }
  20. return width || 0;
  21. }
  22. function getViewportHeight(): number {
  23. let height;
  24. if (document.documentElement) {
  25. height = document.documentElement.clientHeight;
  26. }
  27. if (!height && document.body) {
  28. height = document.body.clientHeight;
  29. }
  30. return height || 0;
  31. }
  32. /**
  33. * Gets the viewport dimensions including any scrollbars.
  34. */
  35. function getViewportDimensions(): ViewportDimensions {
  36. return {
  37. width: window.innerWidth || getViewportWidth(),
  38. height: window.innerHeight || getViewportHeight()
  39. };
  40. }
  41. /**
  42. * Gets the viewport dimensions excluding any scrollbars.
  43. */
  44. getViewportDimensions.withoutScrollbars = function (): ViewportDimensions {
  45. return {
  46. width: getViewportWidth(),
  47. height: getViewportHeight()
  48. };
  49. };
  50. module.exports = getViewportDimensions;