DOM Events

onClick, onInput, onKeyDown, and 50+ more — typed event handlers for every DOM event.

Kraft provides extension functions on CommonAttributeGroupFacade for all standard DOM events. Each handler receives a properly typed event object — MouseEvent for clicks, KeyboardEvent for key presses, and so on.

div {
    onClick { evt ->
        // evt is PointerEvent
        console.log("Clicked at ${evt.clientX}, ${evt.clientY}")
    }
    onKeyDown { evt ->
        // evt is KeyboardEvent
        if (evt.key == "Enter") submit()
    }
    onMouseEnter { evt ->
        // evt is MouseEvent
        showTooltip()
    }
}

One handler per event

Each element supports one handler per event type. Calling the same event function twice on the same element replaces the previous handler — the last one wins:

div {
    onClick { console.log("first") }   // this is replaced
    onClick { console.log("second") }  // only this one runs
}

This is how the underlying Preact/kotlinx.html property model works — event handlers are property assignments, not listener registrations. If you need multiple behaviors, compose them in a single handler:

div {
    onClick { evt ->
        logAnalytics(evt)
        doNavigation(evt)
    }
}

Alternatively, nest elements and give each its own handler:

div {
    onClick { logAnalytics(it) }
    button {
        onClick { doNavigation(it) }
        +"Go"
    }
}

Mouse events

Handler Event type When it fires
onClick PointerEvent Primary button click
onAuxClick PointerEvent Non-primary button (middle, right)
onDblClick MouseEvent Double-click
onContextMenu PointerEvent Right-click (context menu)
onMouseDown MouseEvent Button pressed
onMouseUp MouseEvent Button released
onMouseEnter MouseEvent Pointer enters element (no bubble)
onMouseLeave MouseEvent Pointer leaves element (no bubble)
onMouseOver MouseEvent Pointer enters element (bubbles)
onMouseOut MouseEvent Pointer leaves element (bubbles)
onMouseMove MouseEvent Pointer moves over element
onWheel WheelEvent Mouse wheel / trackpad scroll

Click helpers

// Prevents default and stops propagation before calling your handler
onClickStoppingEvent { evt -> doSomething() }

// Fires on both primary click AND auxiliary click (middle-click, etc.)
onClickOrAuxClick { evt -> navigate(evt) }

Smart navigation with modifier keys

When you pass the mouse event to router.navToUri, Kraft detects modifier keys and opens a new tab on Ctrl+Click (Cmd+Click on Mac) — just like native links:

ui.item A {
    onClick { evt -> router.navToUri(evt, Nav.about()) }
    +"About"
}

Keyboard events

Handler Event type When it fires
onKeyDown KeyboardEvent Key is pressed
onKeyUp KeyboardEvent Key is released
input {
    onKeyDown { evt ->
        when (evt.key) {
            "Enter" -> submit()
            "Escape" -> cancel()
        }
    }
}

Focus events

Handler Event type Bubbles?
onFocus FocusEvent No
onBlur Event No
onFocusIn FocusEvent Yes
onFocusOut FocusEvent Yes

Use onFocusIn/onFocusOut when you need to detect focus changes on child elements from a parent container (they bubble). Use onFocus/onBlur when you only care about the element itself.

Form & input events

Handler Event type When it fires
onInput InputEvent Value changes (fires immediately)
onChange Event Value committed (on blur or Enter)
onBeforeInput InputEvent Before the value is modified
onSelect Event Text is selected in input/textarea
onSubmit Event Form submitted (preventDefault is called automatically)
form {
    onSubmit { evt ->
        // preventDefault is already called — no page reload
        saveData()
    }

    input {
        onInput { evt ->
            // fires on every keystroke
            val value = (evt.target as HTMLInputElement).value
            updateSearch(value)
        }
    }
}

Pointer events

Pointer events unify mouse, touch, and pen input into a single API:

Handler Bubbles? When it fires
onPointerDown Yes Pointer becomes active
onPointerUp Yes Pointer released
onPointerMove Yes Pointer moves
onPointerEnter No Enters element
onPointerLeave No Leaves element
onPointerOver Yes Enters element (bubbles)
onPointerOut Yes Leaves element (bubbles)
onPointerCancel Yes Pointer interrupted

Touch events

Handler When it fires
onTouchStart Finger placed on screen
onTouchMove Finger moves on screen
onTouchEnd Finger lifted from screen
onTouchCancel Touch interrupted

Drag events

Handler Event type When it fires
onDragStart DragEvent User starts dragging
onDrag DragEvent While dragging (continuous)
onDragEnd DragEvent Drag ends (drop or cancel)
onDragEnter DragEvent Enters a drop target
onDragOver DragEvent Over a drop target (continuous)
onDragLeave DragEvent Leaves a drop target
onDrop DragEvent Dropped on a target

For Kraft's higher-level type-safe drag & drop system, see Drag & Drop.

Clipboard events

Handler Event type
onCopy ClipboardEvent
onCut ClipboardEvent
onPaste ClipboardEvent

Animation & transition events

Handler When it fires
onAnimationStart CSS animation starts
onAnimationEnd CSS animation completes
onAnimationIteration CSS animation completes one cycle
onTransitionEnd CSS transition completes

Other events

Handler When it fires
onScroll Element is scrolled
onLoad Resource finished loading (img, iframe)
onError Resource failed to load