Events
Event handlers are attached with on<event> attributes, like onclick and onkeydown:
export default function Component() {
function handleClick() {
console.log("Clicked!");
}
@render {
<button onclick={handleClick}>Click me</button>
}
} The event object
Handlers receive the native event object as their first argument, and this is set to the element that raised the event:
export default function Component() {
function handleKeyDown(e: KeyboardEvent) {
if (e.key === "Enter") {
console.log((e.currentTarget as HTMLInputElement).value);
}
}
@render {
<input onkeydown={handleKeyDown} />
}
} Common events like click, input, keydown and change are handled with a single delegated listener per event type. Other events fall back to a standard addEventListener. Because the delegated listeners live on the element's document (including inside shadow roots and iframes), mounting a component into a node that isn't connected to that document (e.g. in a test) needs the node attached before delegated events will fire. A native stopPropagation() from code outside Torpor — for example a listener added in $onmount — stops the event before it reaches the document, so delegated handlers won't run.