1
1
import React , { useLayoutEffect , useMemo , useCallback , PropsWithChildren , useRef } from 'react'
2
2
import Yoga , { YogaNode } from 'yoga-layout-prebuilt'
3
- import { Vector3 , Group , Box3 } from 'three'
3
+ import { Vector3 , Group , Box3 , Object3D } from 'three'
4
4
import { useFrame , useThree , ReactThreeFiber } from '@react-three/fiber'
5
5
6
6
import { setYogaProperties , rmUndefFromObj , vectorFromObject , Axis , getDepthAxis , getFlex2DSize } from './util'
@@ -22,6 +22,18 @@ export type FlexProps = PropsWithChildren<
22
22
R3FlexProps &
23
23
Omit < ReactThreeFiber . Object3DNode < THREE . Group , typeof Group > , 'children' >
24
24
>
25
+ interface BoxesItem {
26
+ node : YogaNode
27
+ group : Group
28
+ flexProps : R3FlexProps
29
+ centerAnchor : boolean
30
+ }
31
+
32
+ // This is not very performant
33
+ // We should probably optimize it, options are
34
+ // * Memoization
35
+ // * Precalculation of this when registering a box
36
+ const hasBoxChildren = ( boxes : BoxesItem [ ] , children : Object3D [ ] ) => boxes . some ( ( { group } ) => children . includes ( group ) )
25
37
26
38
/**
27
39
* Flex container. Can contain Boxes
@@ -198,7 +210,7 @@ export function Flex({
198
210
] )
199
211
200
212
// Keeps track of the yoga nodes of the children and the related wrapper groups
201
- const boxesRef = useRef < { node : YogaNode ; group : Group ; flexProps : R3FlexProps ; centerAnchor : boolean } [ ] > ( [ ] )
213
+ const boxesRef = useRef < BoxesItem [ ] > ( [ ] )
202
214
const registerBox = useCallback (
203
215
( node : YogaNode , group : Group , flexProps : R3FlexProps , centerAnchor : boolean = false ) => {
204
216
const i = boxesRef . current . findIndex ( ( b ) => b . node === node )
@@ -272,7 +284,7 @@ export function Flex({
272
284
// Forced size, no need to calculate bounding box
273
285
node . setWidth ( scaledWidth )
274
286
node . setHeight ( scaledHeight )
275
- } else {
287
+ } else if ( ! hasBoxChildren ( boxesRef . current , group . children ) ) {
276
288
// No size specified, calculate bounding box
277
289
boundingBox . setFromObject ( group ) . getSize ( vec )
278
290
node . setWidth ( scaledWidth || vec [ mainAxis ] * scaleFactor )
0 commit comments