lustre/event

Functions

pub fn checked(event: Dynamic) -> Result(Bool, List(DecodeError))

Similar to value, decoding a checkbox’s checked state is common enough to warrant a dedicated decoder. This attempts to decode event.target.checked as a boolean.

pub fn emit(event: String, data: Json) -> Effect(a)

Dispatches a custom message from a Lustre component. This lets components communicate with their parents the same way native DOM elements do.

Any JSON-serialisable payload can be attached as additional data for any event listeners to decode. This data will be on the event’s detail property.

pub fn mouse_position(
  event: Dynamic,
) -> Result(#(Float, Float), List(DecodeError))

Decodes the mouse position from any event that has a clientX and clientY property.

pub fn on(
  name: String,
  handler: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Attribute(a)

Listens for the given event and applies the handler to the event object. If the handler returns an Ok the resulting message will be dispatched, otherwise the event (and any decoding error) will be ignored.

The event name is typically an all-lowercase string such as “click” or “mousemove”. If you’re listening for non-standard events (like those emitted by a custom element) their event names might be slightly different.

pub fn on_blur(msg: a) -> Attribute(a)
pub fn on_check(msg: fn(Bool) -> a) -> Attribute(a)
pub fn on_click(msg: a) -> Attribute(a)
pub fn on_focus(msg: a) -> Attribute(a)
pub fn on_input(msg: fn(String) -> a) -> Attribute(a)
pub fn on_keydown(msg: fn(String) -> a) -> Attribute(a)

Listens for key dow events on an element, and dispatches a message with the current key being pressed.

pub fn on_keypress(msg: fn(String) -> a) -> Attribute(a)

Listens for key presses on an element, and dispatches a message with the current key being pressed.

pub fn on_keyup(msg: fn(String) -> a) -> Attribute(a)

Listens for key up events on an element, and dispatches a message with the current key being released.

pub fn on_mouse_down(msg: a) -> Attribute(a)
pub fn on_mouse_enter(msg: a) -> Attribute(a)
pub fn on_mouse_leave(msg: a) -> Attribute(a)
pub fn on_mouse_out(msg: a) -> Attribute(a)
pub fn on_mouse_over(msg: a) -> Attribute(a)
pub fn on_mouse_up(msg: a) -> Attribute(a)
pub fn on_submit(msg: a) -> Attribute(a)

Listens for the form’s submit event, and dispatches the given message. This will automatically call prevent_default to stop the form from submitting.

pub fn prevent_default(event: Dynamic) -> Nil

Calls an event’s preventDefault method. If the Dynamic does not have a preventDefault method, this function does nothing.

As the name implies, preventDefault will prevent any default action associated with an event from occuring. For example, if you call preventDefault on a submit event, the form will not be submitted.

See: https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

pub fn stop_propagation(event: Dynamic) -> Nil

Calls an event’s stopPropagation method. If the Dynamic does not have a stopPropagation method, this function does nothing.

Stopping event propagation means the event will not “bubble” up to parent elements. If any elements higher up in the DOM have event listeners for the same event, they will not be called.

See: https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

pub fn value(event: Dynamic) -> Result(String, List(DecodeError))

Decoding an input element’s value is such a common operation that we have a dedicated decoder for it. This attempts to decoder event.target.value as a string.

Search Document