Skip to content

Commit 1cc6964

Browse files
RyanMullinsLIT team
authored and
LIT team
committed
Improve logic for header content and tooltips in the table element.
If ColumnHeader.html is nullish, generates only the HTMLDivElement necessary to display ColumnHeader.name inside the <th> element and assigns it to ColumnHeader.html. Adds a ColumnHeader.tooltip?: string property containing the content for the tooltip. If present, ColumnHeader.htmlU will be wrapped in a LitTooltip. If nullish, then the content will only be wrapped if the content it exceeds the maximum width of the <th> element. PiperOrigin-RevId: 521794328
1 parent f576d5b commit 1cc6964

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

lit_nlp/client/elements/table.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -332,23 +332,36 @@ export class DataTable extends ReactiveElement {
332332
{name: colInfo} :
333333
{...colInfo};
334334

335-
const shouldDisplayTooltip = this.headerTextMaxWidth != null ?
336-
header.name.length > this.headerTextMaxWidth : false;
337-
338335
const headerWidthStyles = styleMap({
339336
'--header-text-max-width': this.headerTextMaxWidth ?
340337
`${this.headerTextMaxWidth}ch` : 'none',
341338
});
342339

343-
header.html =
344-
header.html ?? html`
345-
<lit-tooltip style="--tooltip-max-width: 500px;"
346-
content=${shouldDisplayTooltip ? header.name : ""}>
347-
<div slot="tooltip-anchor" style=${headerWidthStyles}
348-
class="header-text">${header.name}</div>
349-
</lit-tooltip>`;
350-
header.rightAlign =
351-
header.rightAlign ?? this.shouldRightAlignColumn(index);
340+
// If undefined, generate the HTML for this header from ColumnHeader.name.
341+
header.html ??= html`<div slot="tooltip-anchor" class="header-text"
342+
style=${headerWidthStyles}>
343+
${header.name}
344+
</div>`;
345+
346+
// If undefined, determine if the header should be right aligned.
347+
header.rightAlign ??= this.shouldRightAlignColumn(index);
348+
349+
// Determine if the HTML should be wrapped in a LitTooltip. If so, do it.
350+
const doesTextOverflow = this.headerTextMaxWidth != null ?
351+
header.name.length > this.headerTextMaxWidth : false;
352+
const shouldDisplayTooltip = header.tooltip != null || doesTextOverflow;
353+
if (shouldDisplayTooltip) {
354+
const tooltipPlacement = header.rightAlign ? 'left' : '';
355+
const tooltipStyles = styleMap({
356+
'--tooltip-max-width': header.tooltipMaxWidth ?? '500px',
357+
'--tooltip-width': header.tooltipWidth ?? 'auto',
358+
});
359+
header.html = html`<lit-tooltip content=${header.tooltip ?? header.name}
360+
style=${tooltipStyles} tooltipPosition=${tooltipPlacement}>
361+
${header.html}
362+
</lit-tooltip>`;
363+
}
364+
352365
return header;
353366
});
354367
}

lit_nlp/client/elements/table_types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export interface ColumnHeader {
5757
minWidth?: string;
5858
/** The width of the column, must be a valid CSS string */
5959
width?: string;
60+
/**
61+
* If defined, the table will provide a LitTooltip for this header and pass
62+
* this value to that tooltip via its content= attribute.
63+
*/
64+
tooltip?: string;
65+
/** A value passed to --tooltip-width via the tooltip's styles= attr. */
66+
tooltipWidth?: string;
67+
/** A value passed to --tooltip-max-width via the tooltip's styles= attr. */
68+
tooltipMaxWidth?: string;
6069
}
6170

6271
/** Internal data, including metadata */

lit_nlp/client/modules/metrics_module.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,12 @@ export class MetricsModule extends LitModule {
362362
const [group, metric] = name.split(': ');
363363
return {
364364
name,
365-
html: html`
366-
<lit-tooltip tooltipPosition="left" content=${spec.description}
367-
style="--tooltip-max-width: 500px; --tooltip-width: 200px">
368-
<div class="header-text" slot="tooltip-anchor">
369-
${group}<br>${metric}
370-
</div>
371-
</lit-tooltip>`,
365+
html: html`<div slot="tooltip-anchor" class="header-text">
366+
${group}<br>${metric}
367+
</div>`,
372368
rightAlign: true,
369+
tooltip: spec.description,
370+
tooltipWidth: '200px',
373371
width: '100px'
374372
};
375373
});

0 commit comments

Comments
 (0)