@uvalib/web-components 1.0.1 → 1.1.1

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 (229) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/README.md +772 -33
  3. package/dist/base/analytics-state.d.ts +36 -0
  4. package/dist/base/analytics-state.js +44 -0
  5. package/dist/base/analytics-state.js.map +1 -0
  6. package/dist/base/analytics.d.ts +31 -27
  7. package/dist/base/analytics.js +48 -37
  8. package/dist/base/analytics.js.map +1 -1
  9. package/dist/base/inline-copy.d.ts +16 -0
  10. package/dist/base/inline-copy.js +92 -0
  11. package/dist/base/inline-copy.js.map +1 -0
  12. package/dist/base/source-children.d.ts +46 -0
  13. package/dist/base/source-children.js +116 -0
  14. package/dist/base/source-children.js.map +1 -0
  15. package/dist/base.css +199 -0
  16. package/dist/base.d.ts +2 -2
  17. package/dist/base.js +1 -1
  18. package/dist/base.js.map +1 -1
  19. package/dist/components/bento-box/BentoSection.d.ts +1 -1
  20. package/dist/components/bento-box/BentoSection.js +7 -2
  21. package/dist/components/bento-box/BentoSection.js.map +1 -1
  22. package/dist/components/events-section/EventsSection.d.ts +3 -0
  23. package/dist/components/events-section/EventsSection.js +12 -1
  24. package/dist/components/events-section/EventsSection.js.map +1 -1
  25. package/dist/components/library-weekly-hours/LibraryWeeklyHours.d.ts +1 -0
  26. package/dist/components/library-weekly-hours/LibraryWeeklyHours.js +5 -0
  27. package/dist/components/library-weekly-hours/LibraryWeeklyHours.js.map +1 -1
  28. package/dist/components/site-alerts/SiteAlerts.d.ts +109 -0
  29. package/dist/components/site-alerts/SiteAlerts.js +542 -0
  30. package/dist/components/site-alerts/SiteAlerts.js.map +1 -0
  31. package/dist/components/site-alerts/alerts-source.d.ts +36 -0
  32. package/dist/components/site-alerts/alerts-source.js +115 -0
  33. package/dist/components/site-alerts/alerts-source.js.map +1 -0
  34. package/dist/components/{site-page → site-alerts}/alerts.js +8 -3
  35. package/dist/components/site-alerts/alerts.js.map +1 -0
  36. package/dist/components/site-alerts/body.d.ts +10 -0
  37. package/dist/components/site-alerts/body.js +48 -0
  38. package/dist/components/site-alerts/body.js.map +1 -0
  39. package/dist/components/site-alerts/dismissed.d.ts +12 -0
  40. package/dist/components/site-alerts/dismissed.js +72 -0
  41. package/dist/components/site-alerts/dismissed.js.map +1 -0
  42. package/dist/components/site-alerts/icons.d.ts +8 -0
  43. package/dist/components/site-alerts/icons.js +15 -0
  44. package/dist/components/site-alerts/icons.js.map +1 -0
  45. package/dist/components/site-alerts/site-alerts.d.ts +1 -0
  46. package/dist/components/site-alerts/site-alerts.js +4 -0
  47. package/dist/components/site-alerts/site-alerts.js.map +1 -0
  48. package/dist/components/site-analytics/SiteAnalytics.d.ts +26 -2
  49. package/dist/components/site-analytics/SiteAnalytics.js +140 -4
  50. package/dist/components/site-analytics/SiteAnalytics.js.map +1 -1
  51. package/dist/components/site-footer/SiteFooter.d.ts +4 -0
  52. package/dist/components/site-footer/SiteFooter.js +28 -4
  53. package/dist/components/site-footer/SiteFooter.js.map +1 -1
  54. package/dist/components/site-header/SiteHeader.d.ts +47 -3
  55. package/dist/components/site-header/SiteHeader.js +198 -21
  56. package/dist/components/site-header/SiteHeader.js.map +1 -1
  57. package/dist/components/site-hours-section/SiteHoursSection.d.ts +4 -1
  58. package/dist/components/site-hours-section/SiteHoursSection.js +18 -3
  59. package/dist/components/site-hours-section/SiteHoursSection.js.map +1 -1
  60. package/dist/components/site-main-nav/SiteMainNav.d.ts +1 -1
  61. package/dist/components/site-main-nav/SiteMainNav.js +11 -1
  62. package/dist/components/site-main-nav/SiteMainNav.js.map +1 -1
  63. package/dist/components/site-page/SitePage.d.ts +43 -15
  64. package/dist/components/site-page/SitePage.js +161 -34
  65. package/dist/components/site-page/SitePage.js.map +1 -1
  66. package/dist/components/uvalib-breadcrumbs/UvalibBreadcrumbs.d.ts +91 -0
  67. package/dist/components/uvalib-breadcrumbs/UvalibBreadcrumbs.js +310 -0
  68. package/dist/components/uvalib-breadcrumbs/UvalibBreadcrumbs.js.map +1 -0
  69. package/dist/components/uvalib-breadcrumbs/uvalib-breadcrumbs.d.ts +1 -0
  70. package/dist/components/uvalib-breadcrumbs/uvalib-breadcrumbs.js +4 -0
  71. package/dist/components/uvalib-breadcrumbs/uvalib-breadcrumbs.js.map +1 -0
  72. package/dist/components/uvalib-card/UvalibCard.d.ts +113 -0
  73. package/dist/components/uvalib-card/UvalibCard.js +643 -0
  74. package/dist/components/uvalib-card/UvalibCard.js.map +1 -0
  75. package/dist/components/uvalib-card/uvalib-card.d.ts +1 -0
  76. package/dist/components/uvalib-card/uvalib-card.js +4 -0
  77. package/dist/components/uvalib-card/uvalib-card.js.map +1 -0
  78. package/dist/components/uvalib-card-grid/UvalibCardGrid.d.ts +60 -0
  79. package/dist/components/uvalib-card-grid/UvalibCardGrid.js +305 -0
  80. package/dist/components/uvalib-card-grid/UvalibCardGrid.js.map +1 -0
  81. package/dist/components/uvalib-card-grid/uvalib-card-grid.d.ts +1 -0
  82. package/dist/components/uvalib-card-grid/uvalib-card-grid.js +4 -0
  83. package/dist/components/uvalib-card-grid/uvalib-card-grid.js.map +1 -0
  84. package/dist/components/uvalib-contents/UvalibContents.d.ts +134 -0
  85. package/dist/components/uvalib-contents/UvalibContents.js +613 -0
  86. package/dist/components/uvalib-contents/UvalibContents.js.map +1 -0
  87. package/dist/components/uvalib-contents/uvalib-contents.d.ts +1 -0
  88. package/dist/components/uvalib-contents/uvalib-contents.js +4 -0
  89. package/dist/components/uvalib-contents/uvalib-contents.js.map +1 -0
  90. package/dist/components/uvalib-date-range/UvalibDateRange.d.ts +10 -0
  91. package/dist/components/uvalib-date-range/UvalibDateRange.js +21 -0
  92. package/dist/components/uvalib-date-range/UvalibDateRange.js.map +1 -1
  93. package/dist/components/uvalib-filter/UvalibFilter.d.ts +1 -0
  94. package/dist/components/uvalib-filter/UvalibFilter.js +11 -1
  95. package/dist/components/uvalib-filter/UvalibFilter.js.map +1 -1
  96. package/dist/components/uvalib-lightbox/UvalibLightbox.d.ts +204 -0
  97. package/dist/components/uvalib-lightbox/UvalibLightbox.js +1115 -0
  98. package/dist/components/uvalib-lightbox/UvalibLightbox.js.map +1 -0
  99. package/dist/components/uvalib-lightbox/media.d.ts +51 -0
  100. package/dist/components/uvalib-lightbox/media.js +127 -0
  101. package/dist/components/uvalib-lightbox/media.js.map +1 -0
  102. package/dist/components/uvalib-lightbox/uvalib-lightbox.d.ts +1 -0
  103. package/dist/components/uvalib-lightbox/uvalib-lightbox.js +4 -0
  104. package/dist/components/uvalib-lightbox/uvalib-lightbox.js.map +1 -0
  105. package/dist/components/uvalib-metadata/UvalibMetadata.d.ts +124 -0
  106. package/dist/components/uvalib-metadata/UvalibMetadata.js +443 -0
  107. package/dist/components/uvalib-metadata/UvalibMetadata.js.map +1 -0
  108. package/dist/components/uvalib-metadata/rights.d.ts +8 -0
  109. package/dist/components/uvalib-metadata/rights.js +47 -0
  110. package/dist/components/uvalib-metadata/rights.js.map +1 -0
  111. package/dist/components/uvalib-metadata/uvalib-metadata.d.ts +1 -0
  112. package/dist/components/uvalib-metadata/uvalib-metadata.js +4 -0
  113. package/dist/components/uvalib-metadata/uvalib-metadata.js.map +1 -0
  114. package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.d.ts +103 -0
  115. package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.js +410 -0
  116. package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.js.map +1 -0
  117. package/dist/components/uvalib-pagefind-search/uvalib-pagefind-search.d.ts +1 -0
  118. package/dist/components/uvalib-pagefind-search/uvalib-pagefind-search.js +7 -0
  119. package/dist/components/uvalib-pagefind-search/uvalib-pagefind-search.js.map +1 -0
  120. package/dist/components/uvalib-pager/UvalibPager.d.ts +94 -0
  121. package/dist/components/uvalib-pager/UvalibPager.js +390 -0
  122. package/dist/components/uvalib-pager/UvalibPager.js.map +1 -0
  123. package/dist/components/uvalib-pager/uvalib-pager.d.ts +1 -0
  124. package/dist/components/uvalib-pager/uvalib-pager.js +4 -0
  125. package/dist/components/uvalib-pager/uvalib-pager.js.map +1 -0
  126. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.d.ts +59 -0
  127. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.js +350 -0
  128. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.js.map +1 -0
  129. package/dist/components/uvalib-photo-grid/uvalib-photo-grid.d.ts +1 -0
  130. package/dist/components/uvalib-photo-grid/uvalib-photo-grid.js +4 -0
  131. package/dist/components/uvalib-photo-grid/uvalib-photo-grid.js.map +1 -0
  132. package/dist/custom-elements.json +10916 -3049
  133. package/dist/index.d.ts +16 -0
  134. package/dist/index.js +11 -0
  135. package/dist/index.js.map +1 -1
  136. package/dist/pagefind.css +388 -0
  137. package/dist/styles/base.js +11 -0
  138. package/dist/styles/base.js.map +1 -1
  139. package/dist/styles/color-scheme.js +24 -11
  140. package/dist/styles/color-scheme.js.map +1 -1
  141. package/dist/styles/controls.d.ts +2 -0
  142. package/dist/styles/controls.js +200 -0
  143. package/dist/styles/controls.js.map +1 -0
  144. package/dist/styles/pagefind.d.ts +1 -0
  145. package/dist/styles/pagefind.js +395 -0
  146. package/dist/styles/pagefind.js.map +1 -0
  147. package/dist/tokens.css +23 -20
  148. package/dist-cdn/base.css +199 -0
  149. package/dist-cdn/bento-box.js +103 -95
  150. package/dist-cdn/bento-box.js.map +1 -1
  151. package/dist-cdn/chunks/SiteAnalytics-Cm-FyhYS.js +243 -0
  152. package/dist-cdn/chunks/SiteAnalytics-Cm-FyhYS.js.map +1 -0
  153. package/dist-cdn/chunks/alerts-C5vkeb-f.js +39 -0
  154. package/dist-cdn/chunks/alerts-C5vkeb-f.js.map +1 -0
  155. package/dist-cdn/chunks/analytics-DTTihuBD.js +43 -0
  156. package/dist-cdn/chunks/analytics-DTTihuBD.js.map +1 -0
  157. package/dist-cdn/chunks/analytics-state-Ce8DKe4F.js +39 -0
  158. package/dist-cdn/chunks/analytics-state-Ce8DKe4F.js.map +1 -0
  159. package/dist-cdn/chunks/{base-DCGd2Oii.js → base-CAFQWnZG.js} +12 -1
  160. package/dist-cdn/chunks/{base-DCGd2Oii.js.map → base-CAFQWnZG.js.map} +1 -1
  161. package/dist-cdn/chunks/{library-weekly-hours-ByQ8ugxh.js → library-weekly-hours-COE7Vv6V.js} +47 -38
  162. package/dist-cdn/chunks/library-weekly-hours-COE7Vv6V.js.map +1 -0
  163. package/dist-cdn/chunks/{site-header-BwDkZ2LL.js → site-header-C9V7Wuw2.js} +169 -49
  164. package/dist-cdn/chunks/site-header-C9V7Wuw2.js.map +1 -0
  165. package/dist-cdn/chunks/{site-main-nav-BLAAunnr.js → site-main-nav-AaxZ9rQf.js} +11 -2
  166. package/dist-cdn/chunks/site-main-nav-AaxZ9rQf.js.map +1 -0
  167. package/dist-cdn/chunks/source-children-RuAec527.js +119 -0
  168. package/dist-cdn/chunks/source-children-RuAec527.js.map +1 -0
  169. package/dist-cdn/custom-elements.json +10916 -3049
  170. package/dist-cdn/events-section.js +78 -66
  171. package/dist-cdn/events-section.js.map +1 -1
  172. package/dist-cdn/library-hours-setup.js +1 -1
  173. package/dist-cdn/library-weekly-hours.js +1 -1
  174. package/dist-cdn/manifest.json +13 -2
  175. package/dist-cdn/pagefind.css +388 -0
  176. package/dist-cdn/site-alerts.js +516 -0
  177. package/dist-cdn/site-alerts.js.map +1 -0
  178. package/dist-cdn/site-analytics.js +1 -1
  179. package/dist-cdn/site-footer.js +33 -18
  180. package/dist-cdn/site-footer.js.map +1 -1
  181. package/dist-cdn/site-header.js +1 -1
  182. package/dist-cdn/site-hours-section.js +33 -21
  183. package/dist-cdn/site-hours-section.js.map +1 -1
  184. package/dist-cdn/site-main-nav.js +1 -1
  185. package/dist-cdn/site-page.js +116 -81
  186. package/dist-cdn/site-page.js.map +1 -1
  187. package/dist-cdn/site-style.js +1 -1
  188. package/dist-cdn/skills/uvalib-web-components/SKILL.md +394 -11
  189. package/dist-cdn/skills/uvalib-web-components/reference/accessibility-testing.md +170 -0
  190. package/dist-cdn/skills/uvalib-web-components/reference/components.md +414 -12
  191. package/dist-cdn/skills/uvalib-web-components/reference/migration.md +15 -0
  192. package/dist-cdn/tokens.css +23 -20
  193. package/dist-cdn/uvalib-analytics.js +1 -1
  194. package/dist-cdn/uvalib-breadcrumbs.js +176 -0
  195. package/dist-cdn/uvalib-breadcrumbs.js.map +1 -0
  196. package/dist-cdn/uvalib-card-grid.js +163 -0
  197. package/dist-cdn/uvalib-card-grid.js.map +1 -0
  198. package/dist-cdn/uvalib-card.js +424 -0
  199. package/dist-cdn/uvalib-card.js.map +1 -0
  200. package/dist-cdn/uvalib-contents.js +381 -0
  201. package/dist-cdn/uvalib-contents.js.map +1 -0
  202. package/dist-cdn/uvalib-date-range.js +33 -18
  203. package/dist-cdn/uvalib-date-range.js.map +1 -1
  204. package/dist-cdn/uvalib-filter.js +32 -11
  205. package/dist-cdn/uvalib-filter.js.map +1 -1
  206. package/dist-cdn/uvalib-lightbox.js +662 -0
  207. package/dist-cdn/uvalib-lightbox.js.map +1 -0
  208. package/dist-cdn/uvalib-metadata.js +313 -0
  209. package/dist-cdn/uvalib-metadata.js.map +1 -0
  210. package/dist-cdn/uvalib-pagefind-search.js +207 -0
  211. package/dist-cdn/uvalib-pagefind-search.js.map +1 -0
  212. package/dist-cdn/uvalib-pager.js +278 -0
  213. package/dist-cdn/uvalib-pager.js.map +1 -0
  214. package/dist-cdn/uvalib-photo-grid.js +202 -0
  215. package/dist-cdn/uvalib-photo-grid.js.map +1 -0
  216. package/package.json +43 -1
  217. package/skills/uvalib-web-components/SKILL.md +394 -11
  218. package/skills/uvalib-web-components/reference/accessibility-testing.md +170 -0
  219. package/skills/uvalib-web-components/reference/components.md +414 -12
  220. package/skills/uvalib-web-components/reference/migration.md +15 -0
  221. package/dist/components/site-page/alerts.js.map +0 -1
  222. package/dist-cdn/chunks/SiteAnalytics-T1Wx3lsb.js +0 -174
  223. package/dist-cdn/chunks/SiteAnalytics-T1Wx3lsb.js.map +0 -1
  224. package/dist-cdn/chunks/analytics-C8D8n_wt.js +0 -55
  225. package/dist-cdn/chunks/analytics-C8D8n_wt.js.map +0 -1
  226. package/dist-cdn/chunks/library-weekly-hours-ByQ8ugxh.js.map +0 -1
  227. package/dist-cdn/chunks/site-header-BwDkZ2LL.js.map +0 -1
  228. package/dist-cdn/chunks/site-main-nav-BLAAunnr.js.map +0 -1
  229. /package/dist/components/{site-page → site-alerts}/alerts.d.ts +0 -0
