From c3a061f8b04ba6a0bf4ea30737df73d8a40728e5 Mon Sep 17 00:00:00 2001 From: Arthur Vivian Date: Fri, 10 Sep 2021 12:01:04 +0100 Subject: [PATCH] some improvements --- README.md | 10 +++++++--- examples/basic/package.json | 2 +- examples/basic/src/App.js | 4 ++-- src/hooks/useRive.tsx | 29 ++++++++++++++++++++++++++++- src/hooks/useStateMachineInput.ts | 15 ++++++++------- src/types.ts | 13 +++++++++++++ 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index edede78..e5d22c6 100644 --- a/README.md +++ b/README.md @@ -121,15 +121,19 @@ function Example() { autoplay: true, }); - const onClickInput = useStateMachineInput(rive, 'button', 'onClick'); + const onClickInput = useStateMachineInput({ + rive: rive, + stateMachineName: 'button', + inputName: 'onClick', + }); - return onClickInput && onClickInput.fire())} />; + return onClickInput && onClickInput.fire()} />; } export default Example; ``` -#### Parameters +#### params - `rive`: A `Rive` object. This is returned by the `useRive` hook. - `stateMachineName`: Name of the state machine. diff --git a/examples/basic/package.json b/examples/basic/package.json index aa62a55..ec54171 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -9,7 +9,7 @@ "react": "^17.0.2", "react-dom": "^17.0.2", "react-scripts": "4.0.3", - "rive-react": "0.0.1", + "rive-react": "file:../..", "web-vitals": "^1.1.2" }, "scripts": { diff --git a/examples/basic/src/App.js b/examples/basic/src/App.js index a80569d..e784b69 100644 --- a/examples/basic/src/App.js +++ b/examples/basic/src/App.js @@ -1,9 +1,9 @@ -import Rive from "rive-react"; +import Rive from 'rive-react'; function App() { return ( // The animation will fit to the parent element. -
+
); diff --git a/src/hooks/useRive.tsx b/src/hooks/useRive.tsx index 1c01bf0..bcf4095 100644 --- a/src/hooks/useRive.tsx +++ b/src/hooks/useRive.tsx @@ -6,7 +6,7 @@ import React, { ComponentProps, RefCallback, } from 'react'; -import { Rive, EventType } from 'rive-js'; +import { Rive, EventType, StateMachineInputType } from 'rive-js'; import { UseRiveParameters, UseRiveOptions, @@ -228,7 +228,34 @@ export default function useRive( ); }, []); + const debug = useCallback(() => { + if (!rive) { + console.log({ + loaded: false, + }); + return; + } + + const stateMachines = rive.stateMachineNames.map((stateMachineName) => ({ + stateMachineName, + stateMachineInputs: (rive.stateMachineInputs(stateMachineName) ?? []).map( + (stateMachineInput) => ({ + name: stateMachineInput.name, + value: stateMachineInput.value, + type: StateMachineInputType[stateMachineInput.type], + }) + ), + })); + + console.log({ + activeArtboard: rive.activeArtboard, + animationsNames: rive.animationNames, + stateMachines, + }); + }, [rive]); + return { + debug, canvas: canvasRef.current, setCanvasRef, setContainerRef, diff --git a/src/hooks/useStateMachineInput.ts b/src/hooks/useStateMachineInput.ts index 8183108..20eb04c 100644 --- a/src/hooks/useStateMachineInput.ts +++ b/src/hooks/useStateMachineInput.ts @@ -1,28 +1,29 @@ import { useState, useEffect } from 'react'; import { Rive, StateMachineInput } from 'rive-js'; +import { UseStateMachineInputParameters } from '../types'; /** * Custom hook for fetching a stateMachine input from a rive file. * - * @param rive - Rive instance - * @param stateMachineName - Name of the state machine - * @param inputName - Name of the input + * @param params.rive - Rive Instance + * @param params.stateMachineName - Name of the state machine + * @param params.inputName - Name of the state machine input * @returns */ export default function useStateMachineInput( - rive: Rive | null, - stateMachineName?: string, - inputName?: string + params: UseStateMachineInputParameters ) { const [input, setInput] = useState(null); + const { rive, stateMachineName, inputName } = params; + useEffect(() => { if (!rive || !stateMachineName || !inputName) { setInput(null); } if (rive && stateMachineName && inputName) { - const inputs = rive.stateMachineInputs(stateMachineName); + const inputs = (rive as Rive).stateMachineInputs(stateMachineName); if (inputs) { const selectedInput = inputs.find((input) => input.name === inputName); setInput(selectedInput || null); diff --git a/src/types.ts b/src/types.ts index 074ece6..376ac01 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,18 @@ import { RefCallback, ComponentProps } from 'react'; import { Rive, RiveParameters } from 'rive-js'; +/** + * @typedef UseStateMachineInputParameters + * @property rive - Rive Instance + * @property stateMachineName - Name of the state machine + * @property inputName - Name of the input + */ +export type UseStateMachineInputParameters = { + rive: Rive | null; + stateMachineName?: string; + inputName?: string; +}; + export type UseRiveParameters = Partial> | null; export type UseRiveOptions = { @@ -24,6 +36,7 @@ export type Dimensions = { * @property rive - The loaded Rive Animation */ export type RiveState = { + debug: () => void; canvas: HTMLCanvasElement | null; setCanvasRef: RefCallback; setContainerRef: RefCallback;