-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathManyViewModels.tsx
More file actions
168 lines (156 loc) · 3.86 KB
/
ManyViewModels.tsx
File metadata and controls
168 lines (156 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import { useState, useMemo } from 'react';
import type { Metadata } from '../helpers/metadata';
import {
DataBindMode,
RiveView,
useRiveFile,
type ViewModelInstance,
} from '@rive-app/react-native';
type BindModeOption =
| 'none'
| 'auto'
| 'red'
| 'green'
| 'blue'
| 'green-instance';
type BindModeSelectorProps = {
selectedMode: BindModeOption;
onModeChange: (mode: BindModeOption) => void;
};
function BindModeSelector({
selectedMode,
onModeChange,
}: BindModeSelectorProps) {
const modes: BindModeOption[] = [
'none',
'auto',
'red',
'green',
'blue',
'green-instance',
];
return (
<View style={selectorStyles.container}>
<Text style={selectorStyles.label}>Binding Mode:</Text>
<View style={selectorStyles.buttonRow}>
{modes.map((mode) => (
<TouchableOpacity
key={mode}
style={[
selectorStyles.button,
selectedMode === mode && selectorStyles.buttonActive,
]}
onPress={() => onModeChange(mode)}
>
<Text
style={[
selectorStyles.buttonText,
selectedMode === mode && selectorStyles.buttonTextActive,
]}
>
{mode === 'green-instance' ? 'green (instance)' : mode}
</Text>
</TouchableOpacity>
))}
</View>
</View>
);
}
function getDataBindValue(
mode: BindModeOption,
greenInstance: ViewModelInstance | undefined
) {
if (mode === 'none') return DataBindMode.None;
if (mode === 'auto') return DataBindMode.Auto;
if (mode === 'green-instance' && greenInstance) return greenInstance;
return { byName: mode };
}
export default function ManyViewModels() {
const { riveFile } = useRiveFile(
require('../../assets/rive/many_viewmodels.riv')
);
const [bindMode, setBindMode] = useState<BindModeOption>('none');
// Create a ViewModelInstance for "green" to demonstrate instance binding
const greenInstance = useMemo(() => {
if (!riveFile) return undefined;
try {
const viewModel = riveFile.defaultArtboardViewModel();
if (!viewModel) return undefined;
return viewModel.createInstanceByName('green');
} catch (e) {
console.error('Failed to create green instance:', e);
return undefined;
}
}, [riveFile]);
const dataBindValue = getDataBindValue(bindMode, greenInstance);
return (
<View style={styles.container}>
<BindModeSelector selectedMode={bindMode} onModeChange={setBindMode} />
{riveFile && (
<RiveView
style={styles.rive}
file={riveFile}
dataBind={dataBindValue}
autoPlay={true}
/>
)}
</View>
);
}
ManyViewModels.metadata = {
name: 'Select View Model',
description:
'Interactive data binding mode selector (none, auto, byName, and instance)',
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
rive: {
flex: 1,
width: '100%',
height: '100%',
},
});
const selectorStyles = StyleSheet.create({
container: {
padding: 16,
backgroundColor: '#f8f8f8',
borderBottomWidth: 1,
borderBottomColor: '#e0e0e0',
},
label: {
fontSize: 14,
fontWeight: '600',
marginBottom: 8,
color: '#333',
},
buttonRow: {
flexDirection: 'row',
gap: 8,
flexWrap: 'wrap',
},
button: {
paddingHorizontal: 16,
paddingVertical: 8,
backgroundColor: '#fff',
borderRadius: 8,
borderWidth: 1,
borderColor: '#d0d0d0',
},
buttonActive: {
backgroundColor: '#007AFF',
borderColor: '#007AFF',
},
buttonText: {
fontSize: 14,
fontWeight: '500',
color: '#333',
textTransform: 'capitalize',
},
buttonTextActive: {
color: '#fff',
},
});