@rive-app/react-canvas 3.0.49 → 3.0.51

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/dist/index.js CHANGED
@@ -1 +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}var o=function(){function e(){}return e.prototype.observe=function(){},e.prototype.unobserve=function(){},e.prototype.disconnect=function(){},e}();var u=globalThis.ResizeObserver||o,s=void 0!==globalThis.ResizeObserver,c=!s;function f(t){var n=e.useState({width:0,height:0}),r=n[0],i=n[1];e.useEffect(function(){if("undefined"!=typeof window){var e=function(){i({width:window.innerWidth,height:window.innerHeight})};return c&&(e(),window.addEventListener("resize",e)),function(){return window.removeEventListener("resize",e)}}},[]);var a,o,f,l=e.useRef(new u((a=function(e){s&&i({width:e[e.length-1].contentRect.width,height:e[e.length-1].contentRect.height})},o=0,f=0,function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];clearTimeout(f),f=window.setTimeout(function(){return a.apply(e,t)},o)})));return e.useEffect(function(){var e=l.current;return t.current&&s&&e.observe(t.current),function(){e.disconnect(),t.current&&s&&e.unobserve(t.current)}},[t,l]),r}function l(){var e="undefined"!=typeof window&&"number"==typeof window.devicePixelRatio?window.devicePixelRatio:1;return Math.min(Math.max(1,e),3)}function d(e){var t=e.setContainerRef,n=e.setCanvasRef,o=e.className,u=void 0===o?"":o,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:u},!u&&{style:f}),r.default.createElement("canvas",i({ref:n,style:{verticalAlign:"top",width:0,height:0}},c)))}var h={useDevicePixelRatio:!0,fitCanvasToArtboardHeight:!1,useOffscreenRenderer:!0};function v(n,a){void 0===a&&(a={});var o=e.useRef(null),u=e.useRef(null),s=e.useState(null),c=s[0],v=s[1],p=e.useState({height:0,width:0}),w=p[0],g=p[1],y=e.useState({height:0,width:0}),b=y[0],m=y[1],R=f(u),O=function(){var t=l(),n=e.useState(t),r=n[0],i=n[1];return e.useEffect(function(){if("undefined"!=typeof window&&"matchMedia"in window){var e=function(){var e=l();i(e)},t=window.matchMedia("screen and (resolution: ".concat(r,"dppx)"));return t.hasOwnProperty("addEventListener")?t.addEventListener("change",e):t.addListener(e),function(){t.hasOwnProperty("removeEventListener")?t.removeEventListener("change",e):t.removeListener(e)}}},[r]),r}(),E=Boolean(n),x=function(e){return Object.assign({},h,e)}(a);function C(){if(u.current){var e=function(){var e,t,n,r,i=null!==(t=null===(e=u.current)||void 0===e?void 0:e.clientWidth)&&void 0!==t?t:0,a=null!==(r=null===(n=u.current)||void 0===n?void 0:n.clientHeight)&&void 0!==r?r:0;if(c&&x.fitCanvasToArtboardHeight){var o=c.bounds;return{width:i,height:i*(o.maxY/o.maxX)}}return{width:i,height:a}}(),t=e.width,n=e.height;if(o.current&&c){var r=t!==w.width||n!==w.height;if(x.fitCanvasToArtboardHeight&&r&&(u.current.style.height=n+"px"),x.useDevicePixelRatio){var i=t*O!==b.width||n*O!==b.height;if(r||i){var a=O*t,s=O*n;o.current.width=a,o.current.height=s,o.current.style.width=t+"px",o.current.style.height=n+"px",m({width:a,height:s})}}else r&&(o.current.width=t,o.current.height=n,m({width:t,height:n}));g({width:t,height:n}),c.startRendering()}c&&c.resizeToCanvas()}}e.useEffect(function(){c&&C()},[c,R,O]);var P=e.useCallback(function(e){if(e&&n&&E){var r=x.useOffscreenRenderer,a=new t.Rive(i(i({useOffscreenRenderer:r},n),{canvas:e}));a.on(t.EventType.Load,function(){o.current?v(a):a.cleanup()})}else null===e&&o.current&&(o.current.height=0,o.current.width=0);o.current=e},[E]),L=e.useCallback(function(e){u.current=e},[]);e.useEffect(function(){var e=new IntersectionObserver(function(e){e[0].isIntersecting?c&&c.startRendering():c&&c.stopRendering()});return o.current&&e.observe(o.current),function(){e.disconnect()}},[c]),e.useEffect(function(){return function(){c&&(c.cleanup(),v(null))}},[c]);var j=null==n?void 0:n.animations;e.useEffect(function(){c&&j&&(c.isPlaying?(c.stop(c.animationNames),c.play(j)):c.isPaused&&(c.stop(c.animationNames),c.pause(j)))},[j,c]);var M=e.useCallback(function(e){return r.default.createElement(d,i({setContainerRef:L,setCanvasRef:P},e))},[]);return{canvas:o.current,setCanvasRef:P,setContainerRef:L,rive:c,RiveComponent:M}}exports.default=function(e){var t=e.src,n=e.artboard,o=e.animations,u=e.stateMachines,s=e.layout,c=e.useOffscreenRenderer,f=void 0===c||c,l=e.shouldDisableRiveListeners,d=void 0!==l&&l,h=a(e,["src","artboard","animations","stateMachines","layout","useOffscreenRenderer","shouldDisableRiveListeners"]),p=v({src:t,artboard:n,animations:o,layout:s,stateMachines:u,autoplay:!0,shouldDisableRiveListeners:d},{useOffscreenRenderer:f}).RiveComponent;return r.default.createElement(p,i({},h))},exports.useRive=v,exports.useStateMachineInput=function(n,r,i,a){var o=e.useState(null),u=o[0],s=o[1];return e.useEffect(function(){function e(){if(n&&r&&i||s(null),n&&r&&i){var e=n.stateMachineInputs(r);if(e){var t=e.find(function(e){return e.name===i});void 0!==a&&t&&(t.value=a),s(t||null)}}else s(null)}e(),n&&n.on(t.EventType.Play,function(){e()})},[n]),u},Object.keys(t).forEach(function(e){"default"===e||exports.hasOwnProperty(e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return t[e]}})});
1
+ Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),n=require("@rive-app/canvas");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=t(e),i=function(){return i=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var i in n=arguments[t])Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i]);return e},i.apply(this,arguments)};function a(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.indexOf(r)<0&&(t[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)n.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(t[r[i]]=e[r[i]])}return t}function o(){var e="undefined"!=typeof window&&"number"==typeof window.devicePixelRatio?window.devicePixelRatio:1;return Math.min(Math.max(1,e),3)}var s=function(){function e(){}return e.prototype.observe=function(){},e.prototype.unobserve=function(){},e.prototype.disconnect=function(){},e}();var u=globalThis.ResizeObserver||s,c=void 0!==globalThis.ResizeObserver,f=!c;function d(n,t){void 0===t&&(t=!0);var r=e.useState({width:0,height:0}),i=r[0],a=r[1];e.useEffect(function(){if("undefined"!=typeof window&&t){var e=function(){a({width:window.innerWidth,height:window.innerHeight})};return f&&(e(),window.addEventListener("resize",e)),function(){return window.removeEventListener("resize",e)}}},[]);var o,s,d,l=e.useRef(new u((o=function(e){c&&a({width:e[e.length-1].contentRect.width,height:e[e.length-1].contentRect.height})},s=0,d=0,function(){for(var e=this,n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];clearTimeout(d),d=window.setTimeout(function(){return o.apply(e,n)},s)})));return e.useEffect(function(){var e=l.current;if(t){var r=n.current;return n.current&&c&&e.observe(n.current),function(){e.disconnect(),r&&c&&e.unobserve(r)}}e.disconnect()},[n,l]),i}var l={useDevicePixelRatio:!0,fitCanvasToArtboardHeight:!1,useOffscreenRenderer:!0,shouldResizeCanvasToContainer:!0};function v(e){return Object.assign({},l,e)}function h(n){var t=n.riveLoaded,r=void 0!==t&&t,i=n.canvasRef,a=n.containerRef,s=n.options,u=void 0===s?{}:s,c=n.onCanvasHasResized,f=n.artboardBounds,l=v(u),h=e.useState({height:0,width:0}),p=h[0],w=p.height,R=p.width,g=h[1],b=e.useState({height:0,width:0}),y=b[0],m=y.height,C=y.width,O=b[1],x=e.useState(!0),E=x[0],P=x[1],T=l.fitCanvasToArtboardHeight,L=l.shouldResizeCanvasToContainer,z=l.useDevicePixelRatio,j=d(a,L),M=function(){var n=o(),t=e.useState(n),r=t[0],i=t[1];return e.useEffect(function(){if("undefined"!=typeof window&&"matchMedia"in window){var e=function(){var e=o();i(e)},n=window.matchMedia("screen and (resolution: ".concat(r,"dppx)"));return n.hasOwnProperty("addEventListener")?n.addEventListener("change",e):n.addListener(e),function(){n.hasOwnProperty("removeEventListener")?n.removeEventListener("change",e):n.removeListener(e)}}},[r]),r}(),S=null!=f?f:{},H=S.maxX,k=S.maxY,D=e.useCallback(function(){var e,n,t,r,i=null!==(n=null===(e=a.current)||void 0===e?void 0:e.clientWidth)&&void 0!==n?n:0,o=null!==(r=null===(t=a.current)||void 0===t?void 0:t.clientHeight)&&void 0!==r?r:0;return T&&f?{width:i,height:i*(f.maxY/f.maxX)}:{width:i,height:o}},[a,T,H,k]);e.useEffect(function(){if(L&&a.current&&r){var e=D(),n=e.width,t=e.height,o=!1;if(i.current){var s=n!==R||t!==w;if(l.fitCanvasToArtboardHeight&&s&&(a.current.style.height=t+"px",o=!0),l.useDevicePixelRatio){if(s||(n*M!==C||t*M!==m)){var u=M*n,f=M*t;i.current.width=u,i.current.height=f,i.current.style.width=n+"px",i.current.style.height=t+"px",O({width:u,height:f}),o=!0}}else s&&(i.current.width=n,i.current.height=t,O({width:n,height:t}),o=!0);g({width:n,height:t})}c&&(E||o)&&c&&c(),E&&P(!1)}},[i,a,j,M,D,E,P,m,C,w,R,c,L,T,z,r])}function p(e){var n=e.setContainerRef,t=e.setCanvasRef,o=e.className,s=void 0===o?"":o,u=e.style,c=e.children,f=a(e,["setContainerRef","setCanvasRef","className","style","children"]),d=i({width:"100%",height:"100%"},u);return r.default.createElement("div",i({ref:n,className:s},!s&&{style:d}),r.default.createElement("canvas",i({ref:t,style:{verticalAlign:"top",width:0,height:0}},f),c))}function w(t,a){void 0===a&&(a={});var o=e.useRef(null),s=e.useRef(null),u=e.useState(null),c=u[0],f=u[1],d=Boolean(t),l=v(a),w=e.useCallback(function(){c&&(c.startRendering(),c.resizeToCanvas())},[c]);h({riveLoaded:!!c,canvasRef:o,containerRef:s,options:l,onCanvasHasResized:w,artboardBounds:null==c?void 0:c.bounds});var R=e.useCallback(function(e){if(e&&t&&d){var r=l.useOffscreenRenderer,a=new n.Rive(i(i({useOffscreenRenderer:r},t),{canvas:e}));a.on(n.EventType.Load,function(){o.current?f(a):a.cleanup()})}else null===e&&o.current&&(o.current.height=0,o.current.width=0);o.current=e},[d]),g=e.useCallback(function(e){s.current=e},[]);e.useEffect(function(){var e=new IntersectionObserver(function(e){e[0].isIntersecting?c&&c.startRendering():c&&c.stopRendering()});return o.current&&e.observe(o.current),function(){e.disconnect()}},[c]),e.useEffect(function(){return function(){c&&(c.cleanup(),f(null))}},[c]);var b=null==t?void 0:t.animations;e.useEffect(function(){c&&b&&(c.isPlaying?(c.stop(c.animationNames),c.play(b)):c.isPaused&&(c.stop(c.animationNames),c.pause(b)))},[b,c]);var y=e.useCallback(function(e){return r.default.createElement(p,i({setContainerRef:g,setCanvasRef:R},e))},[R,g]);return{canvas:o.current,container:s.current,setCanvasRef:R,setContainerRef:g,rive:c,RiveComponent:y}}exports.default=function(e){var n=e.src,t=e.artboard,o=e.animations,s=e.stateMachines,u=e.layout,c=e.useOffscreenRenderer,f=void 0===c||c,d=e.shouldDisableRiveListeners,l=void 0!==d&&d,v=e.shouldResizeCanvasToContainer,h=void 0===v||v,p=e.children,R=a(e,["src","artboard","animations","stateMachines","layout","useOffscreenRenderer","shouldDisableRiveListeners","shouldResizeCanvasToContainer","children"]),g=w({src:n,artboard:t,animations:o,layout:u,stateMachines:s,autoplay:!0,shouldDisableRiveListeners:l},{useOffscreenRenderer:f,shouldResizeCanvasToContainer:h}).RiveComponent;return r.default.createElement(g,i({},R),p)},exports.useResizeCanvas=h,exports.useRive=w,exports.useStateMachineInput=function(t,r,i,a){var o=e.useState(null),s=o[0],u=o[1];return e.useEffect(function(){function e(){if(t&&r&&i||u(null),t&&r&&i){var e=t.stateMachineInputs(r);if(e){var n=e.find(function(e){return e.name===i});void 0!==a&&n&&(n.value=a),u(n||null)}}else u(null)}e(),t&&t.on(n.EventType.Play,function(){e()})},[t]),s},Object.keys(n).forEach(function(e){"default"===e||exports.hasOwnProperty(e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return n[e]}})});
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rive-app/react-canvas",
3
- "version": "3.0.49",
3
+ "version": "3.0.51",
4
4
  "description": "React wrapper around the @rive-app/canvas library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/types/index.d.ts",
