getViewportDimensions.js.flow 1.3 KB

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