Skip to content

Commit 9f31422

Browse files
committed
feat: Tooltip arrow offset for different placements and RTL mode
1 parent 1facddd commit 9f31422

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/components/popover/popover.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export default class IgcPopoverComponent extends LitElement {
8383
@property({ attribute: false })
8484
public arrow: HTMLElement | null = null;
8585

86+
/** Additional offset to apply to the arrow element if enabled. */
87+
@property({ type: Number, attribute: 'arrow-offset' })
88+
public arrowOffset = 0;
89+
8690
/**
8791
* Improves positioning for inline reference elements that span over multiple lines.
8892
* Useful for tooltips or similar components.
@@ -152,6 +156,7 @@ export default class IgcPopoverComponent extends LitElement {
152156
}
153157

154158
@watch('arrow', { waitUntilFirstUpdate: true })
159+
@watch('arrowOffset', { waitUntilFirstUpdate: true })
155160
@watch('flip', { waitUntilFirstUpdate: true })
156161
@watch('inline', { waitUntilFirstUpdate: true })
157162
@watch('offset', { waitUntilFirstUpdate: true })
@@ -305,8 +310,8 @@ export default class IgcPopoverComponent extends LitElement {
305310
this.arrow.part = currentPlacement;
306311

307312
Object.assign(this.arrow.style, {
308-
left: x != null ? `${roundByDPR(x)}px` : '',
309-
top: y != null ? `${roundByDPR(y)}px` : '',
313+
left: x != null ? `${roundByDPR(x + this.arrowOffset)}px` : '',
314+
top: y != null ? `${roundByDPR(y + this.arrowOffset)}px` : '',
310315
[staticSide]: '-4px',
311316
});
312317
}

src/components/tooltip/tooltip.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { watch } from '../common/decorators/watch.js';
1010
import { registerComponent } from '../common/definitions/register.js';
1111
import type { Constructor } from '../common/mixins/constructor.js';
1212
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
13-
import { asNumber } from '../common/util.js';
13+
import { asNumber, isLTR } from '../common/util.js';
1414
import IgcIconComponent from '../icon/icon.js';
1515
import IgcPopoverComponent, {
1616
type PopoverPlacement,
@@ -90,6 +90,32 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
9090
@query('#arrow')
9191
private _arrowElement!: HTMLElement;
9292

93+
private get _arrowOffset() {
94+
if (/-/.test(this.placement)) {
95+
// Horizontal start | end placement
96+
97+
if (/^(left|right)-start$/.test(this.placement)) {
98+
return -8;
99+
}
100+
101+
if (/^(left|right)-end$/.test(this.placement)) {
102+
return 8;
103+
}
104+
105+
// Vertical start | end placement
106+
107+
if (/start$/.test(this.placement)) {
108+
return isLTR(this) ? -8 : 8;
109+
}
110+
111+
if (/end$/.test(this.placement)) {
112+
return isLTR(this) ? 8 : -8;
113+
}
114+
}
115+
116+
return 0;
117+
}
118+
93119
/**
94120
* Whether the tooltip is showing.
95121
*
@@ -391,6 +417,7 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
391417
.offset=${this.offset}
392418
.anchor=${this._controller.anchor ?? undefined}
393419
.arrow=${this.disableArrow ? null : this._arrowElement}
420+
.arrowOffset=${this._arrowOffset}
394421
.shiftPadding=${8}
395422
?open=${this.open}
396423
?inline=${this.inline}

0 commit comments

Comments
 (0)