import studio from '@theatre/studio' import {useLayoutEffect, useMemo, useState} from 'react' import useDrag from './useDrag' studio.initialize() const boxObjectConfig = { x: 0, y: 0, } const Box = ({id, sheet, selectedObject}) => { // This is cheap to call and always returns the same value, so no need for useMemo() const obj = sheet.object(id, boxObjectConfig) const isSelected = selectedObject === obj const [pos, setPos] = useState({x: 0, y: 0}) useLayoutEffect(() => { const unsubscribeFromChanges = obj.onValuesChange((newValues) => { setPos(newValues) }) return unsubscribeFromChanges }, [id, obj]) const [divRef, setDivRef] = useState(null) const dragOpts = useMemo(() => { let scrub let initial let firstOnDragCalled = false return { onDragStart() { scrub = studio.scrub() initial = obj.value firstOnDragCalled = false }, onDrag(x, y) { if (!firstOnDragCalled) { studio.setSelection([obj]) firstOnDragCalled = true } scrub.capture(({set}) => { set(obj.props, {x: x + initial.x, y: y + initial.y}) }) }, onDragEnd(dragHappened) { if (dragHappened) { scrub.commit() } else { scrub.discard() } }, lockCursorTo: 'move', } }, []) useDrag(divRef, dragOpts) return (