mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-30 09:54:02 +08:00
It was in panels/elements but it's a useful component that can be used anywhere where we want to link to an element, so I've pulled it out into ui/components so it can be depended on from across the frontend. Bug: none Change-Id: I4ca4192237ad2e360028f014589545404a1145eb Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3707813 Auto-Submit: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Andres Olivares <andoli@chromium.org> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org> Reviewed-by: Andres Olivares <andoli@chromium.org>
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import * as LitHtml from '../../../ui/lit-html/lit-html.js';
|
|
import * as ComponentHelpers from '../helpers/helpers.js';
|
|
|
|
import nodeTextStyles from './nodeText.css.js';
|
|
|
|
const {render, html} = LitHtml;
|
|
|
|
export interface NodeTextData {
|
|
nodeTitle: string;
|
|
nodeId?: string;
|
|
nodeClasses?: string[];
|
|
}
|
|
|
|
export class NodeText extends HTMLElement {
|
|
static readonly litTagName = LitHtml.literal`devtools-node-text`;
|
|
|
|
readonly #shadow = this.attachShadow({mode: 'open'});
|
|
#nodeTitle: string = '';
|
|
#nodeId?: string = '';
|
|
#nodeClasses?: string[] = [];
|
|
|
|
connectedCallback(): void {
|
|
this.#shadow.adoptedStyleSheets = [nodeTextStyles];
|
|
}
|
|
|
|
set data(data: NodeTextData) {
|
|
this.#nodeTitle = data.nodeTitle;
|
|
this.#nodeId = data.nodeId;
|
|
this.#nodeClasses = data.nodeClasses;
|
|
this.#render();
|
|
}
|
|
|
|
#render(): void {
|
|
const hasId = Boolean(this.#nodeId);
|
|
const hasNodeClasses = Boolean(this.#nodeClasses && this.#nodeClasses.length > 0);
|
|
|
|
const parts = [
|
|
html`<span class="node-label-name">${this.#nodeTitle}</span>`,
|
|
];
|
|
|
|
if (this.#nodeId) {
|
|
const classes = LitHtml.Directives.classMap({
|
|
'node-label-id': true,
|
|
'node-multiple-descriptors': hasNodeClasses,
|
|
});
|
|
parts.push(html`<span class=${classes}>#${CSS.escape(this.#nodeId)}</span>`);
|
|
}
|
|
|
|
if (this.#nodeClasses && this.#nodeClasses.length > 0) {
|
|
const text = this.#nodeClasses.map(c => `.${CSS.escape(c)}`).join('');
|
|
const classes = LitHtml.Directives.classMap({
|
|
'node-label-class': true,
|
|
'node-multiple-descriptors': hasId,
|
|
});
|
|
parts.push(html`<span class=${classes}>${text}</span>`);
|
|
}
|
|
|
|
// Disabled until https://crbug.com/1079231 is fixed.
|
|
// clang-format off
|
|
render(html`
|
|
${parts}
|
|
`, this.#shadow, {
|
|
host: this,
|
|
});
|
|
// clang-format on
|
|
}
|
|
}
|
|
|
|
ComponentHelpers.CustomElements.defineComponent('devtools-node-text', NodeText);
|
|
|
|
declare global {
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
interface HTMLElementTagNameMap {
|
|
'devtools-node-text': NodeText;
|
|
}
|
|
}
|