Skip to content

Commit 3ef7086

Browse files
Merge branch 'master' into v9
2 parents 8ebcdbd + bae3556 commit 3ef7086

File tree

7 files changed

+64
-131
lines changed

7 files changed

+64
-131
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"react": "^18.0.0",
7878
"react-dom": "^18.0.0",
7979
"react-native": "0.69.3",
80-
"react-nil": "^1.2.0",
8180
"react-test-renderer": "^18.0.0",
8281
"regenerator-runtime": "^0.13.9",
8382
"three": "^0.141.0",

packages/fiber/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @react-three/fiber
22

3+
## 8.8.1
4+
5+
### Patch Changes
6+
7+
- 370d3ae5: refactor: pull context bridge from its-fine
8+
39
## 8.8.0
410

511
### Minor Changes

packages/fiber/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@babel/runtime": "^7.17.8",
4646
"@types/react-reconciler": "^0.26.7",
47+
"its-fine": "^1.0.0",
4748
"react-reconciler": "^0.27.0",
4849
"react-use-measure": "^2.1.1",
4950
"scheduler": "^0.21.0",

packages/fiber/src/core/utils.ts

-82
Original file line numberDiff line numberDiff line change
@@ -31,88 +31,6 @@ export function useMutableCallback<T>(fn: T): React.MutableRefObject<T> {
3131
return ref
3232
}
3333

34-
/**
35-
* Traverses up or down a {@link Fiber}, return `true` to stop and select a node.
36-
*/
37-
function traverseFiber(fiber: Fiber, ascending: boolean, selector: (node: Fiber) => boolean | void): Fiber | undefined {
38-
if (selector(fiber) === true) return fiber
39-
40-
let child = ascending ? fiber.return : fiber.child
41-
while (child) {
42-
const match = traverseFiber(child, ascending, selector)
43-
if (match) return match
44-
45-
child = child.sibling
46-
}
47-
}
48-
49-
// Active contexts
50-
const contexts: React.Context<any>[] = []
51-
52-
/**
53-
* Represents a react-context bridge provider component.
54-
*/
55-
export type ContextBridge = React.FC<{ children?: React.ReactNode }>
56-
57-
/**
58-
* React Context currently cannot be shared across [React renderers](https://reactjs.org/docs/codebase-overview.html#renderers) but explicitly forwarded between providers (see [react#17275](https://github.com/facebook/react/issues/17275)). This hook returns a {@link ContextBridge} of live context providers to pierce Context across renderers.
59-
*
60-
* Pass {@link ContextBridge} as a component to a secondary renderer to enable context-sharing within its children.
61-
*/
62-
export function useContextBridge(fiber?: Fiber): ContextBridge {
63-
if (fiber) {
64-
traverseFiber(fiber, true, (node) => {
65-
const context = node.type?._context
66-
if (!context || contexts.includes(context)) return
67-
68-
// In development, React will warn about using contexts between renderers because
69-
// of the above issue. We'll hide the warning because this hook works as expected
70-
// https://github.com/facebook/react/pull/12779
71-
Object.defineProperties(context, {
72-
_currentRenderer: {
73-
get() {
74-
return null
75-
},
76-
set() {},
77-
},
78-
_currentRenderer2: {
79-
get() {
80-
return null
81-
},
82-
set() {},
83-
},
84-
})
85-
86-
contexts.push(context)
87-
})
88-
}
89-
90-
return contexts.reduce(
91-
(Prev, context) => {
92-
const value = (
93-
React as any
94-
).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher.current?.readContext(context)
95-
return (props) => React.createElement(Prev, null, React.createElement(context.Provider, { ...props, value }))
96-
},
97-
(props) => React.createElement(React.Fragment, props),
98-
)
99-
}
100-
101-
/**
102-
* Exposes the current react-internal {@link Fiber}.
103-
*/
104-
export class FiberProvider extends React.Component<{ setFiber: React.Dispatch<Fiber>; children?: React.ReactNode }> {
105-
private _reactInternals!: Fiber
106-
107-
componentDidMount() {
108-
this.props.setFiber(this._reactInternals)
109-
}
110-
111-
render() {
112-
return this.props.children
113-
}
114-
}
115-
11634
export type SetBlock = false | Promise<null> | null
11735
export type UnblockProps = { set: React.Dispatch<React.SetStateAction<SetBlock>>; children: React.ReactNode }
11836

packages/fiber/src/native/Canvas.tsx

