Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 933x 933x 933x 933x 933x 933x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 191x 5472x 5472x 5472x 191x 180x 180x 191x 5472x 5472x 90x 90x 5472x 5472x 5472x 933x 933x 933x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 933x 933x 933x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 395x 395x 5472x 5472x 279x 279x 5472x 5472x 5472x 933x 933x 933x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 91x 53x 53x 53x 91x 5472x 5472x 5472x 5472x 933x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 959x 959x 959x 5472x 5472x 5472x 5472x 5472x 89x 63x 63x 63x 63x 63x 63x 63x 63x 26x 26x 26x 26x 89x 5472x 5472x 5472x 5472x 933x 933x 5472x 5472x 5472x 5472x 5472x 933x 5472x 5472x 1x 1x 1x 384x 384x 1x 1x 575x 76x 76x 68x 76x 575x 575x 1x 1x 422x 422x 422x 422x 421x 380x 421x 422x 422x 1x 1x 497x 497x 497x 111x 497x 497x 1x 1x 1x | import {
Component,
DestroyRef,
ElementRef,
NgZone,
afterRenderEffect,
computed,
inject,
input,
} from '@angular/core';
import {
observeElementOutsideAngular,
observeSizeOutsideAngular,
simpleMD,
subscribe,
subscribeDocument,
subscribeWindow,
textToLine,
} from '../../utils/internal-utils';
import { Callback } from '../../utils/util.types';
import {
TYPOGRAPHY_VARIANT_DEFAULT,
TypographyDirective,
} from '../../typography/typography.directive';
@Component({
selector: 'gc-tooltip',
templateUrl: './tooltip.component.html',
styleUrls: ['./tooltip.component.css'],
providers: [{ provide: TYPOGRAPHY_VARIANT_DEFAULT, useValue: 'label' }],
hostDirectives: [TypographyDirective],
host: {
role: 'tooltip',
'aria-hidden': 'true',
popover: 'hint',
'[attr.data-testid]': 'testId()',
'[class.gc-horizontal]': 'orientation() === "horizontal"',
'[class.gc-vertical]': 'orientation() === "vertical"',
// negative offsets can be applied as overall (negative) margin, positive offsets need padding to catch mouse events when moving from invoker to tooltip
// -> set separate variables to avoid conditional logic (or extra CSS classes for selecting) in the styles
'[style.--gc-tooltip-offset.px]': 'offset() < 0 ? 0 : offset()',
'[style.--gc-tooltip-extra-margin.px]': 'offset() < 0 ? offset() : 0', // e.g. gc-table-cell-simple
'[style.--gc-tooltip-anchor-name]': 'anchorName()',
},
})
export class TooltipComponent {
private static readonly TOOLTIP_SHOW_TIMEOUT_MS = 2000;
private static readonly SUPPORTS_ANCHORED_CONTAINER_QUERIES = CSS.supports(
'container-type',
'anchored',
);
/**
* Text displayed as the tooltip
*/
public readonly text = input.required<string, string>({
transform: text => simpleMD(textToLine(text, 32, '<br>')),
});
public readonly offset = input(0);
public readonly orientation = input<'vertical' | 'horizontal'>('vertical');
/**
* Optional callback to permit showing of the tooltip.
* By default the tooltip is always permitted to be visible.
*/
public readonly permitTooltip = input<() => boolean>();
public readonly contentId = input.required<string>();
public readonly testId = input<string>();
/**
* Set the owner of the tooltip
*/
public readonly invoker = input.required<
HTMLElement,
ElementRef<HTMLElement> | HTMLElement
>({
transform: e => ('nativeElement' in e ? e.nativeElement : e),
});
/**
* Separate element that shows the tooltip instead of the invoker when focused.
*/
public readonly focusInvoker = input<
HTMLElement,
ElementRef<HTMLElement> | HTMLElement | undefined
>(undefined, {
transform: e =>
e !== undefined && 'nativeElement' in e ? e.nativeElement : e,
});
private readonly anchorName = computed(() => `--${this.contentId()}`);
private readonly tooltipElement = inject<ElementRef<HTMLElement>>(
ElementRef<HTMLElement>,
);
private tooltipTimer?: ReturnType<typeof setTimeout>;
/** @ignore */
private hoverOwner = false;
/** @ignore */
private hoverTooltip = false;
/** @ignore */
private focused = false;
public constructor() {
// anchoring of tooltip arrow via second manual anchor; it should be possible to reuse the implicit anchor from
// the popover (https://github.com/w3c/csswg-drafts/issues/8913, `position-anchor: match-parent`) in the future,
// but currently that's not supported in browsers
afterRenderEffect({
write: onCleanup => {
const tooltipAnchorName = this.anchorName();
const invoker = this.invoker();
const anchorNames = invoker.style.anchorName
.split(',')
.map(s => s.trim())
.filter(s => s !== '');
anchorNames.push(tooltipAnchorName);
invoker.style.anchorName = anchorNames.join(', ');
onCleanup(() => {
const anchorNames = invoker.style.anchorName
.split(',')
.map(s => s.trim());
invoker.style.anchorName = anchorNames
.filter(s => s !== tooltipAnchorName)
.join(', ');
});
},
});
afterRenderEffect({
read: onCleanup => {
const invoker = this.invoker();
// Firefox delivers a mouse enter/leave on disabled buttons
const isButtonDisabled = () => {
return invoker instanceof HTMLButtonElement && invoker.disabled;
};
const unsubShow = subscribe(invoker, 'mouseenter', () => {
if (!isButtonDisabled()) {
this.hoverOwner = true;
this.triggerShowTooltip(invoker);
}
});
const unsubHide = subscribe(invoker, 'mouseleave', () => {
this.hoverOwner = false;
this.triggerHideTooltip();
});
onCleanup(() => {
unsubShow();
unsubHide();
this.hoverOwner = false;
});
},
});
afterRenderEffect({
read: onCleanup => {
const invoker = this.invoker();
const tooltipElement = this.tooltipElement.nativeElement;
const unsubShow = subscribe(tooltipElement, 'mouseenter', () => {
this.hoverTooltip = true;
this.triggerShowTooltip(invoker);
});
const unsubHide = subscribe(tooltipElement, 'mouseleave', () => {
this.hoverTooltip = false;
this.triggerHideTooltip();
});
onCleanup(() => {
unsubShow();
unsubHide();
this.hoverTooltip = false;
});
},
});
afterRenderEffect({
read: onCleanup => {
const target = this.focusInvoker() ?? this.invoker();
const invoker = this.invoker();
const unsubShow = subscribe(target, 'focus', () => {
this.focused = true;
this.triggerShowTooltip(invoker);
});
const unsubHide = subscribe(target, 'blur', () => {
this.focused = false;
this.triggerHideTooltip();
});
onCleanup(() => {
unsubShow();
unsubHide();
this.focused = false;
});
},
});
const zone = inject(NgZone);
/*
* hide the tooltip, if the owner element gets disabled, even when no blur or mouse-left event is delivered
*/
afterRenderEffect({
read: onCleanup => {
const target = this.focusInvoker() ?? this.invoker();
const unsub = observeElementOutsideAngular(
target,
{ attributeFilter: ['disabled'] },
zone,
() => {
if (target.getAttribute('disabled') !== null) {
this.focused = false;
this.hoverOwner = false;
this.triggerHideTooltip();
}
},
);
onCleanup(() => {
unsub();
});
},
});
// https://developer.mozilla.org/en-US/docs/Web/API/Popover_API/Using#using_hint_popover_state
// "Note also that popover="hint" falls back to popover="manual" in unsupporting browsers." (e.g. Safari <= 27.3)
const SUPPORTS_POPOVER_HINT =
this.tooltipElement.nativeElement.popover === 'hint';
afterRenderEffect({
read: onCleanup => {
const invoker = this.invoker();
let whilstVisibleSubscriptions: Callback[] = [];
const deattachWhilstVisibleHandlers = () => {
whilstVisibleSubscriptions.forEach(cleanup => cleanup());
whilstVisibleSubscriptions = [];
};
const toggleSubCleanup = subscribe(
this.tooltipElement.nativeElement,
'beforetoggle',
event => {
if (event.newState === 'open') {
whilstVisibleSubscriptions.push(
subscribeDocument('scroll', () => this.hide(), true),
);
if (!TooltipComponent.SUPPORTS_ANCHORED_CONTAINER_QUERIES) {
// need to detect changes in the anchor position to manually apply arrow flips
whilstVisibleSubscriptions.push(
observeSizeOutsideAngular(
zone,
[invoker, this.tooltipElement.nativeElement],
() => {
this.fixArrowPosition(invoker);
return undefined; // nothing to run inside the zone, direct DOM access and using only signals / on-push changes
},
),
);
whilstVisibleSubscriptions.push(
subscribeWindow(
'resize',
() => this.fixArrowPosition(invoker),
true,
),
);
this.fixArrowPosition(invoker);
}
if (!SUPPORTS_POPOVER_HINT) {
subscribeDocument(
'keydown',
event => {
if (event.key === 'Escape' || event.key === 'Esc') {
this.tooltipElement.nativeElement.hidePopover();
}
},
true,
);
}
} else if (event.newState === 'closed') {
deattachWhilstVisibleHandlers();
}
},
);
onCleanup(() => {
toggleSubCleanup();
deattachWhilstVisibleHandlers();
});
},
});
inject(DestroyRef).onDestroy(() => {
clearTimeout(this.tooltipTimer);
});
}
/** (unconditionally) hide the tooltip popover */
public hide(): void {
this.tooltipElement.nativeElement.hidePopover();
}
private triggerShowTooltip(invoker: HTMLElement) {
this.tooltipTimer = setTimeout(() => {
this.tooltipTimer = undefined;
if (this.calculateVisibility()) {
this.tooltipElement.nativeElement.showPopover({ source: invoker });
}
}, TooltipComponent.TOOLTIP_SHOW_TIMEOUT_MS);
}
private triggerHideTooltip() {
clearTimeout(this.tooltipTimer);
// multiple events when moving mouse between owner and tooltip -> need to delay decision and check for all states after
// all events have been processed
setTimeout(() => {
if (!this.calculateVisibility()) {
this.hide();
}
});
}
private calculateVisibility(): boolean {
const permitTooltip = this.permitTooltip();
return (
(this.hoverOwner || this.hoverTooltip || this.focused) &&
(permitTooltip === undefined || permitTooltip())
);
}
private fixArrowPosition(anchorElement: HTMLElement): void {
const anchorBox = anchorElement.getBoundingClientRect();
const anchorCenter = {
x: anchorBox.x + anchorBox.width / 2,
y: anchorBox.y + anchorBox.height / 2,
};
const tooltipBox =
this.tooltipElement.nativeElement.getBoundingClientRect();
const tooltipCenter = {
x: tooltipBox.x + tooltipBox.width / 2,
y: tooltipBox.y + tooltipBox.height / 2,
};
const classFlipHorizontal = 'gc-flip-horizontal';
if (anchorCenter.x > tooltipCenter.x) {
this.tooltipElement.nativeElement.classList.add(classFlipHorizontal);
} else {
this.tooltipElement.nativeElement.classList.remove(classFlipHorizontal);
}
const classFlipVertical = 'gc-flip-vertical';
if (anchorCenter.y > tooltipCenter.y) {
this.tooltipElement.nativeElement.classList.add(classFlipVertical);
} else {
this.tooltipElement.nativeElement.classList.remove(classFlipVertical);
}
}
}
|