@react-three/fiber 8.0.0-beta-03 → 8.0.0-beta-04

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.
@@ -1,76 +1,35 @@
1
- import { c as createEvents, a as createLoop, b as createRenderer, d as calculateDpr, e as createStore, f as context, g as dispose, i as isRenderer, h as extend, p as pick, o as omit } from './hooks-15c12e3e.esm.js';
2
- export { t as ReactThreeFiber, k as addAfterEffect, j as addEffect, l as addTail, f as context, g as dispose, h as extend, n as useFrame, q as useGraph, r as useLoader, u as useStore, m as useThree } from './hooks-15c12e3e.esm.js';
1
+ import { c as createEvents, e as extend, p as pick, o as omit, a as createRoot, u as unmountComponentAtNode } from './index-fccd77b0.esm.js';
2
+ export { t as ReactThreeFiber, y as _roots, x as act, v as addAfterEffect, s as addEffect, w as addTail, q as advance, l as applyProps, i as context, j as createPortal, a as createRoot, m as dispose, e as extend, n as invalidate, k as reconciler, r as render, u as unmountComponentAtNode, f as useFrame, g as useGraph, h as useLoader, b as useStore, d as useThree } from './index-fccd77b0.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';
6
6
  import mergeRefs from 'react-merge-refs';
7
7
  import useMeasure from 'react-use-measure';
8
- import { DefaultEventPriority, ContinuousEventPriority, DiscreteEventPriority, ConcurrentRoot } from 'react-reconciler/constants';
9
- import 'react-reconciler';
10
- import 'scheduler';
11
8
  import 'suspend-react';
9
+ import 'react-reconciler/constants';
12
10
  import 'zustand';
11
+ import 'react-reconciler';
12
+ import 'scheduler';
13
13
 
