Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add useStateMachineInputs hook #310

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
60 changes: 60 additions & 0 deletions src/hooks/useStateMachineInputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useState, useEffect } from 'react';
import { EventType, Rive, StateMachineInput } from '@rive-app/canvas';

/**
* Custom hook for fetching multiple stateMachine inputs from a rive file.
* Particularly useful for fetching multiple inputs from a variable number of input names.
*
* @param rive - Rive instance
* @param stateMachineName - Name of the state machine
* @param inputNames - Names of the inputs
* @returns StateMachineInput[] | null
*/
export default function useStateMachineInputs(
rive: Rive | null,
stateMachineName?: string,
inputNames?: {
name: string;
initialValue?: number | boolean;
}[]
) {
const [inputs, setInputs] = useState<StateMachineInput[] | null>(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about removing the union with null and having inputs only be StateMachineInput[]?
I think an empty array is a simpler api to deal with.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that definitely seems like a better option. I'll change that!


useEffect(() => {
function setStateMachineInput() {
if (!rive || !stateMachineName || !inputNames) {
setInputs(null);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary? It seems the next lines are checking the same and the else statement is also handling the alternate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it doesn't seem necessary to me either, but I had that in because I found the same logic in the useStateMachineInput code.
I could create another pull request to refactor the useStateMachineInput hook if you're okay with it.
I'll work on removing some of the repeated logic.


if (rive && stateMachineName && inputNames) {
const inputs = rive.stateMachineInputs(stateMachineName);
if (inputs) {
const selectedInputs = inputs.filter((input) =>
inputNames.some((inputName) => inputName.name === input.name)
);
if (selectedInputs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this validation isn't needed, a filter call will always return an array at this point

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! I'll fix that too!

selectedInputs.forEach((input) => {
const targetInputName = inputNames.find(inputName => inputName.name === input.name);
if(targetInputName?.initialValue){
input.value = targetInputName.initialValue
}
});
}
setInputs(selectedInputs);
}
} else {
setInputs(null);
}
}
setStateMachineInput();
if (rive) {
rive.on(EventType.Load, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for safety, the useEffect hook can return a clean up function to clear this callback.
If not, multiple instances of the function might try to set the input values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll add a rive.off() cleanup function.

// Check if the component/canvas is mounted before setting state to avoid setState
// on an unmounted component in some rare cases
setStateMachineInput();
});
}
}, [inputNames, rive, stateMachineName]);

return inputs;
}
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ import useRive from './hooks/useRive';
import useStateMachineInput from './hooks/useStateMachineInput';
import useResizeCanvas from './hooks/useResizeCanvas';
import useRiveFile from './hooks/useRiveFile';
import useStateMachineInputs from './hooks/useStateMachineInputs';

export default Rive;
export { useRive, useStateMachineInput, useResizeCanvas, useRiveFile , RiveProps };
export { RiveState, UseRiveParameters, UseRiveFileParameters, UseRiveOptions } from './types';
export {
useRive,
useStateMachineInput,
useStateMachineInputs,
useResizeCanvas,
useRiveFile,
RiveProps,
};
export {
RiveState,
UseRiveParameters,
UseRiveFileParameters,
UseRiveOptions,
} from './types';

export * from '@rive-app/canvas';