Skip to content

Commit bd22772

Browse files
Copilotkdinev
andcommitted
Fix tooltip max-width constraint for custom content
Co-authored-by: kdinev <1472513+kdinev@users.noreply.github.com>
1 parent ad0a029 commit bd22772

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/components/tooltip/themes/tooltip.base.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
font-weight: 600;
1717
line-height: rem(16px);
1818
text-align: start;
19-
max-width: 200px;
2019
display: flex;
2120
align-items: flex-start;
2221
gap: rem(8px);
2322
position: relative;
23+
24+
&.simple-text {
25+
max-width: 200px;
26+
}
2427
}
2528

2629
#arrow {

src/components/tooltip/tooltip.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,40 @@ describe('Tooltip', () => {
276276
expect(defaultSlot?.assignedElements()[0].matches('button')).to.be.true;
277277
});
278278

279+
it('should apply simple-text class when using message property only', async () => {
280+
const template = html`
281+
<div>
282+
<button>Hover</button>
283+
<igc-tooltip message="Simple text tooltip"></igc-tooltip>
284+
</div>
285+
`;
286+
const container = await fixture(template);
287+
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
288+
await elementUpdated(tooltip);
289+
290+
const baseElement = tooltip.renderRoot.querySelector('[part="base"]');
291+
expect(baseElement).not.to.be.null;
292+
expect(baseElement?.classList.contains('simple-text')).to.be.true;
293+
});
294+
295+
it('should not apply simple-text class when using custom content', async () => {
296+
const template = html`
297+
<div>
298+
<button>Hover</button>
299+
<igc-tooltip>
300+
<div>Custom content with complex structure</div>
301+
</igc-tooltip>
302+
</div>
303+
`;
304+
const container = await fixture(template);
305+
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
306+
await elementUpdated(tooltip);
307+
308+
const baseElement = tooltip.renderRoot.querySelector('[part="base"]');
309+
expect(baseElement).not.to.be.null;
310+
expect(baseElement?.classList.contains('simple-text')).to.be.false;
311+
});
312+
279313
it('should render a default close button when in `sticky` mode', async () => {
280314
tooltip.sticky = true;
281315
await elementUpdated(tooltip);

src/components/tooltip/tooltip.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { html, LitElement, nothing, type PropertyValues } from 'lit';
2-
import { property, query } from 'lit/decorators.js';
2+
import { property, query, state } from 'lit/decorators.js';
3+
import { classMap } from 'lit/directives/class-map.js';
34
import { createRef, ref } from 'lit/directives/ref.js';
45
import { EaseOut } from '../../animations/easings.js';
56
import { addAnimationController } from '../../animations/player.js';
@@ -101,6 +102,12 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
101102
@query('#arrow')
102103
private _arrowElement!: HTMLElement;
103104

105+
@query('slot:not([name])')
106+
private _defaultSlot!: HTMLSlotElement;
107+
108+
@state()
109+
private _hasCustomContent = false;
110+
104111
private get _arrowOffset() {
105112
if (this.placement.includes('-')) {
106113
// Horizontal start | end placement
@@ -291,6 +298,7 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
291298
this.requestUpdate();
292299
});
293300
}
301+
this._checkForCustomContent();
294302
}
295303

296304
protected override willUpdate(changedProperties: PropertyValues<this>): void {
@@ -429,7 +437,23 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
429437
this._emitEvent('igcClosed');
430438
}
431439

440+
private _checkForCustomContent(): void {
441+
if (this._defaultSlot) {
442+
const assignedNodes = this._defaultSlot.assignedNodes({ flatten: true });
443+
// If there are assigned nodes, we have custom content
444+
this._hasCustomContent = assignedNodes.length > 0;
445+
}
446+
}
447+
448+
private _handleSlotChange(): void {
449+
this._checkForCustomContent();
450+
}
451+
432452
protected override render() {
453+
const classes = {
454+
'simple-text': !this._hasCustomContent,
455+
};
456+
433457
return html`
434458
<igc-popover
435459
.inert=${!this.open}
@@ -443,8 +467,8 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
443467
flip
444468
shift
445469
>
446-
<div ${ref(this._containerRef)} part="base">
447-
<slot>${this.message}</slot>
470+
<div ${ref(this._containerRef)} part="base" class=${classMap(classes)}>
471+
<slot @slotchange=${this._handleSlotChange}>${this.message}</slot>
448472
${this.sticky
449473
? html`
450474
<slot name="close-button" @click=${this._setAutoHide}>

0 commit comments

Comments
 (0)