@react-three/fiber 8.6.2 → 8.7.2

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.
@@ -39,6 +39,7 @@ var threeTypes = /*#__PURE__*/Object.freeze({
39
39
 
40
40
  var _window$document, _window$navigator;
41
41
  const isOrthographicCamera = def => def && def.isOrthographicCamera;
42
+ const isRef = obj => obj && obj.hasOwnProperty('current');
42
43
  /**
43
44
  * An SSR-friendly useLayoutEffect.
44
45
  *
@@ -503,9 +504,7 @@ function removeInteractivity(store, object) {
503
504
  });
504
505
  }
505
506
  function createEvents(store) {
506
- const temp = new THREE__namespace.Vector3();
507
507
  /** Calculates delta */
508
-
509
508
  function calculateDistance(event) {
510
509
  const {
511
510
  internal
@@ -605,108 +604,113 @@ function createEvents(store) {
605
604
 
606
605
 
607
606
  function handleIntersects(intersections, event, delta, callback) {
608
- const {
609
- raycaster,
610
- pointer,
611
- camera,
612
- internal
613
- } = store.getState(); // If anything has been found, forward it to the event listeners
614
-
607
+ // If anything has been found, forward it to the event listeners
615
608
  if (intersections.length) {
616
- const unprojectedPoint = temp.set(pointer.x, pointer.y, 0).unproject(camera);
617
609
  const localState = {
618
610
  stopped: false
619
611
  };
620
612
 
621
613
  for (const hit of intersections) {
622
- const hasPointerCapture = id => {
623
- var _internal$capturedMap, _internal$capturedMap2;
614
+ const state = getRootState(hit.object);
624
615
 
625
- return (_internal$capturedMap = (_internal$capturedMap2 = internal.capturedMap.get(id)) == null ? void 0 : _internal$capturedMap2.has(hit.eventObject)) != null ? _internal$capturedMap : false;
626
- };
616
+ if (state) {
617
+ const {
618
+ raycaster,
619
+ pointer,
620
+ camera,
621
+ internal
622
+ } = state;
623
+ const unprojectedPoint = new THREE__namespace.Vector3(pointer.x, pointer.y, 0).unproject(camera);
624
+
625
+ const hasPointerCapture = id => {
626
+ var _internal$capturedMap, _internal$capturedMap2;
627
627
 
628
- const setPointerCapture = id => {
629
- const captureData = {
630
- intersection: hit,
631
- target: event.target
628
+ return (_internal$capturedMap = (_internal$capturedMap2 = internal.capturedMap.get(id)) == null ? void 0 : _internal$capturedMap2.has(hit.eventObject)) != null ? _internal$capturedMap : false;
632
629
  };
633
630
 
634
- if (internal.capturedMap.has(id)) {
635
- // if the pointerId was previously captured, we add the hit to the
636
- // event capturedMap.
637
- internal.capturedMap.get(id).set(hit.eventObject, captureData);
638
- } else {
639
- // if the pointerId was not previously captured, we create a map
640
- // containing the hitObject, and the hit. hitObject is used for
641
- // faster access.
642
- internal.capturedMap.set(id, new Map([[hit.eventObject, captureData]]));
643
- } // Call the original event now
644
- event.target.setPointerCapture(id);
645
- };
631
+ const setPointerCapture = id => {
632
+ const captureData = {
633
+ intersection: hit,
634
+ target: event.target
635
+ };
636
+
637
+ if (internal.capturedMap.has(id)) {
638
+ // if the pointerId was previously captured, we add the hit to the
639
+ // event capturedMap.
640
+ internal.capturedMap.get(id).set(hit.eventObject, captureData);
641
+ } else {
642
+ // if the pointerId was not previously captured, we create a map
643
+ // containing the hitObject, and the hit. hitObject is used for
644
+ // faster access.
645
+ internal.capturedMap.set(id, new Map([[hit.eventObject, captureData]]));
646
+ } // Call the original event now
647
+ event.target.setPointerCapture(id);
648
+ };
646
649
 
647
- const releasePointerCapture = id => {
648
- const captures = internal.capturedMap.get(id);
650
+ const releasePointerCapture = id => {
651
+ const captures = internal.capturedMap.get(id);
649
652
 
650
- if (captures) {
651
- releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
652
- }
653
- }; // Add native event props
653
+ if (captures) {
654
+ releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
655
+ }
656
+ }; // Add native event props
654
657
 
655
658
 
656
- let extractEventProps = {}; // This iterates over the event's properties including the inherited ones. Native PointerEvents have most of their props as getters which are inherited, but polyfilled PointerEvents have them all as their own properties (i.e. not inherited). We can't use Object.keys() or Object.entries() as they only return "own" properties; nor Object.getPrototypeOf(event) as that *doesn't* return "own" properties, only inherited ones.
659
+ let extractEventProps = {}; // This iterates over the event's properties including the inherited ones. Native PointerEvents have most of their props as getters which are inherited, but polyfilled PointerEvents have them all as their own properties (i.e. not inherited). We can't use Object.keys() or Object.entries() as they only return "own" properties; nor Object.getPrototypeOf(event) as that *doesn't* return "own" properties, only inherited ones.
657
660
 
658
- for (let prop in event) {
659
- let property = event[prop]; // Only copy over atomics, leave functions alone as these should be
660
- // called as event.nativeEvent.fn()
661
+ for (let prop in event) {
662
+ let property = event[prop]; // Only copy over atomics, leave functions alone as these should be
663
+ // called as event.nativeEvent.fn()
661
664
 
662
- if (typeof property !== 'function') extractEventProps[prop] = property;
663
- }
665
+ if (typeof property !== 'function') extractEventProps[prop] = property;
666
+ }
664
667
 
665
- let raycastEvent = { ...hit,
666
- ...extractEventProps,
667
- pointer,
668
- intersections,
669
- stopped: localState.stopped,
670
- delta,
671
- unprojectedPoint,
672
- ray: raycaster.ray,
673
- camera: camera,
674
- // Hijack stopPropagation, which just sets a flag
675
- stopPropagation: () => {
676
- // https://github.com/pmndrs/react-three-fiber/issues/596
677
- // Events are not allowed to stop propagation if the pointer has been captured
678
- const capturesForPointer = 'pointerId' in event && internal.capturedMap.get(event.pointerId); // We only authorize stopPropagation...
679
-
680
- if ( // ...if this pointer hasn't been captured
681
- !capturesForPointer || // ... or if the hit object is capturing the pointer
682
- capturesForPointer.has(hit.eventObject)) {
683
- raycastEvent.stopped = localState.stopped = true; // Propagation is stopped, remove all other hover records
684
- // An event handler is only allowed to flush other handlers if it is hovered itself
685
-
686
- if (internal.hovered.size && Array.from(internal.hovered.values()).find(i => i.eventObject === hit.eventObject)) {
687
- // Objects cannot flush out higher up objects that have already caught the event
688
- const higher = intersections.slice(0, intersections.indexOf(hit));
689
- cancelPointer([...higher, hit]);
668
+ let raycastEvent = { ...hit,
669
+ ...extractEventProps,
670
+ pointer,
671
+ intersections,
672
+ stopped: localState.stopped,
673
+ delta,
674
+ unprojectedPoint,
675
+ ray: raycaster.ray,
676
+ camera: camera,
677
+ // Hijack stopPropagation, which just sets a flag
678
+ stopPropagation: () => {
679
+ // https://github.com/pmndrs/react-three-fiber/issues/596
680
+ // Events are not allowed to stop propagation if the pointer has been captured
681
+ const capturesForPointer = 'pointerId' in event && internal.capturedMap.get(event.pointerId); // We only authorize stopPropagation...
682
+
683
+ if ( // ...if this pointer hasn't been captured
684
+ !capturesForPointer || // ... or if the hit object is capturing the pointer
685
+ capturesForPointer.has(hit.eventObject)) {
686
+ raycastEvent.stopped = localState.stopped = true; // Propagation is stopped, remove all other hover records
687
+ // An event handler is only allowed to flush other handlers if it is hovered itself
688
+
689
+ if (internal.hovered.size && Array.from(internal.hovered.values()).find(i => i.eventObject === hit.eventObject)) {
690
+ // Objects cannot flush out higher up objects that have already caught the event
691
+ const higher = intersections.slice(0, intersections.indexOf(hit));
692
+ cancelPointer([...higher, hit]);
693
+ }
690
694
  }
691
- }
692
- },
693
- // there should be a distinction between target and currentTarget
694
- target: {
695
- hasPointerCapture,
696
- setPointerCapture,
697
- releasePointerCapture
698
- },
699
- currentTarget: {
700
- hasPointerCapture,
701
- setPointerCapture,
702
- releasePointerCapture
703
- },
704
- nativeEvent: event
705
- }; // Call subscribers
706
-
707
- callback(raycastEvent); // Event bubbling may be interrupted by stopPropagation
708
-
709
- if (localState.stopped === true) break;
695
+ },
696
+ // there should be a distinction between target and currentTarget
697
+ target: {
698
+ hasPointerCapture,
699
+ setPointerCapture,
700
+ releasePointerCapture
701
+ },
702
+ currentTarget: {
703
+ hasPointerCapture,
704
+ setPointerCapture,
705
+ releasePointerCapture
706
+ },
707
+ nativeEvent: event
708
+ }; // Call subscribers
709
+
710
+ callback(raycastEvent); // Event bubbling may be interrupted by stopPropagation
711
+
712
+ if (localState.stopped === true) break;
713
+ }
710
714
  }
711
715
  }
712
716
 
@@ -1495,11 +1499,24 @@ const addAfterEffect = callback => createSubs(callback, globalAfterEffects);
1495
1499
  const addTail = callback => createSubs(callback, globalTailEffects);
1496
1500
 
1497
1501
  function run(effects, timestamp) {
1502
+ if (!effects.size) return;
1498
1503
  effects.forEach(({
1499
1504
  callback
1500
1505
  }) => callback(timestamp));
1501
1506
  }
1502
1507
 
1508
+ function flushGlobalEffects(type, timestamp) {
1509
+ switch (type) {
1510
+ case 'before':
1511
+ return run(globalEffects, timestamp);
1512
+
1513
+ case 'after':
1514
+ return run(globalAfterEffects, timestamp);
1515
+
1516
+ case 'tail':
1517
+ return run(globalTailEffects, timestamp);
1518
+ }
1519
+ }
1503
1520
  let subscribers;
1504
1521
  let subscription;
1505
1522
 
@@ -1539,7 +1556,7 @@ function createLoop(roots) {
1539
1556
  running = true;
1540
1557
  repeat = 0; // Run effects
1541
1558
 
1542
- if (globalEffects.size) run(globalEffects, timestamp); // Render all roots
1559
+ flushGlobalEffects('before', timestamp); // Render all roots
1543
1560
 
1544
1561
  roots.forEach(root => {
1545
1562
  var _state$gl$xr;
@@ -1551,11 +1568,11 @@ function createLoop(roots) {
1551
1568
  }
1552
1569
  }); // Run after-effects
1553
1570
 
1554
- if (globalAfterEffects.size) run(globalAfterEffects, timestamp); // Stop the loop if nothing invalidates it
1571
+ flushGlobalEffects('after', timestamp); // Stop the loop if nothing invalidates it
1555
1572
 
1556
1573
  if (repeat === 0) {
1557
1574
  // Tail call effects, they are called when rendering stops
1558
- if (globalTailEffects.size) run(globalTailEffects, timestamp); // Flag end of operation
1575
+ flushGlobalEffects('tail', timestamp); // Flag end of operation
1559
1576
 
1560
1577
  running = false;
1561
1578
  return cancelAnimationFrame(frame);
@@ -1577,9 +1594,9 @@ function createLoop(roots) {
1577
1594
  }
1578
1595
 
1579
1596
  function advance(timestamp, runGlobalEffects = true, state, frame) {
1580
- if (runGlobalEffects) run(globalEffects, timestamp);
1597
+ if (runGlobalEffects) flushGlobalEffects('before', timestamp);
1581
1598
  if (!state) roots.forEach(root => render$1(timestamp, root.store.getState()));else render$1(timestamp, state, frame);
1582
- if (runGlobalEffects) run(globalAfterEffects, timestamp);
1599
+ if (runGlobalEffects) flushGlobalEffects('after', timestamp);
1583
1600
  }
1584
1601
 
1585
1602
  return {
@@ -1599,6 +1616,17 @@ function createLoop(roots) {
1599
1616
  };
1600
1617
  }
1601
1618
 
1619
+ /**
1620
+ * Exposes an object's {@link LocalState}.
1621
+ * @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#useInstanceHandle
1622
+ *
1623
+ * **Note**: this is an escape hatch to react-internal fields. Expect this to change significantly between versions.
1624
+ */
1625
+ function useInstanceHandle(ref) {
1626
+ const instance = React__namespace.useRef(null);
1627
+ useIsomorphicLayoutEffect(() => void (instance.current = ref.current.__r3f), [ref]);
1628
+ return instance;
1629
+ }
1602
1630
  function useStore() {
1603
1631
  const store = React__namespace.useContext(context);
1604
1632
  if (!store) throw new Error('R3F: Hooks can only be used within the Canvas component!');
@@ -2148,6 +2176,7 @@ exports.dispose = dispose;
2148
2176
  exports.extend = extend;
2149
2177
  exports.getRootState = getRootState;
2150
2178
  exports.invalidate = invalidate;
2179
+ exports.isRef = isRef;
2151
2180
  exports.reconciler = reconciler;
2152
2181
  exports.render = render;
2153
2182
  exports.roots = roots;
@@ -2155,6 +2184,7 @@ exports.threeTypes = threeTypes;
2155
2184
  exports.unmountComponentAtNode = unmountComponentAtNode;
2156
2185
  exports.useFrame = useFrame;
2157
2186
  exports.useGraph = useGraph;
2187
+ exports.useInstanceHandle = useInstanceHandle;
2158
2188
  exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
2159
2189
  exports.useLoader = useLoader;
2160
2190
  exports.useMutableCallback = useMutableCallback;
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./index-9a338808.cjs.dev.js');
5
+ var index = require('./index-407f3d70.cjs.dev.js');
6
6
  var _extends = require('@babel/runtime/helpers/extends');
7
7
  var React = require('react');
8
8
  var THREE = require('three');
@@ -187,7 +187,7 @@ const Canvas = /*#__PURE__*/React__namespace.forwardRef(function Canvas({
187
187
  onPointerMissed: (...args) => handlePointerMissed.current == null ? void 0 : handlePointerMissed.current(...args),
188
188
  onCreated: state => {
189
189
  // Connect to event source
190
- state.events.connect == null ? void 0 : state.events.connect(eventSource ? eventSource : divRef.current); // Set up compute function
190
+ state.events.connect == null ? void 0 : state.events.connect(eventSource ? index.isRef(eventSource) ? eventSource.current : eventSource : divRef.current); // Set up compute function
191
191
 
192
192
  if (eventPrefix) {
193
193
  state.setEvents({
@@ -263,6 +263,7 @@ exports.render = index.render;
263
263
  exports.unmountComponentAtNode = index.unmountComponentAtNode;
264
264
  exports.useFrame = index.useFrame;
265
265
  exports.useGraph = index.useGraph;
266
+ exports.useInstanceHandle = index.useInstanceHandle;
266
267
  exports.useLoader = index.useLoader;
267
268
  exports.useStore = index.useStore;
268
269
  exports.useThree = index.useThree;
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./index-f368ca4a.cjs.prod.js');
5
+ var index = require('./index-5350eaac.cjs.prod.js');
6
6
  var _extends = require('@babel/runtime/helpers/extends');
7
7
  var React = require('react');
8
8
  var THREE = require('three');
@@ -187,7 +187,7 @@ const Canvas = /*#__PURE__*/React__namespace.forwardRef(function Canvas({
187
187
  onPointerMissed: (...args) => handlePointerMissed.current == null ? void 0 : handlePointerMissed.current(...args),
188
188
  onCreated: state => {
189
189
  // Connect to event source
190
- state.events.connect == null ? void 0 : state.events.connect(eventSource ? eventSource : divRef.current); // Set up compute function
190
+ state.events.connect == null ? void 0 : state.events.connect(eventSource ? index.isRef(eventSource) ? eventSource.current : eventSource : divRef.current); // Set up compute function
191
191
 
192
192
  if (eventPrefix) {
193
193
  state.setEvents({
@@ -263,6 +263,7 @@ exports.render = index.render;
263
263
  exports.unmountComponentAtNode = index.unmountComponentAtNode;
264
264
  exports.useFrame = index.useFrame;
265
265
  exports.useGraph = index.useGraph;
266
+ exports.useInstanceHandle = index.useInstanceHandle;
266
267
  exports.useLoader = index.useLoader;
267
268
  exports.useStore = index.useStore;
268
269
  exports.useThree = index.useThree;
@@ -1,5 +1,5 @@
1
- import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, E as ErrorBoundary, B as Block, b as useIsomorphicLayoutEffect, d as unmountComponentAtNode } from './index-cecf55e8.esm.js';
2
- export { t as ReactThreeFiber, s as _roots, q as act, n as addAfterEffect, m as addEffect, o as addTail, l as advance, i as applyProps, f as context, c as createEvents, g as createPortal, a as createRoot, j as dispose, e as extend, p as getRootState, k as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, x as useFrame, y as useGraph, z as useLoader, v as useStore, w as useThree } from './index-cecf55e8.esm.js';
1
+ import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, i as isRef, E as ErrorBoundary, B as Block, b as useIsomorphicLayoutEffect, d as unmountComponentAtNode } from './index-05f8627d.esm.js';
2
+ export { t as ReactThreeFiber, v as _roots, s as act, o as addAfterEffect, n as addEffect, p as addTail, m as advance, j as applyProps, f as context, c as createEvents, g as createPortal, a as createRoot, k as dispose, e as extend, q as getRootState, l as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, z as useFrame, A as useGraph, w as useInstanceHandle, C as useLoader, x as useStore, y as useThree } from './index-05f8627d.esm.js';
3
3
  import _extends from '@babel/runtime/helpers/esm/extends';
4
4
  import * as React from 'react';
5
5
  import * as THREE from 'three';
@@ -160,7 +160,7 @@ const Canvas = /*#__PURE__*/React.forwardRef(function Canvas({
160
160
  onPointerMissed: (...args) => handlePointerMissed.current == null ? void 0 : handlePointerMissed.current(...args),
161
161
  onCreated: state => {
162
162
  // Connect to event source
163
- state.events.connect == null ? void 0 : state.events.connect(eventSource ? eventSource : divRef.current); // Set up compute function
163
+ state.events.connect == null ? void 0 : state.events.connect(eventSource ? isRef(eventSource) ? eventSource.current : eventSource : divRef.current); // Set up compute function
164
164
 
165
165
  if (eventPrefix) {
166
166
  state.setEvents({
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-9a338808.cjs.dev.js');
5
+ var index = require('../../dist/index-407f3d70.cjs.dev.js');
6
6
  var _extends = require('@babel/runtime/helpers/extends');
7
7
  var React = require('react');
8
8
  var THREE = require('three');
@@ -401,6 +401,7 @@ exports.render = index.render;
401
401
  exports.unmountComponentAtNode = index.unmountComponentAtNode;
402
402
  exports.useFrame = index.useFrame;
403
403
  exports.useGraph = index.useGraph;
404
+ exports.useInstanceHandle = index.useInstanceHandle;
404
405
  exports.useLoader = index.useLoader;
405
406
  exports.useStore = index.useStore;
406
407
  exports.useThree = index.useThree;
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-f368ca4a.cjs.prod.js');
5
+ var index = require('../../dist/index-5350eaac.cjs.prod.js');
6
6
  var _extends = require('@babel/runtime/helpers/extends');
7
7
  var React = require('react');
8
8
  var THREE = require('three');
@@ -401,6 +401,7 @@ exports.render = index.render;
401
401
  exports.unmountComponentAtNode = index.unmountComponentAtNode;
402
402
  exports.useFrame = index.useFrame;
403
403
  exports.useGraph = index.useGraph;
404
+ exports.useInstanceHandle = index.useInstanceHandle;
404
405
  exports.useLoader = index.useLoader;
405
406
  exports.useStore = index.useStore;
406
407
  exports.useThree = index.useThree;
@@ -1,5 +1,5 @@
1
- import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, E as ErrorBoundary, B as Block, d as unmountComponentAtNode } from '../../dist/index-cecf55e8.esm.js';
2
- export { t as ReactThreeFiber, s as _roots, q as act, n as addAfterEffect, m as addEffect, o as addTail, l as advance, i as applyProps, f as context, g as createPortal, a as createRoot, j as dispose, e as extend, p as getRootState, k as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, x as useFrame, y as useGraph, z as useLoader, v as useStore, w as useThree } from '../../dist/index-cecf55e8.esm.js';
1
+ import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, E as ErrorBoundary, B as Block, d as unmountComponentAtNode } from '../../dist/index-05f8627d.esm.js';
2
+ export { t as ReactThreeFiber, v as _roots, s as act, o as addAfterEffect, n as addEffect, p as addTail, m as advance, j as applyProps, f as context, g as createPortal, a as createRoot, k as dispose, e as extend, q as getRootState, l as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, z as useFrame, A as useGraph, w as useInstanceHandle, C as useLoader, x as useStore, y as useThree } from '../../dist/index-05f8627d.esm.js';
3
3
  import _extends from '@babel/runtime/helpers/esm/extends';
4
4
  import * as React from 'react';
5
5
  import * as THREE from 'three';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/fiber",
3
- "version": "8.6.2",
3
+ "version": "8.7.2",
4
4
  "description": "A React renderer for Threejs",
5
5
  "keywords": [
6
6
  "react",