Skip to content

Commit 0cdc246

Browse files
committed
feat: allow RiveView to be called with a file argument
1 parent ea6543f commit 0cdc246

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

src/core/RiveView.tsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { HybridViewProps } from 'react-native-nitro-modules';
2+
import type { RiveFile } from '../specs/RiveFile.nitro';
3+
import '../specs/RiveView.nitro';
4+
import type { Alignment } from './Alignment';
5+
import type { Fit } from './Fit';
6+
import { NitroRiveView } from 'react-native-rive';
7+
import type { ReferencedAssets } from '../hooks/useRiveFile';
8+
import type { RiveFileInput } from '../../lib/typescript/src';
9+
import { useRiveFile } from '../hooks/useRiveFile';
10+
import { ActivityIndicator, Text } from 'react-native';
11+
import type { ComponentProps } from 'react';
12+
13+
export interface RiveViewProps
14+
extends Omit<ComponentProps<typeof NitroRiveView>, 'file'> {
15+
/** Name of the artboard to display from the Rive file */
16+
artboardName?: string;
17+
/** Name of the state machine to play */
18+
stateMachineName?: string;
19+
/** Whether to automatically bind the state machine and artboard */
20+
autoBind?: boolean;
21+
/** Whether to automatically start playing the state machine */
22+
autoPlay?: boolean;
23+
/** The Rive file to be displayed */
24+
file: RiveFile | RiveFileInput;
25+
/** How the Rive graphic should be aligned within its container */
26+
alignment?: Alignment;
27+
/** How the Rive graphic should fit within its container */
28+
fit?: Fit;
29+
/** The scale factor to apply to the Rive graphic when using Fit.Layout */
30+
layoutScaleFactor?: number;
31+
/** Referenced assets for out-of-band asset loading */
32+
referencedAssets?: ReferencedAssets;
33+
}
34+
35+
function isNitroFile(file: RiveFile | RiveFileInput): file is RiveFile {
36+
return (
37+
typeof file === 'object' &&
38+
'__type' in file &&
39+
file.__type === 'HybridObject<RiveFile>'
40+
);
41+
}
42+
43+
function RiveViewWithFileInput(
44+
props: Omit<RiveViewProps, 'file'> & {
45+
file: RiveFileInput;
46+
referencedAssets?: ReferencedAssets;
47+
}
48+
) {
49+
const { file, referencedAssets, ...rest } = props;
50+
const { riveFile, isLoading, error } = useRiveFile(file, referencedAssets);
51+
52+
if (isLoading) {
53+
return <ActivityIndicator />;
54+
} else if (error != null) {
55+
return <Text>{error}</Text>;
56+
} else {
57+
return <NitroRiveView {...rest} file={riveFile} />;
58+
}
59+
}
60+
61+
export function RiveView(props: RiveViewProps) {
62+
const { file, referencedAssets, ...rest } = props;
63+
64+
if (isNitroFile(file)) {
65+
return <NitroRiveView {...rest} file={file} />;
66+
} else {
67+
return (
68+
<RiveViewWithFileInput
69+
{...rest}
70+
file={file}
71+
referencedAssets={referencedAssets}
72+
/>
73+
);
74+
}
75+
}

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function multiply(a: number, b: number): number {
4646
* - play(): Starts playing the animation
4747
* - pause(): Pauses the animation
4848
*/
49-
export const RiveView = getHostComponent<RiveViewProps, RiveViewMethods>(
49+
export const NitroRiveView = getHostComponent<RiveViewProps, RiveViewMethods>(
5050
'RiveView',
5151
() => RiveViewConfig
5252
) as ReactNativeView<RiveViewProps, RiveViewTSMethods>;
@@ -78,3 +78,4 @@ export { useRiveColor } from './hooks/useRiveColor';
7878
export { useRiveTrigger } from './hooks/useRiveTrigger';
7979
export { useRiveFile } from './hooks/useRiveFile';
8080
export { type RiveFileInput } from './hooks/useRiveFile';
81+
export { RiveView } from './core/RiveView';

src/specs/RiveView.nitro.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,4 @@ export type RiveViewTSMethods = Exclude<RiveViewMethods, 'onEventListener'> & {
105105
onEventListener(onEvent: (event: RiveEvent) => void): void;
106106
};
107107

108-
/**
109-
* Type definition for the RiveView component.
110-
* Combines RiveViewProps and RiveViewMethods with the HybridView type.
111-
*/
112108
export type RiveView = HybridView<RiveViewProps, RiveViewMethods>;

0 commit comments

Comments
 (0)