Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update Dimensions API Flow types #31898

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions Libraries/Utilities/Dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ import EventEmitter, {
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import NativeDeviceInfo, {
type DisplayMetrics,
type DisplayMetricsAndroid,
type DimensionsPayload,
} from './NativeDeviceInfo';
import invariant from 'invariant';

type DimensionsValue = {
window?: DisplayMetrics,
screen?: DisplayMetrics,
...
};

const eventEmitter = new EventEmitter<{
change: [DimensionsValue],
change: [DimensionsPayload],
}>();
let dimensionsInitialized = false;
let dimensions: DimensionsValue;
let dimensions: DimensionsPayload;

class Dimensions {
/**
Expand All @@ -46,9 +41,9 @@ class Dimensions {
* Example: `const {height, width} = Dimensions.get('window');`
*
* @param {string} dim Name of dimension as defined when calling `set`.
* @returns {Object?} Value for the dimension.
* @returns {DisplayMetrics?} Value for the dimension.
*/
static get(dim: string): Object {
static get(dim: string): DisplayMetrics | DisplayMetricsAndroid {
invariant(dimensions[dim], 'No dimension set for key ' + dim);
return dimensions[dim];
}
Expand All @@ -57,9 +52,9 @@ class Dimensions {
* This should only be called from native code by sending the
* didUpdateDimensions event.
*
* @param {object} dims Simple string-keyed object of dimensions to set
* @param {DimensionsPayload} dims Simple string-keyed object of dimensions to set
*/
static set(dims: $ReadOnly<{[key: string]: any, ...}>): void {
static set(dims: $ReadOnly<DimensionsPayload>): void {
// We calculate the window dimensions in JS so that we don't encounter loss of
// precision in transferring the dimensions (which could be non-integers) over
// the bridge.
Expand Down Expand Up @@ -128,7 +123,7 @@ class Dimensions {
}
}

let initialDims: ?$ReadOnly<{[key: string]: any, ...}> =
let initialDims: ?$ReadOnly<DimensionsPayload> =
global.nativeExtensions &&
global.nativeExtensions.DeviceInfo &&
global.nativeExtensions.DeviceInfo.Dimensions;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Utilities/NativeDeviceInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

type DisplayMetricsAndroid = {|
export type DisplayMetricsAndroid = {|
width: number,
height: number,
scale: number,
Expand Down
7 changes: 5 additions & 2 deletions Libraries/Utilities/useWindowDimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
*/

import Dimensions from './Dimensions';
import {type DisplayMetrics} from './NativeDeviceInfo';
import {
type DisplayMetrics,
type DisplayMetricsAndroid

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Insert ,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma-dangle: Missing trailing comma.

} from './NativeDeviceInfo';
import {useEffect, useState} from 'react';

export default function useWindowDimensions(): DisplayMetrics {
export default function useWindowDimensions(): DisplayMetrics | DisplayMetricsAndroid {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Replace ·DisplayMetrics with ⏎··|·DisplayMetrics⏎·

const [dimensions, setDimensions] = useState(() => Dimensions.get('window'));
useEffect(() => {
function handleChange({window}) {
Expand Down