Implemented useRefreshSnapshot() and <RefreshSnapshot />

This commit is contained in:
Aria Minaei 2021-08-08 22:38:03 +02:00
parent d1fb0300e0
commit e6a4f3ae3f
4 changed files with 42 additions and 1 deletions

View file

@ -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

View 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

View 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()
}, [])
}

View file

@ -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()