@internetarchive/collection-browser 3.4.1-alpha-webdev7761.2 → 3.4.1-alpha-webdev7761.4

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 (208) hide show
  1. package/dist/src/app-root.js +19 -28
  2. package/dist/src/app-root.js.map +1 -1
  3. package/dist/src/collection-browser.d.ts +14 -10
  4. package/dist/src/collection-browser.js +870 -886
  5. package/dist/src/collection-browser.js.map +1 -1
  6. package/dist/src/collection-facets/facet-row.js +3 -4
  7. package/dist/src/collection-facets/facet-row.js.map +1 -1
  8. package/dist/src/collection-facets/models.js.map +1 -1
  9. package/dist/src/collection-facets/more-facets-content.js +145 -156
  10. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  11. package/dist/src/collection-facets/more-facets-pagination.js +6 -10
  12. package/dist/src/collection-facets/more-facets-pagination.js.map +1 -1
  13. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js +16 -21
  14. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js.map +1 -1
  15. package/dist/src/collection-facets/smart-facets/smart-facet-bar.js +7 -10
  16. package/dist/src/collection-facets/smart-facets/smart-facet-bar.js.map +1 -1
  17. package/dist/src/collection-facets/smart-facets/smart-facet-button.js +3 -2
  18. package/dist/src/collection-facets/smart-facets/smart-facet-button.js.map +1 -1
  19. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.js +9 -11
  20. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.js.map +1 -1
  21. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.js +7 -7
  22. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.js.map +1 -1
  23. package/dist/src/collection-facets/toggle-switch.js +4 -6
  24. package/dist/src/collection-facets/toggle-switch.js.map +1 -1
  25. package/dist/src/collection-facets.js +34 -50
  26. package/dist/src/collection-facets.js.map +1 -1
  27. package/dist/src/combo-box/caret-closed.js +5 -11
  28. package/dist/src/combo-box/caret-closed.js.map +1 -1
  29. package/dist/src/combo-box/caret-open.js +5 -11
  30. package/dist/src/combo-box/caret-open.js.map +1 -1
  31. package/dist/src/combo-box/clear.d.ts +2 -0
  32. package/dist/src/combo-box/clear.js +11 -0
  33. package/dist/src/combo-box/clear.js.map +1 -0
  34. package/dist/src/combo-box/ia-combo-box.d.ts +40 -9
  35. package/dist/src/combo-box/ia-combo-box.js +363 -272
  36. package/dist/src/combo-box/ia-combo-box.js.map +1 -1
  37. package/dist/src/combo-box/models.d.ts +14 -0
  38. package/dist/src/combo-box/models.js +32 -1
  39. package/dist/src/combo-box/models.js.map +1 -1
  40. package/dist/src/data-source/collection-browser-data-source.js +35 -47
  41. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  42. package/dist/src/empty-placeholder.js +19 -18
  43. package/dist/src/empty-placeholder.js.map +1 -1
  44. package/dist/src/expanded-date-picker.js +6 -10
  45. package/dist/src/expanded-date-picker.js.map +1 -1
  46. package/dist/src/language-code-handler/language-code-handler.js +2 -2
  47. package/dist/src/language-code-handler/language-code-handler.js.map +1 -1
  48. package/dist/src/manage/manage-bar.js +86 -92
  49. package/dist/src/manage/manage-bar.js.map +1 -1
  50. package/dist/src/manage/remove-items-modal-content.js +2 -2
  51. package/dist/src/manage/remove-items-modal-content.js.map +1 -1
  52. package/dist/src/models.js +36 -40
  53. package/dist/src/models.js.map +1 -1
  54. package/dist/src/restoration-state-handler.js +9 -10
  55. package/dist/src/restoration-state-handler.js.map +1 -1
  56. package/dist/src/sort-filter-bar/alpha-bar.js +9 -14
  57. package/dist/src/sort-filter-bar/alpha-bar.js.map +1 -1
  58. package/dist/src/sort-filter-bar/sort-filter-bar.js +14 -24
  59. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -1
  60. package/dist/src/tiles/base-tile-component.js +1 -2
  61. package/dist/src/tiles/base-tile-component.js.map +1 -1
  62. package/dist/src/tiles/grid/account-tile.js +36 -38
  63. package/dist/src/tiles/grid/account-tile.js.map +1 -1
  64. package/dist/src/tiles/grid/collection-tile.js +79 -82
  65. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  66. package/dist/src/tiles/grid/item-tile.js +154 -164
  67. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  68. package/dist/src/tiles/grid/search-tile.js +42 -43
  69. package/dist/src/tiles/grid/search-tile.js.map +1 -1
  70. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js +119 -119
  71. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js.map +1 -1
  72. package/dist/src/tiles/grid/tile-stats.js +2 -3
  73. package/dist/src/tiles/grid/tile-stats.js.map +1 -1
  74. package/dist/src/tiles/hover/hover-pane-controller.js +42 -49
  75. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
  76. package/dist/src/tiles/hover/tile-hover-pane.js +113 -114
  77. package/dist/src/tiles/hover/tile-hover-pane.js.map +1 -1
  78. package/dist/src/tiles/image-block.js +5 -8
  79. package/dist/src/tiles/image-block.js.map +1 -1
  80. package/dist/src/tiles/item-image.js +12 -19
  81. package/dist/src/tiles/item-image.js.map +1 -1
  82. package/dist/src/tiles/list/tile-list-compact.js +114 -122
  83. package/dist/src/tiles/list/tile-list-compact.js.map +1 -1
  84. package/dist/src/tiles/list/tile-list.js +326 -347
  85. package/dist/src/tiles/list/tile-list.js.map +1 -1
  86. package/dist/src/tiles/overlay/icon-overlay.js +1 -2
  87. package/dist/src/tiles/overlay/icon-overlay.js.map +1 -1
  88. package/dist/src/tiles/overlay/text-overlay.js +2 -4
  89. package/dist/src/tiles/overlay/text-overlay.js.map +1 -1
  90. package/dist/src/tiles/text-snippet-block.js +2 -4
  91. package/dist/src/tiles/text-snippet-block.js.map +1 -1
  92. package/dist/src/tiles/tile-dispatcher.js +233 -241
  93. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  94. package/dist/src/tiles/tile-display-value-provider.js +5 -9
  95. package/dist/src/tiles/tile-display-value-provider.js.map +1 -1
  96. package/dist/src/tiles/tile-mediatype-icon.js +12 -19
  97. package/dist/src/tiles/tile-mediatype-icon.js.map +1 -1
  98. package/dist/src/utils/collapse-repeated-quotes.js +1 -1
  99. package/dist/src/utils/collapse-repeated-quotes.js.map +1 -1
  100. package/dist/src/utils/facet-utils.js +3 -5
  101. package/dist/src/utils/facet-utils.js.map +1 -1
  102. package/dist/src/utils/format-count.js +10 -10
  103. package/dist/src/utils/format-count.js.map +1 -1
  104. package/dist/src/utils/format-date.js.map +1 -1
  105. package/dist/src/utils/resolve-mediatype.js +2 -3
  106. package/dist/src/utils/resolve-mediatype.js.map +1 -1
  107. package/dist/test/collection-browser.test.js +131 -185
  108. package/dist/test/collection-browser.test.js.map +1 -1
  109. package/dist/test/collection-facets/facet-row.test.js +60 -75
  110. package/dist/test/collection-facets/facet-row.test.js.map +1 -1
  111. package/dist/test/collection-facets/facets-template.test.js +17 -23
  112. package/dist/test/collection-facets/facets-template.test.js.map +1 -1
  113. package/dist/test/collection-facets/more-facets-content.test.js +22 -32
  114. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -1
  115. package/dist/test/collection-facets/more-facets-pagination.test.js +16 -22
  116. package/dist/test/collection-facets/more-facets-pagination.test.js.map +1 -1
  117. package/dist/test/collection-facets/toggle-switch.test.js +22 -19
  118. package/dist/test/collection-facets/toggle-switch.test.js.map +1 -1
  119. package/dist/test/collection-facets.test.js +80 -97
  120. package/dist/test/collection-facets.test.js.map +1 -1
  121. package/dist/test/empty-placeholder.test.js +11 -17
  122. package/dist/test/empty-placeholder.test.js.map +1 -1
  123. package/dist/test/expanded-date-picker.test.js +8 -14
  124. package/dist/test/expanded-date-picker.test.js.map +1 -1
  125. package/dist/test/icon-overlay.test.js +7 -6
  126. package/dist/test/icon-overlay.test.js.map +1 -1
  127. package/dist/test/image-block.test.js +16 -26
  128. package/dist/test/image-block.test.js.map +1 -1
  129. package/dist/test/item-image.test.js +23 -32
  130. package/dist/test/item-image.test.js.map +1 -1
  131. package/dist/test/manage/manage-bar.test.js +21 -33
  132. package/dist/test/manage/manage-bar.test.js.map +1 -1
  133. package/dist/test/manage/remove-items-modal-content.test.js +10 -15
  134. package/dist/test/manage/remove-items-modal-content.test.js.map +1 -1
  135. package/dist/test/mocks/mock-search-service.js +2 -3
  136. package/dist/test/mocks/mock-search-service.js.map +1 -1
  137. package/dist/test/restoration-state-handler.test.js +13 -21
  138. package/dist/test/restoration-state-handler.test.js.map +1 -1
  139. package/dist/test/review-block.test.js +16 -18
  140. package/dist/test/review-block.test.js.map +1 -1
  141. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js +2 -3
  142. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js.map +1 -1
  143. package/dist/test/sort-filter-bar/alpha-bar.test.js +18 -24
  144. package/dist/test/sort-filter-bar/alpha-bar.test.js.map +1 -1
  145. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +178 -180
  146. package/dist/test/sort-filter-bar/sort-filter-bar.test.js.map +1 -1
  147. package/dist/test/text-overlay.test.js +16 -15
  148. package/dist/test/text-overlay.test.js.map +1 -1
  149. package/dist/test/text-snippet-block.test.js +14 -19
  150. package/dist/test/text-snippet-block.test.js.map +1 -1
  151. package/dist/test/tile-stats.test.js +73 -34
  152. package/dist/test/tile-stats.test.js.map +1 -1
  153. package/dist/test/tiles/grid/account-tile.test.js +25 -25
  154. package/dist/test/tiles/grid/account-tile.test.js.map +1 -1
  155. package/dist/test/tiles/grid/collection-tile.test.js +13 -19
  156. package/dist/test/tiles/grid/collection-tile.test.js.map +1 -1
  157. package/dist/test/tiles/grid/item-tile.test.js +141 -168
  158. package/dist/test/tiles/grid/item-tile.test.js.map +1 -1
  159. package/dist/test/tiles/grid/search-tile.test.js +9 -13
  160. package/dist/test/tiles/grid/search-tile.test.js.map +1 -1
  161. package/dist/test/tiles/hover/hover-pane-controller.test.js +50 -62
  162. package/dist/test/tiles/hover/hover-pane-controller.test.js.map +1 -1
  163. package/dist/test/tiles/hover/tile-hover-pane.test.js +12 -16
  164. package/dist/test/tiles/hover/tile-hover-pane.test.js.map +1 -1
  165. package/dist/test/tiles/list/tile-list-compact.test.js +104 -118
  166. package/dist/test/tiles/list/tile-list-compact.test.js.map +1 -1
  167. package/dist/test/tiles/list/tile-list.test.js +202 -231
  168. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  169. package/dist/test/tiles/tile-dispatcher.test.js +97 -110
  170. package/dist/test/tiles/tile-dispatcher.test.js.map +1 -1
  171. package/dist/test/tiles/tile-mediatype-icon.test.js +12 -24
  172. package/dist/test/tiles/tile-mediatype-icon.test.js.map +1 -1
  173. package/dist/test/utils/format-date.test.js.map +1 -1
  174. package/index.html +1 -1
  175. package/package.json +5 -3
  176. package/src/collection-browser.ts +3060 -3030
  177. package/src/collection-facets/models.ts +10 -10
  178. package/src/collection-facets/more-facets-content.ts +639 -639
  179. package/src/collection-facets.ts +1 -1
  180. package/src/combo-box/caret-closed.ts +5 -11
  181. package/src/combo-box/caret-open.ts +5 -11
  182. package/src/combo-box/clear.ts +11 -0
  183. package/src/combo-box/ia-combo-box.ts +1288 -1180
  184. package/src/combo-box/models.ts +31 -1
  185. package/src/manage/manage-bar.ts +247 -247
  186. package/src/restoration-state-handler.ts +5 -1
  187. package/src/tiles/base-tile-component.ts +65 -65
  188. package/src/tiles/grid/account-tile.ts +113 -113
  189. package/src/tiles/grid/collection-tile.ts +163 -163
  190. package/src/tiles/grid/item-tile.ts +340 -340
  191. package/src/tiles/grid/search-tile.ts +90 -90
  192. package/src/tiles/grid/styles/tile-grid-shared-styles.ts +130 -130
  193. package/src/tiles/hover/hover-pane-controller.ts +613 -613
  194. package/src/tiles/hover/tile-hover-pane.ts +184 -184
  195. package/src/tiles/list/tile-list-compact.ts +239 -239
  196. package/src/tiles/list/tile-list.ts +700 -700
  197. package/src/tiles/tile-dispatcher.ts +517 -517
  198. package/src/utils/format-date.ts +62 -62
  199. package/test/collection-facets/facet-row.test.ts +375 -375
  200. package/test/collection-facets.test.ts +928 -928
  201. package/test/tiles/grid/item-tile.test.ts +520 -520
  202. package/test/tiles/hover/hover-pane-controller.test.ts +418 -418
  203. package/test/tiles/list/tile-list-compact.test.ts +282 -282
  204. package/test/tiles/list/tile-list.test.ts +552 -552
  205. package/test/tiles/tile-dispatcher.test.ts +283 -283
  206. package/test/utils/format-date.test.ts +89 -89
  207. package/tsconfig.json +8 -3
  208. package/vite.config.ts +29 -22
@@ -1,613 +1,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
- 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
+ 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
+ }