mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-09 21:28:43 +08:00
IconButton was not inheriting font size, family, color from the outside, resulting in wrong values. Bug: chromium:1178785 Change-Id: I299a5792908d403edc56f11e181b70e2d2bf2d53 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2697193 Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
// Copyright 2021 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 '../../third_party/lit-html/lit-html.js';
|
|
import {IconData} from './Icon.js';
|
|
|
|
export interface IconWithTextData {
|
|
iconName: string;
|
|
iconColor?: string;
|
|
text?: string;
|
|
}
|
|
|
|
export interface IconButtonData {
|
|
clickHandler: () => void;
|
|
groups: IconWithTextData[];
|
|
}
|
|
|
|
export class IconButton extends HTMLElement {
|
|
private readonly shadow = this.attachShadow({mode: 'open'});
|
|
private clickHandler: () => void = () => {};
|
|
private groups: IconWithTextData[] = [];
|
|
|
|
set data(data: IconButtonData) {
|
|
this.groups = data.groups;
|
|
this.clickHandler = data.clickHandler;
|
|
this.render();
|
|
}
|
|
|
|
setTexts(texts: (string|undefined)[]): void {
|
|
if (texts.length !== this.groups.length) {
|
|
throw new Error(`Wrong number of texts, expected ${this.groups.length} but got ${texts.length}`);
|
|
}
|
|
for (let i = 0; i < texts.length; ++i) {
|
|
this.groups[i].text = texts[i];
|
|
}
|
|
this.render();
|
|
}
|
|
|
|
private onClickHandler(event: Event): void {
|
|
event.preventDefault();
|
|
this.clickHandler();
|
|
}
|
|
|
|
private render(): void {
|
|
// Disabled until https://crbug.com/1079231 is fixed.
|
|
// clang-format off
|
|
return LitHtml.render(LitHtml.html`
|
|
<style>
|
|
:host {
|
|
white-space: normal;
|
|
}
|
|
|
|
.icon-button {
|
|
cursor: pointer;
|
|
background-color: var(--toolbar-bg-color);
|
|
border: 1px solid var(--divider-color);
|
|
border-radius: 2px;
|
|
margin-right: 2px;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
color: inherit;
|
|
font-size: inherit;
|
|
font-family: inherit;
|
|
}
|
|
|
|
.icon-button:hover,
|
|
.icon-button:focus-visible {
|
|
background-color: var(--toolbar-hover-bg-color);
|
|
}
|
|
|
|
.icon-button-title {
|
|
margin-left: 0.5ex;
|
|
}
|
|
|
|
.status-icon {
|
|
margin-left: 1ex;
|
|
}
|
|
|
|
.status-icon:first-child {
|
|
margin-left: inherit;
|
|
}
|
|
</style>
|
|
<button class="icon-button" @click=${this.onClickHandler}>
|
|
${this.groups.filter(counter => counter.text !== undefined).map(counter =>
|
|
LitHtml.html`
|
|
<devtools-icon class="status-icon"
|
|
.data=${{iconName: counter.iconName, color: counter.iconColor || '', width: '1.5ex', height: '1.5ex'} as IconData}>
|
|
</devtools-icon>
|
|
<span class="icon-button-title">${counter.text}</span>
|
|
</button>`)}
|
|
`, this.shadow, { eventContext: this});
|
|
// clang-format on
|
|
}
|
|
}
|
|
|
|
customElements.define('icon-button', IconButton);
|
|
|
|
declare global {
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
interface HTMLElementTagNameMap {
|
|
'icon-button': IconButton;
|
|
}
|
|
}
|