tldraw/example/src/imperative.tsx
Steve Ruiz fe2e3c81fe
[improvement] Migrations (#217)
* Add better migrations

* Improves migrations

* rename dev to example

* renames migrate

* Removes workers from git

* Remove rogue dependency

* Fix dropdown navigation by keyboard
2021-11-04 15:48:39 +00:00

56 lines
1.3 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-non-null-assertion */
import * as React from 'react'
import { ColorStyle, TLDraw, TLDrawShapeType, TLDrawState } from '@tldraw/tldraw'
export default function Imperative(): JSX.Element {
const rTLDrawState = React.useRef<TLDrawState>()
const handleMount = React.useCallback((state: TLDrawState) => {
rTLDrawState.current = state
state.createShapes(
{
id: 'rect1',
type: TLDrawShapeType.Rectangle,
name: 'Rectangle',
childIndex: 1,
point: [0, 0],
size: [100, 100],
},
{
id: 'rect2',
name: 'Rectangle',
type: TLDrawShapeType.Rectangle,
point: [200, 200],
size: [100, 100],
}
)
}, [])
React.useEffect(() => {
let i = 0
const interval = setInterval(() => {
const tlstate = rTLDrawState.current!
const rect1 = tlstate.getShape('rect1')
if (!rect1) {
// clearInterval(interval)
return
}
const color = i % 2 ? ColorStyle.Red : ColorStyle.Blue
tlstate.patchShapes({
id: 'rect1',
style: {
...rect1.style,
color,
},
})
i++
}, 1000)
return () => clearInterval(interval)
}, [])
return <TLDraw onMount={handleMount} />
}