Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions src/manywidgets/stat/doc.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
23 changes: 22 additions & 1 deletion src/manywidgets/stat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,25 @@ function render({ model, el }: RenderProps<StatModel>): () => 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 = "";
Expand All @@ -55,7 +69,14 @@ function render({ model, el }: RenderProps<StatModel>): () => 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 };
6 changes: 5 additions & 1 deletion src/manywidgets/stat/style.css
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -19,8 +21,9 @@

.manywidgets-stat__value-row {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 4px;
column-gap: 4px;
margin-top: 6px;
}

Expand All @@ -29,6 +32,7 @@
font-weight: 700;
color: var(--mw-color-text);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}

.manywidgets-stat__unit {
Expand Down
30 changes: 30 additions & 0 deletions src/manywidgets/stat/tests/stat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but do these tests actually run in CI? 😅

The JS job seems to run:

- run: npm ci
- run: npm run typecheck
- run: npm run build

Maybe we can also add npm test?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in 0710e93

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<HTMLElement>(".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;
}
});
});
Loading