@@ -0,0 +1,1115 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { css, html, nothing, unsafeCSS } from 'lit';
8
+ import { property, state } from 'lit/decorators.js';
9
+ import { trackEvent } from '../../base/analytics.js';
10
+ import { UvalibElement } from '../../base/UvalibElement.js';
11
+ import { installLightDomStyles } from '../../base/light-dom-styles.js';
12
+ import { accessible } from '../../styles/accessible.js';
13
+ import { hrefWithParam, iiifIdentifier, paramFromLocation, sizedIiifUrl, usableImageUrl, usesHashParam, viewportIiifSize, } from './media.js';
14
+ const normalize = (s) => String(s ?? '')
15
+ .replace(/\s+/g, ' ')
16
+ .trim();
17
+ /**
18
+ * Kept with `no-style`: the triggers stay put, `[hidden]` hides, the dialog hugs its panel
19
+ * (so a click outside it is the backdrop), controls are at least 44px, and the image stays
20
+ * within the viewport. `host` is `:host` (shadow) or the tag (light DOM).
21
+ */
22
+ const structuralRules = (host) => `
23
+ ${host} { display: block; }
24
+ ${host} .viewer { display: contents; }
25
+ ${host} dialog {
26
+ margin: auto;
27
+ padding: 0;
28
+ border: none;
29
+ background: transparent;
30
+ width: min(72rem, calc(100% - 2rem));
31
+ max-height: calc(100% - 2rem);
32
+ }
33
+ ${host} .panel {
34
+ box-sizing: border-box;
35
+ display: flex;
36
+ flex-direction: column;
37
+ gap: 0.75rem;
38
+ max-height: calc(100dvh - 2rem);
39
+ overflow: auto;
40
+ overscroll-behavior: contain;
41
+ padding: 0.75rem;
42
+ }
43
+ ${host} .head, ${host} .toolbar {
44
+ display: flex;
45
+ flex-wrap: wrap;
46
+ align-items: center;
47
+ gap: 0.5rem;
48
+ }
49
+ ${host} .head { justify-content: space-between; }
50
+ ${host} .caption { margin: 0; min-width: 0; overflow-wrap: anywhere; }
51
+ ${host} .figure { margin: 0; }
52
+ ${host} dialog img { display: block; max-width: 100%; max-height: 70dvh; width: auto; height: auto; margin: auto; }
53
+ ${host} .count, ${host} .status { margin: 0; }
54
+ ${host} dialog button, ${host} dialog .full {
55
+ box-sizing: border-box;
56
+ min-width: 44px;
57
+ min-height: 44px;
58
+ padding: 0.5rem 0.75rem;
59
+ font: inherit;
60
+ }
61
+ ${host} .details:empty { display: none; }
62
+ ${host} > [slot="details"] { display: none; }
63
+ ${host}[hidden], ${host} [hidden] { display: none !important; }
64
+ `;
65
+ const shadowStructure = unsafeCSS(structuralRules(':host'));
66
+ let nextCaption = 0;
67
+ /**
68
+ * An accessible image viewer over a set of plain links. With JavaScript a primary click opens
69
+ * a modal dialog; without it (or with a modified click) the link opens the image as before.
70
+ * Wrap the links, or point at them with `selector` (for example `a.pb-image` on a reading
71
+ * page). There is no autoplay and no carousel.
72
+ *
73
+ * The dialog is labelled by the caption (`data-caption`, else the image alt, else the link
74
+ * text). Focus moves to Close on open and back to the trigger on close. Escape and a click on
75
+ * the backdrop close it. With more than one image, Previous and Next and the arrow keys move
76
+ * through the set (wrapping), and a polite “3 of 13” counter is shown. Full size opens the
77
+ * original image in a new tab. A IIIF Image API URL keeps its region; only the size becomes
78
+ * `!W,H` for the viewport. TIFF and JPEG2000 URLs are left as written. `url-param` (for
79
+ * example `photo`) keeps `?photo=<id>` in sync, including on a hash route
80
+ * (`#/text?photo=<id>`): opening pushes a history entry, stepping replaces it, and
81
+ * Back closes.
82
+ *
83
+ * Optional details: children with `slot="details"` (and `data-for` equal to the item's id or
84
+ * its position from 1; without `data-for` they show for every image), or `data-details-id` on
85
+ * a link pointing at an element elsewhere on the page. Either is shown inside the dialog and
86
+ * put back when the viewer moves on or closes.
87
+ *
88
+ * @tagname uvalib-lightbox
89
+ * @slot - The image links. A plain click opens the viewer; without JavaScript they open the image.
90
+ * @slot details - Details for the open image. `data-for` selects one item by its id or position (from 1); without it the details show for every image.
91
+ * @csspart dialog - The modal dialog.
92
+ * @csspart panel - The surface inside the dialog.
93
+ * @csspart caption - The caption that names the dialog.
94
+ * @csspart image - The image.
95
+ * @csspart controls - Previous, Next, the counter and Full size.
96
+ * @csspart details - The details panel.
97
+ * @fires uvalib-analytics-event - For `<site-analytics>`: `['uvalib-lightbox', 'open'|'next'|'previous'|'full-size', 'image N', N]` (N from 1, the image's place in the set). Never the URL or the caption. Not fired for a viewer restored from the URL or from Back and Forward. Bubbles and is composed.
98
+ * @cssprop --uva-color-surface-raised - Dark-mode role token: dialog surface (else `--uva-color-surface`, else #fff).
99
+ * @cssprop --uva-color-surface - Dark-mode role token: the figure behind the image (else #fff).
100
+ * @cssprop --uva-color-text - Dark-mode role token: text (else `--uva-grey-darkest`).
101
+ * @cssprop --uva-color-shadow - Dark-mode role token: the backdrop scrim (else rgba(16, 23, 43, 0.72)).
102
+ * @cssprop --uva-color-border - Dark-mode role token: dialog border (else `--uva-grey-100`).
103
+ * @cssprop --uva-color-focus - Dark-mode role token: focus ring (else `--uva-brand-orange`).
104
+ * @cssprop --uva-color-link - Dark-mode role token: Full size text when it is not the filled control (else `--uva-brand-blue`).
105
+ * @cssprop --uva-color-accent-bg - Dark-mode role token: Close and Full size background (else `--uva-brand-blue`).
106
+ * @cssprop --uva-color-on-accent - Text on those controls (else #fff).
107
+ * @cssprop [--uva-brand-orange=#e57200] - Focus ring when `--uva-color-focus` is unset.
108
+ * @cssprop [--uva-brand-blue=#232d4b] - Control background when `--uva-color-accent-bg` is unset.
109
+ * @cssprop [--uva-grey-darkest=#2b2b2b] - Text when `--uva-color-text` is unset.
110
+ * @cssprop [--uva-grey-100=#dadada] - Border when `--uva-color-border` is unset.
111
+ */
112
+ export class UvalibLightbox extends UvalibElement {
113
+ constructor() {
114
+ super(...arguments);
115
+ /**
116
+ * CSS selector for image links elsewhere in this root (the page, or the shadow root the
117
+ * component sits in). Combined with any links inside the component. Example: `a.pb-image`.
118
+ */
119
+ this.selector = '';
120
+ /**
121
+ * Query parameter kept in sync with the open image, for example `photo` → `?photo=<id>`.
122
+ * The id is the link's `data-id`, else its `id`, else the IIIF identifier, else its position
123
+ * from 1. On a hash route the parameter is written inside the hash. Empty: no deep link.
124
+ */
125
+ this.urlParam = '';
126
+ this.slides = [];
127
+ this.index = 0;
128
+ this.open = false;
129
+ this.status = 'loading';
130
+ this.fitW = 0;
131
+ this.fitH = 0;
132
+ this.captionId = `uvalib-lightbox-caption-${++nextCaption}`;
133
+ this.trigger = null;
134
+ /** The open image pushed a history entry, so Back (and Close) returns to the page. */
135
+ this.historyPushed = false;
136
+ /** The visitor closed the viewer; a later mutation must not reopen it from the URL. */
137
+ this.userClosed = false;
138
+ /** Disconnect (and the dialog it closes) is not a visitor action, so it must not touch history. */
139
+ this.suppressClose = false;
140
+ this.warnedSelector = false;
141
+ this.lastSrc = '';
142
+ this.syncQueued = false;
143
+ this.resizeTimer = 0;
144
+ /** A close event from the dialog we just shut must not dismiss the next open. */
145
+ this.ignoreClose = false;
146
+ this.dismissing = false;
147
+ this.revealTries = 0;
148
+ this.revealFrame = 0;
149
+ this.listened = false;
150
+ /** `history.back()` from Close has not fired `popstate` yet. */
151
+ this.pendingBack = false;
152
+ /** Whether that entry kept the parameter in the hash. The pop leaves a hash that may not say so. */
153
+ this.backInHash = false;
154
+ /** An image chosen while that Back is still in flight, opened once the popstate arrives. */
155
+ this.queuedOpen = null;
156
+ /**
157
+ * Set while a history traversal is opening the dialog. The traversal can close the
158
+ * dialog it just showed; that close is not the visitor, so show it again.
159
+ */
160
+ this.reopenGuard = false;
161
+ /** Details elements moved into the dialog, and where they go back to. */
162
+ this.parked = [];
163
+ this.parkedKey = '';
164
+ this.onResize = () => {
165
+ if (!this.open)
166
+ return;
167
+ window.clearTimeout(this.resizeTimer);
168
+ this.resizeTimer = window.setTimeout(() => {
169
+ if (this.isConnected && this.open)
170
+ this.measure();
171
+ }, 150);
172
+ };
173
+ this.onClick = (e) => {
174
+ if (!(e instanceof MouseEvent) ||
175
+ e.button !== 0 ||
176
+ e.metaKey ||
177
+ e.ctrlKey ||
178
+ e.shiftKey ||
179
+ e.altKey)
180
+ return;
181
+ const link = this.linkFrom(e);
182
+ if (!link)
183
+ return;
184
+ const slides = this.collect();
185
+ const i = slides.findIndex((s) => s.el === link);
186
+ if (i < 0)
187
+ return;
188
+ e.preventDefault();
189
+ this.activate(i, slides);
190
+ };
191
+ this.onPop = () => {
192
+ if (!this.urlParam)
193
+ return;
194
+ if (this.pendingBack) {
195
+ this.pendingBack = false;
196
+ const queued = this.queuedOpen;
197
+ this.queuedOpen = null;
198
+ if (!this.isConnected || !queued)
199
+ return;
200
+ this.slides = queued.slides;
201
+ this.index = queued.i;
202
+ this.trigger = queued.slides[queued.i]?.el ?? null;
203
+ this.userClosed = false;
204
+ this.measure();
205
+ const id = queued.slides[queued.i]?.id ?? null;
206
+ this.historyPushed = this.writeParam(id, 'push', this.backInHash);
207
+ if (!this.open)
208
+ this.open = true;
209
+ return;
210
+ }
211
+ const id = paramFromLocation(this.loc(), this.urlParam);
212
+ if (!id) {
213
+ this.historyPushed = false;
214
+ if (this.open)
215
+ this.dismiss('pop');
216
+ return;
217
+ }
218
+ this.userClosed = false;
219
+ const slides = this.collect();
220
+ const i = slides.findIndex((s) => s.id === id);
221
+ if (i < 0) {
222
+ if (this.open)
223
+ this.dismiss('pop');
224
+ return;
225
+ }
226
+ this.slides = slides;
227
+ this.index = i;
228
+ this.trigger = slides[i]?.el ?? null;
229
+ this.historyPushed = true;
230
+ this.measure();
231
+ this.armReopenGuard();
232
+ if (!this.open)
233
+ this.open = true;
234
+ };
235
+ this.onKey = (e) => {
236
+ if (!this.multiple || this.typing(e))
237
+ return;
238
+ // Left is previous in LTR and next in RTL, so the key matches the direction of the set.
239
+ const rtl = getComputedStyle(this).direction === 'rtl';
240
+ const back = rtl ? 'ArrowRight' : 'ArrowLeft';
241
+ const forward = rtl ? 'ArrowLeft' : 'ArrowRight';
242
+ if (e.key !== back && e.key !== forward)
243
+ return;
244
+ if (e.altKey || e.metaKey || e.ctrlKey)
245
+ return;
246
+ e.preventDefault();
247
+ this.step(e.key === forward ? 1 : -1);
248
+ };
249
+ this.onDialogClick = (e) => {
250
+ if (e.target === this.dialogEl)
251
+ this.dismiss('ui');
252
+ };
253
+ this.onDialogClose = () => {
254
+ this.onNativeClose();
255
+ };
256
+ this.onFullSize = () => {
257
+ const i = this.index;
258
+ trackEvent(this, ['uvalib-lightbox', 'full-size', `image ${i + 1}`, i + 1]);
259
+ };
260
+ this.onLoad = () => {
261
+ if (this.status === 'loading')
262
+ this.status = 'ready';
263
+ };
264
+ this.onError = () => {
265
+ this.status = 'error';
266
+ };
267
+ /** Stable identity for a details element, so parking it does not look like a new one. */
268
+ this.detailTokens = new WeakMap();
269
+ this.detailTokenSeq = 0;
270
+ }
271
+ static { this.styles = [
272
+ accessible,
273
+ shadowStructure,
274
+ css `
275
+ .panel {
276
+ background: var(--uva-color-surface-raised, var(--uva-color-surface, #fff));
277
+ color: var(--uva-color-text, var(--uva-grey-darkest, #2b2b2b));
278
+ border: 1px solid var(--uva-color-border, var(--uva-grey-100, #dadada));
279
+ }
280
+ dialog::backdrop {
281
+ background: var(--uva-color-shadow, rgba(16, 23, 43, 0.72));
282
+ }
283
+ .details a {
284
+ color: var(--uva-color-link, var(--uva-brand-blue, #232d4b));
285
+ }
286
+ .figure {
287
+ background: var(--uva-color-surface, #fff);
288
+ }
289
+ .caption {
290
+ font-size: 1.125rem;
291
+ line-height: 1.4;
292
+ font-weight: 600;
293
+ }
294
+ .count {
295
+ font-variant-numeric: tabular-nums;
296
+ }
297
+ dialog button,
298
+ dialog .full {
299
+ padding: 0.5rem 0.75rem;
300
+ border: 2px solid transparent;
301
+ text-decoration: none;
302
+ cursor: pointer;
303
+ color: var(--uva-color-on-accent, #fff);
304
+ background: var(--uva-color-accent-bg, var(--uva-brand-blue, #232d4b));
305
+ }
306
+ dialog button:focus-visible,
307
+ dialog .full:focus-visible {
308
+ outline: 3px solid var(--uva-color-focus, var(--uva-brand-orange, #e57200));
309
+ outline-offset: 2px;
310
+ }
311
+ dialog button.prev,
312
+ dialog button.next {
313
+ color: var(--uva-color-link, var(--uva-brand-blue, #232d4b));
314
+ background: var(--uva-color-surface, #fff);
315
+ border-color: var(--uva-color-border, var(--uva-grey-100, #dadada));
316
+ }
317
+ .status {
318
+ font-size: 1rem;
319
+ }
320
+ @media (prefers-reduced-motion: no-preference) {
321
+ .panel {
322
+ animation: uvalib-lightbox-in 0.15s ease;
323
+ }
324
+ }
325
+ @media (prefers-reduced-motion: reduce) {
326
+ dialog,
327
+ dialog * {
328
+ animation: none !important;
329
+ transition: none !important;
330
+ scroll-behavior: auto !important;
331
+ }
332
+ }
333
+ @media (forced-colors: active) {
334
+ .panel,
335
+ dialog button,
336
+ dialog .full {
337
+ border: 1px solid CanvasText;
338
+ }
339
+ }
340
+ @keyframes uvalib-lightbox-in {
341
+ from {
342
+ opacity: 0;
343
+ }
344
+ to {
345
+ opacity: 1;
346
+ }
347
+ }
348
+ `,
349
+ ]; }
350
+ /** Shadow mode follows the base class. Light mode renders into `.viewer`. */
351
+ get isLight() {
352
+ return this.noShadowDom || this.hasAttribute('no-shadow-dom');
353
+ }
354
+ createRenderRoot() {
355
+ if (!this.isLight) {
356
+ const root = super.createRenderRoot();
357
+ if (this.noStyle && root instanceof ShadowRoot) {
358
+ const sheets = [accessible.styleSheet, shadowStructure.styleSheet].filter((sheet) => sheet != null);
359
+ root.adoptedStyleSheets = sheets;
360
+ }
361
+ return root;
362
+ }
363
+ const mount = this.ownerDocument.createElement('div');
364
+ mount.className = 'viewer';
365
+ this.mount = mount;
366
+ this.append(mount);
367
+ return mount;
368
+ }
369
+ connectedCallback() {
370
+ const light = this.isLight;
371
+ super.connectedCallback();
372
+ // A move in the DOM disconnects and reconnects. The disconnect guard must not stick,
373
+ // or Escape would close the dialog and leave the viewer thinking it is still open.
374
+ this.suppressClose = false;
375
+ if (light)
376
+ this.installLight(light);
377
+ this.listen();
378
+ this.slides = this.collect();
379
+ this.watch();
380
+ this.openFromUrl();
381
+ }
382
+ disconnectedCallback() {
383
+ super.disconnectedCallback();
384
+ this.unlisten();
385
+ this.observer?.disconnect();
386
+ this.dialogObserver?.disconnect();
387
+ this.dialogObserver = undefined;
388
+ this.observedDialog = undefined;
389
+ window.clearTimeout(this.resizeTimer);
390
+ this.cancelReveal();
391
+ this.restoreDetails();
392
+ this.pendingBack = false;
393
+ this.queuedOpen = null;
394
+ this.reopenGuard = false;
395
+ // Closing the dialog here fires `close` synchronously. That is not the visitor dismissing
396
+ // it: do not run history.back(), which could leave the page. `open` goes false so a later
397
+ // connect can open again from the URL.
398
+ this.suppressClose = true;
399
+ this.open = false;
400
+ const dialog = this.dialogEl;
401
+ if (dialog?.open)
402
+ dialog.close();
403
+ this.suppressClose = false;
404
+ this.ignoreClose = false;
405
+ }
406
+ willUpdate() {
407
+ if (!this.open) {
408
+ this.lastSrc = '';
409
+ return;
410
+ }
411
+ const src = this.displaySrc;
412
+ if (src !== this.lastSrc) {
413
+ this.lastSrc = src;
414
+ this.status = src ? 'loading' : 'error';
415
+ }
416
+ else if (!src && this.status !== 'error') {
417
+ // A refused URL (javascript:) has an empty src. On the first open `lastSrc` is already
418
+ // empty, so the change above does not run, and the status would stay "loading".
419
+ this.status = 'error';
420
+ }
421
+ }
422
+ updated(changed) {
423
+ super.updated(changed);
424
+ if (changed.has('selector'))
425
+ this.watch();
426
+ if (!this.open) {
427
+ if (changed.has('open'))
428
+ this.restoreDetails();
429
+ return;
430
+ }
431
+ if (changed.has('open') || changed.has('index') || changed.has('slides'))
432
+ this.placeDetails();
433
+ this.bindDialogObserver();
434
+ if (changed.has('open'))
435
+ this.reveal();
436
+ // Reading the image and writing `status` from inside `updated` makes Lit warn and schedule
437
+ // a nested update. A microtask is still before the next paint.
438
+ queueMicrotask(() => {
439
+ if (this.isConnected)
440
+ this.settleImage();
441
+ });
442
+ }
443
+ installLight(light) {
444
+ if (!light)
445
+ return;
446
+ const node = this.getRootNode();
447
+ const root = node instanceof Document || node instanceof ShadowRoot ? node : undefined;
448
+ if (this.noStyle) {
449
+ installLightDomStyles(`${this.localName}::structure`, unsafeCSS(structuralRules(this.localName)), { root, scope: false });
450
+ installLightDomStyles(`${this.localName}::a11y`, accessible, { root });
451
+ return;
452
+ }
453
+ installLightDomStyles(this.localName, this.constructor.elementStyles, {
454
+ root,
455
+ });
456
+ }
457
+ get scope() {
458
+ const root = this.getRootNode();
459
+ return root instanceof Document || root instanceof ShadowRoot ? root : this.ownerDocument;
460
+ }
461
+ get base() {
462
+ return this.ownerDocument.baseURI || location.href;
463
+ }
464
+ loc() {
465
+ return { pathname: location.pathname, search: location.search, hash: location.hash };
466
+ }
467
+ listen() {
468
+ if (this.listened)
469
+ return;
470
+ this.listened = true;
471
+ this.listenRoot = this.scope;
472
+ this.listenRoot.addEventListener('click', this.onClick, true);
473
+ window.addEventListener('popstate', this.onPop);
474
+ window.addEventListener('resize', this.onResize);
475
+ }
476
+ unlisten() {
477
+ if (!this.listened)
478
+ return;
479
+ this.listened = false;
480
+ this.listenRoot?.removeEventListener('click', this.onClick, true);
481
+ this.listenRoot = undefined;
482
+ window.removeEventListener('popstate', this.onPop);
483
+ window.removeEventListener('resize', this.onResize);
484
+ }
485
+ watch() {
486
+ this.observer?.disconnect();
487
+ this.observer = new MutationObserver(() => this.scheduleSync());
488
+ this.observer.observe(this, {
489
+ attributes: true,
490
+ attributeFilter: [
491
+ 'href',
492
+ 'id',
493
+ 'alt',
494
+ 'title',
495
+ 'data-caption',
496
+ 'data-id',
497
+ 'data-details-id',
498
+ 'slot',
499
+ ],
500
+ childList: true,
501
+ subtree: true,
502
+ });
503
+ if (this.selector)
504
+ this.observer.observe(this.scope, { childList: true, subtree: true });
505
+ }
506
+ /** Ignore mutations inside the viewer (the dialog, parked details) so a render cannot loop. */
507
+ scheduleSync() {
508
+ if (this.syncQueued)
509
+ return;
510
+ this.syncQueued = true;
511
+ queueMicrotask(() => {
512
+ this.syncQueued = false;
513
+ if (!this.isConnected)
514
+ return;
515
+ const next = this.collect();
516
+ const same = next.length === this.slides.length &&
517
+ next.every((s, i) => s.el === this.slides[i]?.el &&
518
+ s.href === this.slides[i]?.href &&
519
+ s.id === this.slides[i]?.id &&
520
+ s.caption === this.slides[i]?.caption &&
521
+ s.detailsId === this.slides[i]?.detailsId);
522
+ if (!same) {
523
+ const current = this.current?.el;
524
+ this.slides = next;
525
+ if (this.open) {
526
+ const at = current ? next.findIndex((s) => s.el === current) : -1;
527
+ if (at >= 0)
528
+ this.index = at;
529
+ else if (!next.length)
530
+ this.dismiss('empty');
531
+ else
532
+ this.index = Math.min(this.index, next.length - 1);
533
+ }
534
+ }
535
+ if (!this.open)
536
+ this.openFromUrl();
537
+ });
538
+ }
539
+ collect() {
540
+ const found = [];
541
+ const detailRoots = new Set();
542
+ const noteDetails = (a) => {
543
+ if (!(a instanceof HTMLAnchorElement))
544
+ return;
545
+ const id = a.getAttribute('data-details-id');
546
+ if (!id)
547
+ return;
548
+ const el = this.scope.getElementById(id);
549
+ if (el)
550
+ detailRoots.add(el);
551
+ };
552
+ const candidates = [...this.querySelectorAll('a[href]')];
553
+ if (this.selector) {
554
+ try {
555
+ candidates.push(...this.scope.querySelectorAll(this.selector));
556
+ }
557
+ catch {
558
+ if (!this.warnedSelector) {
559
+ this.warnedSelector = true;
560
+ console.warn(`${this.localName}: selector ${JSON.stringify(this.selector)} is not a valid CSS selector.`);
561
+ }
562
+ }
563
+ }
564
+ for (const a of candidates)
565
+ noteDetails(a);
566
+ const push = (a) => {
567
+ if (!(a instanceof HTMLAnchorElement) || !a.hasAttribute('href'))
568
+ return;
569
+ if (a.closest('[hidden]'))
570
+ return;
571
+ if (this.mount?.contains(a) || a.closest('dialog'))
572
+ return;
573
+ if (a.closest('[slot="details"]'))
574
+ return;
575
+ for (const root of detailRoots) {
576
+ if (root.contains(a))
577
+ return;
578
+ }
579
+ const owner = a.closest('uvalib-lightbox');
580
+ if (owner && owner !== this)
581
+ return;
582
+ found.push(a);
583
+ };
584
+ for (const a of candidates)
585
+ push(a);
586
+ const unique = [...new Set(found)];
587
+ unique.sort((a, b) => {
588
+ if (a === b)
589
+ return 0;
590
+ const pos = a.compareDocumentPosition(b);
591
+ if (pos & Node.DOCUMENT_POSITION_FOLLOWING)
592
+ return -1;
593
+ if (pos & Node.DOCUMENT_POSITION_PRECEDING)
594
+ return 1;
595
+ return 0;
596
+ });
597
+ return unique.map((el, i) => this.slide(el, i));
598
+ }
599
+ slide(el, index) {
600
+ const href = el.getAttribute('href') ?? '';
601
+ const img = el.querySelector('img');
602
+ const alt = normalize(img?.getAttribute('data-uvalib-alt') || img?.getAttribute('alt'));
603
+ const text = this.linkText(el);
604
+ const caption = normalize(el.dataset.caption) ||
605
+ alt ||
606
+ text ||
607
+ normalize(el.getAttribute('title')) ||
608
+ 'Image';
609
+ const id = normalize(el.dataset.id) || el.id || iiifIdentifier(href, this.base) || String(index + 1);
610
+ return {
611
+ el,
612
+ id,
613
+ href,
614
+ caption,
615
+ alt: alt || caption,
616
+ detailsId: normalize(el.dataset.detailsId) || null,
617
+ };
618
+ }
619
+ /** The link's own text, not the alt of its image and not a details subtree. */
620
+ linkText(el) {
621
+ const parts = [];
622
+ const walk = (n) => {
623
+ if (n instanceof HTMLImageElement)
624
+ return;
625
+ if (n instanceof HTMLElement && (n.hidden || n.getAttribute('slot') === 'details'))
626
+ return;
627
+ if (n.nodeType === Node.TEXT_NODE) {
628
+ parts.push(n.textContent ?? '');
629
+ return;
630
+ }
631
+ n.childNodes.forEach(walk);
632
+ };
633
+ el.childNodes.forEach(walk);
634
+ return normalize(parts.join(' '));
635
+ }
636
+ get current() {
637
+ return this.slides[this.index];
638
+ }
639
+ get multiple() {
640
+ return this.slides.length > 1;
641
+ }
642
+ /** The URL the `<img>` should load: a viewport-sized IIIF request, or the href itself. */
643
+ get displaySrc() {
644
+ const href = this.current?.href ?? '';
645
+ if (!usableImageUrl(href, this.base))
646
+ return '';
647
+ const w = this.fitW || viewportIiifSize(window).w;
648
+ const h = this.fitH || viewportIiifSize(window).h;
649
+ return sizedIiifUrl(href, this.base, w, h) ?? href;
650
+ }
651
+ measure() {
652
+ const { w, h } = viewportIiifSize(window);
653
+ if (w !== this.fitW)
654
+ this.fitW = w;
655
+ if (h !== this.fitH)
656
+ this.fitH = h;
657
+ }
658
+ linkFrom(e) {
659
+ for (const n of e.composedPath()) {
660
+ if (n instanceof HTMLAnchorElement)
661
+ return n;
662
+ }
663
+ const t = e.target;
664
+ return t instanceof Element ? t.closest('a[href]') : null;
665
+ }
666
+ /** A visitor chose `slides[i]`. The first open pushes a history entry; a later choice replaces it. */
667
+ activate(i, slides) {
668
+ const already = this.open;
669
+ if (already && this.index === i)
670
+ return;
671
+ this.slides = slides;
672
+ this.index = i;
673
+ this.trigger = slides[i]?.el ?? null;
674
+ this.userClosed = false;
675
+ this.measure();
676
+ this.open = true;
677
+ if (this.pendingBack) {
678
+ // Close already asked for history.back(). Writing now would push on the entry that
679
+ // popstate is about to remove, and that pop would shut the viewer again.
680
+ this.queuedOpen = { i, slides };
681
+ }
682
+ else if (this.urlParam) {
683
+ const id = slides[i]?.id ?? null;
684
+ const hashMode = usesHashParam(location.hash);
685
+ if (already)
686
+ this.writeParam(id, 'replace', hashMode);
687
+ else {
688
+ // The address can already carry this param (a leftover entry). Replace it away
689
+ // on the current entry, then push, so Back still closes onto this page. Both
690
+ // writes share hashMode: stripping `?photo` from `#chapter?photo=1` leaves
691
+ // `#chapter`, which would otherwise fall out of the hash.
692
+ if (paramFromLocation(this.loc(), this.urlParam))
693
+ this.writeParam(null, 'replace', hashMode);
694
+ this.historyPushed = this.writeParam(id, 'push', hashMode);
695
+ }
696
+ }
697
+ trackEvent(this, ['uvalib-lightbox', 'open', `image ${i + 1}`, i + 1]);
698
+ }
699
+ /** Restore a viewer the URL already points at. No analytics: it is not a new action. */
700
+ openFromUrl() {
701
+ if (this.open || this.userClosed || !this.urlParam)
702
+ return;
703
+ const id = paramFromLocation(this.loc(), this.urlParam);
704
+ if (!id)
705
+ return;
706
+ const slides = this.slides.length ? this.slides : this.collect();
707
+ const i = slides.findIndex((s) => s.id === id);
708
+ if (i < 0)
709
+ return;
710
+ this.slides = slides;
711
+ this.index = i;
712
+ this.trigger = slides[i]?.el ?? null;
713
+ this.measure();
714
+ // Make Back close the viewer onto this page instead of leaving it. Remember the hash
715
+ // route before the first write removes the `?` that marked it.
716
+ const hashMode = usesHashParam(location.hash);
717
+ this.writeParam(null, 'replace', hashMode);
718
+ this.historyPushed = this.writeParam(id, 'push', hashMode);
719
+ this.open = true;
720
+ }
721
+ step(delta) {
722
+ const n = this.slides.length;
723
+ if (n < 2)
724
+ return;
725
+ const i = (this.index + delta + n) % n;
726
+ this.index = i;
727
+ this.trigger = this.slides[i]?.el ?? this.trigger;
728
+ if (this.urlParam)
729
+ this.writeParam(this.slides[i]?.id ?? null, 'replace');
730
+ const action = delta < 0 ? 'previous' : 'next';
731
+ trackEvent(this, ['uvalib-lightbox', action, `image ${i + 1}`, i + 1]);
732
+ }
733
+ /**
734
+ * Close. `ui` is Close, Escape or the backdrop (Back when this viewer pushed, else drop the
735
+ * param). `pop` is the browser already moving: the URL is already right. `empty` is the
736
+ * last image leaving the page: drop the param and do not reopen from it.
737
+ */
738
+ dismiss(source) {
739
+ if (this.dismissing)
740
+ return;
741
+ const dialog = this.dialogEl;
742
+ if (!this.open && !dialog?.open)
743
+ return;
744
+ this.dismissing = true;
745
+ try {
746
+ const pushed = this.historyPushed;
747
+ this.historyPushed = false;
748
+ this.ignoreClose = true;
749
+ this.cancelReveal();
750
+ this.open = false;
751
+ if (source === 'ui' || source === 'empty')
752
+ this.userClosed = true;
753
+ this.restoreDetails();
754
+ if (dialog?.open)
755
+ dialog.close();
756
+ this.focusTrigger();
757
+ if (source === 'ui' && this.urlParam) {
758
+ if (pushed) {
759
+ this.backInHash = usesHashParam(location.hash);
760
+ this.pendingBack = true;
761
+ history.back();
762
+ }
763
+ else
764
+ this.writeParam(null, 'replace', usesHashParam(location.hash));
765
+ }
766
+ else if (source === 'empty' && this.urlParam) {
767
+ this.pendingBack = false;
768
+ this.queuedOpen = null;
769
+ this.writeParam(null, 'replace', usesHashParam(location.hash));
770
+ }
771
+ }
772
+ finally {
773
+ // Leave `ignoreClose` set. `dialog.close()` can queue a second close event for the
774
+ // next task; clearing the guard here lets that event dismiss a viewer that has
775
+ // already been opened again. `reveal` clears it once `showModal` has succeeded, which
776
+ // is still before a later Escape. A throw while restoring details must not leave the
777
+ // viewer stuck dismissing.
778
+ this.dismissing = false;
779
+ }
780
+ }
781
+ /** Returns false when this document cannot change the address (for example an iframe srcdoc). */
782
+ writeParam(id, mode, hashMode) {
783
+ if (!this.urlParam)
784
+ return false;
785
+ const next = hrefWithParam(this.loc(), this.urlParam, id, hashMode);
786
+ const now = location.pathname + location.search + location.hash;
787
+ if (next === now)
788
+ return false;
789
+ const prev = history.state && typeof history.state === 'object' ? history.state : {};
790
+ try {
791
+ history[mode === 'push' ? 'pushState' : 'replaceState']({ ...prev, uvalibLightbox: this.urlParam }, '', next);
792
+ return true;
793
+ }
794
+ catch {
795
+ // A sandboxed document (an iframe srcdoc) cannot take a history entry. The viewer
796
+ // still opens; the address is left as it is.
797
+ return false;
798
+ }
799
+ }
800
+ /** Ignore the close event a history traversal fires on the dialog it just opened. */
801
+ armReopenGuard() {
802
+ this.reopenGuard = true;
803
+ const view = this.ownerDocument.defaultView;
804
+ if (!view)
805
+ return;
806
+ view.requestAnimationFrame(() => {
807
+ this.reopenGuard = false;
808
+ });
809
+ }
810
+ typing(e) {
811
+ const t = e.target;
812
+ if (!(t instanceof HTMLElement))
813
+ return false;
814
+ return t.isContentEditable || !!t.closest('input, textarea, select');
815
+ }
816
+ /** The dialog element is stable across renders. Watch `open` on whichever node is current. */
817
+ bindDialogObserver() {
818
+ const dialog = this.dialogEl;
819
+ if (!dialog || dialog === this.observedDialog)
820
+ return;
821
+ this.dialogObserver?.disconnect();
822
+ this.observedDialog = dialog;
823
+ const observer = new MutationObserver(() => this.onNativeClose());
824
+ observer.observe(dialog, { attributes: true, attributeFilter: ['open'] });
825
+ this.dialogObserver = observer;
826
+ }
827
+ /**
828
+ * Escape, the backdrop's close, and a direct `dialog.close()` all clear `open`. The
829
+ * event itself is a later task; the attribute change is observable on the microtask
830
+ * that `await updateComplete` flushes. A close that leaves the dialog open (a queued
831
+ * event after `showModal`, or a history traversal) is not the visitor dismissing it.
832
+ */
833
+ onNativeClose() {
834
+ if (this.ignoreClose || this.suppressClose || !this.open)
835
+ return;
836
+ if (this.dialogEl?.open)
837
+ return;
838
+ if (this.reopenGuard) {
839
+ const view = this.ownerDocument.defaultView;
840
+ view?.queueMicrotask(() => {
841
+ if (this.open && this.isConnected && !this.dialogEl?.open)
842
+ this.reveal();
843
+ });
844
+ return;
845
+ }
846
+ this.dismiss('ui');
847
+ }
848
+ get dialogEl() {
849
+ return this.renderRoot.querySelector('dialog');
850
+ }
851
+ get closeBtn() {
852
+ return this.renderRoot.querySelector('button.close');
853
+ }
854
+ get detailsBox() {
855
+ return this.renderRoot.querySelector('.details');
856
+ }
857
+ focusTrigger() {
858
+ const trigger = this.trigger;
859
+ if (trigger?.isConnected)
860
+ trigger.focus();
861
+ }
862
+ /**
863
+ * Open the dialog and focus Close. `showModal` can throw while a dialog is still leaving
864
+ * the top layer (a reopen from Back/Forward, or the first paint), so try again on the next
865
+ * frames. Those frames are queued before a test's own `nextFrame`.
866
+ */
867
+ /** History restore can drop the light-DOM mount or orphan a shadow root. Put it back. */
868
+ ensureRoot() {
869
+ const root = this.renderRoot;
870
+ if (!this.isConnected)
871
+ return;
872
+ if (root instanceof HTMLElement && !root.isConnected)
873
+ this.append(root);
874
+ if (root instanceof ShadowRoot && this.shadowRoot && root !== this.shadowRoot) {
875
+ this.renderRoot = this.shadowRoot;
876
+ this.requestUpdate();
877
+ }
878
+ }
879
+ cancelReveal() {
880
+ const view = this.ownerDocument.defaultView;
881
+ if (this.revealFrame && view)
882
+ view.cancelAnimationFrame(this.revealFrame);
883
+ this.revealFrame = 0;
884
+ }
885
+ reveal() {
886
+ this.ensureRoot();
887
+ const dialog = this.dialogEl;
888
+ if (!this.open || !dialog)
889
+ return;
890
+ const again = () => {
891
+ if (this.revealTries++ > 8) {
892
+ this.ignoreClose = false;
893
+ return;
894
+ }
895
+ const view = this.ownerDocument.defaultView;
896
+ if (!view)
897
+ return;
898
+ this.revealFrame = view.requestAnimationFrame(() => {
899
+ this.revealFrame = 0;
900
+ if (this.open)
901
+ this.reveal();
902
+ });
903
+ };
904
+ if (!dialog.isConnected) {
905
+ again();
906
+ return;
907
+ }
908
+ if (!dialog.open) {
909
+ try {
910
+ dialog.showModal();
911
+ }
912
+ catch {
913
+ again();
914
+ return;
915
+ }
916
+ }
917
+ this.revealTries = 0;
918
+ this.cancelReveal();
919
+ // A close event already queued by the dialog we shut must not dismiss this open.
920
+ // Clearing the guard here is synchronous, so Escape after this update still closes.
921
+ this.ignoreClose = false;
922
+ // A later reveal (a retry, or the history traversal showing the dialog again) must
923
+ // not pull focus back to Close after the visitor has moved it inside the dialog.
924
+ const active = this.shadowRoot?.activeElement ?? this.ownerDocument.activeElement;
925
+ if (!active || !dialog.contains(active))
926
+ this.closeBtn?.focus();
927
+ }
928
+ /** A cached image may already be complete before the load listener runs. */
929
+ settleImage() {
930
+ const img = this.renderRoot.querySelector('img');
931
+ if (!img || this.status !== 'loading')
932
+ return;
933
+ if (img.complete && img.naturalWidth > 0)
934
+ this.status = 'ready';
935
+ else if (img.complete && img.getAttribute('src') && img.naturalWidth === 0)
936
+ this.status = 'error';
937
+ }
938
+ detailToken(el) {
939
+ let n = this.detailTokens.get(el);
940
+ if (n === undefined)
941
+ this.detailTokens.set(el, (n = ++this.detailTokenSeq));
942
+ return String(n);
943
+ }
944
+ /**
945
+ * Details for the open image: `slot="details"` children (honouring `data-for`) and the
946
+ * element named by `data-details-id`. Elements already parked in the dialog stay in the
947
+ * set, so this does not move them again.
948
+ */
949
+ matchingDetails() {
950
+ const slide = this.current;
951
+ const id = slide?.id ?? '';
952
+ const position = String(this.index + 1);
953
+ const pool = [];
954
+ const add = (el) => {
955
+ if (el && el !== this && !pool.includes(el))
956
+ pool.push(el);
957
+ };
958
+ for (const el of this.querySelectorAll(':scope > [slot="details"]')) {
959
+ if (el instanceof HTMLElement)
960
+ add(el);
961
+ }
962
+ for (const item of this.parked)
963
+ add(item.el);
964
+ if (slide?.detailsId)
965
+ add(this.scope.getElementById(slide.detailsId));
966
+ return pool.filter((el) => {
967
+ if (el.getAttribute('slot') === 'details') {
968
+ const f = el.getAttribute('data-for');
969
+ return !f || f === id || f === position;
970
+ }
971
+ return !!slide?.detailsId && el.id === slide.detailsId;
972
+ });
973
+ }
974
+ placeDetails() {
975
+ const want = this.matchingDetails();
976
+ const key = `${want.map((el) => this.detailToken(el)).join('|')}@${this.index}`;
977
+ if (key === this.parkedKey)
978
+ return;
979
+ const keep = new Set(want);
980
+ const still = [];
981
+ for (const item of this.parked) {
982
+ if (keep.has(item.el) && this.detailsBox?.contains(item.el))
983
+ still.push(item);
984
+ else
985
+ this.putBack(item);
986
+ }
987
+ this.parked = still;
988
+ const box = this.detailsBox;
989
+ if (box) {
990
+ for (const el of want) {
991
+ if (box.contains(el))
992
+ continue;
993
+ const parent = el.parentNode;
994
+ if (!parent)
995
+ continue;
996
+ this.parked.push({ el, parent, next: el.nextSibling, hidden: el.hidden });
997
+ el.hidden = false;
998
+ box.append(el);
999
+ }
1000
+ }
1001
+ this.parkedKey = key;
1002
+ }
1003
+ /**
1004
+ * Put a parked element back. Restoring later siblings first keeps `next` inside `parent`;
1005
+ * if it is not (it was parked too), insert at the end instead of throwing NotFoundError.
1006
+ */
1007
+ putBack(item) {
1008
+ item.el.hidden = item.hidden;
1009
+ const next = item.next?.parentNode === item.parent ? item.next : null;
1010
+ item.parent.insertBefore(item.el, next);
1011
+ }
1012
+ restoreDetails() {
1013
+ for (let i = this.parked.length - 1; i >= 0; i--)
1014
+ this.putBack(this.parked[i]);
1015
+ this.parked = [];
1016
+ this.parkedKey = '';
1017
+ }
1018
+ get statusText() {
1019
+ if (!this.open)
1020
+ return '';
1021
+ if (this.status === 'loading')
1022
+ return 'Loading image.';
1023
+ if (this.status === 'error')
1024
+ return 'This image could not be loaded.';
1025
+ return '';
1026
+ }
1027
+ render() {
1028
+ const slide = this.open ? this.current : undefined;
1029
+ const src = slide ? this.displaySrc : '';
1030
+ const full = slide && usableImageUrl(slide.href, this.base) ? slide.href : '';
1031
+ const shown = this.statusText;
1032
+ const counter = this.multiple
1033
+ ? html `<p class="count" aria-live="polite">${this.index + 1} of ${this.slides.length}</p>`
1034
+ : nothing;
1035
+ // Shadow mode needs a slot or the author's links are not rendered and cannot take focus.
1036
+ // Light mode renders into `.viewer`; a `<slot>` there would not show the siblings.
1037
+ return html `
1038
+ ${this.isLight ? nothing : html `<slot></slot><slot name="details" hidden></slot>`}
1039
+ <dialog
1040
+ part="dialog"
1041
+ aria-labelledby=${this.captionId}
1042
+ @click=${this.onDialogClick}
1043
+ @close=${this.onDialogClose}
1044
+ @keydown=${this.onKey}
1045
+ >
1046
+ <div class="panel" part="panel">
1047
+ <div class="head">
1048
+ <p class="caption" part="caption" id=${this.captionId}>${slide?.caption ?? 'Image'}</p>
1049
+ <button type="button" class="close" autofocus @click=${() => this.dismiss('ui')}>
1050
+ Close
1051
+ </button>
1052
+ </div>
1053
+ <figure class="figure">
1054
+ ${src
1055
+ ? html `<img
1056
+ part="image"
1057
+ alt=${slide?.alt ?? ''}
1058
+ src=${src}
1059
+ aria-busy=${this.status === 'loading' ? 'true' : 'false'}
1060
+ @load=${this.onLoad}
1061
+ @error=${this.onError}
1062
+ />`
1063
+ : nothing}
1064
+ </figure>
1065
+ <p class="status ${shown ? '' : 'visually-hidden'}" role="status">${shown}</p>
1066
+ <div class="toolbar" part="controls">
1067
+ ${this.multiple
1068
+ ? html `<button type="button" class="prev" @click=${() => this.step(-1)}>
1069
+ Previous
1070
+ </button>
1071
+ <button type="button" class="next" @click=${() => this.step(1)}>Next</button>
1072
+ ${counter}`
1073
+ : nothing}
1074
+ ${full
1075
+ ? html `<a
1076
+ class="full"
1077
+ href=${full}
1078
+ target="_blank"
1079
+ rel="noopener noreferrer"
1080
+ @click=${this.onFullSize}
1081
+ >Full size<span class="visually-hidden"> (opens in a new tab)</span></a
1082
+ >`
1083
+ : nothing}
1084
+ </div>
1085
+ <div class="details" part="details"></div>
1086
+ </div>
1087
+ </dialog>
1088
+ `;
1089
+ }
1090
+ }
1091
+ __decorate([
1092
+ property({ type: String })
1093
+ ], UvalibLightbox.prototype, "selector", void 0);
1094
+ __decorate([
1095
+ property({ attribute: 'url-param' })
1096
+ ], UvalibLightbox.prototype, "urlParam", void 0);
1097
+ __decorate([
1098
+ state()
1099
+ ], UvalibLightbox.prototype, "slides", void 0);
1100
+ __decorate([
1101
+ state()
1102
+ ], UvalibLightbox.prototype, "index", void 0);
1103
+ __decorate([
1104
+ state()
1105
+ ], UvalibLightbox.prototype, "open", void 0);
1106
+ __decorate([
1107
+ state()
1108
+ ], UvalibLightbox.prototype, "status", void 0);
1109
+ __decorate([
1110
+ state()
1111
+ ], UvalibLightbox.prototype, "fitW", void 0);
1112
+ __decorate([
1113
+ state()
1114
+ ], UvalibLightbox.prototype, "fitH", void 0);
1115
+ //# sourceMappingURL=UvalibLightbox.js.map