Skip to content

Commit fa5328c

Browse files
authored
example: add on interaction end example (tldraw#7143)
An example showing how you could use a callback with `onInteractionEnd` ### Change type - [ ] `bugfix` - [ ] `improvement` - [ ] `feature` - [ ] `api` - [x] `other` <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds a new example demonstrating `onInteractionEnd` to control behavior after translating a newly created shape. > > - **Examples**: > - Add `apps/examples/src/examples/interaction-end-callback/InteractionEndExample.tsx` showcasing a custom `QuickShapeTool` that: > - Creates a `geo` shape on pointer down, switches to `select.translating`, and uses `onInteractionEnd` to update the shape's `fill` to `pattern` and return to `quick-shape`. > - Add `apps/examples/src/examples/interaction-end-callback/README.md` documenting usage of `onInteractionEnd` for translate/resize/rotate interactions. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit ecafafb. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 74eafd3 commit fa5328c

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { StateNode, TLGeoShape, TLPointerEventInfo, Tldraw, createShapeId } from 'tldraw'
2+
import 'tldraw/tldraw.css'
3+
4+
// [1]
5+
class QuickShapeTool extends StateNode {
6+
static override id = 'quick-shape'
7+
8+
override onEnter() {
9+
this.editor.setCursor({ type: 'cross', rotation: 0 })
10+
}
11+
12+
override onPointerDown(info: TLPointerEventInfo) {
13+
const { currentPagePoint } = this.editor.inputs
14+
const shapeId = createShapeId()
15+
16+
this.editor.createShape<TLGeoShape>({
17+
id: shapeId,
18+
type: 'geo',
19+
x: currentPagePoint.x - 50,
20+
y: currentPagePoint.y - 50,
21+
props: { w: 100, h: 100, fill: 'solid' },
22+
})
23+
24+
this.editor.setSelectedShapes([shapeId])
25+
26+
// [2]
27+
this.editor.setCurrentTool('select.translating', {
28+
...info,
29+
target: 'shape',
30+
shape: this.editor.getShape(shapeId),
31+
isCreating: true,
32+
// [3]
33+
onInteractionEnd: () => {
34+
// Change fill to semi-transparent after dragging
35+
this.editor.updateShape<TLGeoShape>({
36+
id: shapeId,
37+
type: 'geo',
38+
props: { fill: 'pattern' },
39+
})
40+
// Return to our custom tool
41+
this.editor.setCurrentTool('quick-shape')
42+
},
43+
})
44+
}
45+
}
46+
47+
export default function InteractionEndExample() {
48+
return (
49+
<div className="tldraw__editor">
50+
<Tldraw tools={[QuickShapeTool]} initialState="quick-shape" hideUi />
51+
</div>
52+
)
53+
}
54+
55+
/*
56+
[1]
57+
Create a simple tool that creates shapes on click. This demonstrates using
58+
onInteractionEnd to control what happens after the user drags the new shape.
59+
60+
[2]
61+
After creating the shape, transition to the translating state so the user can
62+
immediately position it.
63+
64+
[3]
65+
Pass a function as onInteractionEnd to execute custom logic when dragging ends.
66+
Here we change the shape's fill style and return to our custom tool. You can also
67+
pass a string like 'quick-shape' or 'select.idle' to simply transition to that tool.
68+
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Interaction end callback
3+
component: ./InteractionEndExample.tsx
4+
category: editor-api
5+
priority: 4
6+
keywords: [callback, interaction, drag, resize, rotate, tool]
7+
---
8+
9+
Control behavior after dragging, resizing, or rotating shapes.
10+
11+
---
12+
13+
When programmatically starting interactions like translating, resizing, or rotating, you can use the `onInteractionEnd` option to control what happens when the interaction completes. Pass a string to transition to a specific tool, or a function to execute custom logic.
14+
15+
In this example, we set the fill of a shape after we finish translating to be patterned.

0 commit comments

Comments
 (0)