1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 * @format
8 */
9
10import {EmitterSubscription} from '../vendor/emitter/EventEmitter';
11
12// Used by Dimensions below
13export interface ScaledSize {
14  width: number;
15  height: number;
16  scale: number;
17  fontScale: number;
18}
19
20/**
21 * Initial dimensions are set before `runApplication` is called so they should
22 * be available before any other require's are run, but may be updated later.
23 *
24 * Note: Although dimensions are available immediately, they may change (e.g
25 * due to device rotation) so any rendering logic or styles that depend on
26 * these constants should try to call this function on every render, rather
27 * than caching the value (for example, using inline styles rather than
28 * setting a value in a `StyleSheet`).
29 *
30 * Example: `const {height, width} = Dimensions.get('window');`
31 *
32 * @param dim Name of dimension as defined when calling `set`.
33 * @returns Value for the dimension.
34 * @see https://reactnative.dev/docs/dimensions#content
35 */
36export interface Dimensions {
37  /**
38       * Initial dimensions are set before runApplication is called so they
39       * should be available before any other require's are run, but may be
40       * updated later.
41       * Note: Although dimensions are available immediately, they may
42       * change (e.g due to device rotation) so any rendering logic or
43       * styles that depend on these constants should try to call this
44       * function on every render, rather than caching the value (for
45       * example, using inline styles rather than setting a value in a
46       * StyleSheet).
47       * Example: const {height, width} = Dimensions.get('window');
48       @param dim Name of dimension as defined when calling set.
49       @returns Value for the dimension.
50       */
51  get(dim: 'window' | 'screen'): ScaledSize;
52
53  /**
54   * This should only be called from native code by sending the didUpdateDimensions event.
55   * @param dims Simple string-keyed object of dimensions to set
56   */
57  set(dims: {[key: string]: any}): void;
58
59  /**
60   * Add an event listener for dimension changes
61   *
62   * @param type the type of event to listen to
63   * @param handler the event handler
64   */
65  addEventListener(
66    type: 'change',
67    handler: ({
68      window,
69      screen,
70    }: {
71      window: ScaledSize;
72      screen: ScaledSize;
73    }) => void,
74  ): EmitterSubscription;
75}
76
77export function useWindowDimensions(): ScaledSize;
78
79export const Dimensions: Dimensions;
80