Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tldraw/tldraw/llms.txt
Use this file to discover all available pages before exploring further.
StateNode
TheStateNode class is the base class for all tools and state nodes in tldraw. It provides a hierarchical state machine implementation for managing editor interactions and tool behavior.
Overview
StateNode implements a tree-based state machine where:- Root nodes are top-level state containers (like the root tool manager)
- Branch nodes have child states and manage transitions between them
- Leaf nodes are terminal states that handle specific interactions
Constructor
The editor instance this state node belongs to
The parent state node in the hierarchy
Static Properties
Unique identifier for this state node
ID of the initial child state (for branch nodes)
Function that returns child state node constructors
Whether this tool can be locked for continuous use
Whether to use coalesced pointer events for better performance
Instance Properties
The unique identifier for this state node instance
The type of state node - determines whether it can have children
Reference to the editor instance
Reference to the parent state node
Whether this tool can be locked
Methods
getPath()
Returns the full path of active state nodes from this node down through its children."select.idle" or "select.translating"
getCurrent()
Returns the current active child state node.getIsActive()
Returns whether this state node is currently active.true if this state is active in the state tree
transition()
Transitions to a new child state.The ID of the child state to transition to. Can be a dot-separated path like
"crop.idle"Optional data to pass to the
onEnter and onExit handlersaddChild()
Adds a child state node to this state node.The constructor for the child state node to add
getCurrentToolIdMask()
Gets the current tool ID mask. This is used to display a different tool as active in the UI.setCurrentToolIdMask()
Sets a tool ID mask to display a different tool as active in the UI.The tool ID to display, or undefined to clear the mask
Event Handlers
All event handlers are optional and can be overridden in subclasses:Pointer Events
onPointerDown(info: TLPointerEventInfo): voidonPointerMove(info: TLPointerEventInfo): voidonPointerUp(info: TLPointerEventInfo): voidonLongPress(info: TLPointerEventInfo): void
Click Events
onDoubleClick(info: TLClickEventInfo): voidonTripleClick(info: TLClickEventInfo): voidonQuadrupleClick(info: TLClickEventInfo): voidonRightClick(info: TLPointerEventInfo): voidonMiddleClick(info: TLPointerEventInfo): void
Keyboard Events
onKeyDown(info: TLKeyboardEventInfo): voidonKeyUp(info: TLKeyboardEventInfo): voidonKeyRepeat(info: TLKeyboardEventInfo): void
Other Events
onWheel(info: TLWheelEventInfo): voidonCancel(info: TLCancelEventInfo): voidonComplete(info: TLCompleteEventInfo): voidonInterrupt(info: TLInterruptEventInfo): voidonTick(info: TLTickEventInfo): void
Lifecycle Events
onEnter(info: any, from: string): void- Called when entering this stateonExit(info: any, to: string): void- Called when exiting this state
Usage Example
See Also
- SelectTool - Example of a complex tool with many child states
- HandTool - Example of a simple panning tool
- Editor - The main editor class