@marimo-team/islands 0.23.15-dev21 → 0.23.15-dev25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.23.15-dev21",
3
+ "version": "0.23.15-dev25",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -12,6 +12,13 @@ export interface ImageComparisonData {
12
12
  height?: string;
13
13
  }
14
14
 
15
+ // Truncate long sources (e.g. base64 data URLs) so error messages stay
16
+ // readable.
17
+ function truncateSrc(src: string): string {
18
+ const MAX_LENGTH = 100;
19
+ return src.length > MAX_LENGTH ? `${src.slice(0, MAX_LENGTH)}…` : src;
20
+ }
21
+
15
22
  const ImageComparisonComponent: React.FC<ImageComparisonData> = ({
16
23
  beforeSrc,
17
24
  afterSrc,
@@ -20,17 +27,61 @@ const ImageComparisonComponent: React.FC<ImageComparisonData> = ({
20
27
  width,
21
28
  height,
22
29
  }) => {
30
+ const [failedSrcs, setFailedSrcs] = React.useState<ReadonlySet<string>>(
31
+ () => new Set(),
32
+ );
33
+
34
+ React.useEffect(() => {
35
+ setFailedSrcs(new Set());
36
+ }, [beforeSrc, afterSrc]);
37
+
38
+ const handleError = React.useCallback((src: string) => {
39
+ setFailedSrcs((prev) => new Set(prev).add(src));
40
+ }, []);
41
+
23
42
  const containerStyle: React.CSSProperties = {
24
43
  width: width || "100%",
25
44
  height: height || (direction === "vertical" ? "400px" : "auto"),
26
45
  maxWidth: "100%",
46
+ // The slider derives its height entirely from its (loaded) images, so a
47
+ // broken/slow-loading source would otherwise collapse it to nothing and
48
+ // render an empty output. Keep a minimum height so it stays visible.
49
+ minHeight: "2rem",
27
50
  };
28
51
 
52
+ // If an image fails to load, the slider collapses to nothing; surface a
53
+ // visible error instead of silently rendering an empty output.
54
+ if (failedSrcs.size > 0) {
55
+ return (
56
+ <div
57
+ style={containerStyle}
58
+ className="flex items-center justify-center rounded border border-destructive/40 bg-destructive/5 p-3 text-sm text-destructive"
59
+ >
60
+ <span>
61
+ Failed to load {failedSrcs.size > 1 ? "images" : "image"}:{" "}
62
+ {[...failedSrcs].map((src) => `"${truncateSrc(src)}"`).join(", ")}
63
+ </span>
64
+ </div>
65
+ );
66
+ }
67
+
29
68
  return (
30
69
  <div style={containerStyle}>
31
70
  <ImgComparisonSlider value={value} direction={direction}>
32
- <img slot="first" src={beforeSrc} alt="Before" width="100%" />
33
- <img slot="second" src={afterSrc} alt="After" width="100%" />
71
+ <img
72
+ slot="first"
73
+ src={beforeSrc}
74
+ alt="Before"
75
+ width="100%"
76
+ onError={() => handleError(beforeSrc)}
77
+ />
78
+ <img
79
+ slot="second"
80
+ src={afterSrc}
81
+ alt="After"
82
+ width="100%"
83
+ onError={() => handleError(afterSrc)}
84
+ />
34
85
  </ImgComparisonSlider>
35
86
  </div>
36
87
  );
@@ -0,0 +1,71 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+
3
+ import { fireEvent, render } from "@testing-library/react";
4
+ import { describe, expect, it } from "vitest";
5
+ import ImageComparisonComponent from "../ImageComparisonComponent";
6
+
7
+ const baseProps = {
8
+ beforeSrc: "before.png",
9
+ afterSrc: "after.png",
10
+ value: 50,
11
+ direction: "horizontal" as const,
12
+ };
13
+
14
+ describe("ImageComparisonComponent", () => {
15
+ it("renders the comparison slider with both images", () => {
16
+ const { getByAltText, container } = render(
17
+ <ImageComparisonComponent {...baseProps} />,
18
+ );
19
+ expect(getByAltText("Before")).toBeTruthy();
20
+ expect(getByAltText("After")).toBeTruthy();
21
+ expect(container.querySelector("img-comparison-slider")).toBeTruthy();
22
+ });
23
+
24
+ it("shows a visible error instead of collapsing when an image fails to load", () => {
25
+ const brokenSrc = "https://example.com/does-not-exist.png";
26
+ const { getByAltText, queryByText, container } = render(
27
+ <ImageComparisonComponent {...baseProps} beforeSrc={brokenSrc} />,
28
+ );
29
+
30
+ // No error before the image fails.
31
+ expect(queryByText(/Failed to load/)).toBeNull();
32
+
33
+ fireEvent.error(getByAltText("Before"));
34
+
35
+ // The slider is replaced with a visible error mentioning the source, so
36
+ // the output is never a silent blank.
37
+ expect(container.querySelector("img-comparison-slider")).toBeNull();
38
+ const error = queryByText(/Failed to load image/);
39
+ expect(error).toBeTruthy();
40
+ expect(error?.textContent).toContain(brokenSrc);
41
+ });
42
+
43
+ it("clears the error when the sources change so new images can load", () => {
44
+ const brokenSrc = "https://example.com/does-not-exist.png";
45
+ const { getByAltText, queryByText, container, rerender } = render(
46
+ <ImageComparisonComponent {...baseProps} beforeSrc={brokenSrc} />,
47
+ );
48
+
49
+ fireEvent.error(getByAltText("Before"));
50
+ expect(queryByText(/Failed to load/)).toBeTruthy();
51
+
52
+ // Re-render with a new source (e.g. the user fixed the notebook): the error
53
+ // should clear and the slider should mount again rather than stay stuck.
54
+ rerender(<ImageComparisonComponent {...baseProps} beforeSrc="fixed.png" />);
55
+ expect(queryByText(/Failed to load/)).toBeNull();
56
+ expect(container.querySelector("img-comparison-slider")).toBeTruthy();
57
+ });
58
+
59
+ it("truncates long sources (e.g. data URLs) in the error message", () => {
60
+ const longSrc = `data:image/png;base64,${"A".repeat(200)}`;
61
+ const { getByAltText, queryByText } = render(
62
+ <ImageComparisonComponent {...baseProps} afterSrc={longSrc} />,
63
+ );
64
+
65
+ fireEvent.error(getByAltText("After"));
66
+
67
+ const error = queryByText(/Failed to load image/);
68
+ expect(error?.textContent).toContain("…");
69
+ expect(error?.textContent).not.toContain("A".repeat(200));
70
+ });
71
+ });