Skip to content
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

feat: added onCentered callback in the useLayoutEffect #1020

Merged
merged 3 commits into from Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/core/Center.tsx
@@ -1,15 +1,18 @@
import { Box3, Vector3, Sphere, Group } from 'three'
import * as React from 'react'

type OnCenteredFunction = (bounds: object) => void

type Props = JSX.IntrinsicElements['group'] & {
top?: boolean
right?: boolean
bottom?: boolean
left?: boolean
onCentered?: OnCenteredFunction
}

export const Center = React.forwardRef<Group, Props>(function Center(
{ children, left, right, top, bottom, ...props },
{ children, left, right, top, bottom, onCentered, ...props },
fRef
) {
const outer = React.useRef<Group>(null!)
Expand All @@ -26,6 +29,17 @@ export const Center = React.forwardRef<Group, Props>(function Center(
const vAlign = top ? height / 2 : bottom ? -height / 2 : 0
const hAlign = left ? -width / 2 : right ? width / 2 : 0
outer.current.position.set(-center.x + hAlign, -center.y + vAlign, -center.z)
if (typeof onCentered !== 'undefined' && width > 1 && height > 1) {
onCentered({
boundingBox: box3,
center: box3.getCenter(center),
boundingSphere: box3.getBoundingSphere(sphere),
width,
height,
verticalAlignment: vAlign,
horizontalAlignment: hAlign,
})
}
}, [children])
return (
<group ref={fRef} {...props}>
Expand Down