-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuseRiveTrigger.ts
More file actions
49 lines (43 loc) · 1.31 KB
/
useRiveTrigger.ts
File metadata and controls
49 lines (43 loc) · 1.31 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
import { useCallback, useMemo } from 'react';
import {
type ViewModelInstance,
type ViewModelTriggerProperty,
} from '../specs/ViewModel.nitro';
import type {
UseRiveTriggerResult,
UseViewModelInstanceTriggerParameters,
} from '../types';
import { useRiveProperty } from './useRiveProperty';
const getTriggerProperty = (vmi: ViewModelInstance, p: string) =>
vmi.triggerProperty(p);
/**
* Hook for interacting with trigger ViewModel instance properties.
*
* @param path - The path to the trigger property
* @param viewModelInstance - The ViewModelInstance containing the trigger property to operate on
* @returns A trigger function that can be called to fire the trigger
*/
export function useRiveTrigger(
path: string,
viewModelInstance?: ViewModelInstance | null,
params?: UseViewModelInstanceTriggerParameters
): UseRiveTriggerResult {
const { onTrigger } = params ?? {};
const triggerOptions = useMemo(
() => ({
getProperty: getTriggerProperty,
onPropertyEventOverride: onTrigger,
}),
[onTrigger]
);
const [_, __, error, property] = useRiveProperty<
ViewModelTriggerProperty,
undefined
>(viewModelInstance, path, triggerOptions);
const trigger = useCallback(() => {
if (property) {
property.trigger();
}
}, [property]);
return { trigger, error };
}