@react-three/fiber 9.6.0 → 9.6.1

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.
Files changed (28) hide show
  1. package/CHANGELOG.md +1204 -1198
  2. package/dist/declarations/src/core/events.d.ts +94 -92
  3. package/dist/declarations/src/core/hooks.d.ts +53 -53
  4. package/dist/declarations/src/core/index.d.ts +13 -13
  5. package/dist/declarations/src/core/loop.d.ts +31 -31
  6. package/dist/declarations/src/core/reconciler.d.ts +50 -50
  7. package/dist/declarations/src/core/renderer.d.ts +89 -89
  8. package/dist/declarations/src/core/store.d.ts +130 -130
  9. package/dist/declarations/src/core/utils.d.ts +262 -262
  10. package/dist/declarations/src/index.d.ts +6 -6
  11. package/dist/declarations/src/native/Canvas.d.ts +13 -13
  12. package/dist/declarations/src/native/events.d.ts +4 -4
  13. package/dist/declarations/src/native.d.ts +6 -6
  14. package/dist/declarations/src/three-types.d.ts +68 -68
  15. package/dist/declarations/src/web/Canvas.d.ts +23 -23
  16. package/dist/declarations/src/web/events.d.ts +4 -4
  17. package/dist/{events-11ca7a92.cjs.prod.js → events-583399dd.cjs.prod.js} +118 -87
  18. package/dist/{events-760a1017.esm.js → events-b389eeca.esm.js} +118 -87
  19. package/dist/{events-508aad4b.cjs.dev.js → events-f19bcc32.cjs.dev.js} +118 -87
  20. package/dist/react-three-fiber.cjs.dev.js +4 -4
  21. package/dist/react-three-fiber.cjs.prod.js +4 -4
  22. package/dist/react-three-fiber.esm.js +5 -5
  23. package/native/dist/react-three-fiber-native.cjs.dev.js +4 -4
  24. package/native/dist/react-three-fiber-native.cjs.prod.js +4 -4
  25. package/native/dist/react-three-fiber-native.esm.js +5 -5
  26. package/native/package.json +5 -5
  27. package/package.json +1 -1
  28. package/readme.md +253 -253
@@ -11,17 +11,17 @@ var threeTypes = /*#__PURE__*/Object.freeze({
11
11
  __proto__: null
12
12
  });
13
13
 
14
- /**
15
- * Returns the instance's initial (outmost) root.
14
+ /**
15
+ * Returns the instance's initial (outmost) root.
16
16
  */
17
17
  function findInitialRoot(instance) {
18
18
  let root = instance.root;
19
19
  while (root.getState().previousRoot) root = root.getState().previousRoot;
20
20
  return root;
21
21
  }
22
- /**
23
- * Safely flush async effects when testing, simulating a legacy root.
24
- * @deprecated Import from React instead. import { act } from 'react'
22
+ /**
23
+ * Safely flush async effects when testing, simulating a legacy root.
24
+ * @deprecated Import from React instead. import { act } from 'react'
25
25
  */
26
26
  // Reference with computed key to break Webpack static analysis
27
27
  // https://github.com/webpack/webpack/issues/14814
@@ -30,14 +30,14 @@ const isOrthographicCamera = def => def && def.isOrthographicCamera;
30
30
  const isRef = obj => obj && obj.hasOwnProperty('current');
31
31
  const isColorRepresentation = value => value != null && (typeof value === 'string' || typeof value === 'number' || value.isColor);
32
32
 
33
- /**
34
- * An SSR-friendly useLayoutEffect.
35
- *
36
- * React currently throws a warning when using useLayoutEffect on the server.
37
- * To get around it, we can conditionally useEffect on the server (no-op) and
38
- * useLayoutEffect elsewhere.
39
- *
40
- * @see https://github.com/facebook/react/issues/14927
33
+ /**
34
+ * An SSR-friendly useLayoutEffect.
35
+ *
36
+ * React currently throws a warning when using useLayoutEffect on the server.
37
+ * To get around it, we can conditionally useEffect on the server (no-op) and
38
+ * useLayoutEffect elsewhere.
39
+ *
40
+ * @see https://github.com/facebook/react/issues/14927
41
41
  */
