mirror of
https://github.com/status-im/react-native.git
synced 2025-01-14 11:34:23 +00:00
cfa4b13472
Summary: This adds new development feature to React Native that provides information about selected element (see the demo in Test Plan). This is how it works: App's root component is rendered to a container that also has a hidden layer called `<InspectorOverlay/>`. When activated, it shows full screen view and captures all touches. On every touch we ask UIManager to find a view for given {x,y} coordinates. Then, we use React's internals to find corresponding React component. `setRootInstance` is used to remember the top level component to start search from, lmk if you have a better idea how to do this. Given a component, we can climb up its owners tree to provice more context on how/where the component is used. In future we could use the `hierarchy` array to inspect and print their props/state. Known bugs and limitations: * InspectorOverlay sometimes receives touches with incorrect coordinates (wtf) * Not integrated with React Chrome Devtools (maybe in followup diffs) * Doesn't work with popovers (maybe put the element inspector into an `<Overlay/>`?) @public Test Plan: https://www.facebook.com/pxlcld/mn5k Works nicely with scrollviews
63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule Inspector
|
|
*/
|
|
'use strict';
|
|
|
|
var ReactInstanceHandles = require('ReactInstanceHandles');
|
|
var ReactInstanceMap = require('ReactInstanceMap');
|
|
var ReactNativeMount = require('ReactNativeMount');
|
|
var ReactNativeTagHandles = require('ReactNativeTagHandles');
|
|
|
|
function traverseOwnerTreeUp(hierarchy, instance) {
|
|
if (instance) {
|
|
hierarchy.unshift(instance);
|
|
traverseOwnerTreeUp(hierarchy, instance._currentElement._owner);
|
|
}
|
|
}
|
|
|
|
function findInstance(component, targetID) {
|
|
if (targetID === findRootNodeID(component)) {
|
|
return component;
|
|
}
|
|
if (component._renderedComponent) {
|
|
return findInstance(component._renderedComponent, targetID);
|
|
} else {
|
|
for (var key in component._renderedChildren) {
|
|
var child = component._renderedChildren[key];
|
|
if (ReactInstanceHandles.isAncestorIDOf(findRootNodeID(child), targetID)) {
|
|
var instance = findInstance(child, targetID);
|
|
if (instance) {
|
|
return instance;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function findRootNodeID(component) {
|
|
var internalInstance = ReactInstanceMap.get(component);
|
|
return internalInstance ? internalInstance._rootNodeID : component._rootNodeID;
|
|
}
|
|
|
|
function findInstanceByNativeTag(rootTag, nativeTag) {
|
|
var containerID = ReactNativeTagHandles.tagToRootNodeID[rootTag];
|
|
var rootInstance = ReactNativeMount._instancesByContainerID[containerID];
|
|
var targetID = ReactNativeTagHandles.tagToRootNodeID[nativeTag];
|
|
return findInstance(rootInstance, targetID);
|
|
}
|
|
|
|
function getOwnerHierarchy(instance) {
|
|
var hierarchy = [];
|
|
traverseOwnerTreeUp(hierarchy, instance);
|
|
return hierarchy;
|
|
}
|
|
|
|
module.exports = {findInstanceByNativeTag, getOwnerHierarchy};
|