mujoco-react 0.1.0 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mujoco-react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Composable React Three Fiber building blocks for MuJoCo WASM simulations",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -7,14 +7,16 @@ import { Canvas } from '@react-three/fiber';
7
7
  import { forwardRef, useEffect } from 'react';
8
8
  import { useMujoco } from './MujocoProvider';
9
9
  import { MujocoSimProvider } from './MujocoSimProvider';
10
- import { MujocoCanvasProps } from '../types';
10
+ import { MujocoCanvasProps, MujocoSimAPI } from '../types';
11
11
 
12
12
  /**
13
13
  * MujocoCanvas — thin R3F Canvas wrapper for MuJoCo scenes.
14
14
  * Accepts all R3F Canvas props and forwards them through.
15
15
  * Supports declarative physics config props (spec 1.1).
16
+ *
17
+ * Forward ref exposes MujocoSimAPI (not the canvas element).
16
18
  */
17
- export const MujocoCanvas = forwardRef<HTMLCanvasElement, MujocoCanvasProps>(
19
+ export const MujocoCanvas = forwardRef<MujocoSimAPI, MujocoCanvasProps>(
18
20
  function MujocoCanvas(
19
21
  {
20
22
  config,
@@ -49,10 +51,11 @@ export const MujocoCanvas = forwardRef<HTMLCanvasElement, MujocoCanvasProps>(
49
51
  }
50
52
 
51
53
  return (
52
- <Canvas ref={ref} {...canvasProps}>
54
+ <Canvas {...canvasProps}>
53
55
  <MujocoSimProvider
54
56
  mujoco={mujoco}
55
57
  config={config}
58
+ apiRef={ref}
56
59
  onReady={onReady}
57
60
  onError={onError}
58
61
  onStep={onStep}
@@ -157,6 +157,7 @@ export function useAfterPhysicsStep(callback: PhysicsStepCallback) {
157
157
  interface MujocoSimProviderProps {
158
158
  mujoco: MujocoModule;
159
159
  config: SceneConfig;
160
+ apiRef?: React.ForwardedRef<MujocoSimAPI>;
160
161
  onReady?: (api: MujocoSimAPI) => void;
161
162
  onError?: (error: Error) => void;
162
163
  onStep?: (time: number) => void;
@@ -174,6 +175,7 @@ interface MujocoSimProviderProps {
174
175
  export function MujocoSimProvider({
175
176
  mujoco,
176
177
  config,
178
+ apiRef: externalApiRef,
177
179
  onReady,
178
180
  onError,
179
181
  onStep,
@@ -355,10 +357,19 @@ export function MujocoSimProvider({
355
357
  };
356
358
  }, [mujoco, config]);
357
359
 
358
- // Fire onReady when status changes to ready
360
+ // Fire onReady and assign external ref when status changes to ready
359
361
  useEffect(() => {
360
- if (status === 'ready' && onReady) {
361
- onReady(apiRef.current);
362
+ if (status === 'ready') {
363
+ const api = apiRef.current;
364
+ if (onReady) onReady(api);
365
+ // Assign the forwarded ref
366
+ if (externalApiRef) {
367
+ if (typeof externalApiRef === 'function') {
368
+ externalApiRef(api);
369
+ } else {
370
+ (externalApiRef as React.MutableRefObject<MujocoSimAPI | null>).current = api;
371
+ }
372
+ }
362
373
  }
363
374
  }, [status]);
364
375