@@ -30,6 +30,10 @@ export interface RiveProps {
30
30
  * Specify whether to disable Rive listeners on the canvas, thus preventing any event listeners to be attached to the canvas element
31
31
  */
32
32
  shouldDisableRiveListeners?: boolean;
33
+ /**
34
+ * Specify whether to resize the canvas to its container automatically
35
+ */
36
+ shouldResizeCanvasToContainer?: boolean;
33
37
  }
34
- declare const Rive: ({ src, artboard, animations, stateMachines, layout, useOffscreenRenderer, shouldDisableRiveListeners, ...rest }: RiveProps & ComponentProps<'canvas'>) => React.JSX.Element;
38
+ declare const Rive: ({ src, artboard, animations, stateMachines, layout, useOffscreenRenderer, shouldDisableRiveListeners, shouldResizeCanvasToContainer, children, ...rest }: RiveProps & ComponentProps<'canvas'>) => React.JSX.Element;
35
39
  export default Rive;
@@ -0,0 +1,10 @@
1
+ import { Dimensions } from '../types';
2
+ /**
3
+ * Hook to listen for a ref element's resize events being triggered. When resized,
4
+ * it sets state to an object of {width: number, height: number} indicating the contentRect
5
+ * size of the element at the new resize.
6
+ *
7
+ * @param containerRef - Ref element to listen for resize events on
8
+ * @returns - Size object with width and height attributes
9
+ */
10
+ export default function useSize(containerRef: React.MutableRefObject<HTMLElement | null>, shouldResizeCanvasToContainer?: boolean): Dimensions;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Listen for devicePixelRatio changes and set the new value accordingly. This could
3
+ * happen for reasons such as:
4
+ * - User moves window from retina screen display to a separate monitor
5
+ * - User controls zoom settings on the browser
6
+ *
7
+ * Source: https://github.com/rexxars/use-device-pixel-ratio/blob/main/index.ts
8
+ *
9
+ * @returns dpr: Number - Device pixel ratio; ratio of physical px to resolution in CSS pixels for current device
10
+ */
11
+ export default function useDevicePixelRatio(): number;
@@ -0,0 +1,52 @@
1
+ import { MutableRefObject } from 'react';
2
+ import { Bounds } from '@rive-app/canvas';
3
+ import { UseRiveOptions } from '../types';
4
+ interface UseResizeCanvasProps {
5
+ /**
6
+ * Whether or not Rive is loaded and renderer is associated with the canvas
7
+ */
8
+ riveLoaded: boolean;
9
+ /**
10
+ * Ref to the canvas element
11
+ */
12
+ canvasRef: MutableRefObject<HTMLCanvasElement | null>;
13
+ /**
14
+ * Ref to the container element of the canvas
15
+ */
16
+ containerRef: MutableRefObject<HTMLElement | null>;
17
+ /**
18
+ * (Optional) Callback to be invoked after the canvas has been resized due to a resize
19
+ * of its parent container. This is where you would want to reset the layout
20
+ * dimensions for the Rive renderer to dictate the new min/max bounds of the
21
+ * canvas.
22
+ *
23
+ * Using the high-level JS runtime, this might be a simple call to `rive.resizeToCanvas()`
24
+ * Using the low-level JSruntime, this might be invoking the renderer's `.align()` method
25
+ * with the Layout and min/max X/Y values of the canvas.
26
+ *
27
+ * @returns void
28
+ */
29
+ onCanvasHasResized?: () => void;
30
+ /**
31
+ * (Optional) Options passed to the useRive hook, including the shouldResizeCanvasToContainer option
32
+ * which prevents the canvas element from resizing to its parent container
33
+ */
34
+ options?: Partial<UseRiveOptions>;
35
+ /**
36
+ * (Optional) AABB bounds of the artboard. If provided, the canvas will be sized to the artboard
37
+ * height if the fitCanvasToArtboardHeight option is true.
38
+ */
39
+ artboardBounds?: Bounds;
40
+ }
41
+ /**
42
+ * Helper hook to listen for changes in the <canvas> parent container size and size the <canvas>
43
+ * to match. If a resize event has occurred, a supplied callback (onCanvasHasResized)
44
+ * will be inokved to allow for any re-calculation needed (i.e. Rive layout on the canvas).
45
+ *
46
+ * This hook is useful if you are not intending to use the `useRive` hook yourself, but still
47
+ * want to use the auto-sizing logic on the canvas/container.
48
+ *
49
+ * @param props - Object to supply necessary props to the hook
50
+ */
51
+ export default function useResizeCanvas({ riveLoaded, canvasRef, containerRef, options, onCanvasHasResized, artboardBounds, }: UseResizeCanvasProps): void;
52
+ export {};
@@ -1,7 +1,8 @@
1
1
  import Rive from './components/Rive';
