An open source design system that's fully customizable and agent ready
TypeScript
fix(thumbnail): show the placeholder when the image fails to load (#3895)
Shahrier Islam Arham97d2d092ebc9
diff --git a/.changeset/thumbnail-error-placeholder.md b/.changeset/thumbnail-error-placeholder.mdnew file mode 100644index 0000000000..031e090cbe--- /dev/null+++ b/.changeset/thumbnail-error-placeholder.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Thumbnail: show the placeholder when the image fails to load++The docs promise a placeholder on load failure, but the img had no error handling, so a broken src rendered a broken image indefinitely. The component now tracks the errored src and falls back to the placeholder, retrying when src changes.+@arham766diff --git a/packages/core/src/Thumbnail/Thumbnail.test.tsx b/packages/core/src/Thumbnail/Thumbnail.test.tsxindex 0319e2a937..771a60fa84 100644--- a/packages/core/src/Thumbnail/Thumbnail.test.tsx+++ b/packages/core/src/Thumbnail/Thumbnail.test.tsx@@ -1,7 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.import {describe, it, expect, vi} from 'vitest';-import {render, screen} from '@testing-library/react';+import {render, screen, fireEvent} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {Thumbnail} from './Thumbnail';@@ -219,4 +219,27 @@ describe('Thumbnail', () => {expect(onRemove).toHaveBeenCalledOnce();});});++ it('shows the placeholder when the image fails to load', () => {+ render(<Thumbnail src="/broken.jpg" alt="Broken" data-testid="thumb" />);++ fireEvent.error(screen.getByRole('img'));++ expect(screen.queryByRole('img')).toBeNull();+ const root = screen.getByTestId('thumb');+ expect(root.querySelector('svg')).toBeInTheDocument();+ });++ it('retries a changed src after a load error', () => {+ const {rerender} = render(+ <Thumbnail src="/broken.jpg" alt="Photo" data-testid="thumb" />,+ );+ fireEvent.error(screen.getByRole('img'));+ expect(screen.queryByRole('img')).toBeNull();++ rerender(<Thumbnail src="/fixed.jpg" alt="Photo" data-testid="thumb" />);++ const img = screen.getByRole('img');+ expect(img).toHaveAttribute('src', '/fixed.jpg');+ });});diff --git a/packages/core/src/Thumbnail/Thumbnail.tsx b/packages/core/src/Thumbnail/Thumbnail.tsxindex f4dbe5a9c1..6e922d010f 100644--- a/packages/core/src/Thumbnail/Thumbnail.tsx+++ b/packages/core/src/Thumbnail/Thumbnail.tsx@@ -25,6 +25,7 @@* - /packages/cli/templates/blocks/components/Thumbnail/ (showcase blocks)*/+import {useState} from 'react';import * as stylex from '@stylexjs/stylex';import {colorVars,@@ -296,11 +297,16 @@ export function Thumbnail({}: ThumbnailProps) {const t = useTranslator();+ // Track the exact src that failed (rather than a boolean) so a changed src+ // gets a fresh load attempt instead of the stale error.+ const [erroredSrc, setErroredSrc] = useState<string | undefined>(undefined);+const hasSrc = src != null;+ const hasError = hasSrc && erroredSrc === src;const showSkeleton = isLoading && !hasSrc;- const showImage = hasSrc && !showSkeleton;+ const showImage = hasSrc && !showSkeleton && !hasError;const showUploadOverlay = isLoading && hasSrc;- const showPlaceholder = !isLoading && !hasSrc;+ const showPlaceholder = (!isLoading && !hasSrc) || hasError;const isInteractive = onClick != null && !isDisabled && !isLoading;const hasRemove = onRemove != null && !isDisabled;const isHoverReveal = hasRemove && showRemoveOn === 'hover';@@ -344,6 +350,7 @@ export function Thumbnail({alt={alt ?? ''}role={isImageDecorative ? 'presentation' : undefined}aria-hidden={isImageDecorative || undefined}+ onError={() => setErroredSrc(src)}{...stylex.props(styles.image)}/>)}