42
42
  const useIsomorphicLayoutEffect = /* @__PURE__ */((_window$document, _window$navigator) => typeof window !== 'undefined' && (((_window$document = window.document) == null ? void 0 : _window$document.createElement) || ((_window$navigator = window.navigator) == null ? void 0 : _window$navigator.product) === 'ReactNative'))() ? React.useLayoutEffect : React.useEffect;
43
43
  function useMutableCallback(fn) {
@@ -45,8 +45,8 @@ function useMutableCallback(fn) {
45
45
  useIsomorphicLayoutEffect(() => void (ref.current = fn), [fn]);
46
46
  return ref;
47
47
  }
48
- /**
49
- * Bridges renderer Context and StrictMode from a primary renderer.
48
+ /**
49
+ * Bridges renderer Context and StrictMode from a primary renderer.
50
50
  */
51
51
  function useBridge() {
52
52
  const fiber = useFiber();
@@ -98,8 +98,8 @@ function calculateDpr(dpr) {
98
98
  return Array.isArray(dpr) ? Math.min(Math.max(dpr[0], target), dpr[1]) : dpr;
99
99
  }
100
100
 
101
- /**
102
- * Returns instance root state
101
+ /**
102
+ * Returns instance root state
103
103
  */
104
104
  function getRootState(obj) {
105
105
  var _r3f;
@@ -502,9 +502,9 @@ function makeId(event) {
502
502
  return (event.eventObject || event.object).uuid + '/' + event.index + event.instanceId;
503
503
  }
504
504
 
505
- /**
506
- * Release pointer captures.
507
- * This is called by releasePointerCapture in the API, and when an object is removed.
505
+ /**
506
+ * Release pointer captures.
507
+ * This is called by releasePointerCapture in the API, and when an object is removed.
508
508
  */
509
509
  function releaseInternalPointerCapture(capturedMap, obj, captures, pointerId) {
510
510
  const captureData = captures.get(obj);
@@ -517,6 +517,37 @@ function releaseInternalPointerCapture(capturedMap, obj, captures, pointerId) {
517
517
  }
518
518
  }
519
519
  }
520
+
521
+ /** This function transfers all interactivity state from one object instance to another. Used when swapping instances due to reconstruction. */
522
+ function swapInteractivity(store, object, newObject) {
523
+ const {
524
+ internal
525
+ } = store.getState();
526
+ for (let i = 0; i < internal.interaction.length; i++) {
527
+ if (internal.interaction[i] === object) internal.interaction[i] = newObject;
528
+ }
529
+ for (let i = 0; i < internal.initialHits.length; i++) {
530
+ if (internal.initialHits[i] === object) internal.initialHits[i] = newObject;
531
+ }
532
+ internal.hovered.forEach((value, key) => {
533
+ if (value.eventObject === object || value.object === object) {
534
+ internal.hovered.delete(key);
535
+ const next = {
536
+ ...value,
537
+ eventObject: value.eventObject === object ? newObject : value.eventObject,
538
+ object: value.object === object ? newObject : value.object
539
+ };
540
+ internal.hovered.set(makeId(next), next);
541
+ }
542
+ });
543
+ internal.capturedMap.forEach(captures => {
544
+ const captureData = captures.get(object);
545
+ if (captureData) {
546
+ captures.delete(object);
547
+ captures.set(newObject, captureData);
548
+ }
549
+ });
550
+ }
520
551
  function removeInteractivity(store, object) {
521
552
  const {
522
553
  internal
@@ -851,19 +882,19 @@ function createEvents(store) {
851
882
  if (!(instance != null && instance.eventCount)) return;
852
883
  const handlers = instance.handlers;
853
884
 
854
- /*
855
- MAYBE TODO, DELETE IF NOT:
856
- Check if the object is captured, captured events should not have intersects running in parallel
857
- But wouldn't it be better to just replace capturedMap with a single entry?
858
- Also, are we OK with straight up making picking up multiple objects impossible?
859
-
860
- const pointerId = (data as ThreeEvent<PointerEvent>).pointerId
861
- if (pointerId !== undefined) {
862
- const capturedMeshSet = internal.capturedMap.get(pointerId)
863
- if (capturedMeshSet) {
864
- const captured = capturedMeshSet.get(eventObject)
865
- if (captured && captured.localState.stopped) return
866
- }
885
+ /*
886
+ MAYBE TODO, DELETE IF NOT:
887
+ Check if the object is captured, captured events should not have intersects running in parallel
888
+ But wouldn't it be better to just replace capturedMap with a single entry?
889
+ Also, are we OK with straight up making picking up multiple objects impossible?
890
+
891
+ const pointerId = (data as ThreeEvent<PointerEvent>).pointerId
892
+ if (pointerId !== undefined) {
893
+ const capturedMeshSet = internal.capturedMap.get(pointerId)
894
+ if (capturedMeshSet) {
895
+ const captured = capturedMeshSet.get(eventObject)
896
+ if (captured && captured.localState.stopped) return
897
+ }
867
898
  }*/
868
899
 
869
900
  if (isPointerMove) {
@@ -1154,11 +1185,11 @@ const createStore = (invalidate, advance) => {
1154
1185
  return rootStore;
1155
1186
  };
1156
1187
 
1157
- /**
1158
- * Exposes an object's {@link Instance}.
1159
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#useInstanceHandle
1160
- *
1161
- * **Note**: this is an escape hatch to react-internal fields. Expect this to change significantly between versions.
1188
+ /**
1189
+ * Exposes an object's {@link Instance}.
1190
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#useInstanceHandle
1191
+ *
1192
+ * **Note**: this is an escape hatch to react-internal fields. Expect this to change significantly between versions.
1162
1193
  */
1163
1194
  function useInstanceHandle(ref) {
1164
1195
  const instance = React.useRef(null);
@@ -1166,9 +1197,9 @@ function useInstanceHandle(ref) {
1166
1197
  return instance;
1167
1198
  }
1168
1199
 
1169
- /**
1170
- * Returns the R3F Canvas' Zustand store. Useful for [transient updates](https://github.com/pmndrs/zustand#transient-updates-for-often-occurring-state-changes).
1171
- * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usestore
1200
+ /**
1201
+ * Returns the R3F Canvas' Zustand store. Useful for [transient updates](https://github.com/pmndrs/zustand#transient-updates-for-often-occurring-state-changes).
1202
+ * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usestore
1172
1203
  */
1173
1204
  function useStore() {
1174
1205
  const store = React.useContext(context);
@@ -1176,18 +1207,18 @@ function useStore() {
1176
1207
  return store;
1177
1208
  }
1178
1209
 
1179
- /**
1180
- * Accesses R3F's internal state, containing renderer, canvas, scene, etc.
1181
- * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usethree
1210
+ /**
1211
+ * Accesses R3F's internal state, containing renderer, canvas, scene, etc.
1212
+ * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usethree
1182
1213
  */
1183
1214
  function useThree(selector = state => state, equalityFn) {
1184
1215
  return useStore()(selector, equalityFn);
1185
1216
  }
1186
1217
 
1187
- /**
1188
- * Executes a callback before render in a shared frame loop.
1189
- * Can order effects with render priority or manually render with a positive priority.
1190
- * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe
1218
+ /**
1219
+ * Executes a callback before render in a shared frame loop.
1220
+ * Can order effects with render priority or manually render with a positive priority.
1221
+ * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe
1191
1222
  */
1192
1223
  function useFrame(callback, renderPriority = 0) {
1193
1224
  const store = useStore();
@@ -1199,9 +1230,9 @@ function useFrame(callback, renderPriority = 0) {
1199
1230
  return null;
1200
1231
  }
1201
1232
 
1202
- /**
1203
- * Returns a node graph of an object with named nodes & materials.
1204
- * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usegraph
1233
+ /**
1234
+ * Returns a node graph of an object with named nodes & materials.
1235
+ * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#usegraph
1205
1236
  */
1206
1237
  function useGraph(object) {
1207
1238
  return React.useMemo(() => buildGraph(object), [object]);
@@ -1237,11 +1268,11 @@ function loadingFn(extensions, onProgress) {
1237
1268
  };
1238
1269
  }
1239
1270
 
1240
- /**
1241
- * Synchronously loads and caches assets with a three loader.
1242
- *
1243
- * Note: this hook's caller must be wrapped with `React.Suspense`
1244
- * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#useloader
1271
+ /**
1272
+ * Synchronously loads and caches assets with a three loader.
1273
+ *
1274
+ * Note: this hook's caller must be wrapped with `React.Suspense`
1275
+ * @see https://docs.pmnd.rs/react-three-fiber/api/hooks#useloader
1245
1276
  */
1246
1277
  function useLoader(loader, input, extensions, onProgress) {
1247
1278
  // Use suspense to load async assets
@@ -1253,16 +1284,16 @@ function useLoader(loader, input, extensions, onProgress) {
1253
1284
  return Array.isArray(input) ? results : results[0];
1254
1285
  }
1255
1286
 
1256
- /**
1257
- * Preloads an asset into cache as a side-effect.
1287
+ /**
1288
+ * Preloads an asset into cache as a side-effect.
1258
1289
  */
1259
1290
  useLoader.preload = function (loader, input, extensions) {
1260
1291
  const keys = Array.isArray(input) ? input : [input];
1261
1292
  return preload(loadingFn(extensions), [loader, ...keys]);
1262
1293
  };
1263
1294
 
1264
- /**
1265
- * Removes a loaded asset from cache.
1295
+ /**
1296
+ * Removes a loaded asset from cache.
1266
1297
  */
1267
1298
  useLoader.clear = function (loader, input) {
1268
1299
  const keys = Array.isArray(input) ? input : [input];
@@ -1284,7 +1315,7 @@ useLoader.clear = function (loader, input) {
1284
1315
 
1285
1316
  var packageData = {
1286
1317
  name: "@react-three/fiber",
1287
- version: "9.6.0",
1318
+ version: "9.6.1",
1288
1319
  description: "A React renderer for Threejs",
1289
1320
  keywords: [
1290
1321
  "react",
@@ -15250,11 +15281,11 @@ function swapInstances() {
15250
15281
  const target = catalogue[toPascalCase(instance.type)];
15251
15282
 
15252
15283
  // Create object
15284
+ const prevObject = instance.object;
15253
15285
  instance.object = (_instance$props$objec = instance.props.object) != null ? _instance$props$objec : new target(...((_instance$props$args = instance.props.args) != null ? _instance$props$args : []));
15254
15286
  instance.object.__r3f = instance;
15255
15287
  setFiberRef(fiber, instance.object);
15256
-
15257
- // Set initial props
15288
+ swapInteractivity(findInitialRoot(instance), prevObject, instance.object);
15258
15289
  applyProps(instance.object, instance.props);
15259
15290
  if (instance.props.attach) {
15260
15291
  attach(parent, instance);
@@ -15864,10 +15895,10 @@ function Portal({
15864
15895
  children,
15865
15896
  container
15866
15897
  }) {
15867
- /** This has to be a component because it would not be able to call useThree/useStore otherwise since
15868
- * if this is our environment, then we are not in r3f's renderer but in react-dom, it would trigger
15869
- * the "R3F hooks can only be used within the Canvas component!" warning:
15870
- * <Canvas>
15898
+ /** This has to be a component because it would not be able to call useThree/useStore otherwise since
15899
+ * if this is our environment, then we are not in r3f's renderer but in react-dom, it would trigger
15900
+ * the "R3F hooks can only be used within the Canvas component!" warning:
15901
+ * <Canvas>
15871
15902
  * {createPortal(...)} */
15872
15903
  const {
15873
15904
  events,
@@ -15948,11 +15979,11 @@ function Portal({
15948
15979
  );
15949
15980
  }
15950
15981
 
15951
- /**
15952
- * Force React to flush any updates inside the provided callback synchronously and immediately.
15953
- * All the same caveats documented for react-dom's `flushSync` apply here (see https://react.dev/reference/react-dom/flushSync).
15954
- * Nevertheless, sometimes one needs to render synchronously, for example to keep DOM and 3D changes in lock-step without
15955
- * having to revert to a non-React solution. Note: this will only flush updates within the `Canvas` root.
15982
+ /**
15983
+ * Force React to flush any updates inside the provided callback synchronously and immediately.
15984
+ * All the same caveats documented for react-dom's `flushSync` apply here (see https://react.dev/reference/react-dom/flushSync).
15985
+ * Nevertheless, sometimes one needs to render synchronously, for example to keep DOM and 3D changes in lock-step without
15986
+ * having to revert to a non-React solution. Note: this will only flush updates within the `Canvas` root.
15956
15987
  */
15957
15988
  function flushSync(fn) {
15958
15989
  // @ts-ignore - reconciler types are not maintained
@@ -15970,21 +16001,21 @@ const globalEffects = new Set();
15970
16001
  const globalAfterEffects = new Set();
15971
16002
  const globalTailEffects = new Set();
15972
16003
 
15973
- /**
15974
- * Adds a global render callback which is called each frame.
15975
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addEffect
16004
+ /**
16005
+ * Adds a global render callback which is called each frame.
16006
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addEffect
15976
16007
  */
15977
16008
  const addEffect = callback => createSubs(callback, globalEffects);
15978
16009
 
15979
- /**
15980
- * Adds a global after-render callback which is called each frame.
15981
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addAfterEffect
16010
+ /**
16011
+ * Adds a global after-render callback which is called each frame.
16012
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addAfterEffect
15982
16013
  */
15983
16014
  const addAfterEffect = callback => createSubs(callback, globalAfterEffects);
15984
16015
 
15985
- /**
15986
- * Adds a global callback which is called when rendering stops.
15987
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addTail
16016
+ /**
16017
+ * Adds a global callback which is called when rendering stops.
16018
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addTail
15988
16019
  */
15989
16020
  const addTail = callback => createSubs(callback, globalTailEffects);
15990
16021
  function run(effects, timestamp) {
@@ -16072,9 +16103,9 @@ function loop(timestamp) {
16072
16103
  }
16073
16104
  }
16074
16105
 
16075
- /**
16076
- * Invalidates the view, requesting a frame to be rendered. Will globally invalidate unless passed a root's state.
16077
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#invalidate
16106
+ /**
16107
+ * Invalidates the view, requesting a frame to be rendered. Will globally invalidate unless passed a root's state.
16108
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#invalidate
16078
16109
  */
16079
16110
  function invalidate(state, frames = 1) {
16080
16111
  var _state$gl$xr2;
@@ -16101,9 +16132,9 @@ function invalidate(state, frames = 1) {
16101
16132
  }
16102
16133
  }
16103
16134
 
16104
- /**
16105
- * Advances the frameloop and runs render effects, useful for when manually rendering via `frameloop="never"`.
16106
- * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#advance
16135
+ /**
16136
+ * Advances the frameloop and runs render effects, useful for when manually rendering via `frameloop="never"`.
16137
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#advance
16107
16138
  */
16108
16139
  function advance(timestamp, runGlobalEffects = true, state, frame) {
16109
16140
  if (runGlobalEffects) flushGlobalEffects('before', timestamp);