2
2
  import useRive from './hooks/useRive';
3
3
  import useStateMachineInput from './hooks/useStateMachineInput';
4
+ import useResizeCanvas from './hooks/useResizeCanvas';
4
5
  export default Rive;
5
- export { useRive, useStateMachineInput };
6
+ export { useRive, useStateMachineInput, useResizeCanvas };
6
7
  export { RiveState, UseRiveParameters, UseRiveOptions } from './types';
7
8
  export * from '@rive-app/canvas';
@@ -5,6 +5,7 @@ export type UseRiveOptions = {
5
5
  useDevicePixelRatio: boolean;
6
6
  fitCanvasToArtboardHeight: boolean;
7
7
  useOffscreenRenderer: boolean;
8
+ shouldResizeCanvasToContainer: boolean;
8
9
  };
9
10
  export type Dimensions = {
10
11
  width: number;
@@ -13,6 +14,7 @@ export type Dimensions = {
13
14
  /**
14
15
  * @typedef RiveState
15
16
  * @property canvas - Canvas element the Rive Animation is attached to.
17
+ * @property container - Container element of the canvas.
16
18
  * @property setCanvasRef - Ref callback to be passed to the canvas element.
17
19
  * @property setContainerRef - Ref callback to be passed to the container element
18
20
  * of the canvas. This is optional, however if not used then the hook will
@@ -22,6 +24,7 @@ export type Dimensions = {
22
24
  */
23
25
  export type RiveState = {
24
26
  canvas: HTMLCanvasElement | null;
27
+ container: HTMLElement | null;
25
28
  setCanvasRef: RefCallback<HTMLCanvasElement>;
26
29
  setContainerRef: RefCallback<HTMLElement>;
27
30
  rive: Rive | null;
@@ -1,15 +1,7 @@
1
- import React from 'react';
2
- import { Dimensions } from './types';
3
- export declare function useSize(containerRef: React.MutableRefObject<HTMLElement | null>): Dimensions;
4
- /**
5
- * Listen for devicePixelRatio changes and set the new value accordingly. This could
6
- * happen for reasons such as:
7
- * - User moves window from retina screen display to a separate monitor
8
- * - User controls zoom settings on the browser
9
- *
10
- * Source: https://github.com/rexxars/use-device-pixel-ratio/blob/main/index.ts
11
- *
12
- * @returns dpr: Number - Device pixel ratio; ratio of physical px to resolution in CSS pixels for current device
13
- */
14
- export declare function useDevicePixelRatio(): number;
15
- export declare function getDevicePixelRatio(): number;
1
+ import { UseRiveOptions } from './types';
2
+ export declare function getOptions(opts: Partial<UseRiveOptions>): {
3
+ useDevicePixelRatio: boolean;
4
+ fitCanvasToArtboardHeight: boolean;
5
+ useOffscreenRenderer: boolean;
6
+ shouldResizeCanvasToContainer: boolean;
7
+ } & Partial<UseRiveOptions>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rive-app/react-canvas",
3
- "version": "3.0.49",
3
+ "version": "3.0.51",
4
4
  "description": "React wrapper around the @rive-app/canvas library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/types/index.d.ts",