Skip to content

Commit 866db00

Browse files
authored
chore: add responsive layout example and updated docs (#34)
* chore: add responsive layout example * chore: rename example export to OutOfBandAssetsExample * docs: expand error handling
1 parent 5c9c2eb commit 866db00

5 files changed

Lines changed: 121 additions & 4 deletions

File tree

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default {
116116

117117
## Error Handling
118118

119-
All Rive operations can be wrapped in try/catch blocks for error handling:
119+
All Rive operations can be wrapped in try/catch blocks for error handling, for example, loading a file:
120120

121121
```js
122122
try {
@@ -125,11 +125,72 @@ try {
125125
);
126126
// Use the riveFile...
127127
} catch (error) {
128-
// Handle any errors that occur during Rive operations
128+
// Handle any errors that occur during Rive file loading
129129
console.error('Error loading Rive file:', error);
130130
}
131131
```
132132

133+
### View-Based Errors
134+
135+
The `RiveView` component provides an `onError` callback prop to handle errors that occur during view configuration or runtime operations:
136+
137+
```js
138+
<RiveView
139+
file={riveFile}
140+
onError={(error) => {
141+
// error.type contains the error type enum value
142+
// error.message contains a descriptive error message
143+
console.error(`Rive Error [${error.type}]: ${error.message}`);
144+
}}
145+
/>
146+
```
147+
148+
#### Error Types
149+
150+
The following error types can occur during view operations:
151+
152+
| Error Type | Value | Description |
153+
| ---------------------------------------------- | ----- | ----------------------------------------------------- |
154+
| `RiveErrorType.Unknown` | 0 | An unknown error occurred |
155+
| `RiveErrorType.FileNotFound` | 1 | The specified Rive file could not be found |
156+
| `RiveErrorType.MalformedFile` | 2 | The Rive file is malformed or corrupted |
157+
| `RiveErrorType.IncorrectArtboardName` | 3 | The specified artboard name does not exist |
158+
| `RiveErrorType.IncorrectStateMachineName` | 4 | The specified state machine name does not exist |
159+
| `RiveErrorType.ViewModelInstanceNotFound` | 6 | The specified view model instance was not found |
160+
| `RiveErrorType.IncorrectStateMachineInputName` | 8 | The specified state machine input name does not exist |
161+
162+
You can use these error types to provide specific error handling:
163+
164+
```js
165+
import { RiveView, RiveErrorType } from 'react-native-rive';
166+
167+
<RiveView
168+
file={riveFile}
169+
artboardName="MainArtboard"
170+
onError={(error) => {
171+
switch (error.type) {
172+
case RiveErrorType.IncorrectArtboardName:
173+
console.error('Artboard not found:', error.message);
174+
// Handle missing artboard (e.g., use default artboard)
175+
break;
176+
case RiveErrorType.IncorrectStateMachineName:
177+
console.error('State machine not found:', error.message);
178+
// Handle missing state machine
179+
break;
180+
case RiveErrorType.MalformedFile:
181+
console.error('Corrupted file:', error.message);
182+
// Handle corrupted file (e.g., show error UI)
183+
break;
184+
default:
185+
console.error('Rive error:', error.message);
186+
}
187+
}}
188+
style={{ width: '100%', height: 400 }}
189+
/>;
190+
```
191+
192+
> **Note**: If no `onError` handler is provided, errors will be logged to the console by default.
193+
133194
## Feature Support
134195

135196
This section provides a comprehensive overview of feature availability in `react-native-rive`, comparing it with the [previous Rive React Native runtime](https://github.com/rive-app/rive-react-native) and outlining the development roadmap.
60.8 KB
Binary file not shown.

example/src/pages/OutOfBandAssets.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { Picker } from '@react-native-picker/picker';
1515
import { type Metadata } from '../helpers/metadata';
1616

17-
export default function StateMachine() {
17+
export default function OutOfBandAssetsExample() {
1818
const [uri, setUri] = React.useState('https://picsum.photos/id/372/500/500');
1919
const { riveFile, isLoading, error } = useRiveFile(
2020
require('../../assets/rive/out_of_band.riv'),
@@ -143,7 +143,7 @@ const styles = StyleSheet.create({
143143
},
144144
});
145145

146-
StateMachine.metadata = {
146+
OutOfBandAssetsExample.metadata = {
147147
name: 'Out-of-Band Assets',
148148
description:
149149
'Shows how to load referenced assets like fonts and images that are not embedded in the Rive file',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
2+
import { Fit, RiveView, useRiveFile } from 'react-native-rive';
3+
import { type Metadata } from '../helpers/metadata';
4+
5+
/**
6+
* Demonstrates responsive layouts using Fit.Layout and layoutScaleFactor
7+
*
8+
* See https://rive.app/docs/runtimes/layout
9+
*/
10+
export default function ResponsiveLayoutsExample() {
11+
const { riveFile, isLoading, error } = useRiveFile(
12+
require('../../assets/rive/layouts_demo.riv')
13+
);
14+
15+
return (
16+
<View style={styles.container}>
17+
{isLoading ? (
18+
<ActivityIndicator size="large" color="#0000ff" />
19+
) : error ? (
20+
<Text style={styles.errorText}>{error}</Text>
21+
) : riveFile ? (
22+
<RiveView
23+
file={riveFile}
24+
fit={Fit.Layout}
25+
layoutScaleFactor={1} // adjust the layout scale factor to control the layout scale
26+
style={styles.rive}
27+
/>
28+
) : null}
29+
</View>
30+
);
31+
}
32+
33+
const styles = StyleSheet.create({
34+
// Adjust the container size and the layout will adjust based on the .riv file layout rules
35+
container: {
36+
width: '100%',
37+
height: '100%',
38+
},
39+
rive: {
40+
justifyContent: 'center',
41+
alignItems: 'center',
42+
width: '100%',
43+
height: '100%',
44+
},
45+
errorText: {
46+
color: 'red',
47+
textAlign: 'center',
48+
padding: 20,
49+
},
50+
});
51+
52+
ResponsiveLayoutsExample.metadata = {
53+
name: 'Responsive Layouts',
54+
description: 'Sample .riv file with responsive layouts',
55+
} satisfies Metadata;

example/src/pages/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { default as StateMachineInputsExample } from './RiveStateMachineInputsEx
66
export { default as TextRunExample } from './RiveTextRunExample';
77
export { default as OutOfBandAssets } from './OutOfBandAssets';
88
export { default as ManyViewModels } from './ManyViewModels';
9+
export { default as ResponsiveLayouts } from './ResponsiveLayouts';

0 commit comments

Comments
 (0)