Skip to content

Commit

Permalink
feature: Bump WASM dependency to support Rive Events
Browse files Browse the repository at this point in the history
  • Loading branch information
zplata committed Sep 13, 2023
1 parent 982addf commit 6ba68fa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
40 changes: 38 additions & 2 deletions examples/stories/StateMachineDocs.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<!-- RiveTestHook.stories.mdx -->

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';

Expand Down Expand Up @@ -160,3 +164,35 @@ Unlike the boolean and number inputs, you invoke the `.fire()` method on a trigg
}}
</Story>
</Canvas>

## Rive Events

To listen for Rive Events reported during state machine play, use the `on` API to add an event listener.

<Canvas withSource="open">
<Story name="Rive Events">
{() => {
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 (
<div className="center">
<RiveComponent className="base-canvas-size" />
<p>Click on the 5 star!</p>
</div>
);
}}
</Story>
</Canvas>
Binary file added examples/stories/assets/rating_animation.riv
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions src/components/Rive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -47,6 +59,7 @@ const Rive = ({
useOffscreenRenderer = true,
shouldDisableRiveListeners = false,
shouldResizeCanvasToContainer = true,
automaticallyHandleEvents = false,
children,
...rest
}: RiveProps & ComponentProps<'canvas'>) => {
Expand All @@ -58,6 +71,7 @@ const Rive = ({
stateMachines,
autoplay: true,
shouldDisableRiveListeners,
automaticallyHandleEvents,
};

const options = {
Expand Down

0 comments on commit 6ba68fa

Please sign in to comment.