Skip to content

Commit 0a192ba

Browse files
authored
Merge pull request #1008 from lumapps/chore/thumbnail-improvement
chore(thumbnail): deal with empty image source
2 parents 23421fa + 8d66b6d commit 0a192ba

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Thumbnail: synchronously set error for image with empty source.
13+
1014
## [3.5.0][] - 2023-07-27
1115

1216
### Added
@@ -1785,7 +1789,5 @@ _Failed released_
17851789
[3.3.0]: https://github.com/lumapps/design-system/tree/v3.3.0
17861790
[unreleased]: https://github.com/lumapps/design-system/compare/v3.4.0...HEAD
17871791
[3.4.0]: https://github.com/lumapps/design-system/tree/v3.4.0
1788-
1789-
1790-
[Unreleased]: https://github.com/lumapps/design-system/compare/v3.5.0...HEAD
1791-
[3.5.0]: https://github.com/lumapps/design-system/tree/v3.5.0
1792+
[unreleased]: https://github.com/lumapps/design-system/compare/v3.5.0...HEAD
1793+
[3.5.0]: https://github.com/lumapps/design-system/tree/v3.5.0

packages/lumx-react/src/components/thumbnail/Thumbnail.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { mdiAbTesting } from '@lumx/icons';
3+
import { mdiAbTesting, mdiImageBroken } from '@lumx/icons';
44
import { Alignment, AspectRatio, Badge, FlexBox, Icon, Size, Thumbnail, ThumbnailVariant } from '@lumx/react';
55
import { CustomLink } from '@lumx/react/stories/utils/CustomLink';
66
import { IMAGE_SIZES, imageArgType, IMAGES } from '@lumx/react/stories/controls/image';
@@ -44,6 +44,10 @@ export const IsLoading = {
4444
args: { ...Simple.args, isLoading: true },
4545
};
4646

47+
export const WithoutSource = {
48+
args: { image: IMAGES.emptyImage, size: Size.xxl, aspectRatio: AspectRatio.square },
49+
};
50+
4751
/** Thumbnail error fallback and size variants */
4852
export const ErrorFallback = {
4953
args: { image: 'foo' },

packages/lumx-react/src/components/thumbnail/useImageLoad.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { useEffect, useState } from 'react';
33
export type LoadingState = 'isLoading' | 'isLoaded' | 'hasError';
44

55
function getState(img: HTMLImageElement | null | undefined, event?: Event) {
6-
// Error event occurred.
7-
if (event?.type === 'error') {
6+
// Error event occurred or image has no source.
7+
if (event?.type === 'error' || (img?.complete && !img.getAttribute('src'))) {
8+
console.log('HAS ERROR');
89
return 'hasError';
910
}
1011
// Image is undefined or incomplete.

packages/lumx-react/src/stories/controls/image.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ const portrait3 = '/demo-assets/portrait3.jpg';
1515
const square1 = '/demo-assets/square1.jpg';
1616
const square2 = '/demo-assets/square2.jpg';
1717
const defaultSvg = '/demo-assets/defaultSvg.svg';
18+
const emptyImage = '';
1819

1920
export const AVATAR_IMAGES = { avatar1, avatar2, avatar3, avatar4 };
2021
export const SQUARE_IMAGES = { square1, square2 };
2122
export const SVG_IMAGES = { defaultSvg };
23+
export const EMPTY_IMAGES = { emptyImage };
2224
export const LANDSCAPE_IMAGES = { landscape1, landscape1s200, landscape2, landscape3 };
2325
export const PORTRAIT_IMAGES = { portrait1, portrait1s200, portrait2, portrait3 };
24-
export const IMAGES = { ...LANDSCAPE_IMAGES, ...PORTRAIT_IMAGES, ...SQUARE_IMAGES, ...AVATAR_IMAGES, ...SVG_IMAGES };
26+
export const IMAGES = {
27+
...LANDSCAPE_IMAGES,
28+
...PORTRAIT_IMAGES,
29+
...SQUARE_IMAGES,
30+
...AVATAR_IMAGES,
31+
...SVG_IMAGES,
32+
...EMPTY_IMAGES,
33+
};
2534

2635
export const avatarImageArgType = getSelectArgType(AVATAR_IMAGES);
2736
export const squareImageArgType = getSelectArgType(SQUARE_IMAGES);
@@ -56,10 +65,14 @@ export const PORTRAIT_IMAGE_SIZES: Record<keyof typeof PORTRAIT_IMAGES, Size> =
5665
export const SVG_IMAGE_SIZES: Record<keyof typeof SVG_IMAGES, Size> = {
5766
defaultSvg: { width: 0, height: 0 },
5867
};
68+
export const EMPTY_IMAGES_SIZES: Record<keyof typeof EMPTY_IMAGES, Size> = {
69+
emptyImage: { width: 0, height: 0 },
70+
};
5971
export const IMAGE_SIZES: Record<keyof typeof IMAGES, Size> = {
6072
...LANDSCAPE_IMAGE_SIZES,
6173
...PORTRAIT_IMAGE_SIZES,
6274
...SQUARE_IMAGE_SIZES,
6375
...AVATAR_IMAGE_SIZES,
6476
...SVG_IMAGE_SIZES,
77+
...EMPTY_IMAGES_SIZES,
6578
};

0 commit comments

Comments
 (0)