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

Show component location for selected element in bottom/right of props panel #17567

Merged
merged 2 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@
font-family: var(--font-family-sans);
}

.Source {
padding: 0.25rem;
border-top: 1px solid var(--color-border);
}

.SourceHeaderRow {
display: flex;
align-items: center;
}

.SourceHeader {
flex: 1 1;
font-family: var(--font-family-sans);
}

.SourceOneLiner {
font-family: var(--font-family-monospace);
font-size: var(--font-size-monospace-normal);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
margin-left: 1rem;
}

.Component,
.Owner {
color: var(--color-component-name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
*/

import {copy} from 'clipboard-js';
import React, {useCallback, useContext} from 'react';
import {TreeDispatcherContext, TreeStateContext} from './TreeContext';
import {BridgeContext, StoreContext} from '../context';
Expand Down Expand Up @@ -272,6 +273,7 @@ function InspectedElementView({
hooks,
owners,
props,
source,
state,
} = inspectedElement;

Expand Down Expand Up @@ -403,6 +405,51 @@ function InspectedElementView({
))}
</div>
)}

{source !== null && (
<Source fileName={source.fileName} lineNumber={source.lineNumber} />
)}
</div>
);
}

// The function below (formatSourceForDisplay) is based on packages/shared/describeComponentFrame.js
const BEFORE_SLASH_RE = /^(.*)[\\\/]/;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you move this inside formatSourceForDisplay? Regexes are mutable, and I'm always wary of sharing them across function runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. This follows the precedent in other files (like describeComponentFrame) but I don't mind making the change here.

function formatSourceForDisplay(fileName: string, lineNumber: string) {
let nameOnly = fileName.replace(BEFORE_SLASH_RE, '');
// In DEV, include code for a common special case:
// prefer "folder/index.js" instead of just "index.js".
if (/^index\./.test(nameOnly)) {
const match = fileName.match(BEFORE_SLASH_RE);
if (match) {
const pathBeforeSlash = match[1];
if (pathBeforeSlash) {
const folderName = pathBeforeSlash.replace(BEFORE_SLASH_RE, '');
nameOnly = folderName + '/' + nameOnly;
}
}
}
return `${nameOnly}:${lineNumber}`;
}

type SourceProps = {|
fileName: string,
lineNumber: string,
|};

function Source({fileName, lineNumber}: SourceProps) {
const handleCopy = () => copy(`${fileName}:${lineNumber}`);
return (
<div className={styles.Source}>
<div className={styles.SourceHeaderRow}>
<div className={styles.SourceHeader}>source</div>
<Button onClick={handleCopy} title="Copy to clipboard">
<ButtonIcon type="copy" />
</Button>
</div>
<div className={styles.SourceOneLiner}>
{formatSourceForDisplay(fileName, lineNumber)}
</div>
</div>
);
}
Expand Down