@@ -3,14 +3,24 @@ import type {
33 ViewModelColorProperty ,
44 ViewModelInstance ,
55} from '../specs/ViewModel.nitro' ;
6- import { type UseRivePropertyResult } from '../types' ;
76import { useRiveProperty } from './useRiveProperty' ;
87import { RiveColor } from '../core/RiveColor' ;
98
109const COLOR_PROPERTY_OPTIONS = {
1110 getProperty : ( vmi : ViewModelInstance , p : string ) => vmi . colorProperty ( p ) ,
1211} ;
1312
13+ type ColorInput = RiveColor | string ;
14+ type ColorSetValueAction =
15+ | ColorInput
16+ | ( ( prevValue : RiveColor | undefined ) => ColorInput ) ;
17+
18+ export interface UseRiveColorResult {
19+ value : RiveColor | undefined ;
20+ setValue : ( value : ColorSetValueAction ) => void ;
21+ error : Error | null ;
22+ }
23+
1424/**
1525 * Hook for interacting with color ViewModel instance properties.
1626 *
@@ -21,9 +31,7 @@ const COLOR_PROPERTY_OPTIONS = {
2131export function useRiveColor (
2232 path : string ,
2333 viewModelInstance ?: ViewModelInstance | null
24- ) : UseRivePropertyResult < RiveColor > & {
25- setValue : ( value : RiveColor | string ) => void ;
26- } {
34+ ) : UseRiveColorResult {
2735 const [ rawValue , setRawValue , error ] = useRiveProperty <
2836 ViewModelColorProperty ,
2937 number
@@ -33,12 +41,20 @@ export function useRiveColor(
3341 rawValue !== undefined ? RiveColor . fromInt ( rawValue ) : undefined ;
3442
3543 const setValue = useCallback (
36- ( newValue : RiveColor | string ) => {
37- const color =
38- typeof newValue === 'string'
39- ? RiveColor . fromHexString ( newValue )
40- : newValue ;
41- setRawValue ( color . toInt ( ) ) ;
44+ ( valueOrUpdater : ColorSetValueAction ) => {
45+ setRawValue ( ( prevRaw : number | undefined ) => {
46+ const prevColor =
47+ prevRaw !== undefined ? RiveColor . fromInt ( prevRaw ) : undefined ;
48+ const newColorInput =
49+ typeof valueOrUpdater === 'function'
50+ ? valueOrUpdater ( prevColor )
51+ : valueOrUpdater ;
52+ const color =
53+ typeof newColorInput === 'string'
54+ ? RiveColor . fromHexString ( newColorInput )
55+ : newColorInput ;
56+ return color . toInt ( ) ;
57+ } ) ;
4258 } ,
4359 [ setRawValue ]
4460 ) ;
0 commit comments