Skip to content

prevent readding boxes at the end of its parent #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,24 @@ function BoxImpl(
setYogaProperties(node, flexProps, scaleFactor)
}, [flexProps, node, scaleFactor])

// Make child known to the parents yoga instance *before* it calculates layout
useLayoutEffect(() => {
if (!group.current || !parent) return

parent.insertChild(node, parent.getChildCount())
registerBox(node, group.current, flexProps, centerAnchor)
if (!parent) return

// Remove child on unmount
return () => {
parent.removeChild(node)
unregisterBox(node)
}
}, [node, parent])

// Make child known to the parents yoga instance *before* it calculates layout
useLayoutEffect(() => {
if (!group.current || !parent) return

if (registerBox(node, group.current, flexProps, centerAnchor)) {
//newly registered node: add it to the parent
parent.insertChild(node, parent.getChildCount())
}
}, [node, parent, flexProps, centerAnchor, registerBox, unregisterBox])

// We need to reflow if props change
Expand Down
10 changes: 8 additions & 2 deletions src/Flex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,16 @@ function FlexImpl(
const registerBox = useCallback(
(node: YogaNode, group: THREE.Group, flexProps: R3FlexProps, centerAnchor: boolean = false) => {
const i = boxesRef.current.findIndex((b) => b.node === node)
const boxItem = { group, node, flexProps, centerAnchor }
if (i !== -1) {
boxesRef.current.splice(i, 1)
//node already contained: update box
boxesRef.current[i] = boxItem
return false
} else {
//node not contained: insert new box
boxesRef.current.push(boxItem)
return true
}
boxesRef.current.push({ group, node, flexProps, centerAnchor })
},
[]
)
Expand Down
3 changes: 2 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { R3FlexProps } from './props'
export interface SharedFlexContext {
scaleFactor: number
requestReflow(): void
registerBox(node: YogaNode, group: Group, flexProps: R3FlexProps, centerAnchor?: boolean): void
registerBox(node: YogaNode, group: Group, flexProps: R3FlexProps, centerAnchor?: boolean): boolean
unregisterBox(node: YogaNode): void
notInitialized?: boolean
}
Expand All @@ -18,6 +18,7 @@ const initialSharedFlexContext: SharedFlexContext = {
},
registerBox() {
console.warn('Flex not initialized! Please report')
return false
},
unregisterBox() {
console.warn('Flex not initialized! Please report')
Expand Down