diff --git a/examples/stories/StateMachineDocs.stories.mdx b/examples/stories/StateMachineDocs.stories.mdx index b48d504..ab480eb 100644 --- a/examples/stories/StateMachineDocs.stories.mdx +++ b/examples/stories/StateMachineDocs.stories.mdx @@ -1,10 +1,14 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { Canvas, Meta, Story, ArgsTable } from '@storybook/addon-docs'; -import RiveComponent, { useRive, useStateMachineInput } from '../../src'; +import RiveComponent, { + EventType, + useRive, + useStateMachineInput, +} from '../../src'; import { Button } from './components/Button'; import './rive-overview.css'; @@ -160,3 +164,35 @@ Unlike the boolean and number inputs, you invoke the `.fire()` method on a trigg }} + +## Rive Events + +To listen for Rive Events reported during state machine play, use the `on` API to add an event listener. + + + + {() => { + const STATE_MACHINE_NAME = 'State Machine 1'; + const { rive, RiveComponent } = useRive({ + src: 'rating_animation.riv', + stateMachines: STATE_MACHINE_NAME, + autoplay: true, + automaticallyHandleEvents: true, + }); + useEffect(() => { + if (rive) { + rive.on(EventType.RiveEvent, onRiveEventReceived); + } + }, [rive]); + const onRiveEventReceived = (riveEvent) => { + console.log('Rive Event Fired', riveEvent); + }; + return ( +
+ +

Click on the 5 star!

+
+ ); + }} +
+
diff --git a/examples/stories/assets/rating_animation.riv b/examples/stories/assets/rating_animation.riv new file mode 100644 index 0000000..36ac380 Binary files /dev/null and b/examples/stories/assets/rating_animation.riv differ diff --git a/package.json b/package.json index 754983a..a0ce473 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,8 @@ }, "homepage": "https://github.com/rive-app/rive-react#readme", "dependencies": { - "@rive-app/canvas": "2.3.1", - "@rive-app/webgl": "2.3.1" + "@rive-app/canvas": "2.4.0", + "@rive-app/webgl": "2.4.0" }, "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0" diff --git a/src/components/Rive.tsx b/src/components/Rive.tsx index d6408ab..3211ad4 100644 --- a/src/components/Rive.tsx +++ b/src/components/Rive.tsx @@ -36,6 +36,18 @@ export interface RiveProps { * Specify whether to resize the canvas to its container automatically */ shouldResizeCanvasToContainer?: boolean; + /** + * Enable Rive Events to be handled by the runtime. This means any special Rive Event may have + * functionality that can be invoked implicitly when detected. + * + * For example, if during the render loop an OpenUrlEvent is detected, the + * browser may try to open the specified URL in the payload. + * + * This flag is false by default to prevent any unwanted behaviors from taking place. + * This means any special Rive Event will have to be handled manually by subscribing to + * EventType.RiveEvent + */ + automaticallyHandleEvents?: boolean; } const Rive = ({ @@ -47,6 +59,7 @@ const Rive = ({ useOffscreenRenderer = true, shouldDisableRiveListeners = false, shouldResizeCanvasToContainer = true, + automaticallyHandleEvents = false, children, ...rest }: RiveProps & ComponentProps<'canvas'>) => { @@ -58,6 +71,7 @@ const Rive = ({ stateMachines, autoplay: true, shouldDisableRiveListeners, + automaticallyHandleEvents, }; const options = {