Skip to content

Commit 9de6300

Browse files
committed
fix: fix animation json data not getting new data
1 parent 72d170b commit 9de6300

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/AnimationFileDataViewer.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { css, cx } from '@linaria/core';
2-
import React, { memo, useRef, useState } from 'react';
2+
import type { AnimationItem } from 'lottie-web';
3+
import React, { memo, useEffect, useRef, useState } from 'react';
34
import { LuInfo, LuX } from 'react-icons/lu';
45
import { AnimationFileDataViewerJsonViewer } from './AnimationFileDataViewerJsonViewer';
56
import { IconButton } from './ComponentButtons';
67
import type { MarkerUnprocessed } from './types/Animation';
78

89
interface AnimationFileDataViewerProps {
9-
animationData: object | null | undefined;
10+
animation: AnimationItem | null;
1011
}
1112

1213
interface Data {
@@ -39,9 +40,8 @@ interface Data {
3940
w: number;
4041
}
4142

42-
43-
export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = memo(({ animationData: _animationData }) => {
44-
const animationData = _animationData as Data;
43+
export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = memo(({ animation }) => {
44+
const animationData = useAnimationData(animation)
4545
const modalRef = useRef <HTMLDialogElement>(null);
4646
const [open, setOpen] = useState(false);
4747

@@ -111,6 +111,34 @@ export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = m
111111
);
112112
});
113113

114+
const tryRun = (fn: () => void) => {
115+
try {
116+
return fn()
117+
} catch (e) {
118+
// ignore
119+
}
120+
}
121+
122+
const useAnimationData = (animation: AnimationItem | null | undefined) => {
123+
const [animationData, setAnimationData] = useState<Data | null>(null) ;
124+
125+
useEffect(() => {
126+
const handler = () => {
127+
setAnimationData((animation as unknown as { animationData: Data }).animationData);
128+
}
129+
tryRun(() => {
130+
animation?.addEventListener("config_ready", handler);
131+
})
132+
return () => {
133+
tryRun(() => {
134+
animation?.removeEventListener("config_ready", handler);
135+
});
136+
}
137+
}, [animation]);
138+
139+
return animationData;
140+
}
141+
114142
const cssDialog = css`
115143
padding: 0;
116144
height: 100%;

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const App = () => {
5050
defaultFile={queryOptions.file}
5151
/>
5252
<AnimationFileDataViewer
53-
animationData={(animation as unknown as { animationData: object } | null)?.animationData}
53+
animation={animation}
5454
/>
5555
</div>
5656
</div>

0 commit comments

Comments
 (0)