Skip to content

Commit

Permalink
fix: useXRInputSourceStateContext can only be used inside the xr stor…
Browse files Browse the repository at this point in the history
…e config
  • Loading branch information
bbohlender committed Nov 10, 2024
1 parent c49cf88 commit feb2a37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/react/xr/src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export function useXRInputSourceStateContext<T extends keyof XRInputSourceStateM
type?: T,
): XRInputSourceStateMap[T] {
const state = useContext(xrInputSourceStateContext)
if (state == null || (type != null && state.type != type)) {
throw new Error(`useXRInputSourceStateContext() can only be used inside a the xr store config`)
if (state == null) {
throw new Error(`useXRInputSourceStateContext() can only be used inside the xr store config`)
}
if (type != null && state.type != type) {
throw new Error(
`useXRInputSourceStateContext(${type}) can not be used inside a component for input type "${state.type}"`,
)
}
return state as XRInputSourceStateMap[T]
}
Expand Down
8 changes: 5 additions & 3 deletions packages/react/xr/src/space.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const XRSpace = forwardRef<
>(({ space, children }, ref) => {
const internalRef = useRef<Group | null>(null)
// eslint-disable-next-line react-hooks/rules-of-hooks
const resolvedSpace = useXRSpace(space as any)
const resolvedSpace = typeof space === 'string' ? useXRSpace(space) : space
useImperativeHandle(ref, () => internalRef.current!, [])
useApplyXRSpaceMatrix(internalRef, resolvedSpace)
const setRef = useCallback((group: Group | null) => {
Expand Down Expand Up @@ -57,10 +57,12 @@ export type XRHandJointSpaceType = XRHandJoint
*/
export function useXRSpace(): XRSpace

export function useXRSpace(type: XRInputSourceSpaceType | XRHandJointSpaceType): XRSpace | undefined

export function useXRSpace(type: XRReferenceSpaceType): XRReferenceSpace | undefined

export function useXRSpace(
type: XRInputSourceSpaceType | XRHandJointSpaceType | XRReferenceSpaceType,
): XRSpace | undefined

export function useXRSpace(type?: XRSpaceType): XRSpace | XRReferenceSpace | undefined {
switch (type) {
case 'grip-space':
Expand Down

0 comments on commit feb2a37

Please sign in to comment.