Files
react-native/Libraries/Inspector/InspectorOverlay.js
T
Eli White 85529fe356 Refactor Inspector to take a react instance
Summary:
We will need to operate on instances instead of react tags. This diff passes through the instance instead of the tag.

This does not make the inspector work in Fabric, but it is a necessary refactor in that direction.

Changelog:
[Internal]

Reviewed By: mdvacca

Differential Revision: D19149647

fbshipit-source-id: 53fa35a2886bf36a146d473a496409dffc114a80
2019-12-17 18:35:00 -08:00

91 lines
2.4 KiB
JavaScript

/**
* 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.
*
* @format
* @flow
*/
'use strict';
const Dimensions = require('../Utilities/Dimensions');
const ElementBox = require('./ElementBox');
const React = require('react');
const StyleSheet = require('../StyleSheet/StyleSheet');
const ReactNative = require('../Renderer/shims/ReactNative');
const UIManager = require('../ReactNative/UIManager');
const View = require('../Components/View/View');
import type {HostComponent} from '../Renderer/shims/ReactNativeTypes';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
import type {PressEvent} from '../Types/CoreEventTypes';
type Inspected = $ReadOnly<{|
frame?: Object,
style?: ViewStyleProp,
|}>;
type Props = $ReadOnly<{|
isFabric: boolean,
inspected?: Inspected,
inspectedView?: ?React.ElementRef<HostComponent<mixed>>,
onTouchViewTag: (tag: number, frame: Object, pointerY: number) => mixed,
|}>;
class InspectorOverlay extends React.Component<Props> {
findViewForTouchEvent: (e: PressEvent) => void = (e: PressEvent) => {
const {locationX, locationY} = e.nativeEvent.touches[0];
UIManager.findSubviewIn(
ReactNative.findNodeHandle(this.props.inspectedView),
[locationX, locationY],
(nativeViewTag, left, top, width, height) => {
this.props.onTouchViewTag(
nativeViewTag,
{left, top, width, height},
locationY,
);
},
);
};
shouldSetResponser: (e: PressEvent) => boolean = (e: PressEvent): boolean => {
this.findViewForTouchEvent(e);
return true;
};
render(): React.Node {
let content = null;
if (this.props.inspected) {
content = (
<ElementBox
frame={this.props.inspected.frame}
style={this.props.inspected.style}
/>
);
}
return (
<View
onStartShouldSetResponder={this.shouldSetResponser}
onResponderMove={this.findViewForTouchEvent}
style={[styles.inspector, {height: Dimensions.get('window').height}]}>
{content}
</View>
);
}
}
const styles = StyleSheet.create({
inspector: {
backgroundColor: 'transparent',
position: 'absolute',
left: 0,
top: 0,
right: 0,
},
});
module.exports = InspectorOverlay;