Skip to content

Commit 2f2f8f2

Browse files
committed
Fix svg imageType not getting set in all cases
1 parent 91d0a4d commit 2f2f8f2

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

.changeset/upset-eyes-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plexinc/react-lightning": patch
3+
---
4+
5+
Fix svg imageType not getting set in all cases

packages/react-lightning/src/element/LightningImageElement.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
import type { INodeProps, NodeLoadedPayload } from '@lightningjs/renderer';
1+
import type {
2+
INode,
3+
INodeProps,
4+
NodeLoadedPayload,
5+
RendererMain,
6+
} from '@lightningjs/renderer';
7+
import type { Fiber } from 'react-reconciler';
8+
import type { Plugin } from '../render/Plugin';
29
import {
10+
type LightningElement,
311
LightningElementType,
412
type LightningImageElementProps,
513
type LightningImageElementStyle,
6-
type LightningTextElementStyle,
7-
type LightningViewElementProps,
814
type RendererNode,
915
} from '../types';
1016
import { LightningViewElement } from './LightningViewElement';
1117

12-
export class LightningImageElement extends LightningViewElement<
13-
LightningImageElementStyle,
14-
LightningImageElementProps<LightningImageElementStyle>
15-
> {
18+
function getImageType(src?: string | null): INode['imageType'] {
19+
if (!src) {
20+
return null;
21+
}
22+
23+
const srcLower = src.toLowerCase();
24+
const isSvg =
25+
srcLower.endsWith('.svg') || srcLower.startsWith('data:image/svg+xml,');
26+
27+
return isSvg ? 'svg' : null;
28+
}
29+
30+
export class LightningImageElement<
31+
TStyleProps extends LightningImageElementStyle = LightningImageElementStyle,
32+
TProps extends
33+
LightningImageElementProps<TStyleProps> = LightningImageElementProps<TStyleProps>,
34+
> extends LightningViewElement<TStyleProps, TProps> {
1635
public override get type() {
1736
return LightningElementType.Image;
1837
}
@@ -30,16 +49,22 @@ export class LightningImageElement extends LightningViewElement<
3049
public set src(v) {
3150
this.node.src = v;
3251

33-
// Attempt to set the svg imageType for data urls if not already set.
34-
// Lightning doesn't handle svgs that are data urls, so this adds support
35-
// for that.
36-
if (this.node.imageType == null && v) {
37-
const src = v.toLowerCase();
38-
const isSvg =
39-
src.endsWith('.svg') || src.startsWith('data:image/svg+xml,');
52+
if (!this.node.imageType) {
53+
this.node.imageType = getImageType(v);
54+
}
55+
}
4056

41-
this.setNodeProp('imageType', isSvg ? 'svg' : null);
57+
public constructor(
58+
initialProps: TProps,
59+
renderer: RendererMain,
60+
plugins: Plugin<LightningElement>[],
61+
fiber: Fiber,
62+
) {
63+
if (!initialProps.imageType) {
64+
initialProps.imageType = getImageType(initialProps.src);
4265
}
66+
67+
super(initialProps, renderer, plugins, fiber);
4368
}
4469

4570
protected override _handleTextureLoaded(event: NodeLoadedPayload): void {
@@ -58,7 +83,7 @@ export class LightningImageElement extends LightningViewElement<
5883
}
5984

6085
public override _toLightningNodeProps(
61-
props: LightningViewElementProps<LightningTextElementStyle> & {
86+
props: TProps & {
6287
text?: string;
6388
} & Record<string, unknown>,
6489
initial?: boolean,

packages/react-lightning/src/types/Props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export type LightningImageElementProps<
6868
TStyleProps extends LightningImageElementStyle = LightningImageElementStyle,
6969
> = LightningViewElementProps<TStyleProps> & {
7070
src?: INodeProps['src'];
71+
imageType?: INodeProps['imageType'];
7172
};
7273

7374
export type LightningTextElementProps<

0 commit comments

Comments
 (0)