import { TLShape, Tldraw, track, useEditor } from '@tldraw/tldraw' import '@tldraw/tldraw/tldraw.css' export default function ShapeMetaExample() { return (
{ editor.getInitialMetaForShape = (shape) => { return { label: `My ${shape.type} shape` } } }} >
) } // By default, the TLShape type's meta property is { [key: string]: any }, but we can type it // by unioning the type with a new type that has a meta property of our choosing. type ShapeWithMyMeta = TLShape & { meta: { label: string } } export const ShapeLabelUiWithHelper = track(function ShapeLabelUiWithHelper() { const editor = useEditor() const onlySelectedShape = editor.onlySelectedShape as ShapeWithMyMeta | null if (!onlySelectedShape) { return null } function onChange(e: React.ChangeEvent) { if (onlySelectedShape) { const { id, type, meta } = onlySelectedShape editor.updateShapes([ { id, type, meta: { ...meta, label: e.currentTarget.value } }, ]) } } return (
shape label:
) })