Skip to content

Commit 4b9433d

Browse files
authored
feat: add interactive scale factor controls to ResponsiveLayouts (#88)
Port scale factor controls from rive-react-native example for feature parity.
1 parent ac6eac0 commit 4b9433d

4 files changed

Lines changed: 90 additions & 15 deletions

File tree

example/src/pages/ResponsiveLayouts.tsx

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
2-
import { Fit, RiveView, useRiveFile } from '@rive-app/react-native';
1+
import { useState, useRef, useEffect } from 'react';
2+
import {
3+
View,
4+
Text,
5+
StyleSheet,
6+
Button,
7+
ActivityIndicator,
8+
useWindowDimensions,
9+
} from 'react-native';
10+
import {
11+
Fit,
12+
RiveView,
13+
useRiveFile,
14+
type RiveViewRef,
15+
} from '@rive-app/react-native';
316
import { type Metadata } from '../helpers/metadata';
417

518
/**
@@ -11,6 +24,22 @@ export default function ResponsiveLayoutsExample() {
1124
const { riveFile, isLoading, error } = useRiveFile(
1225
require('../../assets/rive/layouts_demo.riv')
1326
);
27+
const [scaleFactor, setScaleFactor] = useState(4.0);
28+
const riveRef = useRef<RiveViewRef>(null);
29+
const { width, height } = useWindowDimensions();
30+
31+
useEffect(() => {
32+
riveRef.current?.playIfNeeded();
33+
}, [width, height]);
34+
35+
const increaseScale = () => {
36+
setScaleFactor((prev) => prev + 0.5);
37+
riveRef.current?.playIfNeeded();
38+
};
39+
const decreaseScale = () => {
40+
setScaleFactor((prev) => Math.max(0.5, prev - 0.5));
41+
riveRef.current?.playIfNeeded();
42+
};
1443

1544
return (
1645
<View style={styles.container}>
@@ -20,36 +49,64 @@ export default function ResponsiveLayoutsExample() {
2049
<Text style={styles.errorText}>{error}</Text>
2150
) : riveFile ? (
2251
<RiveView
52+
hybridRef={{ f: (ref) => (riveRef.current = ref) }}
2353
file={riveFile}
2454
fit={Fit.Layout}
25-
layoutScaleFactor={1} // adjust the layout scale factor to control the layout scale
55+
layoutScaleFactor={scaleFactor}
2656
style={styles.rive}
57+
autoPlay={true}
2758
/>
2859
) : null}
60+
<View style={styles.controls}>
61+
<Text style={styles.label}>Layout Scale Factor</Text>
62+
<View style={styles.scaleControls}>
63+
<Button title="-" onPress={decreaseScale} />
64+
<View style={styles.scaleText}>
65+
<Text>{scaleFactor.toFixed(1)}x</Text>
66+
</View>
67+
<Button title="+" onPress={increaseScale} />
68+
</View>
69+
</View>
2970
</View>
3071
);
3172
}
3273

74+
ResponsiveLayoutsExample.metadata = {
75+
name: 'Responsive Layouts',
76+
description: 'Interactive layout scale factor controls',
77+
} satisfies Metadata;
78+
3379
const styles = StyleSheet.create({
34-
// Adjust the container size and the layout will adjust based on the .riv file layout rules
3580
container: {
36-
width: '100%',
37-
height: '100%',
81+
flex: 1,
3882
},
3983
rive: {
40-
justifyContent: 'center',
41-
alignItems: 'center',
4284
width: '100%',
43-
height: '100%',
85+
flex: 1,
86+
},
87+
controls: {
88+
padding: 16,
89+
alignItems: 'center',
90+
},
91+
scaleControls: {
92+
flexDirection: 'row',
93+
alignItems: 'center',
94+
marginVertical: 16,
95+
gap: 16,
96+
},
97+
scaleText: {
98+
minWidth: 50,
99+
alignItems: 'center',
100+
},
101+
label: {
102+
fontSize: 16,
103+
fontWeight: '500',
104+
marginTop: 16,
44105
},
45106
errorText: {
46107
color: 'red',
47108
textAlign: 'center',
48109
padding: 20,
110+
flex: 1,
49111
},
50112
});
51-
52-
ResponsiveLayoutsExample.metadata = {
53-
name: 'Responsive Layouts',
54-
description: 'Sample .riv file with responsive layouts',
55-
} satisfies Metadata;

expo-example/app.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "expo-example",
44
"slug": "expo-example",
55
"version": "1.0.0",
6-
"orientation": "portrait",
6+
"orientation": "default",
77
"icon": "./assets/images/icon.png",
88
"scheme": "expoexample",
99
"userInterfaceStyle": "automatic",
@@ -40,6 +40,12 @@
4040
"backgroundColor": "#000000"
4141
}
4242
}
43+
],
44+
[
45+
"expo-screen-orientation",
46+
{
47+
"initialOrientation": "DEFAULT"
48+
}
4349
]
4450
],
4551
"experiments": {

expo-example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"expo-image": "~3.0.10",
2525
"expo-linking": "~8.0.8",
2626
"expo-router": "~6.0.13",
27+
"expo-screen-orientation": "~9.0.8",
2728
"expo-splash-screen": "~31.0.10",
2829
"expo-status-bar": "~3.0.8",
2930
"expo-symbols": "~1.0.7",

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8234,6 +8234,7 @@ __metadata:
82348234
expo-image: ~3.0.10
82358235
expo-linking: ~8.0.8
82368236
expo-router: ~6.0.13
8237+
expo-screen-orientation: ~9.0.8
82378238
expo-splash-screen: ~31.0.10
82388239
expo-status-bar: ~3.0.8
82398240
expo-symbols: ~1.0.7
@@ -8412,6 +8413,16 @@ __metadata:
84128413
languageName: node
84138414
linkType: hard
84148415

8416+
"expo-screen-orientation@npm:~9.0.8":
8417+
version: 9.0.8
8418+
resolution: "expo-screen-orientation@npm:9.0.8"
8419+
peerDependencies:
8420+
expo: "*"
8421+
react-native: "*"
8422+
checksum: 9971094875bd8756bfa16a88f6a85ef7661e284ffa85a8a415fcb359f3571d2c69ea341edbf6d844dbb007e035bed13c3be0a0c20d569f10048ff5e46683ebc6
8423+
languageName: node
8424+
linkType: hard
8425+
84158426
"expo-server@npm:^1.0.3, expo-server@npm:^1.0.4":
84168427
version: 1.0.4
84178428
resolution: "expo-server@npm:1.0.4"

0 commit comments

Comments
 (0)