Implemented useRefreshSnapshot()
and <RefreshSnapshot />
This commit is contained in:
parent
d1fb0300e0
commit
e6a4f3ae3f
4 changed files with 42 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
import {editable as e, Wrapper} from '@theatre/plugin-r3f'
|
import {editable as e, RefreshSnapshot, Wrapper} from '@theatre/plugin-r3f'
|
||||||
import {OrbitControls, Stars} from '@react-three/drei'
|
import {OrbitControls, Stars} from '@react-three/drei'
|
||||||
import {getProject} from '@theatre/core'
|
import {getProject} from '@theatre/core'
|
||||||
import React, {Suspense} from 'react'
|
import React, {Suspense} from 'react'
|
||||||
|
@ -80,6 +80,7 @@ function App() {
|
||||||
/>
|
/>
|
||||||
</e.perspectiveCamera>
|
</e.perspectiveCamera>
|
||||||
<Suspense fallback={null}>
|
<Suspense fallback={null}>
|
||||||
|
<RefreshSnapshot />
|
||||||
<Model url={sceneGLB} />
|
<Model url={sceneGLB} />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
<OrbitControls
|
<OrbitControls
|
||||||
|
|
27
packages/plugin-r3f/src/components/RefreshSnapshot.tsx
Normal file
27
packages/plugin-r3f/src/components/RefreshSnapshot.tsx
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import React, {useEffect} from 'react'
|
||||||
|
import useRefreshSnapshot from './useRefreshSnapshot'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Putting this element in a suspense tree makes sure the snapshot editor
|
||||||
|
* gets refreshed once the tree renders.
|
||||||
|
*
|
||||||
|
* Alternatively you can use
|
||||||
|
* @link useRefreshSnapshot()
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```jsx
|
||||||
|
* <Suspense fallback={null}>
|
||||||
|
* <RefreshSnapshot />
|
||||||
|
* <Model url={sceneGLB} />
|
||||||
|
* </Suspense>
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
const RefreshSnapshot: React.FC<{}> = (props) => {
|
||||||
|
const refreshSnapshot = useRefreshSnapshot()
|
||||||
|
useEffect(() => {
|
||||||
|
refreshSnapshot()
|
||||||
|
}, [])
|
||||||
|
return <></>
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RefreshSnapshot
|
11
packages/plugin-r3f/src/components/useRefreshSnapshot.ts
Normal file
11
packages/plugin-r3f/src/components/useRefreshSnapshot.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import {useCallback} from 'react'
|
||||||
|
import {useEditorStore} from '../store'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a function that can be called to refresh the snapshot in the snapshot editor.
|
||||||
|
*/
|
||||||
|
export default function useRefreshSnapshot() {
|
||||||
|
return useCallback(() => {
|
||||||
|
useEditorStore.getState().createSnapshot()
|
||||||
|
}, [])
|
||||||
|
}
|
|
@ -4,6 +4,8 @@ export type {EditorHelperProps} from './components/EditorHelper'
|
||||||
export {default as editable} from './components/editable'
|
export {default as editable} from './components/editable'
|
||||||
export type {EditableState, BindFunction} from './store'
|
export type {EditableState, BindFunction} from './store'
|
||||||
export {default as Wrapper} from './Wrapper'
|
export {default as Wrapper} from './Wrapper'
|
||||||
|
export {default as useRefreshSnapshot} from './components/useRefreshSnapshot'
|
||||||
|
export {default as RefreshSnapshot} from './components/RefreshSnapshot'
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
setupPlugin()
|
setupPlugin()
|
||||||
|
|
Loading…
Reference in a new issue