Skip to main content
Dioxus provides comprehensive event handling for HTML elements through type-safe event handlers. All events are handled through the on* attributes.

Mouse Events

Mouse events are triggered by mouse interactions with elements.

onclick

Fired when a mouse button is clicked (pressed and released) on an element.
rsx! {
    button { 
        onclick: move |event| {
            println!("Clicked at: {:?}", event.coordinates());
        },
        "Click me"
    }
}
Event Data: MouseData
  • coordinates() - Element, page, client, and screen coordinates
  • held_buttons() - Set of mouse buttons currently held
  • trigger_button() - Which button triggered the event
  • modifiers() - Keyboard modifiers (Ctrl, Alt, Shift, Meta)
Bubbles: Yes | Cancelable: Yes Reference: MDN

ondoubleclick

Fired when a mouse button is double-clicked on an element.
rsx! {
    div { 
        ondoubleclick: move |_| println!("Double clicked!"),
        "Double click me"
    }
}
Event Data: MouseData Reference: MDN

oncontextmenu

Fired when the right mouse button is clicked (before context menu appears).
rsx! {
    div { 
        oncontextmenu: move |event| {
            event.prevent_default();
            println!("Custom context menu");
        },
        "Right click me"
    }
}
Event Data: MouseData Reference: MDN

onmousedown

Fired when a mouse button is pressed on an element.
rsx! {
    div { 
        onmousedown: move |event| {
            println!("Mouse down: {:?}", event.trigger_button());
        },
        "Press mouse here"
    }
}
Event Data: MouseData Reference: MDN

onmouseup

Fired when a mouse button is released on an element.
rsx! {
    div { onmouseup: move |_| println!("Mouse up"), "Release here" }
}
Event Data: MouseData Reference: MDN

onmousemove

Fired when the mouse pointer moves over an element.
rsx! {
    div { 
        onmousemove: move |event| {
            let coords = event.element_coordinates();
            println!("Mouse at: ({}, {})", coords.x, coords.y);
        },
        "Move mouse here"
    }
}
Event Data: MouseData Reference: MDN

onmouseenter

Fired when the mouse pointer enters an element (does not bubble).
rsx! {
    div { onmouseenter: move |_| println!("Mouse entered"), "Hover zone" }
}
Event Data: MouseData Bubbles: No Reference: MDN

onmouseleave

Fired when the mouse pointer leaves an element (does not bubble).
rsx! {
    div { onmouseleave: move |_| println!("Mouse left"), "Hover zone" }
}
Event Data: MouseData Bubbles: No Reference: MDN

onmouseover

Fired when the mouse pointer moves onto an element (bubbles).
rsx! {
    div { onmouseover: move |_| println!("Mouse over"), "Content" }
}
Event Data: MouseData Reference: MDN

onmouseout

Fired when the mouse pointer moves out of an element (bubbles).
rsx! {
    div { onmouseout: move |_| println!("Mouse out"), "Content" }
}
Event Data: MouseData Reference: MDN

Keyboard Events

Keyboard events are triggered by keyboard interactions.

onkeydown

Fired when a key is pressed down.
use dioxus::prelude::*;
use keyboard_types::{Code, Key};

rsx! {
    input { 
        onkeydown: move |event| {
            if event.key() == Key::Enter {
                println!("Enter pressed!");
            }
            if event.code() == Code::KeyA && event.modifiers().ctrl() {
                println!("Ctrl+A pressed!");
            }
        }
    }
}
Event Data: KeyboardData
  • key() - Key value (considers modifiers and layout)
  • code() - Physical key code
  • location() - Physical location of the key
  • is_auto_repeating() - Whether the key is being held
  • is_composing() - Whether key is part of composition
  • modifiers() - Active modifier keys (Ctrl, Alt, Shift, Meta)
Reference: MDN

onkeyup

Fired when a key is released.
rsx! {
    input { onkeyup: move |event| println!("Key: {:?}", event.key()) }
}
Event Data: KeyboardData Reference: MDN

onkeypress

Fired when a key that produces a character value is pressed down.
rsx! {
    input { onkeypress: move |event| println!("Char: {:?}", event.key()) }
}
Event Data: KeyboardData Note: This event is deprecated in favor of onkeydown. Reference: MDN

Form Events

Form events are triggered by form control interactions.

oninput

Fired when the value of an input, select, or textarea changes.
let mut value = use_signal(|| String::new());

rsx! {
    input {
        value: "{value}",
        oninput: move |event| value.set(event.value())
    }
    p { "Value: {value}" }
}
Event Data: FormData
  • value() - Current value as string
  • parsed<T>() - Parse value to type T
  • checked() - For checkboxes (returns bool)
  • values() - All form values (for forms)
  • files() - Selected files (for file inputs)
