@internetarchive/collection-browser 3.5.2-webdev-8162.2 → 3.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +27 -27
  3. package/.github/workflows/gh-pages-main.yml +39 -39
  4. package/.github/workflows/npm-publish.yml +39 -39
  5. package/.github/workflows/pr-preview.yml +38 -38
  6. package/.husky/pre-commit +4 -0
  7. package/.prettierignore +1 -1
  8. package/LICENSE +661 -661
  9. package/README.md +83 -83
  10. package/dist/src/app-root.js +606 -606
  11. package/dist/src/app-root.js.map +1 -1
  12. package/dist/src/tiles/grid/collection-tile.js +77 -77
  13. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  14. package/dist/src/tiles/grid/item-tile.js +137 -137
  15. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  16. package/dist/src/tiles/hover/hover-pane-controller.d.ts +8 -0
  17. package/dist/src/tiles/hover/hover-pane-controller.js +41 -29
  18. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
  19. package/dist/src/tiles/models.js.map +1 -1
  20. package/dist/src/tiles/tile-dispatcher.js +215 -215
  21. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  22. package/dist/test/collection-browser.test.js +1 -1
  23. package/dist/test/collection-browser.test.js.map +1 -1
  24. package/eslint.config.mjs +53 -53
  25. package/index.html +24 -24
  26. package/local.archive.org.cert +86 -86
  27. package/local.archive.org.key +27 -27
  28. package/package.json +119 -121
  29. package/renovate.json +6 -6
  30. package/src/app-root.ts +1140 -1140
  31. package/src/tiles/grid/collection-tile.ts +163 -163
  32. package/src/tiles/grid/item-tile.ts +340 -340
  33. package/src/tiles/hover/hover-pane-controller.ts +627 -613
  34. package/src/tiles/models.ts +1 -1
  35. package/src/tiles/tile-dispatcher.ts +517 -517
  36. package/test/collection-browser.test.ts +1 -1
  37. package/tsconfig.json +25 -25
  38. package/web-dev-server.config.mjs +30 -30
  39. package/web-test-runner.config.mjs +41 -52
