|
1 | 1 | --- |
2 | | -// Click-to-zoom for Mermaid diagrams. Diagrams render inside the ~46–58rem |
3 | | -// content column, so wide ones (and dense ones) read small inline; clicking |
4 | | -// any diagram opens it in a lightbox — small diagrams scale up to fill the |
5 | | -// viewport, wide ones show at natural size with pan-scroll for full detail. |
| 2 | +// Click-to-zoom for Mermaid diagrams. Dense diagrams read small inline; |
| 3 | +// clicking any diagram opens it in a lightbox that always FITS the viewport |
| 4 | +// (small diagrams scale up, capped 2.5×; oversized ones scale down — never |
| 5 | +// scrollbars by default). Diagrams larger than the stage also get a 1:1 |
| 6 | +// toggle: natural size with pan + edge fades, for full label detail. |
6 | 7 | // |
7 | 8 | // Rendered once (from Footer.astro, which is on every page). The script uses |
8 | 9 | // event delegation on `document` so it survives Astro view-transition |
|
42 | 43 | overlay.setAttribute("aria-modal", "true"); |
43 | 44 | overlay.setAttribute("aria-label", "Enlarged diagram"); |
44 | 45 | overlay.innerHTML = |
| 46 | + '<button class="wh-zoom__one" type="button" hidden aria-pressed="false" ' + |
| 47 | + 'aria-label="Toggle natural size (pan to explore)">1:1</button>' + |
45 | 48 | '<button class="wh-zoom__close" type="button" aria-label="Close enlarged diagram">' + |
46 | 49 | '<svg viewBox="0 0 24 24" width="22" height="22" aria-hidden="true" fill="none" ' + |
47 | 50 | 'stroke="currentColor" stroke-width="2" stroke-linecap="round">' + |
48 | 51 | '<path d="M6 6l12 12M18 6L6 18"/></svg></button>' + |
49 | 52 | '<div class="wh-zoom__stage"></div>'; |
50 | 53 | overlay.addEventListener("click", (e) => { |
| 54 | + if (e.target.closest(".wh-zoom__one")) { |
| 55 | + const btn = e.target.closest(".wh-zoom__one"); |
| 56 | + if (overlay.__whSetMode) overlay.__whSetMode(btn.getAttribute("aria-pressed") !== "true"); |
| 57 | + return; |
| 58 | + } |
51 | 59 | // Backdrop / stage padding click closes; clicks on the SVG itself don't. |
52 | 60 | if (!e.target.closest("svg") || e.target.closest(".wh-zoom__close")) close(); |
53 | 61 | }); |
|
88 | 96 | (window.innerWidth * 0.94 - padX) / natW, |
89 | 97 | (window.innerHeight * 0.9 - padY) / natH, |
90 | 98 | ); |
91 | | - // Enlarge diagrams smaller than the viewport (cap 2.5×); show larger |
92 | | - // ones at natural size and let the stage scroll for pan. Floor the |
93 | | - // pixel sizes so fractional rounding can't tip a perfect fit into a |
94 | | - // 1px overflow (which renders as phantom scrollbars). |
95 | | - const scale = fit > 1 ? Math.min(fit, 2.5) : 1; |
96 | | - clone.style.width = Math.floor(natW * scale) + "px"; |
97 | | - clone.style.height = Math.floor(natH * scale) + "px"; |
| 99 | + // The modal ALWAYS fits the diagram to the stage — scaling UP small |
| 100 | + // diagrams (cap 2.5×) and DOWN oversized ones — so the default view |
| 101 | + // never grows scrollbars (which also squared the sheet's rounded |
| 102 | + // corners). Diagrams larger than the stage get a 1:1 toggle instead: |
| 103 | + // natural size + pan, with the ScrollHints edge fades as the wayfinding |
| 104 | + // (pinch-zoom also works natively on touch). Floor the pixel sizes so |
| 105 | + // fractional rounding can't tip a perfect fit into a 1px overflow. |
| 106 | + const fitScale = Math.min(fit, 2.5); |
| 107 | + const sizeTo = (scale) => { |
| 108 | + clone.style.width = Math.floor(natW * scale) + "px"; |
| 109 | + clone.style.height = Math.floor(natH * scale) + "px"; |
| 110 | + }; |
| 111 | + sizeTo(fitScale); |
98 | 112 | clone.style.maxWidth = "none"; |
99 | 113 | clone.style.minWidth = "0"; |
100 | 114 | stage.appendChild(clone); |
101 | 115 |
|
| 116 | + const oneBtn = overlay.querySelector(".wh-zoom__one"); |
| 117 | + oneBtn.hidden = !(fit < 0.98); // 1:1 only buys anything when fit shrank it |
| 118 | + oneBtn.setAttribute("aria-pressed", "false"); |
| 119 | + overlay.__whSetMode = (natural) => { |
| 120 | + sizeTo(natural ? 1 : fitScale); |
| 121 | + oneBtn.setAttribute("aria-pressed", String(natural)); |
| 122 | + stage.scrollLeft = 0; |
| 123 | + stage.scrollTop = 0; |
| 124 | + // Stamp the edge fades for the new size (ScrollHints' capture-phase |
| 125 | + // scroll listener keeps them updated as the user pans). |
| 126 | + requestAnimationFrame(() => { |
| 127 | + stage.toggleAttribute( |
| 128 | + "data-wh-more", |
| 129 | + stage.scrollWidth - stage.clientWidth - stage.scrollLeft > 8, |
| 130 | + ); |
| 131 | + stage.toggleAttribute("data-wh-more-start", false); |
| 132 | + }); |
| 133 | + }; |
| 134 | + |
102 | 135 | overlay.removeAttribute("hidden"); |
103 | | - // Same right-edge fade contract as the inline scrollers (ScrollHints |
104 | | - // owns the [data-wh-more] CSS + the capture-phase scroll listener that |
105 | | - // clears/re-stamps it as the user pans) — without it the pannable stage |
106 | | - // hard-clipped nodes at its edge with no hint that it scrolls. |
107 | | - requestAnimationFrame(() => { |
108 | | - const more = stage.scrollWidth - stage.clientWidth - stage.scrollLeft > 3; |
109 | | - stage.toggleAttribute("data-wh-more", more); |
110 | | - }); |
111 | 136 | // Next frame so the un-hide paints before the open transition. |
112 | 137 | requestAnimationFrame(() => overlay.classList.add("is-open")); |
113 | 138 | document.documentElement.style.overflow = "hidden"; |
|
205 | 230 | .wh-zoom__stage > svg { |
206 | 231 | display: block; |
207 | 232 | } |
208 | | - .wh-zoom__close { |
| 233 | + .wh-zoom__close, |
| 234 | + .wh-zoom__one { |
209 | 235 | position: fixed; |
210 | 236 | top: clamp(0.5rem, 2vw, 1.25rem); |
211 | 237 | right: clamp(0.5rem, 2vw, 1.25rem); |
|
218 | 244 | background: var(--wh-surface, #14171c); |
219 | 245 | color: var(--wh-ink, #f1f3f7); |
220 | 246 | cursor: pointer; |
221 | | - transition: background 0.12s ease, transform 0.12s ease; |
| 247 | + transition: background 0.12s ease, transform 0.12s ease, |
| 248 | + border-color 0.12s ease; |
| 249 | + } |
| 250 | + .wh-zoom__one { |
| 251 | + right: calc(clamp(0.5rem, 2vw, 1.25rem) + 3.1rem); |
| 252 | + font-family: var(--sl-font-mono); |
| 253 | + font-size: 0.75rem; |
| 254 | + font-weight: 650; |
| 255 | + letter-spacing: 0.02em; |
| 256 | + } |
| 257 | + .wh-zoom__one[hidden] { |
| 258 | + display: none; |
| 259 | + } |
| 260 | + .wh-zoom__one[aria-pressed="true"] { |
| 261 | + border-color: var(--wh-accent, #06b0bf); |
| 262 | + color: var(--wh-accent-text, var(--wh-accent-hi, #1dc9d8)); |
222 | 263 | } |
223 | | - .wh-zoom__close:hover { |
| 264 | + .wh-zoom__close:hover, |
| 265 | + .wh-zoom__one:hover { |
224 | 266 | background: var(--wh-surface-2, #1b1f26); |
225 | 267 | transform: scale(1.05); |
226 | 268 | } |
227 | | - .wh-zoom__close:focus-visible { |
| 269 | + .wh-zoom__close:focus-visible, |
| 270 | + .wh-zoom__one:focus-visible { |
228 | 271 | outline: 2px solid var(--wh-accent-text, var(--wh-accent-hi)); |
229 | 272 | outline-offset: 2px; |
230 | 273 | } |
|
0 commit comments