|
| 1 | +// Small badge shown while a rebuild is in progress. It lives in a shadow root |
| 2 | +// so page styles cannot affect it; styles go through the CSSOM and SVG |
| 3 | +// presentation attributes, which a strict `style-src` CSP allows. |
| 4 | + |
| 5 | +import theme from "./theme.js"; |
| 6 | + |
| 7 | +const INDICATOR_ID = "webpack-dev-middleware-building-indicator"; |
| 8 | +const SVG_NS = "http://www.w3.org/2000/svg"; |
| 9 | +// Circumference of the progress ring (r = 6). |
| 10 | +const RING_LENGTH = 2 * Math.PI * 6; |
| 11 | + |
| 12 | +/** @type {HTMLElement | null} */ |
| 13 | +let host = null; |
| 14 | +/** @type {HTMLElement | null} */ |
| 15 | +let label = null; |
| 16 | +/** @type {HTMLElement | null} */ |
| 17 | +let dot = null; |
| 18 | +/** @type {SVGSVGElement | null} */ |
| 19 | +let ring = null; |
| 20 | +/** @type {SVGCircleElement | null} */ |
| 21 | +let ringValue = null; |
| 22 | + |
| 23 | +// eslint-disable-next-line jsdoc/reject-any-type |
| 24 | +/** @typedef {any} EXPECTED_ANY */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {EXPECTED_ANY} element element |
| 28 | + * @param {Record<string, string | number>} style style map |
| 29 | + */ |
| 30 | +function applyStyle(element, style) { |
| 31 | + for (const key of Object.keys(style)) { |
| 32 | + element.style[key] = style[key]; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Create (or reuse) the indicator host element. |
| 38 | + */ |
| 39 | +function ensureIndicator() { |
| 40 | + if (host && host.parentNode) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (!document.body) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + host = document.createElement("div"); |
| 49 | + host.id = INDICATOR_ID; |
| 50 | + applyStyle(host, { |
| 51 | + position: "fixed", |
| 52 | + right: "16px", |
| 53 | + bottom: "16px", |
| 54 | + zIndex: 9999, |
| 55 | + pointerEvents: "none", |
| 56 | + }); |
| 57 | + |
| 58 | + const root = host.attachShadow({ mode: "open" }); |
| 59 | + |
| 60 | + const badge = document.createElement("div"); |
| 61 | + applyStyle(badge, { |
| 62 | + display: "flex", |
| 63 | + alignItems: "center", |
| 64 | + gap: "8px", |
| 65 | + background: theme.panelTranslucent, |
| 66 | + color: theme.text, |
| 67 | + fontFamily: "Menlo, Consolas, 'Courier New', monospace", |
| 68 | + fontSize: "12px", |
| 69 | + padding: "6px 12px", |
| 70 | + borderRadius: "16px", |
| 71 | + boxShadow: "0 2px 12px rgba(0,0,0,0.35)", |
| 72 | + }); |
| 73 | + |
| 74 | + // Indeterminate mode: a pulsing dot. |
| 75 | + dot = document.createElement("span"); |
| 76 | + applyStyle(dot, { |
| 77 | + width: "8px", |
| 78 | + height: "8px", |
| 79 | + borderRadius: "50%", |
| 80 | + background: theme.accent, |
| 81 | + }); |
| 82 | + |
| 83 | + // Pulse through the Web Animations API — no <style> element involved. |
| 84 | + if (typeof dot.animate === "function") { |
| 85 | + dot.animate([{ opacity: 1 }, { opacity: 0.2 }, { opacity: 1 }], { |
| 86 | + duration: 1000, |
| 87 | + iterations: Number.POSITIVE_INFINITY, |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // Determinate mode: a progress ring drawn with SVG presentation attributes. |
| 92 | + ring = document.createElementNS(SVG_NS, "svg"); |
| 93 | + ring.setAttribute("viewBox", "0 0 16 16"); |
| 94 | + applyStyle(ring, { width: "14px", height: "14px", display: "none" }); |
| 95 | + |
| 96 | + const track = document.createElementNS(SVG_NS, "circle"); |
| 97 | + track.setAttribute("cx", "8"); |
| 98 | + track.setAttribute("cy", "8"); |
| 99 | + track.setAttribute("r", "6"); |
| 100 | + track.setAttribute("fill", "none"); |
| 101 | + track.setAttribute("stroke", "rgba(255,255,255,0.25)"); |
| 102 | + track.setAttribute("stroke-width", "2.5"); |
| 103 | + |
| 104 | + ringValue = document.createElementNS(SVG_NS, "circle"); |
| 105 | + ringValue.setAttribute("cx", "8"); |
| 106 | + ringValue.setAttribute("cy", "8"); |
| 107 | + ringValue.setAttribute("r", "6"); |
| 108 | + ringValue.setAttribute("fill", "none"); |
| 109 | + ringValue.setAttribute("stroke", theme.accent); |
| 110 | + ringValue.setAttribute("stroke-width", "2.5"); |
| 111 | + ringValue.setAttribute("stroke-linecap", "round"); |
| 112 | + ringValue.setAttribute("stroke-dasharray", String(RING_LENGTH)); |
| 113 | + ringValue.setAttribute("stroke-dashoffset", String(RING_LENGTH)); |
| 114 | + // Start the ring at 12 o'clock. |
| 115 | + ringValue.setAttribute("transform", "rotate(-90 8 8)"); |
| 116 | + |
| 117 | + ring.append(track, ringValue); |
| 118 | + |
| 119 | + label = document.createElement("span"); |
| 120 | + badge.append(dot, ring, label); |
| 121 | + root.append(badge); |
| 122 | + document.body.append(host); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Show the indicator (idempotent). With a percent the badge renders a |
| 127 | + * progress ring; without one it renders a pulsing dot. |
| 128 | + * @param {string=} text label text |
| 129 | + * @param {number=} percent compilation progress (0-100) |
| 130 | + */ |
| 131 | +export function show(text, percent) { |
| 132 | + ensureIndicator(); |
| 133 | + |
| 134 | + if (!label) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + label.textContent = text || "Rebuilding…"; |
| 139 | + |
| 140 | + const determinate = typeof percent === "number"; |
| 141 | + |
| 142 | + /** @type {HTMLElement} */ (dot).style.display = determinate |
| 143 | + ? "none" |
| 144 | + : "inline-block"; |
| 145 | + /** @type {SVGSVGElement} */ (ring).style.display = determinate |
| 146 | + ? "block" |
| 147 | + : "none"; |
| 148 | + |
| 149 | + if (determinate) { |
| 150 | + const clamped = Math.min(100, Math.max(0, percent)); |
| 151 | + |
| 152 | + /** @type {SVGCircleElement} */ (ringValue).setAttribute( |
| 153 | + "stroke-dashoffset", |
| 154 | + String(RING_LENGTH * (1 - clamped / 100)), |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Remove the indicator from the page. |
| 161 | + */ |
| 162 | +export function hide() { |
| 163 | + if (host && host.parentNode) { |
| 164 | + host.remove(); |
| 165 | + } |
| 166 | + |
| 167 | + host = null; |
| 168 | + label = null; |
| 169 | + dot = null; |
| 170 | + ring = null; |
| 171 | + ringValue = null; |
| 172 | +} |
0 commit comments