Skip to content

Commit b38d737

Browse files
author
v 1 r t l
authored
Merge pull request #57 from saitonakamura/fix-nested-boxes-sizes-calc
Don't calculate sizes for Box that has Box children
2 parents 0649960 + 4871b3a commit b38d737

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Flex.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useLayoutEffect, useMemo, useCallback, PropsWithChildren, useRef } from 'react'
22
import Yoga, { YogaNode } from 'yoga-layout-prebuilt'
3-
import { Vector3, Group, Box3 } from 'three'
3+
import { Vector3, Group, Box3, Object3D } from 'three'
44
import { useFrame, useThree, ReactThreeFiber } from '@react-three/fiber'
55

66
import { setYogaProperties, rmUndefFromObj, vectorFromObject, Axis, getDepthAxis, getFlex2DSize } from './util'
@@ -22,6 +22,18 @@ export type FlexProps = PropsWithChildren<
2222
R3FlexProps &
2323
Omit<ReactThreeFiber.Object3DNode<THREE.Group, typeof Group>, 'children'>
2424
>
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))
2537

2638
/**
2739
* Flex container. Can contain Boxes
@@ -198,7 +210,7 @@ export function Flex({
198210
])
199211

200212
// 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[]>([])
202214
const registerBox = useCallback(
203215
(node: YogaNode, group: Group, flexProps: R3FlexProps, centerAnchor: boolean = false) => {
204216
const i = boxesRef.current.findIndex((b) => b.node === node)
@@ -272,7 +284,7 @@ export function Flex({
272284
// Forced size, no need to calculate bounding box
273285
node.setWidth(scaledWidth)
274286
node.setHeight(scaledHeight)
275-
} else {
287+
} else if (!hasBoxChildren(boxesRef.current, group.children)) {
276288
// No size specified, calculate bounding box
277289
boundingBox.setFromObject(group).getSize(vec)
278290
node.setWidth(scaledWidth || vec[mainAxis] * scaleFactor)

0 commit comments

Comments
 (0)