Skip to content

Commit f0d4be2

Browse files
pfaffeDevtools-frontend LUCI CQ
authored andcommitted
Automatically set jslog for devtools-tooltip
Bug: 392078321 Change-Id: Ib2b1d5b4e26ddd19f81f9f0c55433ea4db292ae0 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6322187 Auto-Submit: Philip Pfaffe <[email protected]> Commit-Queue: Philip Pfaffe <[email protected]> Commit-Queue: Danil Somsikov <[email protected]> Reviewed-by: Danil Somsikov <[email protected]>
1 parent abecc01 commit f0d4be2

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

front_end/ui/components/tooltips/Tooltip.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ import * as Lit from '../../lit/lit.js';
88

99
import * as Tooltips from './tooltips.js';
1010

11-
const {html} = Lit;
11+
const {html, nothing} = Lit;
1212

1313
interface RenderProps {
1414
variant?: Tooltips.Tooltip.TooltipVariant;
1515
attribute?: 'aria-describedby'|'aria-details';
1616
useClick?: boolean;
17+
jslogContext?: string;
1718
}
1819

1920
function renderTooltip({
2021
variant = 'simple',
2122
attribute = 'aria-describedby',
2223
useClick = false,
24+
jslogContext = undefined,
2325
}: RenderProps = {}) {
2426
const container = document.createElement('div');
2527
// clang-format off
@@ -28,7 +30,7 @@ function renderTooltip({
2830
html`<button aria-details="tooltip-id">Button</button>` :
2931
html`<button aria-describedby="tooltip-id">Button</button>`
3032
}
31-
<devtools-tooltip id="tooltip-id" variant=${variant} ?use-click=${useClick}>
33+
<devtools-tooltip id="tooltip-id" variant=${variant} ?use-click=${useClick} jslogContext=${jslogContext??nothing}>
3234
${variant === 'rich' ? html`<p>Rich content</p>` : 'Simple content'}
3335
</devtools-tooltip>
3436
`, container);
@@ -169,4 +171,21 @@ describe('Tooltip', () => {
169171

170172
assert.isFalse(container.querySelector('devtools-tooltip')?.open);
171173
});
174+
175+
it('automatically sets and updates jslog', () => {
176+
const container = renderTooltip({jslogContext: 'context'});
177+
const tooltip = container.querySelector('devtools-tooltip');
178+
assert.exists(tooltip);
179+
assert.strictEqual(tooltip.getAttribute('jslog'), 'Popover; context: context; parent: mapped');
180+
181+
tooltip.setAttribute('jslogcontext', 'context2');
182+
assert.strictEqual(tooltip.getAttribute('jslog'), 'Popover; context: context2; parent: mapped');
183+
184+
const anchor = container.createChild('button');
185+
anchor.setAttribute('aria-details', 'constructed-tooltip-id');
186+
const constructedTooltip =
187+
new Tooltips.Tooltip.Tooltip({id: 'constructed-tooltip-id', jslogContext: 'context3', anchor});
188+
container.appendChild(constructedTooltip);
189+
assert.strictEqual(constructedTooltip.getAttribute('jslog'), 'Popover; context: context3; parent: mapped');
190+
});
172191
});

front_end/ui/components/tooltips/Tooltip.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import * as Lit from '../../lit/lit.js';
6+
import * as VisualLogging from '../../visual_logging/visual_logging.js';
67

78
import tooltipStyles from './tooltip.css.js';
89

@@ -14,6 +15,7 @@ export interface TooltipProperties {
1415
id: string;
1516
variant?: TooltipVariant;
1617
anchor?: HTMLElement;
18+
jslogContext?: string;
1719
}
1820

1921
/**
@@ -28,7 +30,7 @@ export interface TooltipProperties {
2830
* @prop {Boolean} useClick - reflects the `"click"` attribute.
2931
*/
3032
export class Tooltip extends HTMLElement {
31-
static readonly observedAttributes = ['id', 'variant'];
33+
static readonly observedAttributes = ['id', 'variant', 'jslogcontext'];
3234

3335
readonly #shadow = this.attachShadow({mode: 'open'});
3436
#anchor: HTMLElement|null = null;
@@ -65,6 +67,14 @@ export class Tooltip extends HTMLElement {
6567
this.setAttribute('variant', variant);
6668
}
6769

70+
get jslogContext(): string|null {
71+
return this.getAttribute('jslogcontext');
72+
}
73+
set jslogContext(jslogContext: string) {
74+
this.setAttribute('jslogcontext', jslogContext);
75+
this.#updateJslog();
76+
}
77+
6878
constructor(properties?: TooltipProperties) {
6979
super();
7080
if (properties) {
@@ -73,6 +83,9 @@ export class Tooltip extends HTMLElement {
7383
if (properties?.variant) {
7484
this.variant = properties.variant;
7585
}
86+
if (properties?.jslogContext) {
87+
this.jslogContext = properties.jslogContext;
88+
}
7689
if (properties?.anchor) {
7790
const ref = properties.anchor.getAttribute('aria-details') ?? properties.anchor.getAttribute('aria-describedby');
7891
if (ref !== properties.id) {
@@ -90,6 +103,8 @@ export class Tooltip extends HTMLElement {
90103
if (name === 'id') {
91104
this.#removeEventListeners();
92105
this.#attachToAnchor();
106+
} else if (name === 'jslogcontext') {
107+
this.#updateJslog();
93108
}
94109
}
95110

@@ -144,11 +159,21 @@ export class Tooltip extends HTMLElement {
144159
}
145160
};
146161

162+
#updateJslog(): void {
163+
if (this.jslogContext && this.#anchor) {
164+
VisualLogging.setMappedParent(this, this.#anchor);
165+
this.setAttribute('jslog', VisualLogging.popover(this.jslogContext).parent('mapped').toString());
166+
} else {
167+
this.removeAttribute('jslog');
168+
}
169+
}
170+
147171
#setAttributes(): void {
148172
if (!this.hasAttribute('role')) {
149173
this.setAttribute('role', 'tooltip');
150174
}
151175
this.setAttribute('popover', this.useClick ? 'auto' : 'manual');
176+
this.#updateJslog();
152177
}
153178

154179
#stopPropagation(event: Event): void {
@@ -234,6 +259,8 @@ export class Tooltip extends HTMLElement {
234259
this.#anchor.style.anchorName = anchorName;
235260
this.style.positionAnchor = anchorName;
236261
this.#observeAnchorRemoval(this.#anchor);
262+
263+
this.#updateJslog();
237264
}
238265

239266
#observeAnchorRemoval(anchor: Element): void {

front_end/ui/visual_logging/KnownContextValues.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ export const knownContextValues = new Set([
841841
'content-length',
842842
'content-type',
843843
'content-visibility',
844+
'context',
845+
'context3',
844846
'contextmenu',
845847
'continue',
846848
'continue-replay',

0 commit comments

Comments
 (0)