All files / react-native-web/src/exports/Dimensions index.js

82.14% Statements 23/28
50% Branches 11/22
100% Functions 7/7
80.77% Lines 21/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                          5x                           5x       4x 4x       1x 1x 1x                         7x       7x 7x   7x             7x             7x 2x               1x 1x             1x 1x         5x 5x 5x    
/**
 * Copyright (c) Nicolas Gallagher.
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */
 
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import invariant from 'fbjs/lib/invariant';
 
export type DisplayMetrics = {|
  fontScale: number,
  height: number,
  scale: number,
  width: number
|};
 
type DimensionsValue = {|
  window: DisplayMetrics,
  screen: DisplayMetrics
|};
 
type DimensionKey = 'window' | 'screen';
 
type DimensionEventListenerType = 'change';
 
const dimensions = {
  window: {
    fontScale: 1,
    height: 0,
    scale: 1,
    width: 0
  },
  screen: {
    fontScale: 1,
    height: 0,
    scale: 1,
    width: 0
  }
};
const listeners = {};
 
export default class Dimensions {
  static get(dimension: DimensionKey): DisplayMetrics {
    invariant(dimensions[dimension], `No dimension set for key ${dimension}`);
    return dimensions[dimension];
  }
 
  static set(initialDimensions: ?DimensionsValue): void {
    Eif (initialDimensions) {
      Eif (canUseDOM) {
        invariant(false, 'Dimensions cannot be set in the browser');
      } else {
        if (initialDimensions.screen != null) {
          dimensions.screen = initialDimensions.screen;
        }
        if (initialDimensions.window != null) {
          dimensions.window = initialDimensions.window;
        }
      }
    }
  }
 
  static _update() {
    Iif (!canUseDOM) {
      return;
    }
 
    const win = window;
    const docEl = win.document.documentElement;
 
    dimensions.window = {
      fontScale: 1,
      height: docEl.clientHeight,
      scale: win.devicePixelRatio || 1,
      width: docEl.clientWidth
    };
 
    dimensions.screen = {
      fontScale: 1,
      height: win.screen.height,
      scale: win.devicePixelRatio || 1,
      width: win.screen.width
    };
 
    if (Array.isArray(listeners['change'])) {
      listeners['change'].forEach((handler) => handler(dimensions));
    }
  }
 
  static addEventListener(
    type: DimensionEventListenerType,
    handler: (DimensionsValue) => void
  ): void {
    listeners[type] = listeners[type] || [];
    listeners[type].push(handler);
  }
 
  static removeEventListener(
    type: DimensionEventListenerType,
    handler: (DimensionsValue) => void
  ): void {
    Eif (Array.isArray(listeners[type])) {
      listeners[type] = listeners[type].filter((_handler) => _handler !== handler);
    }
  }
}
 
Eif (canUseDOM) {
  Dimensions._update();
  window.addEventListener('resize', Dimensions._update, false);
}