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

Added useCenterAnchor hook #60

Merged
merged 1 commit into from
Aug 29, 2021
Merged
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
8 changes: 4 additions & 4 deletions src/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Yoga from 'yoga-layout-prebuilt'
import { ReactThreeFiber, useFrame } from '@react-three/fiber'

import { setYogaProperties, rmUndefFromObj } from './util'
import { boxContext, flexContext } from './context'
import { boxContext, flexContext, SharedBoxContext } from './context'
import { R3FlexProps } from './props'
import { useReflow, useContext } from './hooks'

Expand Down Expand Up @@ -75,7 +75,7 @@ export function Box({
...props
}: {
centerAnchor?: boolean
children: React.ReactNode | ((width: number, height: number) => React.ReactNode)
children: React.ReactNode | ((width: number, height: number, centerAnchor?: boolean) => React.ReactNode)
} & R3FlexProps &
Omit<ReactThreeFiber.Object3DNode<THREE.Group, typeof THREE.Group>, 'children'>) {
// must memoize or the object literal will cause every dependent of flexProps to rerender everytime
Expand Down Expand Up @@ -225,12 +225,12 @@ export function Box({
}
})

const sharedBoxContext = useMemo(() => ({ node, size }), [node, size])
const sharedBoxContext = useMemo<SharedBoxContext>(() => ({ node, size, centerAnchor }), [node, size, centerAnchor])

return (
<group ref={group} {...props}>
<boxContext.Provider value={sharedBoxContext}>
{typeof children === 'function' ? children(size[0], size[1]) : children}
{typeof children === 'function' ? children(size[0], size[1], centerAnchor) : children}
</boxContext.Provider>
</group>
)
Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const flexContext = createContext<SharedFlexContext>(initialSharedFlexCon
export interface SharedBoxContext {
node: YogaNode | null
size: [number, number]
centerAnchor?: boolean
}

const initialSharedBoxContext: SharedBoxContext = {
Expand Down
13 changes: 10 additions & 3 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useContext as useContextImpl } from 'react'
import { useCallback, useContext as useContextImpl, useMemo } from 'react'
import { Mesh, Vector3 } from 'three'
import { flexContext, boxContext } from './context'

Expand All @@ -15,9 +15,16 @@ export function useReflow() {
return requestReflow
}

/**
* @returns [width, height, centerAnchor]
*/
export function useFlexSize() {
const { size } = useContext(boxContext)
return size
const {
size: [width, height],
centerAnchor,
} = useContext(boxContext)
const value = useMemo(() => [width, height, centerAnchor] as const, [width, height, centerAnchor])
return value
}

export function useFlexNode() {
Expand Down