@marimo-team/frontend 0.23.1-dev8 → 0.23.1-dev9

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.
package/dist/index.html CHANGED
@@ -66,7 +66,7 @@
66
66
  <marimo-server-token data-token="{{ server_token }}" hidden></marimo-server-token>
67
67
  <!-- /TODO -->
68
68
  <title>{{ title }}</title>
69
- <script type="module" crossorigin src="./assets/index-bjxpaV0V.js"></script>
69
+ <script type="module" crossorigin src="./assets/index-Bm25ctN7.js"></script>
70
70
  <link rel="modulepreload" crossorigin href="./assets/preload-helper-D2MJg03u.js">
71
71
  <link rel="modulepreload" crossorigin href="./assets/chunk-LvLJmgfZ.js">
72
72
  <link rel="modulepreload" crossorigin href="./assets/react-Bj1aDYRI.js">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/frontend",
3
- "version": "0.23.1-dev8",
3
+ "version": "0.23.1-dev9",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -297,13 +297,7 @@ export class MatplotlibRenderer {
297
297
  // Create canvas
298
298
  const canvas = document.createElement("canvas");
299
299
  canvas.className = "block cursor-crosshair";
300
- const dpr = globalThis.devicePixelRatio ?? 1;
301
- canvas.width = this.#state.width * dpr;
302
- canvas.height = this.#state.height * dpr;
303
- canvas.style.width = `${this.#state.width}px`;
304
- canvas.style.maxWidth = "100%";
305
- canvas.style.height = "auto";
306
- canvas.style.aspectRatio = `${this.#state.width} / ${this.#state.height}`;
300
+ this.#syncCanvasSize(canvas);
307
301
  canvas.style.touchAction = "none";
308
302
  container.append(canvas);
309
303
  this.#canvas = canvas;
@@ -322,6 +316,10 @@ export class MatplotlibRenderer {
322
316
  signal: options.signal,
323
317
  });
324
318
 
319
+ // Watch for devicePixelRatio changes (e.g. browser zoom, moving between
320
+ // displays). matchMedia fires exactly once per DPR transition.
321
+ this.#watchDevicePixelRatio(options.signal);
322
+
325
323
  // Clean up on abort
326
324
  options.signal.addEventListener("abort", () => {
327
325
  cancelAnimationFrame(this.#rafId);
@@ -333,6 +331,38 @@ export class MatplotlibRenderer {
333
331
  this.#restoreSelection(this.#state.value);
334
332
  }
335
333
 
334
+ /** Set the canvas buffer + CSS size to match current logical size and DPR. */
335
+ #syncCanvasSize(canvas: HTMLCanvasElement = this.#canvas): void {
336
+ const dpr = globalThis.devicePixelRatio ?? 1;
337
+ const { width, height } = this.#state;
338
+ canvas.width = width * dpr;
339
+ canvas.height = height * dpr;
340
+ canvas.style.width = `${width}px`;
341
+ canvas.style.maxWidth = "100%";
342
+ canvas.style.height = "auto";
343
+ canvas.style.aspectRatio = `${width} / ${height}`;
344
+ }
345
+
346
+ /**
347
+ * Observe devicePixelRatio changes via matchMedia. Each listener fires once
348
+ * per transition, so we re-register after every change.
349
+ */
350
+ #watchDevicePixelRatio(signal: AbortSignal): void {
351
+ if (signal.aborted) {
352
+ return;
353
+ }
354
+ const mq = matchMedia(
355
+ `(resolution: ${globalThis.devicePixelRatio ?? 1}dppx)`,
356
+ );
357
+ const onChange = () => {
358
+ this.#syncCanvasSize();
359
+ this.#drawCanvas();
360
+ // Re-register for the next DPR transition
361
+ this.#watchDevicePixelRatio(signal);
362
+ };
363
+ mq.addEventListener("change", onChange, { once: true, signal });
364
+ }
365
+
336
366
  update(state: MatplotlibState): void {
337
367
  const prev = this.#state;
338
368
  this.#state = state;
@@ -341,13 +371,7 @@ export class MatplotlibRenderer {
341
371
 
342
372
  // Update canvas dimensions if changed
343
373
  if (state.width !== prev.width || state.height !== prev.height) {
344
- const dpr = globalThis.devicePixelRatio ?? 1;
345
- this.#canvas.width = state.width * dpr;
346
- this.#canvas.height = state.height * dpr;
347
- this.#canvas.style.width = `${state.width}px`;
348
- this.#canvas.style.maxWidth = "100%";
349
- this.#canvas.style.height = "auto";
350
- this.#canvas.style.aspectRatio = `${state.width} / ${state.height}`;
374
+ this.#syncCanvasSize();
351
375
  needsRedraw = true;
352
376
  }
353
377