@@ -1,613 +1,627 @@
1
- import type { SortParam } from '@internetarchive/search-service';
2
- import {
3
- html,
4
- HTMLTemplateResult,
5
- nothing,
6
- ReactiveController,
7
- ReactiveControllerHost,
8
- } from 'lit';
9
- import type { TileModel } from '../../models';
10
- import type { CollectionTitles } from '../../data-source/models';
11
- import { msg } from '@lit/localize';
12
-
13
- type HoverPaneState = 'hidden' | 'shown' | 'fading-out';
14
-
15
- // the anchor point of the hover pane
16
- // can be either the mouse cursor or near the host element
17
- // in the case of mouse navigation, we want it to follow the cursor
18
- // in the case of keyboard navigation, we want it to appear near the host element
19
- type HoverPanePositionAnchor = 'host' | 'cursor';
20
-
21
- export interface HoverPaneProperties {
22
- model?: TileModel;
23
- baseNavigationUrl?: string;
24
- baseImageUrl?: string;
25
- loggedIn: boolean;
26
- suppressBlurring: boolean;
27
- sortParam: SortParam | null;
28
- collectionTitles?: CollectionTitles;
29
- }
30
-
31
- export interface HoverPaneControllerOptions {
32
- offsetX?: number;
33
- offsetY?: number;
34
- enableLongPress?: boolean;
35
- showDelay?: number;
36
- hideDelay?: number;
37
- longPressDelay?: number;
38
- mobileBreakpoint?: number;
39
- }
40
-
41
- /** A common interface for providing a hover pane element. */
42
- export interface HoverPaneProviderInterface {
43
- /** Returns the provider's currently rendered hover pane element. */
44
- getHoverPane(): HTMLElement | undefined;
45
- /** Returns properties that should be passed to the hover pane. */
46
- getHoverPaneProps(): HoverPaneProperties;
47
- /** When user has keyboard navigated out of more info, we want the host to get focus */
48
- acquireFocus(): void;
49
- /** When user has keyboard navigated out of more info, we want the host to lose focus */
50
- releaseFocus(): void;
51
- }
52
-
53
- export interface ToggleHoverPaneOptions {
54
- coords: { x: number; y: number };
55
- enableTouchBackdrop?: boolean;
56
- }
57
-
58
- /**
59
- * An interface for interacting with hover pane controllers (e.g.,
60
- * to retrieve their current hover pane template).
61
- */
62
- export interface HoverPaneControllerInterface extends ReactiveController {
63
- /**
64
- * Returns the hover pane template to render based on this controller's
65
- * current state. The returned template may be `nothing` if the hover
66
- * pane should not currently be rendered.
67
- */
68
- getTemplate(): HTMLTemplateResult | typeof nothing;
69
-
70
- /**
71
- * Requests to manually toggle the state of the hover pane.
72
- * If the hover pane is already shown, it will begin fading out and then
73
- * subsequently be hidden and removed. If the hover pane is already fading
74
- * out or hidden, it will fade back in and be shown.
75
- */
76
- toggleHoverPane(options: ToggleHoverPaneOptions): void;
77
- }
78
-
79
- const clamp = (val: number, min = -Infinity, max = Infinity) =>
80
- Math.max(min, Math.min(val, max));
81
-
82
- export class HoverPaneController implements HoverPaneControllerInterface {
83
- /**
84
- * The hover pane element attached to this controller's host.
85
- */
86
- private hoverPane?: HTMLElement;
87
-
88
- /**
89
- * The properties to be passed to the hover pane element
90
- */
91
- private hoverPaneProps?: HoverPaneProperties;
92
-
93
- /**
94
- * The breakpoint (in pixels) below which the mobile interface should be used.
95
- */
96
- private mobileBreakpoint?: number = 600;
97
-
98
- /**
99
- * The number of horizontal pixels the hover pane should be offset from the
100
- * pointer position.
101
- */
102
- private offsetX: number = -10;
103
-
104
- /**
105
- * The number of vertical pixels the hover pane should be offset from the
106
- * pointer position.
107
- */
108
- private offsetY: number = 15;
109
-
110
- /**
111
- * The delay between the mouse idling within the host element and when the hover
112
- * pane should begin fading in (in milliseconds).
113
- */
114
- private showDelay: number = 300;
115
-
116
- /**
117
- * The delay between when the mouse leaves the host element and when the hover
118
- * pane should begin fading out (in milliseconds).
119
- */
120
- private hideDelay: number = 100;
121
-
122
- /**
123
- * The delay between when a touch event begins on the host element and when the
124
- * hover pane should begin fading in (in milliseconds).
125
- */
126
- private longPressDelay: number = 600;
127
-
128
- /**
129
- * Whether long press interactions should cause the hover pane to appear (when
130
- * below the mobile breakpoint).
131
- */
132
- private enableLongPress: boolean = false;
133
-
134
- /**
135
- * Used to control the current state of this provider's hover pane.
136
- * - `'hidden'` => The hover pane is not present at all.
137
- * - `'shown'` => The hover pane is either fading in or fully visible.
138
- * - `'fading-out'` => The hover pane is fading out and about to be removed.
139
- */
140
- private hoverPaneState: HoverPaneState = 'hidden';
141
-
142
- /** The timer ID for showing the hover pane */
143
- private showTimer?: number;
144
-
145
- /** The timer ID for hiding the hover pane */
146
- private hideTimer?: number;
147
-
148
- /** The timer ID for recognizing a long press event */
149
- private longPressTimer?: number;
150
-
151
- /**
152
- * Whether the touch backdrop should currently be rendered irrespective of other touch
153
- * interactions being enabled.
154
- */
155
- private forceTouchBackdrop: boolean = false;
156
-
157
- /** A record of the last mouse position on the host element, for positioning the hover pane */
158
- private lastPointerClientPos = { x: 0, y: 0 };
159
-
160
- constructor(
161
- /** The host element to which this controller should attach listeners */
162
- private readonly host: ReactiveControllerHost &
163
- HoverPaneProviderInterface &
164
- HTMLElement,
165
- /** Options for adjusting the hover pane behavior (offsets, delays, etc.) */
166
- options: HoverPaneControllerOptions = {},
167
- ) {
168
- this.mobileBreakpoint = options.mobileBreakpoint ?? this.mobileBreakpoint;
169
- this.offsetX = options.offsetX ?? this.offsetX;
170
- this.offsetY = options.offsetY ?? this.offsetY;
171
- this.showDelay = options.showDelay ?? this.showDelay;
172
- this.hideDelay = options.hideDelay ?? this.hideDelay;
173
- this.longPressDelay = options.longPressDelay ?? this.longPressDelay;
174
- this.enableLongPress = options.enableLongPress ?? this.enableLongPress;
175
-
176
- this.host.addController(this);
177
- }
178
-
179
- hostConnected(): void {
180
- this.attachListeners();
181
- }
182
-
183
- hostDisconnected(): void {
184
- this.detachListeners();
185
- }
186
-
187
- hostUpdated(): void {
188
- this.hoverPane = this.host.getHoverPane();
189
- this.hoverPaneProps = this.host.getHoverPaneProps();
190
- }
191
-
192
- /** @inheritdoc */
193
- getTemplate(): HTMLTemplateResult | typeof nothing {
194
- this.hoverPaneProps = this.host.getHoverPaneProps();
195
-
196
- return this.shouldRenderHoverPane
197
- ? html`
198
- ${this.touchBackdropTemplate}
199
- <tile-hover-pane
200
- popover
201
- tabindex="-1"
202
- aria-describedby="tile-hover-pane-aria-description"
203
- .model=${this.hoverPaneProps?.model}
204
- .baseNavigationUrl=${this.hoverPaneProps?.baseNavigationUrl}
205
- .baseImageUrl=${this.hoverPaneProps?.baseImageUrl}
206
- .loggedIn=${this.hoverPaneProps?.loggedIn}
207
- .suppressBlurring=${this.hoverPaneProps?.suppressBlurring}
208
- .sortParam=${this.hoverPaneProps?.sortParam}
209
- .collectionTitles=${this.hoverPaneProps?.collectionTitles}
210
- .mobileBreakpoint=${this.mobileBreakpoint}
211
- .currentWidth=${window.innerWidth}
212
- ></tile-hover-pane>
213
- <div id="tile-hover-pane-aria-description" class="sr-only">
214
- ${msg('Press Up Arrow to exit item detail preview')}
215
- </div>
216
- `
217
- : nothing;
218
- }
219
-
220
- /** @inheritdoc */
221
- toggleHoverPane(options: ToggleHoverPaneOptions): void {
222
- if (this.hoverPaneState === 'shown') {
223
- this.fadeOutHoverPane();
224
- this.forceTouchBackdrop = false;
225
- } else {
226
- this.lastPointerClientPos = options.coords;
227
- this.forceTouchBackdrop = options.enableTouchBackdrop ?? false;
228
- this.showHoverPane();
229
- }
230
- }
231
-
232
- /**
233
- * Produces a template for the invisible touch capture backdrop that
234
- * is used to cancel the hover pane on touch devices. We want any
235
- * touch interaction on the backdrop to remove the hover pane, and
236
- * we don't want to bubble up mouse events that would otherwise
237
- * affect the state of the hover pane (e.g., fading it back in).
238
- */
239
- private get touchBackdropTemplate(): HTMLTemplateResult | typeof nothing {
240
- return this.showTouchBackdrop
241
- ? html`<div
242
- id="touch-backdrop"
243
- @touchstart=${this.handleBackdropInteraction}
244
- @touchmove=${this.handleBackdropInteraction}
245
- @touchend=${this.handleBackdropInteraction}
246
- @touchcancel=${this.handleBackdropInteraction}
247
- @mouseenter=${(e: MouseEvent) => e.stopPropagation()}
248
- @mousemove=${(e: MouseEvent) => e.stopPropagation()}
249
- @mouseleave=${(e: MouseEvent) => e.stopPropagation()}
250
- ></div>`
251
- : nothing;
252
- }
253
-
254
- private get showTouchBackdrop(): boolean {
255
- return (
256
- (this.isTouchEnabled && this.enableLongPress) || this.forceTouchBackdrop
257
- );
258
- }
259
-
260
- /** Whether to use the mobile layout */
261
- private get isMobileView(): boolean {
262
- return !!this.mobileBreakpoint && window.innerWidth < this.mobileBreakpoint;
263
- }
264
-
265
- private get isHoverEnabled(): boolean {
266
- return window.matchMedia('(hover: hover)').matches;
267
- }
268
-
269
- private get isTouchEnabled(): boolean {
270
- return (
271
- 'ontouchstart' in window &&
272
- window.matchMedia('(any-pointer: coarse)').matches
273
- );
274
- }
275
-
276
- /** Whether this controller should currently render its hover pane. */
277
- private get shouldRenderHoverPane(): boolean {
278
- return this.hoverPaneState !== 'hidden';
279
- }
280
-
281
- /**
282
- * Returns the desired top/left offsets (in pixels) for this tile's hover pane.
283
- * The desired offsets balance positioning the hover pane under the primary pointer
284
- * while preventing it from flowing outside the viewport. The returned offsets are
285
- * relative to the viewport, intended to position the pane as a popover element.
286
- *
287
- * These offsets are only valid if the hover pane is already rendered with its
288
- * correct width and height. If the hover pane is not present, the returned offsets
289
- * will simply represent the current pointer position.
290
- */
291
- private makePaneDesiredOffsets(anchor: HoverPanePositionAnchor): {
292
- top: number;
293
- left: number;
294
- } {
295
- // Try to find offsets for the hover pane that:
296
- // (a) cause it to lie entirely within the viewport, and
297
- // (b) to the extent possible, minimize the distance between the
298
- // nearest corner of the hover pane and the mouse/host element position
299
- // (with some additional offsets applied after the fact).
300
-
301
- let [left, top] = [0, 0];
302
- switch (anchor) {
303
- case 'host':
304
- const hostRect = this.host.getBoundingClientRect();
305
- // slight inset from host top left corner
306
- left = hostRect.left + 20;
307
- top = hostRect.top + 30;
308
- break;
309
- case 'cursor':
310
- left = this.lastPointerClientPos.x;
311
- top = this.lastPointerClientPos.y;
312
- break;
313
- }
314
-
315
- // Flip the hover pane according to which quadrant of the viewport the coordinates are in.
316
- // (Similar to how Wikipedia's link hover panes work)
317
- const flipHorizontal = left > window.innerWidth / 2;
318
- const flipVertical = top > window.innerHeight / 2;
319
-
320
- const hoverPaneRect = this.hoverPane?.getBoundingClientRect();
321
- if (hoverPaneRect) {
322
- // If we need to flip the hover pane, do so by subtracting its width/height from left/top
323
- if (flipHorizontal) {
324
- left -= hoverPaneRect.width;
325
- }
326
- if (flipVertical) {
327
- top -= hoverPaneRect.height;
328
- }
329
-
330
- // Apply desired offsets from the target position
331
- left += (flipHorizontal ? -1 : 1) * this.offsetX;
332
- top += (flipVertical ? -1 : 1) * this.offsetY;
333
-
334
- // On mobile view, shunt the hover pane to avoid overflowing the viewport
335
- if (this.isMobileView) {
336
- left = clamp(left, 20, window.innerWidth - hoverPaneRect.width - 20);
337
- top = clamp(top, 20, window.innerHeight - hoverPaneRect.height - 20);
338
- }
339
- }
340
-
341
- left += window.scrollX;
342
- top += window.scrollY;
343
-
344
- return { left, top };
345
- }
346
-
347
- /**
348
- * Adds to the host element all the listeners necessary to make the
349
- * hover pane functional.
350
- */
351
- private attachListeners(): void {
352
- // keyboard navigation listeners
353
- this.host.addEventListener('focus', this.handleFocus);
354
- this.host.addEventListener('blur', this.handleBlur);
355
- this.host.addEventListener('keyup', this.handleKeyUp);
356
- this.host.addEventListener('keydown', this.handleKeyDown);
357
-
358
- if (this.isHoverEnabled) {
359
- this.host.addEventListener('mouseenter', this.handleMouseEnter);
360
- this.host.addEventListener('mousemove', this.handleMouseMove);
361
- this.host.addEventListener('mouseleave', this.handleMouseLeave);
362
- }
363
-
364
- if (this.isTouchEnabled && this.enableLongPress) {
365
- this.host.addEventListener('touchstart', this.handleTouchStart);
366
- this.host.addEventListener('touchmove', this.handleLongPressCancel);
367
- this.host.addEventListener('touchend', this.handleLongPressCancel);
368
- this.host.addEventListener('touchcancel', this.handleLongPressCancel);
369
- this.host.addEventListener('contextmenu', this.handleContextMenu);
370
- }
371
- }
372
-
373
- /**
374
- * Removes all the hover pane listeners from the host element.
375
- */
376
- private detachListeners(): void {
377
- this.host.removeEventListener('mouseenter', this.handleMouseEnter);
378
- this.host.removeEventListener('mousemove', this.handleMouseMove);
379
- this.host.removeEventListener('mouseleave', this.handleMouseLeave);
380
- this.host.removeEventListener('touchstart', this.handleTouchStart);
381
- this.host.removeEventListener('touchmove', this.handleLongPressCancel);
382
- this.host.removeEventListener('touchend', this.handleLongPressCancel);
383
- this.host.removeEventListener('touchcancel', this.handleLongPressCancel);
384
- this.host.removeEventListener('contextmenu', this.handleContextMenu);
385
-
386
- // keyboard navigation listeners
387
- this.host.removeEventListener('focus', this.handleFocus);
388
- this.host.removeEventListener('blur', this.handleBlur);
389
- this.host.removeEventListener('keyup', this.handleKeyUp);
390
- this.host.removeEventListener('keydown', this.handleKeyDown);
391
- }
392
-
393
- private handleFocus = (): void => {
394
- if (this.hoverPaneState === 'hidden') {
395
- this.showHoverPane({
396
- anchor: 'host',
397
- });
398
- }
399
- };
400
-
401
- private handleBlur = (): void => {
402
- if (this.hoverPaneState !== 'hidden') {
403
- this.fadeOutHoverPane();
404
- }
405
- };
406
-
407
- private handleKeyDown = (e: KeyboardEvent): void => {
408
- if (
409
- (e.key === 'ArrowDown' || e.key === 'ArrowUp') &&
410
- this.hoverPaneState !== 'hidden'
411
- ) {
412
- e.preventDefault();
413
- }
414
- };
415
-
416
- private handleKeyUp = (e: KeyboardEvent): void => {
417
- if (this.hoverPaneState === 'hidden' || !this.hoverPane) return;
418
- if (e.key === 'ArrowDown') {
419
- this.hoverPane.tabIndex = 1;
420
- this.hoverPane.focus();
421
- }
422
-
423
- const isArrowUp = e.key === 'ArrowUp';
424
- const isEscape = e.key === 'Escape' || e.key === 'Esc';
425
-
426
- if (isEscape) {
427
- this.fadeOutHoverPane();
428
- }
429
- if (isArrowUp || isEscape) {
430
- this.hoverPane.tabIndex = -1;
431
- this.host.acquireFocus();
432
- }
433
- };
434
-
435
- /**
436
- * Handler for the mouseenter event on the host element.
437
- */
438
- // NB: Arrow function so 'this' remains bound to the controller
439
- private handleMouseEnter = (e: MouseEvent): void => {
440
- // Delegate to the mousemove handler, as they are currently processed identically
441
- this.handleMouseMove(e);
442
- };
443
-
444
- /**
445
- * Handler for the mousemove event on the host element.
446
- * Aborts any pending hide/fade-out for the hover pane, and restarts the
447
- * timer to show it.
448
- */
449
- // NB: Arrow function so 'this' remains bound to the controller
450
- private handleMouseMove = (e: MouseEvent): void => {
451
- // The mouse is within the tile, so abort any pending removal of the hover pane
452
- clearTimeout(this.hideTimer);
453
-
454
- // If the hover pane is currently fading out, just make it fade back in where it is
455
- if (this.hoverPaneState === 'fading-out') {
456
- this.hoverPaneState = 'shown';
457
- this.hoverPane?.classList.add('fade-in');
458
- }
459
-
460
- // Restart the timer to show the hover pane anytime the mouse moves within the tile
461
- if (this.hoverPaneState === 'hidden') {
462
- this.restartShowHoverPaneTimer();
463
- this.lastPointerClientPos = { x: e.clientX, y: e.clientY };
464
- }
465
- };
466
-
467
- /**
468
- * Handler for the mouseleave event on the host element.
469
- * Hides the hover pane if present, and aborts the timer for showing it.
470
- */
471
- // NB: Arrow function so 'this' remains bound to the controller
472
- private handleMouseLeave = (): void => {
473
- this.host.releaseFocus();
474
-
475
- // Abort any timer to show the hover pane, as the mouse has left the tile
476
- clearTimeout(this.showTimer);
477
-
478
- // Hide the hover pane if it's already been shown
479
- if (this.hoverPaneState !== 'hidden') {
480
- this.hideTimer = window.setTimeout(() => {
481
- this.fadeOutHoverPane();
482
- }, this.hideDelay);
483
- }
484
- };
485
-
486
- /**
487
- * Handler for the touchstart event on the host element.
488
- * Begins the timer for recognizing a long press event.
489
- */
490
- // NB: Arrow function so 'this' remains bound to the controller
491
- private handleTouchStart = (e: TouchEvent): void => {
492
- clearTimeout(this.longPressTimer);
493
-
494
- if (e.touches.length === 1) {
495
- this.longPressTimer = window.setTimeout(() => {
496
- if (this.hoverPaneState === 'hidden') {
497
- this.showHoverPane();
498
- }
499
- }, this.longPressDelay);
500
-
501
- this.lastPointerClientPos = {
502
- x: e.touches[0].clientX,
503
- y: e.touches[0].clientY,
504
- };
505
- }
506
- };
507
-
508
- /**
509
- * Handler for events that should cancel a pending long press event
510
- * (touchmove, touchend, touchcancel). Aborts the timer for recognizing
511
- * a long press.
512
- */
513
- // NB: Arrow function so 'this' remains bound to the controller
514
- private handleLongPressCancel = (): void => {
515
- clearTimeout(this.longPressTimer);
516
- };
517
-
518
- /**
519
- * Handler for the contextmenu event, which should be suppressed during
520
- * mobile long-press events on the host element.
521
- */
522
- // NB: Arrow function so 'this' remains bound to the controller
523
- private handleContextMenu = (e: Event): void => {
524
- e.preventDefault();
525
- };
526
-
527
- /**
528
- * Immediately causes the hover pane to begin fading out, if it is present.
529
- */
530
- // NB: Arrow function so 'this' remains bound to the controller
531
- private handleBackdropInteraction = (e: Event): void => {
532
- if (this.hoverPaneState !== 'hidden') {
533
- this.fadeOutHoverPane();
534
- }
535
- e.stopPropagation();
536
- };
537
-
538
- /**
539
- * Aborts and restarts the timer for showing the hover pane.
540
- */
541
- private restartShowHoverPaneTimer(): void {
542
- clearTimeout(this.showTimer);
543
- this.showTimer = window.setTimeout(() => {
544
- this.host.acquireFocus();
545
- this.showHoverPane();
546
- }, this.showDelay);
547
- }
548
-
549
- /**
550
- * Causes this tile's hover pane to be rendered, positioned, and made visible.
551
- */
552
- private async showHoverPane(
553
- options: {
554
- anchor: HoverPanePositionAnchor;
555
- } = {
556
- anchor: 'cursor',
557
- },
558
- ): Promise<void> {
559
- this.hoverPaneState = 'shown';
560
- this.host.requestUpdate();
561
-
562
- // Wait for the state update to render the hover pane
563
- await this.host.updateComplete;
564
-
565
- // Ensure the hover pane element is still in the document before showing,
566
- // as it might have been removed by the previous update.
567
- if (!this.hoverPane?.isConnected) return;
568
-
569
- this.hoverPane?.showPopover?.();
570
- await new Promise(resolve => {
571
- // Pane sizes aren't accurate until next frame
572
- requestAnimationFrame(resolve);
573
- });
574
-
575
- // Apply the correct positioning to the hover pane
576
- this.repositionHoverPane(options.anchor);
577
-
578
- // The hover pane is initially not visible (to avoid it shifting around
579
- // while being positioned). Since it now has the correct positioning, we
580
- // can make it visible and begin its fade-in animation.
581
- this.hoverPane?.classList.add('visible', 'fade-in');
582
- }
583
-
584
- /**
585
- * Causes this tile's hover pane to begin fading out and starts
586
- * the timer for it to be removed.
587
- */
588
- private fadeOutHoverPane(): void {
589
- this.hoverPaneState = 'fading-out';
590
- this.hoverPane?.classList.remove('fade-in');
591
-
592
- clearTimeout(this.hideTimer);
593
- this.hideTimer = window.setTimeout(() => {
594
- this.hoverPaneState = 'hidden';
595
- if (this.hoverPane) {
596
- this.hoverPane.tabIndex = -1;
597
- }
598
- this.host.requestUpdate();
599
- }, 100);
600
- }
601
-
602
- /**
603
- * Positions the hover pane with the correct offsets.
604
- */
605
- private repositionHoverPane(anchor: HoverPanePositionAnchor): void {
606
- if (!this.hoverPane) return;
607
-
608
- const { top, left } = this.makePaneDesiredOffsets(anchor);
609
-
610
- this.hoverPane.style.top = `${top}px`;
611
- this.hoverPane.style.left = `${left}px`;
612
- }
613
- }
1
+ import type { SortParam } from '@internetarchive/search-service';
2
+ import {
3
+ html,
4
+ HTMLTemplateResult,
5
+ nothing,
6
+ ReactiveController,
7
+ ReactiveControllerHost,
8
+ } from 'lit';
9
+ import type { TileModel } from '../../models';
10
+ import type { CollectionTitles } from '../../data-source/models';
11
+ import { msg } from '@lit/localize';
12
+
13
+ type HoverPaneState = 'hidden' | 'shown' | 'fading-out';
14
+
15
+ // the anchor point of the hover pane
16
+ // can be either the mouse cursor or near the host element
17
+ // in the case of mouse navigation, we want it to follow the cursor
18
+ // in the case of keyboard navigation, we want it to appear near the host element
19
+ type HoverPanePositionAnchor = 'host' | 'cursor';
20
+
21
+ export interface HoverPaneProperties {
22
+ model?: TileModel;
23
+ baseNavigationUrl?: string;
24
+ baseImageUrl?: string;
25
+ loggedIn: boolean;
26
+ suppressBlurring: boolean;
27
+ sortParam: SortParam | null;
28
+ collectionTitles?: CollectionTitles;
29
+ }
30
+
31
+ export interface HoverPaneControllerOptions {
32
+ offsetX?: number;
33
+ offsetY?: number;
34
+ enableLongPress?: boolean;
35
+ showDelay?: number;
36
+ hideDelay?: number;
37
+ longPressDelay?: number;
38
+ mobileBreakpoint?: number;
39
+ }
40
+
41
+ /** A common interface for providing a hover pane element. */
42
+ export interface HoverPaneProviderInterface {
43
+ /** Returns the provider's currently rendered hover pane element. */
44
+ getHoverPane(): HTMLElement | undefined;
45
+ /** Returns properties that should be passed to the hover pane. */
46
+ getHoverPaneProps(): HoverPaneProperties;
47
+ /** When user has keyboard navigated out of more info, we want the host to get focus */
48
+ acquireFocus(): void;
49
+ /** When user has keyboard navigated out of more info, we want the host to lose focus */
50
+ releaseFocus(): void;
51
+ }
52
+
53
+ export interface ToggleHoverPaneOptions {
54
+ coords: { x: number; y: number };
55
+ enableTouchBackdrop?: boolean;
56
+ }
57
+
58
+ /**
59
+ * An interface for interacting with hover pane controllers (e.g.,
60
+ * to retrieve their current hover pane template).
61
+ */
62
+ export interface HoverPaneControllerInterface extends ReactiveController {
63
+ /**
64
+ * Returns the hover pane template to render based on this controller's
65
+ * current state. The returned template may be `nothing` if the hover
66
+ * pane should not currently be rendered.
67
+ */
68
+ getTemplate(): HTMLTemplateResult | typeof nothing;
69
+
70
+ /**
71
+ * Requests to manually toggle the state of the hover pane.
72
+ * If the hover pane is already shown, it will begin fading out and then
73
+ * subsequently be hidden and removed. If the hover pane is already fading
74
+ * out or hidden, it will fade back in and be shown.
75
+ */
76
+ toggleHoverPane(options: ToggleHoverPaneOptions): void;
77
+ }
78
+
79
+ const clamp = (val: number, min = -Infinity, max = Infinity) =>
80
+ Math.max(min, Math.min(val, max));
81
+
82
+ export class HoverPaneController implements HoverPaneControllerInterface {
83
+ /**
84
+ * The hover pane element attached to this controller's host.
85
+ */
86
+ private hoverPane?: HTMLElement;
87
+
88
+ /**
89
+ * The properties to be passed to the hover pane element
90
+ */
91
+ private hoverPaneProps?: HoverPaneProperties;
92
+
93
+ /**
94
+ * The breakpoint (in pixels) below which the mobile interface should be used.
95
+ */
96
+ private mobileBreakpoint?: number = 600;
97
+
98
+ /**
99
+ * The number of horizontal pixels the hover pane should be offset from the
100
+ * pointer position.
101
+ */
102
+ private offsetX: number = -10;
103
+
104
+ /**
105
+ * The number of vertical pixels the hover pane should be offset from the
106
+ * pointer position.
107
+ */
108
+ private offsetY: number = 15;
109
+
110
+ /**
111
+ * The delay between the mouse idling within the host element and when the hover
112
+ * pane should begin fading in (in milliseconds).
113
+ */
114
+ private showDelay: number = 300;
115
+
116
+ /**
117
+ * The delay between when the mouse leaves the host element and when the hover
118
+ * pane should begin fading out (in milliseconds).
119
+ */
120
+ private hideDelay: number = 100;
121
+
122
+ /**
123
+ * The delay between when a touch event begins on the host element and when the
124
+ * hover pane should begin fading in (in milliseconds).
125
+ */
126
+ private longPressDelay: number = 600;
127
+
128
+ /**
129
+ * Whether long press interactions should cause the hover pane to appear (when
130
+ * below the mobile breakpoint).
131
+ */
132
+ private enableLongPress: boolean = false;
133
+
134
+ /**
135
+ * Used to control the current state of this provider's hover pane.
136
+ * - `'hidden'` => The hover pane is not present at all.
137
+ * - `'shown'` => The hover pane is either fading in or fully visible.
138
+ * - `'fading-out'` => The hover pane is fading out and about to be removed.
139
+ */
140
+ private hoverPaneState: HoverPaneState = 'hidden';
141
+
142
+ /** The timer ID for showing the hover pane */
143
+ private showTimer?: number;
144
+
145
+ /** The timer ID for hiding the hover pane */
146
+ private hideTimer?: number;
147
+
148
+ /** The timer ID for recognizing a long press event */
149
+ private longPressTimer?: number;
150
+
151
+ /**
152
+ * Whether the touch backdrop should currently be rendered irrespective of other touch
153
+ * interactions being enabled.
154
+ */
155
+ private forceTouchBackdrop: boolean = false;
156
+
157
+ /** A record of the last mouse position on the host element, for positioning the hover pane */
158
+ private lastPointerClientPos = { x: 0, y: 0 };
159
+
160
+ /**
161
+ * A flag to track whether the host element is being clicked by a pointer device, so that we
162
+ * don't trigger unnecessary keyboard focus behaviors on click. This is needed, e.g., to prevent
163
+ * the hover pane from appearing immediately at its `host` positioning on click, which can
164
+ * obstruct the host element itself (due to the ordering of events fired).
165
+ */
166
+ private clicking = false;
167
+
168
+ constructor(
169
+ /** The host element to which this controller should attach listeners */
170
+ private readonly host: ReactiveControllerHost &
171
+ HoverPaneProviderInterface &
172
+ HTMLElement,
173
+ /** Options for adjusting the hover pane behavior (offsets, delays, etc.) */
174
+ options: HoverPaneControllerOptions = {},
175
+ ) {
176
+ this.mobileBreakpoint = options.mobileBreakpoint ?? this.mobileBreakpoint;
177
+ this.offsetX = options.offsetX ?? this.offsetX;
178
+ this.offsetY = options.offsetY ?? this.offsetY;
179
+ this.showDelay = options.showDelay ?? this.showDelay;
180
+ this.hideDelay = options.hideDelay ?? this.hideDelay;
181
+ this.longPressDelay = options.longPressDelay ?? this.longPressDelay;
182
+ this.enableLongPress = options.enableLongPress ?? this.enableLongPress;
183
+
184
+ this.host.addController(this);
185
+ }
186
+
187
+ hostConnected(): void {
188
+ this.attachListeners();
189
+ }
190
+
191
+ hostDisconnected(): void {
192
+ this.detachListeners();
193
+ }
194
+
195
+ hostUpdated(): void {
196
+ this.hoverPane = this.host.getHoverPane();
197
+ this.hoverPaneProps = this.host.getHoverPaneProps();
198
+ }
199
+
200
+ /** @inheritdoc */
201
+ getTemplate(): HTMLTemplateResult | typeof nothing {
202
+ this.hoverPaneProps = this.host.getHoverPaneProps();
203
+
204
+ return this.shouldRenderHoverPane
205
+ ? html`
206
+ ${this.touchBackdropTemplate}
207
+ <tile-hover-pane
208
+ popover
209
+ tabindex="-1"
210
+ aria-describedby="tile-hover-pane-aria-description"
211
+ .model=${this.hoverPaneProps?.model}
212
+ .baseNavigationUrl=${this.hoverPaneProps?.baseNavigationUrl}
213
+ .baseImageUrl=${this.hoverPaneProps?.baseImageUrl}
214
+ .loggedIn=${this.hoverPaneProps?.loggedIn}
215
+ .suppressBlurring=${this.hoverPaneProps?.suppressBlurring}
216
+ .sortParam=${this.hoverPaneProps?.sortParam}
217
+ .collectionTitles=${this.hoverPaneProps?.collectionTitles}
218
+ .mobileBreakpoint=${this.mobileBreakpoint}
219
+ .currentWidth=${window.innerWidth}
220
+ ></tile-hover-pane>
221
+ <div id="tile-hover-pane-aria-description" class="sr-only">
222
+ ${msg('Press Up Arrow to exit item detail preview')}
223
+ </div>
224
+ `
225
+ : nothing;
226
+ }
227
+
228
+ /** @inheritdoc */
229
+ toggleHoverPane(options: ToggleHoverPaneOptions): void {
230
+ if (this.hoverPaneState === 'shown') {
231
+ this.fadeOutHoverPane();
232
+ this.forceTouchBackdrop = false;
233
+ } else {
234
+ this.lastPointerClientPos = options.coords;
235
+ this.forceTouchBackdrop = options.enableTouchBackdrop ?? false;
236
+ this.showHoverPane();
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Produces a template for the invisible touch capture backdrop that
242
+ * is used to cancel the hover pane on touch devices. We want any
243
+ * touch interaction on the backdrop to remove the hover pane, and
244
+ * we don't want to bubble up mouse events that would otherwise
245
+ * affect the state of the hover pane (e.g., fading it back in).
246
+ */
247
+ private get touchBackdropTemplate(): HTMLTemplateResult | typeof nothing {
248
+ return this.showTouchBackdrop
249
+ ? html`<div
250
+ id="touch-backdrop"
251
+ @touchstart=${this.handleBackdropInteraction}
252
+ @touchmove=${this.handleBackdropInteraction}
253
+ @touchend=${this.handleBackdropInteraction}
254
+ @touchcancel=${this.handleBackdropInteraction}
255
+ @mouseenter=${(e: MouseEvent) => e.stopPropagation()}
256
+ @mousemove=${(e: MouseEvent) => e.stopPropagation()}
257
+ @mouseleave=${(e: MouseEvent) => e.stopPropagation()}
258
+ ></div>`
259
+ : nothing;
260
+ }
261
+
262
+ private get showTouchBackdrop(): boolean {
263
+ return (
264
+ (this.isTouchEnabled && this.enableLongPress) || this.forceTouchBackdrop
265
+ );
266
+ }
267
+
268
+ /** Whether to use the mobile layout */
269
+ private get isMobileView(): boolean {
270
+ return !!this.mobileBreakpoint && window.innerWidth < this.mobileBreakpoint;
271
+ }
272
+
273
+ private get isHoverEnabled(): boolean {
274
+ return window.matchMedia('(hover: hover)').matches;
275
+ }
276
+
277
+ private get isTouchEnabled(): boolean {
278
+ return (
279
+ 'ontouchstart' in window &&
280
+ window.matchMedia('(any-pointer: coarse)').matches
281
+ );
282
+ }
283
+
284
+ /** Whether this controller should currently render its hover pane. */
285
+ private get shouldRenderHoverPane(): boolean {
286
+ return this.hoverPaneState !== 'hidden';
287
+ }
288
+
289
+ /**
290
+ * Returns the desired top/left offsets (in pixels) for this tile's hover pane.
291
+ * The desired offsets balance positioning the hover pane under the primary pointer
292
+ * while preventing it from flowing outside the viewport. The returned offsets are
293
+ * relative to the viewport, intended to position the pane as a popover element.
294
+ *
295
+ * These offsets are only valid if the hover pane is already rendered with its
296
+ * correct width and height. If the hover pane is not present, the returned offsets
297
+ * will simply represent the current pointer position.
298
+ */
299
+ private makePaneDesiredOffsets(anchor: HoverPanePositionAnchor): {
300
+ top: number;
301
+ left: number;
302
+ } {
303
+ // Try to find offsets for the hover pane that:
304
+ // (a) cause it to lie entirely within the viewport, and
305
+ // (b) to the extent possible, minimize the distance between the
306
+ // nearest corner of the hover pane and the mouse/host element position
307
+ // (with some additional offsets applied after the fact).
308
+
309
+ let [left, top] = [0, 0];
310
+ switch (anchor) {
311
+ case 'host':
312
+ const hostRect = this.host.getBoundingClientRect();
313
+ // slight inset from host top left corner
314
+ left = hostRect.left + 20;
315
+ top = hostRect.top + 30;
316
+ break;
317
+ case 'cursor':
318
+ left = this.lastPointerClientPos.x;
319
+ top = this.lastPointerClientPos.y;
320
+ break;
321
+ }
322
+
323
+ // Flip the hover pane according to which quadrant of the viewport the coordinates are in.
324
+ // (Similar to how Wikipedia's link hover panes work)
325
+ const flipHorizontal = left > window.innerWidth / 2;
326
+ const flipVertical = top > window.innerHeight / 2;
327
+
328
+ const hoverPaneRect = this.hoverPane?.getBoundingClientRect();
329
+ if (hoverPaneRect) {
330
+ // If we need to flip the hover pane, do so by subtracting its width/height from left/top
331
+ if (flipHorizontal) {
332
+ left -= hoverPaneRect.width;
333
+ }
334
+ if (flipVertical) {
335
+ top -= hoverPaneRect.height;
336
+ }
337
+
338
+ // Apply desired offsets from the target position
339
+ left += (flipHorizontal ? -1 : 1) * this.offsetX;
340
+ top += (flipVertical ? -1 : 1) * this.offsetY;
341
+
342
+ // On mobile view, shunt the hover pane to avoid overflowing the viewport
343
+ if (this.isMobileView) {
344
+ left = clamp(left, 20, window.innerWidth - hoverPaneRect.width - 20);
345
+ top = clamp(top, 20, window.innerHeight - hoverPaneRect.height - 20);
346
+ }
347
+ }
348
+
349
+ left += window.scrollX;
350
+ top += window.scrollY;
351
+
352
+ return { left, top };
353
+ }
354
+
355
+ /**
356
+ * Adds to the host element all the listeners necessary to make the
357
+ * hover pane functional.
358
+ */
359
+ private attachListeners(): void {
360
+ // keyboard navigation listeners
361
+ this.host.addEventListener('focus', this.handleFocus);
362
+ this.host.addEventListener('blur', this.handleBlur);
363
+ this.host.addEventListener('pointerdown', this.handlePointerDown);
364
+ this.host.addEventListener('keyup', this.handleKeyUp);
365
+ this.host.addEventListener('keydown', this.handleKeyDown);
366
+
367
+ if (this.isHoverEnabled) {
368
+ this.host.addEventListener('mouseenter', this.handleMouseEnter);
369
+ this.host.addEventListener('mousemove', this.handleMouseMove);
370
+ this.host.addEventListener('mouseleave', this.handleMouseLeave);
371
+ }
372
+
373
+ if (this.isTouchEnabled && this.enableLongPress) {
374
+ this.host.addEventListener('touchstart', this.handleTouchStart);
375
+ this.host.addEventListener('touchmove', this.handleLongPressCancel);
376
+ this.host.addEventListener('touchend', this.handleLongPressCancel);
377
+ this.host.addEventListener('touchcancel', this.handleLongPressCancel);
378
+ this.host.addEventListener('contextmenu', this.handleContextMenu);
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Removes all the hover pane listeners from the host element.
384
+ */
385
+ private detachListeners(): void {
386
+ this.host.removeEventListener('mouseenter', this.handleMouseEnter);
387
+ this.host.removeEventListener('mousemove', this.handleMouseMove);
388
+ this.host.removeEventListener('mouseleave', this.handleMouseLeave);
389
+ this.host.removeEventListener('touchstart', this.handleTouchStart);
390
+ this.host.removeEventListener('touchmove', this.handleLongPressCancel);
391
+ this.host.removeEventListener('touchend', this.handleLongPressCancel);
392
+ this.host.removeEventListener('touchcancel', this.handleLongPressCancel);
393
+ this.host.removeEventListener('contextmenu', this.handleContextMenu);
394
+
395
+ // keyboard navigation listeners
396
+ this.host.removeEventListener('focus', this.handleFocus);
397
+ this.host.removeEventListener('blur', this.handleBlur);
398
+ this.host.removeEventListener('keyup', this.handleKeyUp);
399
+ this.host.removeEventListener('keydown', this.handleKeyDown);
400
+ }
401
+
402
+ private handleFocus = (): void => {
403
+ if (!this.clicking && this.hoverPaneState === 'hidden') {
404
+ this.showHoverPane({
405
+ anchor: 'host',
406
+ });
407
+ }
408
+ this.clicking = false;
409
+ };
410
+
411
+ private handleBlur = (): void => {
412
+ if (this.hoverPaneState !== 'hidden') {
413
+ this.fadeOutHoverPane();
414
+ }
415
+ };
416
+
417
+ private handlePointerDown = (): void => {
418
+ this.clicking = true;
419
+ };
420
+
421
+ private handleKeyDown = (e: KeyboardEvent): void => {
422
+ if (
423
+ (e.key === 'ArrowDown' || e.key === 'ArrowUp') &&
424
+ this.hoverPaneState !== 'hidden'
425
+ ) {
426
+ e.preventDefault();
427
+ }
428
+ };
429
+
430
+ private handleKeyUp = (e: KeyboardEvent): void => {
431
+ if (this.hoverPaneState === 'hidden' || !this.hoverPane) return;
432
+ if (e.key === 'ArrowDown') {
433
+ this.hoverPane.tabIndex = 1;
434
+ this.hoverPane.focus();
435
+ }
436
+
437
+ const isArrowUp = e.key === 'ArrowUp';
438
+ const isEscape = e.key === 'Escape' || e.key === 'Esc';
439
+
440
+ if (isEscape) {
441
+ this.fadeOutHoverPane();
442
+ }
443
+ if (isArrowUp || isEscape) {
444
+ this.hoverPane.tabIndex = -1;
445
+ this.host.acquireFocus();
446
+ }
447
+ };
448
+
449
+ /**
450
+ * Handler for the mouseenter event on the host element.
451
+ */
452
+ // NB: Arrow function so 'this' remains bound to the controller
453
+ private handleMouseEnter = (e: MouseEvent): void => {
454
+ // Delegate to the mousemove handler, as they are currently processed identically
455
+ this.handleMouseMove(e);
456
+ };
457
+
458
+ /**
459
+ * Handler for the mousemove event on the host element.
460
+ * Aborts any pending hide/fade-out for the hover pane, and restarts the
461
+ * timer to show it.
462
+ */
463
+ // NB: Arrow function so 'this' remains bound to the controller
464
+ private handleMouseMove = (e: MouseEvent): void => {
465
+ // The mouse is within the tile, so abort any pending removal of the hover pane
466
+ clearTimeout(this.hideTimer);
467
+
468
+ // If the hover pane is currently fading out, just make it fade back in where it is
469
+ if (this.hoverPaneState === 'fading-out') {
470
+ this.hoverPaneState = 'shown';
471
+ this.hoverPane?.classList.add('fade-in');
472
+ }
473
+
474
+ // Restart the timer to show the hover pane anytime the mouse moves within the tile
475
+ if (this.hoverPaneState === 'hidden') {
476
+ this.restartShowHoverPaneTimer();
477
+ this.lastPointerClientPos = { x: e.clientX, y: e.clientY };
478
+ }
479
+ };
480
+
481
+ /**
482
+ * Handler for the mouseleave event on the host element.
483
+ * Hides the hover pane if present, and aborts the timer for showing it.
484
+ */
485
+ // NB: Arrow function so 'this' remains bound to the controller
486
+ private handleMouseLeave = (): void => {
487
+ this.host.releaseFocus();
488
+
489
+ // Abort any timer to show the hover pane, as the mouse has left the tile
490
+ clearTimeout(this.showTimer);
491
+
492
+ // Hide the hover pane if it's already been shown
493
+ if (this.hoverPaneState !== 'hidden') {
494
+ this.hideTimer = window.setTimeout(() => {
495
+ this.fadeOutHoverPane();
496
+ }, this.hideDelay);
497
+ }
498
+ };
499
+
500
+ /**
501
+ * Handler for the touchstart event on the host element.
502
+ * Begins the timer for recognizing a long press event.
503
+ */
504
+ // NB: Arrow function so 'this' remains bound to the controller
505
+ private handleTouchStart = (e: TouchEvent): void => {
506
+ clearTimeout(this.longPressTimer);
507
+
508
+ if (e.touches.length === 1) {
509
+ this.longPressTimer = window.setTimeout(() => {
510
+ if (this.hoverPaneState === 'hidden') {
511
+ this.showHoverPane();
512
+ }
513
+ }, this.longPressDelay);
514
+
515
+ this.lastPointerClientPos = {
516
+ x: e.touches[0].clientX,
517
+ y: e.touches[0].clientY,
518
+ };
519
+ }
520
+ };
521
+
522
+ /**
523
+ * Handler for events that should cancel a pending long press event
524
+ * (touchmove, touchend, touchcancel). Aborts the timer for recognizing
525
+ * a long press.
526
+ */
527
+ // NB: Arrow function so 'this' remains bound to the controller
528
+ private handleLongPressCancel = (): void => {
529
+ clearTimeout(this.longPressTimer);
530
+ };
531
+
532
+ /**
533
+ * Handler for the contextmenu event, which should be suppressed during
534
+ * mobile long-press events on the host element.
535
+ */
536
+ // NB: Arrow function so 'this' remains bound to the controller
537
+ private handleContextMenu = (e: Event): void => {
538
+ e.preventDefault();
539
+ };
540
+
541
+ /**
542
+ * Immediately causes the hover pane to begin fading out, if it is present.
543
+ */
544
+ // NB: Arrow function so 'this' remains bound to the controller
545
+ private handleBackdropInteraction = (e: Event): void => {
546
+ if (this.hoverPaneState !== 'hidden') {
547
+ this.fadeOutHoverPane();
548
+ }
549
+ e.stopPropagation();
550
+ };
551
+
552
+ /**
553
+ * Aborts and restarts the timer for showing the hover pane.
554
+ */
555
+ private restartShowHoverPaneTimer(): void {
556
+ clearTimeout(this.showTimer);
557
+ this.showTimer = window.setTimeout(() => {
558
+ this.host.acquireFocus();
559
+ this.showHoverPane();
560
+ }, this.showDelay);
561
+ }
562
+
563
+ /**
564
+ * Causes this tile's hover pane to be rendered, positioned, and made visible.
565
+ */
566
+ private async showHoverPane(
567
+ options: {
568
+ anchor: HoverPanePositionAnchor;
569
+ } = {
570
+ anchor: 'cursor',
571
+ },
572
+ ): Promise<void> {
573
+ this.hoverPaneState = 'shown';
574
+ this.host.requestUpdate();
575
+
576
+ // Wait for the state update to render the hover pane
577
+ await this.host.updateComplete;
578
+
579
+ // Ensure the hover pane element is still in the document before showing,
580
+ // as it might have been removed by the previous update.
581
+ if (!this.hoverPane?.isConnected) return;
582
+
583
+ this.hoverPane?.showPopover?.();
584
+ await new Promise(resolve => {
585
+ // Pane sizes aren't accurate until next frame
586
+ requestAnimationFrame(resolve);
587
+ });
588
+
589
+ // Apply the correct positioning to the hover pane
590
+ this.repositionHoverPane(options.anchor);
591
+
592
+ // The hover pane is initially not visible (to avoid it shifting around
593
+ // while being positioned). Since it now has the correct positioning, we
594
+ // can make it visible and begin its fade-in animation.
595
+ this.hoverPane?.classList.add('visible', 'fade-in');
596
+ }
597
+
598
+ /**
599
+ * Causes this tile's hover pane to begin fading out and starts
600
+ * the timer for it to be removed.
601
+ */
602
+ private fadeOutHoverPane(): void {
603
+ this.hoverPaneState = 'fading-out';
604
+ this.hoverPane?.classList.remove('fade-in');
605
+
606
+ clearTimeout(this.hideTimer);
607
+ this.hideTimer = window.setTimeout(() => {
608
+ this.hoverPaneState = 'hidden';
609
+ if (this.hoverPane) {
610
+ this.hoverPane.tabIndex = -1;
611
+ }
612
+ this.host.requestUpdate();
613
+ }, 100);
614
+ }
615
+
616
+ /**
617
+ * Positions the hover pane with the correct offsets.
618
+ */
619
+ private repositionHoverPane(anchor: HoverPanePositionAnchor): void {
620
+ if (!this.hoverPane) return;
621
+
622
+ const { top, left } = this.makePaneDesiredOffsets(anchor);
623
+
624
+ this.hoverPane.style.top = `${top}px`;
625
+ this.hoverPane.style.left = `${left}px`;
626
+ }
627
+ }