Reference: MDN

onchange

Fired when the value of an input element is committed.
rsx! {
    input { 
        onchange: move |event| {
            println!("Changed to: {}", event.value());
        }
    }
}
Event Data: FormData Difference from oninput: onchange fires when the value is committed (e.g., on blur for text inputs), while oninput fires on every change. Reference: MDN

onsubmit

Fired when a form is submitted.
rsx! {
    form {
        onsubmit: move |event| {
            event.prevent_default();
            let values = event.values();
            println!("Form submitted: {:?}", values);
        },
        input { name: "username" }
        button { r#type: "submit", "Submit" }
    }
}
Event Data: FormData Reference: MDN

onreset

Fired when a form is reset.
rsx! {
    form {
        onreset: move |_| println!("Form reset"),
        button { r#type: "reset", "Reset" }
    }
}
Event Data: FormData Reference: MDN

oninvalid

Fired when a form control fails validation.
rsx! {
    input {
        required: true,
        oninvalid: move |_| println!("Invalid input!")
    }
}
Event Data: FormData Reference: MDN

Focus Events

Focus events are triggered when elements gain or lose focus.

onfocus

Fired when an element receives focus (does not bubble).
rsx! {
    input { onfocus: move |_| println!("Input focused") }
}
Event Data: FocusData Bubbles: No Reference: MDN

onblur

Fired when an element loses focus (does not bubble).
rsx! {
    input { onblur: move |_| println!("Input blurred") }
}
Event Data: FocusData Bubbles: No Reference: MDN

onfocusin

Fired when an element is about to receive focus (bubbles).
rsx! {
    div { 
        onfocusin: move |_| println!("Focus entering"),
        input {}
    }
}
Event Data: FocusData Reference: MDN

onfocusout

Fired when an element is about to lose focus (bubbles).
rsx! {
    div { 
        onfocusout: move |_| println!("Focus leaving"),
        input {}
    }
}
Event Data: FocusData Reference: MDN

Clipboard Events

Clipboard events are triggered by clipboard operations.

oncopy

Fired when content is copied.
rsx! {
    div { oncopy: move |_| println!("Content copied"), "Copy this text" }
}
Event Data: ClipboardData Reference: MDN

oncut

Fired when content is cut.
rsx! {
    input { oncut: move |_| println!("Content cut") }
}
Event Data: ClipboardData Reference: MDN

onpaste

Fired when content is pasted.
rsx! {
    input { onpaste: move |_| println!("Content pasted") }
}
Event Data: ClipboardData Reference: MDN

Drag Events

Drag events are triggered during drag-and-drop operations.

ondrag

Fired continuously while an element is being dragged.
rsx! {
    div { 
        draggable: "true",
        ondrag: move |_| println!("Dragging..."),
        "Drag me"
    }
}
Event Data: DragData Reference: MDN

ondragstart

Fired when a drag operation starts.
rsx! {
    div { 
        draggable: "true",
        ondragstart: move |event| {
            println!("Drag started");
        },
        "Drag me"
    }
}
Event Data: DragData Reference: MDN

ondragend

Fired when a drag operation ends.
rsx! {
    div { 
        draggable: "true",
        ondragend: move |_| println!("Drag ended"),
        "Drag me"
    }
}
Event Data: DragData Reference: MDN

ondragenter

Fired when a dragged element enters a drop target.
rsx! {
    div { 
        ondragenter: move |_| println!("Drag enter"),
        "Drop zone"
    }
}
Event Data: DragData Reference: MDN

ondragleave

Fired when a dragged element leaves a drop target.
rsx! {
    div { 
        ondragleave: move |_| println!("Drag leave"),
        "Drop zone"
    }
}
Event Data: DragData Reference: MDN

ondragover

Fired when a dragged element is over a drop target.
rsx! {
    div { 
        ondragover: move |event| {
            event.prevent_default(); // Required to allow drop
        },
        "Drop zone"
    }
}
Event Data: DragData Reference: MDN

ondrop

Fired when an element is dropped.
rsx! {
    div { 
        ondragover: move |e| e.prevent_default(),
        ondrop: move |event| {
            event.prevent_default();
            println!("Dropped!");
        },
        "Drop zone"
    }
}
Event Data: DragData Reference: MDN

Scroll Events

onscroll

Fired when an element is scrolled.
rsx! {
    div { 
        onscroll: move |_| println!("Scrolling..."),
        style: "height: 200px; overflow: auto;",
        div { style: "height: 1000px;", "Scrollable content" }
    }
}
Event Data: ScrollData Reference: MDN

Media Events

onplay

Fired when media playback starts.
rsx! {
    video { src: "/video.mp4", onplay: move |_| println!("Playing") }
}
Event Data: MediaData Reference: MDN

onpause

Fired when media playback is paused.
rsx! {
    video { src: "/video.mp4", onpause: move |_| println!("Paused") }
}
Event Data: MediaData Reference: MDN

onended

Fired when media playback reaches the end.
rsx! {
    audio { src: "/audio.mp3", onended: move |_| println!("Finished") }
}
Event Data: MediaData Reference: MDN

Pointer Events

Pointer events provide a unified model for mouse, touch, and pen input.

onpointerdown

Fired when a pointer becomes active.
rsx! {
    div { 
        onpointerdown: move |event| {
            println!("Pointer type: {:?}", event.pointer_type());
        },
        "Touch or click here"
    }
}
Event Data: PointerData
  • pointer_type() - Type of pointer (mouse, touch, pen)
  • pointer_id() - Unique identifier
  • pressure() - Pressure applied
  • width(), height() - Contact geometry
Reference: MDN

onpointerup

Fired when a pointer is no longer active. Event Data: PointerData Reference: MDN

onpointermove

Fired when a pointer moves. Event Data: PointerData Reference: MDN

Touch Events

Touch events provide information about touch interactions.

ontouchstart

Fired when a touch point is placed on the touch surface.
rsx! {
    div { ontouchstart: move |_| println!("Touch start"), "Touch here" }
}
Event Data: TouchData Reference: MDN

ontouchmove

Fired when a touch point moves along the touch surface. Event Data: TouchData Reference: MDN

ontouchend

Fired when a touch point is removed from the touch surface. Event Data: TouchData Reference: MDN

ontouchcancel

Fired when a touch point has been disrupted. Event Data: TouchData Reference: MDN

Wheel Events

onwheel

Fired when a wheel button of a pointing device is rotated.
rsx! {
    div { 
        onwheel: move |event| {
            println!("Delta Y: {}", event.delta_y());
        },
        "Scroll with wheel"
    }
}
Event Data: WheelData
  • delta_x(), delta_y(), delta_z() - Scroll amounts
  • delta_mode() - Unit of delta values
Reference: MDN

Animation Events

onanimationstart

Fired when a CSS animation starts.
rsx! {
    div { 
        onanimationstart: move |_| println!("Animation started"),
        style: "animation: slide 1s;",
        "Animated"
    }
}
Event Data: AnimationData Reference: MDN

onanimationend

Fired when a CSS animation ends. Event Data: AnimationData Reference: MDN

onanimationiteration

Fired when a CSS animation iteration completes. Event Data: AnimationData Reference: MDN

Transition Events

ontransitionend

Fired when a CSS transition completes.
rsx! {
    div { 
        ontransitionend: move |_| println!("Transition done"),
        style: "transition: opacity 0.3s;",
        "Fading"
    }
}
Event Data: TransitionData Reference: MDN

Mounted Events

onmounted

Special Dioxus event fired when an element is mounted to the DOM.
rsx! {
    div { 
        onmounted: move |element| {
            // Access the mounted DOM element
            println!("Element mounted: {:?}", element);
        },
        "Content"
    }
}
Event Data: MountedData
  • Access to the underlying DOM element
  • Useful for measuring, focusing, or integrating with JavaScript

Event Methods

All events provide these common methods:

prevent_default()

Prevents the default browser action.
rsx! {
    a { 
        href: "https://example.com",
        onclick: move |event| {
            event.prevent_default();
            println!("Link click prevented");
        },
        "Link"
    }
}

stop_propagation()

Stops the event from bubbling up to parent elements.
rsx! {
    div { 
        onclick: move |_| println!("Outer"),
        button { 
            onclick: move |event| {
                event.stop_propagation();
                println!("Inner only");
            },
            "Click"
        }
    }
}

Async Event Handlers

Event handlers can be async:
rsx! {
    button {
        onclick: move |_| async move {
            let result = fetch_data().await;
            println!("Result: {:?}", result);
        },
        "Fetch"
    }
}

Event Handler Patterns

Capturing Values

let mut count = use_signal(|| 0);

rsx! {
    button {
        onclick: move |_| count += 1,
        "Count: {count}"
    }
}

Extracting Event Data

rsx! {
    input {
        oninput: move |event| {
            let value = event.value();
            let parsed: i32 = event.parsed().unwrap_or(0);
            println!("Value: {}, Parsed: {}", value, parsed);
        }
    }
}

Inline Event Handlers

rsx! {
    button { onclick: move |_| println!("Clicked"), "Click" }
}

Named Event Handlers

let handle_click = move |_| println!("Clicked");

rsx! {
    button { onclick: handle_click, "Click" }
}

See Also

Build docs developers (and LLMs) love