Before delete shape

You can intercept the creation of any record in the store. This example intercepts arrow creation to make sure each arrow has a label. You can do the same thing to change the props of any newly created shape.

import { Editor, Tldraw, createShapeId } from 'tldraw'

export default function BeforeDeleteShapeExample() {
	return (
		<div className="tldraw__editor">
			<Tldraw
				onMount={(editor) => {
					// register a handler to run before any shape is deleted:
					editor.sideEffects.registerBeforeDeleteHandler('shape', (shape) => {
						// if the shape is red, prevent the deletion:
						if ('color' in shape.props && shape.props.color === 'red') {
							return false
						}

						return
					})

					createDemoShapes(editor)
				}}
			/>
		</div>
	)
}

// create some shapes to demonstrate the side-effect we added
function createDemoShapes(editor: Editor) {
	editor
		.createShapes([
			{
				id: createShapeId(),
				type: 'text',
				props: {
					text: "Red shapes can't be deleted",
					color: 'red',
				},
			},
			{
				id: createShapeId(),
				type: 'text',
				y: 30,
				props: {
					text: 'but other shapes can',
					color: 'black',
				},
			},
		])
		.zoomToFit({ animation: { duration: 0 } })
}
Prev
Before create/update shape
Next
After create/update shape