Skip to content

Commit b29316b

Browse files
committed
disable default controls while dragging the pivot or transform handles
1 parent f9a3c4f commit b29316b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

packages/react/handle/src/handles/pivot.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { ThreeElements, useFrame } from '@react-three/fiber'
88
import { createContext, forwardRef, useContext, useEffect, useImperativeHandle, useMemo, useRef } from 'react'
99
import { Group } from 'three'
10+
import { useApplyThatDisablesDefaultControls } from './utils.js'
1011

1112
export type PivotHandlesProperties = PivotHandlesContextProperties & PivotHandlesHandlesProperties
1213

@@ -37,9 +38,10 @@ export const PivotHandlesContext = forwardRef<Group, PivotHandlesContextProperti
3738
({ alwaysUpdate, apply, stopPropagation, children, ...props }, ref) => {
3839
const internalRef = useRef<Group>(null)
3940
useImperativeHandle(ref, () => internalRef.current!, [])
41+
const applyThatDisablesControls = useApplyThatDisablesDefaultControls(apply)
4042
const options: HandleOptions<any> = {
4143
alwaysUpdate,
42-
apply,
44+
apply: applyThatDisablesControls,
4345
stopPropagation,
4446
}
4547
const optionsRef = useRef(options)

packages/react/handle/src/handles/transform.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { ThreeElements, useFrame } from '@react-three/fiber'
1212
import { createContext, forwardRef, useContext, useEffect, useImperativeHandle, useMemo, useRef } from 'react'
1313
import { Euler, Group, Vector2Tuple } from 'three'
14+
import { useApplyThatDisablesDefaultControls } from './utils.js'
1415

1516
export type TransformHandlesProperties = TransformHandlesContextProperties & TransformHandlesHandlesProperties
1617

@@ -60,10 +61,11 @@ export type TransformHandlesContextProperties = Omit<ThreeElements['group'], 'sc
6061
export const TransformHandlesContext = forwardRef<Group, TransformHandlesContextProperties>(
6162
({ alwaysUpdate, apply, stopPropagation, children, context: providedContext, space, ...props }, ref) => {
6263
const internalRef = useRef<Group>(null)
64+
const applyThatDisablesControls = useApplyThatDisablesDefaultControls(apply)
6365
useImperativeHandle(ref, () => internalRef.current!, [])
6466
const options: HandleOptions<any> = {
6567
alwaysUpdate,
66-
apply,
68+
apply: applyThatDisablesControls,
6769
stopPropagation,
6870
}
6971
const optionsRef = useRef(options)
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defaultApply, HandleOptions, HandleState } from '@pmndrs/handle'
2+
import { useThree } from '@react-three/fiber'
3+
import { useCallback } from 'react'
4+
import { Object3D } from 'three'
5+
6+
type ControlsProto = {
7+
enabled: boolean
8+
}
9+
10+
export function useApplyThatDisablesDefaultControls<T>(apply: HandleOptions<T>['apply']): HandleOptions<T>['apply'] {
11+
const controls = useThree((s) => s.controls as unknown as ControlsProto | undefined)
12+
return useCallback(
13+
(state: HandleState<T>, target: Object3D) => {
14+
if (controls != null && state.first) {
15+
controls.enabled = false
16+
}
17+
if (controls != null && state.last) {
18+
controls.enabled = true
19+
}
20+
return (apply ?? defaultApply)(state, target)
21+
},
22+
[apply, controls],
23+
)
24+
}

0 commit comments

Comments
 (0)