+19-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as React from 'react'
22
import * as THREE from 'three'
33
import { View, ViewProps, ViewStyle, LayoutChangeEvent, StyleSheet, PixelRatio } from 'react-native'
44
import { ExpoWebGLRenderingContext, GLView } from 'expo-gl'
5-
import { SetBlock, Block, ErrorBoundary, useMutableCallback, useContextBridge, FiberProvider } from '../core/utils'
5+
import { useContextBridge, FiberProvider } from 'its-fine'
6+
import { SetBlock, Block, ErrorBoundary, useMutableCallback } from '../core/utils'
67
import { extend, createRoot, unmountComponentAtNode, RenderProps, ReconcilerRoot } from '../core'
78
import { createTouchEvents } from './events'
89
import { RootState, Size } from '../core/store'
@@ -13,11 +14,7 @@ export interface CanvasProps extends Omit<RenderProps<HTMLCanvasElement>, 'size'
1314
style?: ViewStyle
1415
}
1516

16-
/**
17-
* A native canvas which accepts threejs elements as children.
18-
* @see https://docs.pmnd.rs/react-three-fiber/api/canvas
19-
*/
20-
export const Canvas = /*#__PURE__*/ React.forwardRef<View, CanvasProps>(
17+
const CanvasImpl = /*#__PURE__*/ React.forwardRef<View, CanvasProps>(
2118
(
2219
{
2320
children,
@@ -45,8 +42,7 @@ export const Canvas = /*#__PURE__*/ React.forwardRef<View, CanvasProps>(
4542
// their own elements by using the createRoot API instead
4643
React.useMemo(() => extend(THREE as any), [])
4744

48-
const [fiber, setFiber] = React.useState<any>(null)
49-
const Bridge = useContextBridge(fiber)
45+
const Bridge = useContextBridge()
5046

5147
const [{ width, height, top, left }, setSize] = React.useState<Size>({ width: 0, height: 0, top: 0, left: 0 })
5248
const [canvas, setCanvas] = React.useState<HTMLCanvasElement | null>(null)
@@ -143,11 +139,21 @@ export const Canvas = /*#__PURE__*/ React.forwardRef<View, CanvasProps>(
143139
}, [canvas])
144140

145141
return (
146-
<FiberProvider setFiber={setFiber}>
147-
<View {...props} ref={viewRef} onLayout={onLayout} style={{ flex: 1, ...style }} {...bind}>
148-
{width > 0 && <GLView onContextCreate={onContextCreate} style={StyleSheet.absoluteFill} />}
149-
</View>
150-
</FiberProvider>
142+
<View {...props} ref={viewRef} onLayout={onLayout} style={{ flex: 1, ...style }} {...bind}>
143+
{width > 0 && <GLView onContextCreate={onContextCreate} style={StyleSheet.absoluteFill} />}
144+
</View>
151145
)
152146
},
153147
)
148+
149+
/**
150+
* A native canvas which accepts threejs elements as children.
151+
* @see https://docs.pmnd.rs/react-three-fiber/api/canvas
152+
*/
153+
export const Canvas = React.forwardRef<View, CanvasProps>(function CanvasWrapper(props, ref) {
154+
return (
155+
<FiberProvider>
156+
<CanvasImpl {...props} ref={ref} />
157+
</FiberProvider>
158+
)
159+
})

packages/fiber/src/web/Canvas.tsx

+24-27
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@ import * as React from 'react'
22
import * as THREE from 'three'
33
import useMeasure from 'react-use-measure'
44
import type { Options as ResizeOptions } from 'react-use-measure'
5-
import {
6-
isRef,
7-
SetBlock,
8-
Block,
9-
ErrorBoundary,
10-
useMutableCallback,
11-
useIsomorphicLayoutEffect,
12-
useContextBridge,
13-
FiberProvider,
14-
} from '../core/utils'
5+
import { useContextBridge, FiberProvider } from 'its-fine'
6+
import { isRef, SetBlock, Block, ErrorBoundary, useMutableCallback, useIsomorphicLayoutEffect } from '../core/utils'
157
import { ReconcilerRoot, extend, createRoot, unmountComponentAtNode, RenderProps } from '../core'
168
import { createPointerEvents } from './events'
179
import { DomEvent } from '../core/events'
@@ -33,11 +25,7 @@ export interface CanvasProps
3325
eventPrefix?: 'offset' | 'client' | 'page' | 'layer' | 'screen'
3426
}
3527

36-
/**
37-
* A DOM canvas which accepts threejs elements as children.
38-
* @see https://docs.pmnd.rs/react-three-fiber/api/canvas
39-
*/
40-
export const Canvas = /*#__PURE__*/ React.forwardRef<HTMLCanvasElement, CanvasProps>(function Canvas(
28+
const CanvasImpl = /*#__PURE__*/ React.forwardRef<HTMLCanvasElement, CanvasProps>(function Canvas(
4129
{
4230
children,
4331
fallback,
@@ -69,8 +57,7 @@ export const Canvas = /*#__PURE__*/ React.forwardRef<HTMLCanvasElement, CanvasPr
6957
// their own elements by using the createRoot API instead
7058
React.useMemo(() => extend(THREE as any), [])
7159

72-
const [fiber, setFiber] = React.useState<any>(null)
73-
const Bridge = useContextBridge(fiber)
60+
const Bridge = useContextBridge()
7461

7562
const [containerRef, containerRect] = useMeasure({ scroll: true, debounce: { scroll: 50, resize: 0 }, ...resize })
7663
const canvasRef = React.useRef<HTMLCanvasElement>(null!)
@@ -148,17 +135,27 @@ export const Canvas = /*#__PURE__*/ React.forwardRef<HTMLCanvasElement, CanvasPr
148135
const pointerEvents = eventSource ? 'none' : 'auto'
149136

150137
return (
151-
<FiberProvider setFiber={setFiber}>
152-
<div
153-
ref={divRef}
154-
style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', pointerEvents, ...style }}
155-
{...props}>
156-
<div ref={containerRef} style={{ width: '100%', height: '100%' }}>
157-
<canvas ref={canvasRef} style={{ display: 'block' }}>
158-
{fallback}
159-
</canvas>
160-
</div>
138+
<div
139+
ref={divRef}
140+
style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', pointerEvents, ...style }}
141+
{...props}>
142+
<div ref={containerRef} style={{ width: '100%', height: '100%' }}>
143+
<canvas ref={canvasRef} style={{ display: 'block' }}>
144+
{fallback}
145+
</canvas>
161146
</div>
147+
</div>
148+
)
149+
})
150+
151+
/**
152+
* A DOM canvas which accepts threejs elements as children.
153+
* @see https://docs.pmnd.rs/react-three-fiber/api/canvas
154+
*/
155+
export const Canvas = React.forwardRef<HTMLCanvasElement, CanvasProps>(function CanvasWrapper(props, ref) {
156+
return (
157+
<FiberProvider>
158+
<CanvasImpl {...props} ref={ref} />
162159
</FiberProvider>
163160
)
164161
})

yarn.lock

+14-8
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,13 @@
27202720
dependencies:
27212721
"@types/react" "*"
27222722

2723+
"@types/react-reconciler@^0.28.0":
2724+
version "0.28.0"
2725+
resolved "https://registry.yarnpkg.com/@types/react-reconciler/-/react-reconciler-0.28.0.tgz#513acbed173140e958c909041ca14eb40412077f"
2726+
integrity sha512-5cjk9ottZAj7eaTsqzPUIlrVbh3hBAO2YaEL1rkjHKB3xNAId7oU8GhzvAX+gfmlfoxTwJnBjPxEHyxkEA1Ffg==
2727+
dependencies:
2728+
"@types/react" "*"
2729+
27232730
"@types/react-test-renderer@^17.0.1":
27242731
version "17.0.2"
27252732
resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-17.0.2.tgz#5f800a39b12ac8d2a2149e7e1885215bcf4edbbf"
@@ -5938,6 +5945,13 @@ istanbul-reports@^3.1.3:
59385945
html-escaper "^2.0.0"
59395946
istanbul-lib-report "^3.0.0"
59405947

5948+
its-fine@^1.0.0:
5949+
version "1.0.0"
5950+
resolved "https://registry.yarnpkg.com/its-fine/-/its-fine-1.0.0.tgz#e1c17f4420a433c9e96d264330e96a82c6edc33b"
5951+
integrity sha512-EEVcyr+sR21lxLZg3U84HpY3Mb9aKmGYqxPsIbf/Ea4fO4qpvE/7lX5qtrkuCnljJ9RLMaSaeMq35PeLa+oM0w==
5952+
dependencies:
5953+
"@types/react-reconciler" "^0.28.0"
5954+
59415955
jest-changed-files@^27.5.1:
59425956
version "27.5.1"
59435957
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-27.5.1.tgz#a348aed00ec9bf671cc58a66fcbe7c3dfd6a68f5"
@@ -8116,14 +8130,6 @@ [email protected]:
81168130
whatwg-fetch "^3.0.0"
81178131
ws "^6.1.4"
81188132

8119-
react-nil@^1.2.0:
8120-
version "1.2.0"
8121-
resolved "https://registry.yarnpkg.com/react-nil/-/react-nil-1.2.0.tgz#0f2e110b50cf12bdf1eebf43bfdb4b348a271c11"
8122-
integrity sha512-54yJ8+vFyJlZHiFBLny0RKQ0/FSG32y8pZt0oV7duxaW3lEM9pc6D2Hhnvu0TToJKPCAJ/A5XmjY8nDETxrrGg==
8123-
dependencies:
8124-
"@types/react-reconciler" "^0.26.7"
8125-
react-reconciler "^0.27.0"
8126-
81278133
react-reconciler@^0.27.0:
81288134
version "0.27.0"
81298135
resolved "https://registry.yarnpkg.com/react-reconciler/-/react-reconciler-0.27.0.tgz#360124fdf2d76447c7491ee5f0e04503ed9acf5b"

0 commit comments

Comments
 (0)