@rive-app/react-canvas 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.js +1 -0
- package/dist/package.json +26 -0
- package/dist/types/components/Rive.d.ts +11 -0
- package/dist/types/hooks/useRive.d.ts +16 -0
- package/dist/types/hooks/useStateMachineInput.d.ts +10 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/types.d.ts +29 -0
- package/dist/types/utils.d.ts +2 -0
- package/package.json +26 -0
package/README.md
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("@rive-app/canvas");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e),i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},i.apply(this,arguments)};function a(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(n[r[i]]=e[r[i]])}return n}function u(e){var t=e.setContainerRef,n=e.setCanvasRef,u=e.className,o=void 0===u?"":u,s=e.style,c=a(e,["setContainerRef","setCanvasRef","className","style"]),f=i({width:"100%",height:"100%"},s);return r.default.createElement("div",i({ref:t,className:o},!o&&{style:f}),r.default.createElement("canvas",i({ref:n,style:{verticalAlign:"top"}},c)))}var o={useDevicePixelRatio:!0,fitCanvasToArtboardHeight:!1,useOffscreenRenderer:!0};function s(n,a){void 0===a&&(a={});var s=e.useRef(null),c=e.useRef(null),f=e.useState(null),l=f[0],d=f[1],v=e.useState({height:0,width:0}),h=v[0],p=v[1],w=function(){var t=e.useState({width:0,height:0}),n=t[0],r=t[1];return e.useEffect(function(){if("undefined"!=typeof window){var e=function(){r({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",e),e(),function(){return window.removeEventListener("resize",e)}}},[]),n}(),g=Boolean(n),y=function(e){return Object.assign({},o,e)}(a);function R(){if(c.current){var e=function(){var e,t,n=null!==(t=null===(e=c.current)||void 0===e?void 0:e.getBoundingClientRect())&&void 0!==t?t:new DOMRect(0,0,0,0),r=n.width,i=n.height;if(l&&y.fitCanvasToArtboardHeight){var a=l.bounds;return{width:r,height:r*(a.maxY/a.maxX)}}return{width:r,height:i}}(),t=e.width,n=e.height,r=t!==h.width||n!==h.height;if(s.current&&l&&r){if(y.fitCanvasToArtboardHeight&&(c.current.style.height=n+"px"),y.useDevicePixelRatio){var i=window.devicePixelRatio||1;s.current.width=i*t,s.current.height=i*n,s.current.style.width=t+"px",s.current.style.height=n+"px"}else s.current.width=t,s.current.height=n;p({width:t,height:n}),l.startRendering()}l&&l.resizeToCanvas()}}e.useEffect(function(){l&&R()},[l,w]);var b=e.useCallback(function(e){if(e&&n){var r=y.useOffscreenRenderer,a=new t.Rive(i(i({useOffscreenRenderer:r},n),{canvas:e}));a.on(t.EventType.Load,function(){return d(a)})}else null===e&&s.current&&(s.current.height=0,s.current.width=0);s.current=e},[g]),O=e.useCallback(function(e){c.current=e},[]);e.useEffect(function(){var e=new IntersectionObserver(function(e){e[0].isIntersecting?l&&l.startRendering():l&&l.stopRendering()});return s.current&&e.observe(s.current),function(){e.disconnect()}},[l]),e.useEffect(function(){return function(){l&&(l.stop(),d(null))}},[l]);var m=null==n?void 0:n.animations;e.useEffect(function(){l&&m&&(l.stop(l.animationNames),l.play(m))},[m,l]);var C=e.useCallback(function(e){return r.default.createElement(u,i({setContainerRef:O,setCanvasRef:b},e))},[]);return{canvas:s.current,setCanvasRef:b,setContainerRef:O,rive:l,RiveComponent:C}}exports.default=function(e){var t=e.src,n=e.artboard,u=e.animations,o=e.layout,c=e.useOffscreenRenderer,f=void 0===c||c,l=a(e,["src","artboard","animations","layout","useOffscreenRenderer"]),d=s({src:t,artboard:n,animations:u,layout:o,autoplay:!0},{useOffscreenRenderer:f}).RiveComponent;return r.default.createElement(d,i({},l))},exports.useRive=s,exports.useStateMachineInput=function(t,n,r,i){var a=e.useState(null),u=a[0],o=a[1];return e.useEffect(function(){if(t&&n&&r||o(null),t&&n&&r){var e=t.stateMachineInputs(n);if(e){var a=e.find(function(e){return e.name===r});void 0!==i&&a&&(a.value=i),o(a||null)}}else o(null)},[t]),u},Object.keys(t).forEach(function(e){"default"===e||exports.hasOwnProperty(e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return t[e]}})});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rive-app/react-canvas",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "React wrapper around the @rive-app/canvas library",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/types/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/rive-app/rive-react.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/rive-app/rive-react/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/rive-app/rive-react#readme",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rive-app/canvas": "1.0.46"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Layout } from '@rive-app/canvas';
|
|
2
|
+
import { ComponentProps } from 'react';
|
|
3
|
+
export declare type RiveProps = {
|
|
4
|
+
src: string;
|
|
5
|
+
artboard?: string;
|
|
6
|
+
animations?: string | string[];
|
|
7
|
+
layout?: Layout;
|
|
8
|
+
useOffscreenRenderer?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const Rive: ({ src, artboard, animations, layout, useOffscreenRenderer, ...rest }: RiveProps & ComponentProps<'canvas'>) => JSX.Element;
|
|
11
|
+
export default Rive;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { UseRiveParameters, UseRiveOptions, RiveState } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Custom Hook for loading a Rive file.
|
|
4
|
+
*
|
|
5
|
+
* Waits until the load event has fired before returning it.
|
|
6
|
+
* We can then listen for changes to this animation in other hooks to detect
|
|
7
|
+
* when it has loaded.
|
|
8
|
+
*
|
|
9
|
+
* @param riveParams - Object containing parameters accepted by the Rive object
|
|
10
|
+
* in the rive-js runtime, with the exception of Canvas as that is attached
|
|
11
|
+
* via the ref callback `setCanvasRef`.
|
|
12
|
+
*
|
|
13
|
+
* @param opts - Optional list of options that are specific for this hook.
|
|
14
|
+
* @returns {RiveAnimationState}
|
|
15
|
+
*/
|
|
16
|
+
export default function useRive(riveParams?: UseRiveParameters, opts?: Partial<UseRiveOptions>): RiveState;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Rive, StateMachineInput } from '@rive-app/canvas';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook for fetching a stateMachine input from a rive file.
|
|
4
|
+
*
|
|
5
|
+
* @param rive - Rive instance
|
|
6
|
+
* @param stateMachineName - Name of the state machine
|
|
7
|
+
* @param inputName - Name of the input
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export default function useStateMachineInput(rive: Rive | null, stateMachineName?: string, inputName?: string, initialValue?: number | boolean): StateMachineInput | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Rive from './components/Rive';
|
|
2
|
+
import useRive from './hooks/useRive';
|
|
3
|
+
import useStateMachineInput from './hooks/useStateMachineInput';
|
|
4
|
+
export default Rive;
|
|
5
|
+
export { useRive, useStateMachineInput };
|
|
6
|
+
export { RiveState, UseRiveParameters, UseRiveOptions } from './types';
|
|
7
|
+
export * from '@rive-app/canvas';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RefCallback, ComponentProps } from 'react';
|
|
2
|
+
import { Rive, RiveParameters } from '@rive-app/canvas';
|
|
3
|
+
export declare type UseRiveParameters = Partial<Omit<RiveParameters, 'canvas'>> | null;
|
|
4
|
+
export declare type UseRiveOptions = {
|
|
5
|
+
useDevicePixelRatio: boolean;
|
|
6
|
+
fitCanvasToArtboardHeight: boolean;
|
|
7
|
+
useOffscreenRenderer: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare type Dimensions = {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @typedef RiveState
|
|
15
|
+
* @property canvas - Canvas element the Rive Animation is attached to.
|
|
16
|
+
* @property setCanvasRef - Ref callback to be passed to the canvas element.
|
|
17
|
+
* @property setContainerRef - Ref callback to be passed to the container element
|
|
18
|
+
* of the canvas. This is optional, however if not used then the hook will
|
|
19
|
+
* not take care of automatically resizing the canvas to it's outer
|
|
20
|
+
* container if the window resizes.
|
|
21
|
+
* @property rive - The loaded Rive Animation
|
|
22
|
+
*/
|
|
23
|
+
export declare type RiveState = {
|
|
24
|
+
canvas: HTMLCanvasElement | null;
|
|
25
|
+
setCanvasRef: RefCallback<HTMLCanvasElement>;
|
|
26
|
+
setContainerRef: RefCallback<HTMLElement>;
|
|
27
|
+
rive: Rive | null;
|
|
28
|
+
RiveComponent: (props: ComponentProps<'canvas'>) => JSX.Element;
|
|
29
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rive-app/react-canvas",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "React wrapper around the @rive-app/canvas library",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/types/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/rive-app/rive-react.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/rive-app/rive-react/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/rive-app/rive-react#readme",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rive-app/canvas": "1.0.46"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|