Skip to content

Commit 9cc920b

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Pressability: Support Rect or Numeric Size
Summary: Introduces `Rect`, an (eventual) replacement for `EdgeInsetsProp`. This new type is then used in `Pressability` to expand support such that `hitSlop` and `pressRectOffset` can be either a `Rect` or a numeric size. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D18742616 fbshipit-source-id: 13dd360f68ab804839938fc950fa2f4b25d3ed8c
1 parent 4bbbe6a commit 9cc920b

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Libraries/Pressability/Pressability.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {isHoverEnabled} from './HoverState.js';
1414
import invariant from 'invariant';
1515
import SoundManager from '../Components/Sound/SoundManager.js';
16-
import type {EdgeInsetsProp} from '../StyleSheet/EdgeInsetsPropType.js';
16+
import {normalizeRect, type RectOrSize} from '../StyleSheet/Rect.js';
1717
import type {
1818
BlurEvent,
1919
FocusEvent,
@@ -40,12 +40,12 @@ export type PressabilityConfig = $ReadOnly<{|
4040
/**
4141
* Amount to extend the `VisualRect` by to create `HitRect`.
4242
*/
43-
hitSlop?: ?EdgeInsetsProp,
43+
hitSlop?: ?RectOrSize,
4444

4545
/**
4646
* Amount to extend the `HitRect` by to create `PressRect`.
4747
*/
48-
pressRectOffset?: ?EdgeInsetsProp,
48+
pressRectOffset?: ?RectOrSize,
4949

5050
/**
5151
* Whether to disable the systemm sound when `onPress` fires on Android.
@@ -753,7 +753,8 @@ export default class Pressability {
753753
top: number,
754754
|}>,
755755
): boolean {
756-
const {hitSlop, pressRectOffset} = this._config;
756+
const hitSlop = normalizeRect(this._config.hitSlop);
757+
const pressRectOffset = normalizeRect(this._config.pressRectOffset);
757758

758759
let regionBottom = responderRegion.bottom;
759760
let regionLeft = responderRegion.left;

Libraries/StyleSheet/EdgeInsetsPropType.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
'use strict';
1212

13-
export type EdgeInsetsProp = $ReadOnly<{|
14-
top?: ?number,
15-
left?: ?number,
16-
bottom?: ?number,
17-
right?: ?number,
18-
|}>;
13+
import type {Rect} from './Rect.js';
14+
15+
export type EdgeInsetsProp = Rect;

Libraries/StyleSheet/Rect.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
* @flow strict
9+
*/
10+
11+
'use strict';
12+
13+
export type Rect = $ReadOnly<{|
14+
bottom?: ?number,
15+
left?: ?number,
16+
right?: ?number,
17+
top?: ?number,
18+
|}>;
19+
20+
export type RectOrSize = Rect | number;
21+
22+
export function createSquare(size: number): Rect {
23+
return {bottom: size, left: size, right: size, top: size};
24+
}
25+
26+
export function normalizeRect(rectOrSize: ?RectOrSize): ?Rect {
27+
return typeof rectOrSize === 'number' ? createSquare(rectOrSize) : rectOrSize;
28+
}

0 commit comments

Comments
 (0)