diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f05188..968562e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,12 +17,13 @@ jobs: node-version: "20" cache: npm - run: npm ci + - run: npm test - run: npm run typecheck - run: npm run build python: name: Python tests - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 strategy: matrix: python-version: ["3.9", "3.12"] diff --git a/src/manywidgets/stat/doc.md b/src/manywidgets/stat/doc.md index 6813159..37a2f4f 100644 --- a/src/manywidgets/stat/doc.md +++ b/src/manywidgets/stat/doc.md @@ -1,6 +1,8 @@ # Stat A compact metric card: label, value, unit, and an optional signed delta. +Long values shrink to fit the card, and the unit wraps below the value when +there isn't room beside it. ## Import diff --git a/src/manywidgets/stat/src/index.ts b/src/manywidgets/stat/src/index.ts index 0f1ae77..96504ee 100644 --- a/src/manywidgets/stat/src/index.ts +++ b/src/manywidgets/stat/src/index.ts @@ -34,11 +34,25 @@ function render({ model, el }: RenderProps): () => void { card.appendChild(deltaEl); el.appendChild(card); + // Shrink the value's font so a long number fits the card instead of being + // clipped; the unit wraps below it via CSS. + function fit(): void { + valueEl.style.fontSize = ""; + const avail = valueRow.clientWidth; + const natural = valueEl.getBoundingClientRect().width; + if (avail > 0 && natural > avail) { + const base = parseFloat(getComputedStyle(valueEl).fontSize); + valueEl.style.fontSize = `${Math.floor((base * avail) / natural)}px`; + } + } + function update(): void { labelEl.textContent = model.get("label"); valueEl.textContent = String(model.get("value")); unitEl.textContent = model.get("unit"); + fit(); + const delta = model.get("delta"); if (delta == null || delta === "") { deltaEl.textContent = ""; @@ -55,7 +69,14 @@ function render({ model, el }: RenderProps): () => void { update(); onChanges(model, ["label", "value", "unit", "delta"], update); - return disposeTheme; + const observer = typeof ResizeObserver !== "undefined" ? new ResizeObserver(fit) : null; + observer?.observe(el); + document.fonts?.ready.then(fit); + + return () => { + observer?.disconnect(); + disposeTheme(); + }; } export default { render }; diff --git a/src/manywidgets/stat/style.css b/src/manywidgets/stat/style.css index c11de33..6c0910e 100644 --- a/src/manywidgets/stat/style.css +++ b/src/manywidgets/stat/style.css @@ -1,6 +1,8 @@ .manywidgets-stat { display: inline-block; min-width: var(--mw-stat-min-width); + /* Never wider than the container (content-box: subtract padding + border). */ + max-width: calc(100% - 2 * var(--mw-panel-padding-x) - 2px); padding: var(--mw-panel-padding-y) var(--mw-panel-padding-x); border: 1px solid var(--mw-color-border); border-radius: var(--mw-panel-radius); @@ -19,8 +21,9 @@ .manywidgets-stat__value-row { display: flex; + flex-wrap: wrap; align-items: baseline; - gap: 4px; + column-gap: 4px; margin-top: 6px; } @@ -29,6 +32,7 @@ font-weight: 700; color: var(--mw-color-text); font-variant-numeric: tabular-nums; + white-space: nowrap; } .manywidgets-stat__unit { diff --git a/src/manywidgets/stat/tests/stat.test.ts b/src/manywidgets/stat/tests/stat.test.ts index 4721642..b0d7ae5 100644 --- a/src/manywidgets/stat/tests/stat.test.ts +++ b/src/manywidgets/stat/tests/stat.test.ts @@ -33,4 +33,34 @@ describe("Stat", () => { model.set("value", 99); expect(el.querySelector(".manywidgets-stat__value")!.textContent).toBe("99"); }); + + it("shrinks a value wider than its row to fit", () => { + const el = mountEl(); + // jsdom has no layout: give the row 150px and the value 300px at 30px. + Object.defineProperty(HTMLElement.prototype, "clientWidth", { configurable: true, get: () => 150 }); + const rect = HTMLElement.prototype.getBoundingClientRect; + HTMLElement.prototype.getBoundingClientRect = function () { + const w = this.classList.contains("manywidgets-stat__value") ? (this.style.fontSize ? 150 : 300) : 0; + return { width: w } as DOMRect; + }; + const style = window.getComputedStyle; + window.getComputedStyle = ((n: Element) => ({ ...style(n), fontSize: "30px" })) as typeof getComputedStyle; + try { + const model = fakeModel({ label: "", value: "173,961,000", unit: "", delta: null }); + widget.render({ model, el } as never); + const value = el.querySelector(".manywidgets-stat__value")!; + expect(value.style.fontSize).toBe("15px"); + + // Natural width now fits: the inline size is cleared back to the token. + HTMLElement.prototype.getBoundingClientRect = function () { + return { width: 100 } as DOMRect; + }; + model.set("value", "42"); + expect(value.style.fontSize).toBe(""); + } finally { + delete (HTMLElement.prototype as { clientWidth?: number }).clientWidth; + HTMLElement.prototype.getBoundingClientRect = rect; + window.getComputedStyle = style; + } + }); });