14
- // @ts-ignore
15
- const CLICK = 'click';
16
- const CONTEXTMENU = 'contextmenu';
17
- const DBLCLICK = 'dblclick';
18
- const POINTERCANCEL = 'pointercancel';
19
- const POINTERDOWN = 'pointerdown';
20
- const POINTERUP = 'pointerup';
21
- const POINTERMOVE = 'pointermove';
22
- const POINTEROUT = 'pointerout';
23
- const POINTEROVER = 'pointerover';
24
- const POINTERENTER = 'pointerenter';
25
- const POINTERLEAVE = 'pointerleave';
26
- const WHEEL = 'wheel'; // https://github.com/facebook/react/tree/main/packages/react-reconciler#getcurrenteventpriority
27
- // Gives React a clue as to how import the current interaction is
28
-
29
- function getEventPriority() {
30
- var _window, _window$event;
31
-
32
- let name = (_window = window) == null ? void 0 : (_window$event = _window.event) == null ? void 0 : _window$event.type;
33
-
34
- switch (name) {
35
- case CLICK:
36
- case CONTEXTMENU:
37
- case DBLCLICK:
38
- case POINTERCANCEL:
39
- case POINTERDOWN:
40
- case POINTERUP:
41
- return DiscreteEventPriority;
42
-
43
- case POINTERMOVE:
44
- case POINTEROUT:
45
- case POINTEROVER:
46
- case POINTERENTER:
47
- case POINTERLEAVE:
48
- case WHEEL:
49
- return ContinuousEventPriority;
50
-
51
- default:
52
- return DefaultEventPriority;
53
- }
54
- }
14
+ const DOM_EVENTS = {
15
+ onClick: ['click', false],
16
+ onContextMenu: ['contextmenu', false],
17
+ onDoubleClick: ['dblclick', false],
18
+ onWheel: ['wheel', true],
19
+ onPointerDown: ['pointerdown', true],
20
+ onPointerUp: ['pointerup', true],
21
+ onPointerLeave: ['pointerleave', true],
22
+ onPointerMove: ['pointermove', true],
23
+ onPointerCancel: ['pointercancel', true],
24
+ onLostPointerCapture: ['lostpointercapture', true]
25
+ };
55
26
  function createPointerEvents(store) {
56
27
  const {
57
28
  handlePointer
58
29
  } = createEvents(store);
59
- const names = {
60
- onClick: [CLICK, false],
61
- onContextMenu: [CONTEXTMENU, false],
62
- onDoubleClick: [DBLCLICK, false],
63
- onWheel: [WHEEL, true],
64
- onPointerDown: [POINTERDOWN, true],
65
- onPointerUp: [POINTERUP, true],
66
- onPointerLeave: [POINTERLEAVE, true],
67
- onPointerMove: [POINTERMOVE, true],
68
- onPointerCancel: [POINTERCANCEL, true],
69
- onLostPointerCapture: ['lostpointercapture', true]
70
- };
71
30
  return {
72
31
  connected: false,
73
- handlers: Object.keys(names).reduce((acc, key) => ({ ...acc,
32
+ handlers: Object.keys(DOM_EVENTS).reduce((acc, key) => ({ ...acc,
74
33
  [key]: handlePointer(key)
75
34
  }), {}),
76
35
  connect: target => {
@@ -87,7 +46,7 @@ function createPointerEvents(store) {
87
46
  }
88
47
  }));
89
48
  Object.entries((_events$handlers = events == null ? void 0 : events.handlers) != null ? _events$handlers : []).forEach(([name, event]) => {
90
- const [eventName, passive] = names[name];
49
+ const [eventName, passive] = DOM_EVENTS[name];
91
50
  target.addEventListener(eventName, event, {
92
51
  passive
93
52
  });
@@ -104,7 +63,7 @@ function createPointerEvents(store) {
104
63
 
105
64
  Object.entries((_events$handlers2 = events.handlers) != null ? _events$handlers2 : []).forEach(([name, event]) => {
106
65
  if (events && events.connected instanceof HTMLElement) {
107
- const [eventName] = names[name];
66
+ const [eventName] = DOM_EVENTS[name];
108
67
  events.connected.removeEventListener(eventName, event);
109
68
  }
110
69
  });
@@ -118,189 +77,6 @@ function createPointerEvents(store) {
118
77
  };
119
78
  }
120
79
 
121
- const roots = new Map();
122
- const {
123
- invalidate,
124
- advance
125
- } = createLoop(roots);
126
- const {
127
- reconciler,
128
- applyProps
129
- } = createRenderer(roots, getEventPriority);
130
-
131
- const createRendererInstance = (gl, canvas) => {
132
- const customRenderer = typeof gl === 'function' ? gl(canvas) : gl;
133
- if (isRenderer(customRenderer)) return customRenderer;
134
- const renderer = new THREE.WebGLRenderer({
135
- powerPreference: 'high-performance',
136
- canvas: canvas,
137
- antialias: true,
138
- alpha: true,
139
- ...gl
140
- }); // Set color management
141
-
142
- renderer.outputEncoding = THREE.sRGBEncoding;
143
- renderer.toneMapping = THREE.ACESFilmicToneMapping; // Set gl props
144
-
145
- if (gl) applyProps(renderer, gl);
146
- return renderer;
147
- };
148
-
149
- function createRoot(canvas, config) {
150
- return {
151
- render: element => {
152
- var _store;
153
-
154
- let {
155
- gl,
156
- size,
157
- events,
158
- onCreated,
159
- ...props
160
- } = config || {}; // Allow size to take on container bounds initially
161
-
162
- if (!size) {
163
- var _canvas$parentElement, _canvas$parentElement2, _canvas$parentElement3, _canvas$parentElement4;
164
-
165
- size = {
166
- width: (_canvas$parentElement = (_canvas$parentElement2 = canvas.parentElement) == null ? void 0 : _canvas$parentElement2.clientWidth) != null ? _canvas$parentElement : 0,
167
- height: (_canvas$parentElement3 = (_canvas$parentElement4 = canvas.parentElement) == null ? void 0 : _canvas$parentElement4.clientHeight) != null ? _canvas$parentElement3 : 0
168
- };
169
- }
170
-
171
- let root = roots.get(canvas);
172
- let fiber = root == null ? void 0 : root.fiber;
173
- let store = root == null ? void 0 : root.store;
174
- let state = (_store = store) == null ? void 0 : _store.getState();
175
-
176
- if (fiber && state) {
177
- // When a root was found, see if any fundamental props must be changed or exchanged
178
- // Check pixelratio
179
- if (props.dpr !== undefined && state.viewport.dpr !== calculateDpr(props.dpr)) state.setDpr(props.dpr); // Check size
180
-
181
- if (state.size.width !== size.width || state.size.height !== size.height) state.setSize(size.width, size.height); // Check frameloop
182
-
183
- if (state.frameloop !== props.frameloop) state.setFrameloop(props.frameloop); // For some props we want to reset the entire root
184
- // Changes to the color-space
185
-
186
- const linearChanged = props.linear !== state.internal.lastProps.linear;
187
-
188
- if (linearChanged) {
189
- unmountComponentAtNode(canvas);
190
- fiber = undefined;
191
- }
192
- }
193
-
194
- if (!fiber) {
195
- // If no root has been found, make one
196
- // Create gl
197
- const glRenderer = createRendererInstance(gl, canvas); // Create store
198
-
199
- store = createStore(applyProps, invalidate, advance, {
200
- gl: glRenderer,
201
- size,
202
- ...props
203
- });
204
- const state = store.getState(); // Create renderer
205
-
206
- fiber = reconciler.createContainer(store, ConcurrentRoot, false, null); // Map it
207
-
208
- roots.set(canvas, {
209
- fiber,
210
- store
211
- }); // Store events internally
212
-
213
- if (events) state.set({
214
- events: events(store)
215
- });
216
- }
217
-
218
- if (store && fiber) {
219
- reconciler.updateContainer( /*#__PURE__*/React.createElement(Provider, {
220
- store: store,
221
- element: element,
222
- onCreated: onCreated,
223
- target: canvas
224
- }), fiber, null, () => undefined);
225
- return store;
226
- } else {
227
- throw 'Error creating root!';
228
- }
229
- },
230
- unmount: () => unmountComponentAtNode(canvas)
231
- };
232
- }
233
-
234
- function render(element, canvas, config = {}) {
235
- console.warn('R3F.render is no longer supported in React 18. Use createRoot instead!');
236
- return createRoot(canvas, config).render(element);
237
- }
238
-
239
- function Provider({
240
- store,
241
- element,
242
- onCreated,
243
- target
244
- }) {
245
- React.useEffect(() => {
246
- const state = store.getState(); // Flag the canvas active, rendering will now begin
247
-
248
- state.set(state => ({
249
- internal: { ...state.internal,
250
- active: true
251
- }
252
- })); // Connect events
253
-
254
- state.events.connect == null ? void 0 : state.events.connect(target); // Notifiy that init is completed, the scene graph exists, but nothing has yet rendered
255
-
256
- if (onCreated) onCreated(state); // eslint-disable-next-line react-hooks/exhaustive-deps
257
- }, []);
258
- return /*#__PURE__*/React.createElement(context.Provider, {
259
- value: store
260
- }, element);
261
- }
262
-
263
- function unmountComponentAtNode(canvas, callback) {
264
- const root = roots.get(canvas);
265
- const fiber = root == null ? void 0 : root.fiber;
266
-
267
- if (fiber) {
268
- const state = root == null ? void 0 : root.store.getState();
269
- if (state) state.internal.active = false;
270
- reconciler.updateContainer(null, fiber, null, () => {
271
- if (state) {
272
- setTimeout(() => {
273
- try {
274
- var _state$gl, _state$gl$renderLists, _state$gl2, _state$gl3;
275
-
276
- state.events.disconnect == null ? void 0 : state.events.disconnect();
277
- (_state$gl = state.gl) == null ? void 0 : (_state$gl$renderLists = _state$gl.renderLists) == null ? void 0 : _state$gl$renderLists.dispose == null ? void 0 : _state$gl$renderLists.dispose();
278
- (_state$gl2 = state.gl) == null ? void 0 : _state$gl2.forceContextLoss == null ? void 0 : _state$gl2.forceContextLoss();
279
- if ((_state$gl3 = state.gl) != null && _state$gl3.xr) state.internal.xr.disconnect();
280
- dispose(state);
281
- roots.delete(canvas);
282
- if (callback) callback(canvas);
283
- } catch (e) {
284
- /* ... */
285
- }
286
- }, 500);
287
- }
288
- });
289
- }
290
- }
291
-
292
- const act = React.unstable_act;
293
-
294
- function createPortal(children, container) {
295
- return reconciler.createPortal(children, container, null, null);
296
- }
297
-
298
- reconciler.injectIntoDevTools({
299
- bundleType: process.env.NODE_ENV === 'production' ? 0 : 1,
300
- rendererPackageName: '@react-three/fiber',
301
- version: '18.0.0'
302
- });
303
-
304
80
  const CANVAS_PROPS = ['gl', 'events', 'shadows', 'linear', 'flat', 'orthographic', 'frameloop', 'dpr', 'performance', 'clock', 'raycaster', 'camera', 'onPointerMissed', 'onCreated'];
305
81
 
306
82
  function Block({
@@ -359,6 +135,7 @@ const Canvas = /*#__PURE__*/React.forwardRef(function Canvas({
359
135
  ...resize
360
136
  });
361
137
  const canvasRef = React.useRef(null);
138
+ const [canvas, setCanvas] = React.useState(null);
362
139
  const canvasProps = pick(props, CANVAS_PROPS);
363
140
  const divProps = omit(props, CANVAS_PROPS);
364
141
  const [block, setBlock] = React.useState(false);
@@ -368,8 +145,8 @@ const Canvas = /*#__PURE__*/React.forwardRef(function Canvas({
368
145
 
369
146
  if (error) throw error;
370
147
 
371
- if (width > 0 && height > 0) {
372
- createRoot(canvasRef.current, { ...canvasProps,
148
+ if (width > 0 && height > 0 && canvas) {
149
+ createRoot(canvas, { ...canvasProps,
373
150
  size: {
374
151
  width,
375
152
  height
@@ -384,10 +161,12 @@ const Canvas = /*#__PURE__*/React.forwardRef(function Canvas({
384
161
  }, children)));
385
162
  }
386
163
 
387
- React.useEffect(() => {
388
- const container = canvasRef.current;
389
- return () => unmountComponentAtNode(container);
164
+ React.useLayoutEffect(() => {
165
+ setCanvas(canvasRef.current);
390
166
  }, []);
167
+ React.useEffect(() => {
168
+ return () => unmountComponentAtNode(canvas);
169
+ }, [canvas]);
391
170
  return /*#__PURE__*/React.createElement("div", _extends({
392
171
  ref: containerRef,
393
172
  style: {
@@ -405,4 +184,4 @@ const Canvas = /*#__PURE__*/React.forwardRef(function Canvas({
405
184
  }, fallback));
406
185
  });
407
186
 
408
- export { Canvas, roots as _roots, act, advance, applyProps, createPortal, createRoot, createPointerEvents as events, invalidate, reconciler, render, unmountComponentAtNode };
187
+ export { Canvas, createPointerEvents as events };