Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Support hiding components in devtools with Symbol.for('react.devtools.hide') #997

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions backend/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ function getData(internalInstance: Object): DataType {
};
}

const hideSymbol = nodeType === 'Composite' &&
typeof Symbol === 'function' &&
typeof type === 'function' &&
type[Symbol.for('react.devtools.hide')];

// $FlowFixMe
return {
nodeType,
Expand All @@ -175,6 +180,7 @@ function getData(internalInstance: Object): DataType {
text,
updater,
publicInstance,
hideSymbol,
};
}

Expand Down
6 changes: 6 additions & 0 deletions backend/getDataFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function getDataFiber(fiber: Object, getOpaqueNode: (fiber: Object) => Object):
var nodeType = null;
var name = null;
var text = null;
var hideSymbol = null;

switch (fiber.tag) {
case FunctionalComponent:
Expand All @@ -58,12 +59,16 @@ function getDataFiber(fiber: Object, getOpaqueNode: (fiber: Object) => Object):
publicInstance = fiber.stateNode;
props = fiber.memoizedProps;
state = fiber.memoizedState;
hideSymbol = typeof Symbol === 'function' && fiber.type[Symbol.for('react.devtools.hide')];
if (publicInstance != null) {
context = publicInstance.context;
if (context && Object.keys(context).length === 0) {
context = null;
}
}
if (name === 'HigherOrderComponent') {
console.log('fiber backend', fiber);
}
const inst = publicInstance;
if (inst) {
updater = {
Expand Down Expand Up @@ -192,6 +197,7 @@ function getDataFiber(fiber: Object, getOpaqueNode: (fiber: Object) => Object):
text,
updater,
publicInstance,
hideSymbol,
};
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/Breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function getBreadcrumbPath(store: Store): BreadcrumbPath {
}

module.exports = decorate({
listeners: () => ['breadcrumbHead', 'selected'],
listeners: () => ['breadcrumbHead', 'selected', 'hideSymbol'],
props(store, props) {
return {
select: id => store.selectBreadcrumb(id),
Expand Down
6 changes: 4 additions & 2 deletions frontend/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PropsType = {
isBottomTagSelected: boolean,
searchRegExp: ?RegExp,
wrappedChildren: ?Array<any>,
hideSymbol: boolean,
onHover: (isHovered: boolean) => void,
onHoverBottom: (isHovered: boolean) => void,
onContextMenu: () => void,
Expand Down Expand Up @@ -185,7 +186,7 @@ class Node extends React.Component<PropsType, StateType> {

let children = node.get('children');

if (node.get('nodeType') === 'Wrapper') {
if (node.get('nodeType') === 'Wrapper' || (this.props.hideSymbol && node.get('hideSymbol'))) {
return (
<span>
{children.map(child =>
Expand Down Expand Up @@ -413,7 +414,7 @@ Node.contextTypes = {

var WrappedNode = decorate({
listeners(props) {
return [props.id];
return [props.id, 'hideSymbol'];
},
props(store, props) {
var node = store.get(props.id);
Expand All @@ -430,6 +431,7 @@ var WrappedNode = decorate({
isBottomTagHovered: store.isBottomTagHovered,
hovered: store.hovered === props.id,
searchRegExp: props.searchRegExp,
hideSymbol: store.hideSymbol,
onToggleCollapse: e => {
e.preventDefault();
store.toggleCollapse(props.id);
Expand Down
2 changes: 2 additions & 0 deletions frontend/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type State = {
preferencesPanelShown: boolean,
themeKey: number,
themeName: ?string,
hideSymbol: boolean,
};

class Panel extends React.Component<Props, State> {
Expand All @@ -92,6 +93,7 @@ class Panel extends React.Component<Props, State> {
isReact: props.alreadyFoundReact,
themeKey: 0,
themeName: props.themeName,
hideSymbol: true,
};
this._unMounted = false;
window.panel = this;
Expand Down
24 changes: 22 additions & 2 deletions frontend/PreferencesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import type {Theme} from './types';

type Props = {
changeTheme: (themeName: string) => void,
changeHideSymbol: (enabled: boolean) => void,
hasCustomTheme: boolean,
hide: () => void,
open: bool,
hideSymbol: bool,
};

type State = {
Expand Down Expand Up @@ -66,7 +68,7 @@ class PreferencesPanel extends React.Component<Props, State> {

render() {
const {browserName, showHiddenThemes, theme, themeName, themes} = this.context;
const {hasCustomTheme, hide, open} = this.props;
const {hasCustomTheme, hide, open, hideSymbol} = this.props;
const {editMode} = this.state;

if (!open) {
Expand Down Expand Up @@ -108,6 +110,16 @@ class PreferencesPanel extends React.Component<Props, State> {
<EditIcon />
</EditButton>
</div>
<div>
<label>
<input
type="checkbox"
checked={hideSymbol}
onChange={this._changeHideSymbol}
/>
Hide components with truthy <code>Symbol.for('react.devtools.hide')</code> property
</label>
</div>
<div style={styles.buttonBar}>
<button
onClick={hide}
Expand All @@ -133,6 +145,12 @@ class PreferencesPanel extends React.Component<Props, State> {
changeTheme(event.target.value);
};

_changeHideSymbol = (event) => {
const {changeHideSymbol} = this.props;

changeHideSymbol(event.target.checked);
}

_hide = () => {
const {hide} = this.props;
const {editMode} = this.state;
Expand Down Expand Up @@ -198,14 +216,16 @@ const blockClick = event => event.stopPropagation();

const WrappedPreferencesPanel = decorate({
listeners() {
return ['preferencesPanelShown'];
return ['preferencesPanelShown', 'hideSymbol'];
},
props(store, props) {
return {
changeTheme: themeName => store.changeTheme(themeName),
changeHideSymbol: enabled => store.changeHideSymbol(enabled),
hasCustomTheme: !!store.themeStore.customTheme,
hide: () => store.hidePreferencesPanel(),
open: store.preferencesPanelShown,
hideSymbol: store.hideSymbol,
};
},
}, PreferencesPanel);
Expand Down
9 changes: 8 additions & 1 deletion frontend/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Store extends EventEmitter {
selectedTab: string;
selected: ?ElementID;
themeStore: ThemeStore;
hideSymbol: boolean;
breadcrumbHead: ?ElementID;
// an object describing the capabilities of the inspected runtime.
capabilities: {
Expand Down Expand Up @@ -140,6 +141,7 @@ class Store extends EventEmitter {
this.colorizerState = null;
this.refreshSearch = false;
this.themeStore = themeStore;
this.hideSymbol = true;

// for debugging
window.store = this;
Expand Down Expand Up @@ -362,6 +364,11 @@ class Store extends EventEmitter {
this.emit('theme');
}

changeHideSymbol(enabled: boolean) {
this.hideSymbol = enabled;
this.emit('hideSymbol');
}

showPreferencesPanel() {
this.preferencesPanelShown = true;
this.emit('preferencesPanelShown');
Expand Down Expand Up @@ -504,7 +511,7 @@ class Store extends EventEmitter {
var node = this.get(id);
var nodeType = node.get('nodeType');

if (nodeType !== 'Wrapper' && nodeType !== 'Native') {
if (nodeType !== 'Wrapper' && nodeType !== 'Native' && !(nodeType === 'Composite' && this.hideSymbol && node.get('hideSymbol'))) {
return id;
}
if (nodeType === 'Native' && (!up || this.get(this._parents.get(id)).get('nodeType') !== 'NativeWrapper')) {
Expand Down
3 changes: 3 additions & 0 deletions test/example/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ function wrapWithHoc(Component) {
return <div><Component /></div>;
}
}
if (typeof Symbol === 'function') {
HigherOrderComponent[Symbol.for('react.devtools.hide')] = true;
}

return HigherOrderComponent;
}
Expand Down