From ef5752cbd3cfbd86658a41bf77bc97fcb1da0297 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Tue, 18 Oct 2022 10:19:20 +0200 Subject: [PATCH] Add a playground for dynamic trees in r3f --- .../src/tests/r3f-dynamic-tree/App.tsx | 88 +++++++++++++++++++ .../src/tests/r3f-dynamic-tree/index.tsx | 10 +++ packages/r3f/src/main/editable.tsx | 10 +-- 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 packages/playground/src/tests/r3f-dynamic-tree/App.tsx create mode 100644 packages/playground/src/tests/r3f-dynamic-tree/index.tsx diff --git a/packages/playground/src/tests/r3f-dynamic-tree/App.tsx b/packages/playground/src/tests/r3f-dynamic-tree/App.tsx new file mode 100644 index 0000000..a46e617 --- /dev/null +++ b/packages/playground/src/tests/r3f-dynamic-tree/App.tsx @@ -0,0 +1,88 @@ +import {editable as e, SheetProvider} from '@theatre/r3f' +import {getProject} from '@theatre/core' +import React, {useEffect, useState} from 'react' +import {Canvas} from '@react-three/fiber' +import {PerspectiveCamera} from '@react-three/drei' +import {useExtensionButton} from '../../shared/utils/useExtensionButton' + +document.body.style.backgroundColor = '#171717' + +const EditableCamera = e(PerspectiveCamera, 'perspectiveCamera') + +function App() { + const project = getProject('R3F Hot Reload Test') + const sheet = project.sheet('Scene') + + return ( +
+ + + + + + + +
+ ) +} + +// initial config of "Cube 1" +const cube1Config1 = {a: 1} +// we change the default value of a, and add a new prop +const cube1Config2 = {a: 2, b: 2} +// we re-use the previous config +const cube1Config3 = cube1Config2 + +function Scene() { + const [state, setState] = useState(1) + + useExtensionButton('Step forward', () => { + setState((s) => s + 1) + }) + + useEffect(() => {}, []) + + if (state === 1) { + return ( + + + + ) + } else if (state === 2) { + return ( + <> + + + + + + + + ) + } else if (state === 3) { + return ( + + + + ) + } else { + return null + } +} + +export default App diff --git a/packages/playground/src/tests/r3f-dynamic-tree/index.tsx b/packages/playground/src/tests/r3f-dynamic-tree/index.tsx new file mode 100644 index 0000000..5507263 --- /dev/null +++ b/packages/playground/src/tests/r3f-dynamic-tree/index.tsx @@ -0,0 +1,10 @@ +import React from 'react' +import ReactDOM from 'react-dom' +import App from './App' +import studio from '@theatre/studio' +import extension from '@theatre/r3f/dist/extension' + +studio.extend(extension) +studio.initialize() + +ReactDOM.render(, document.getElementById('root')) diff --git a/packages/r3f/src/main/editable.tsx b/packages/r3f/src/main/editable.tsx index 85dead8..87476c6 100644 --- a/packages/r3f/src/main/editable.tsx +++ b/packages/r3f/src/main/editable.tsx @@ -1,11 +1,5 @@ -import { - ComponentProps, - ComponentType, - Ref, - RefAttributes, - useMemo, - useState, -} from 'react' +import type {ComponentProps, ComponentType, Ref, RefAttributes} from 'react' +import {useMemo, useState} from 'react' import React, {forwardRef, useEffect, useLayoutEffect, useRef} from 'react' import {allRegisteredObjects, editorStore} from './store' import mergeRefs from 'react-merge-refs'