Skip to content

Commit 65b2fc4

Browse files
authored
feat: icon image (#377)
Signed-off-by: Philippe Martin <[email protected]>
1 parent e422209 commit 65b2fc4

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**********************************************************************
2+
* Copyright (C) 2023-2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import '@testing-library/jest-dom/vitest';
20+
21+
import { render } from '@testing-library/svelte';
22+
import { expect, test } from 'vitest';
23+
24+
import IconImage from './IconImage.svelte';
25+
26+
test('Expect valid source and alt text with dark mode', async () => {
27+
const image = render(IconImage, { image: { light: 'light.png', dark: 'dark.png' }, alt: 'this is alt text' });
28+
29+
const imageElement = image.getByRole('img');
30+
31+
expect(imageElement).toHaveAttribute('src', 'dark.png');
32+
expect(imageElement).toHaveAttribute('alt', 'this is alt text');
33+
34+
await image.rerender({ image: { light: 'light2.png', dark: 'dark2.png' }, alt: 'this is another alt text' });
35+
expect(imageElement).toHaveAttribute('src', 'dark2.png');
36+
});
37+
38+
test('Expect no alt attribute if missing and default image', async () => {
39+
const image = render(IconImage, { image: 'image.png' });
40+
41+
const imageElement = image.getByRole('img');
42+
expect(imageElement).toHaveAttribute('src', 'image.png');
43+
expect(imageElement).not.toHaveAttribute('alt');
44+
});
45+
46+
test('Expect string as image', async () => {
47+
const image = render(IconImage, { image: 'image1', alt: 'this is alt text' });
48+
49+
const imageElement = image.getByRole('img');
50+
51+
expect(imageElement).toHaveAttribute('src', 'image1');
52+
expect(imageElement).toHaveAttribute('alt', 'this is alt text');
53+
54+
await image.rerender({ image: 'image2', alt: 'this is another alt text' });
55+
expect(imageElement).toHaveAttribute('src', 'image2');
56+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script lang="ts">
2+
import type { Snippet } from 'svelte';
3+
4+
type Image = string | { light: string; dark: string };
5+
6+
interface Props {
7+
image?: Image;
8+
alt?: string;
9+
class?: string;
10+
children?: Snippet;
11+
}
12+
let { image, alt, class: className, children }: Props = $props();
13+
14+
let imgSrc = $state<string>();
15+
16+
$effect(() => {
17+
image;
18+
getImgSrc(image);
19+
});
20+
21+
function getImage(icon?: Image): string | undefined {
22+
if (!icon) {
23+
return undefined;
24+
}
25+
26+
if (typeof icon === 'string') {
27+
return icon;
28+
}
29+
30+
// TODO: add light/dark mode support (https://github.com/podman-desktop/extension-kubernetes-dashboard/issues/369)
31+
if (/*isDarkTheme &&*/ icon.dark) {
32+
return icon.dark;
33+
} else if (/*!isDarkTheme &&*/ icon.light) {
34+
return icon.light;
35+
}
36+
return undefined;
37+
}
38+
39+
function getImgSrc(image: string | { light: string; dark: string } | undefined): void {
40+
imgSrc = getImage(image);
41+
}
42+
</script>
43+
44+
{#if imgSrc}
45+
<img src={imgSrc} alt={alt} class={className} />
46+
{:else}
47+
{@render children?.()}
48+
{/if}

0 commit comments

Comments
 (0)