| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Event
Description
DOM event handlers and component lifecycle hooks for View.
There are two axes of event handling:
- DOM events —
on,onCapture,onWithOptions: attach JavaScript event listeners to VDOM nodes. Decoded event payloads are dispatched asactionvalues through the MVU loop. - Lifecycle hooks —
onCreated,onDestroyed, etc.: fire Haskell callbacks at specific points in a DOM element's mount/unmount lifecycle.
See Miso.Event.Decoder for building custom Decoder values and
Miso.Event.Types for structured payload types (KeyboardEvent,
PointerEvent, etc.).
Synopsis
- on :: MisoString -> Decoder result -> (result -> model -> DOMRef -> action) -> Attribute model action
- onMain :: MisoString -> Decoder result -> (result -> model -> DOMRef -> action) -> EventHandler model action
- onCapture :: MisoString -> Decoder result -> (result -> model -> DOMRef -> action) -> Attribute model action
- onWithOptions :: Phase -> Options -> MisoString -> Decoder result -> (result -> model -> DOMRef -> action) -> Attribute model action
- onMainWithOptions :: Phase -> Options -> MisoString -> Decoder result -> (result -> model -> DOMRef -> action) -> EventHandler model action
- data Phase
- onCreated :: action -> Attribute model action
- onCreatedWith :: (DOMRef -> action) -> Attribute model action
- onBeforeCreated :: action -> Attribute model action
- onDestroyed :: action -> Attribute model action
- onBeforeDestroyed :: action -> Attribute model action
- onBeforeDestroyedWith :: (DOMRef -> action) -> Attribute model action
- module Miso.Event.Decoder
- module Miso.Event.Types
Smart constructors
Arguments
| :: MisoString | DOM event name (e.g. |
| -> Decoder result | How to extract a Haskell value from the browser event object |
| -> (result -> model -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute model action |
Attach a bubble-phase event handler to a VDOM node.
Convenience wrapper for .onWithOptions BUBBLE defaultOptions
The decoded event payload is converted to an action by toAction and
dispatched into the component's update function.
let clickHandler = on "click" emptyDecoder $ \() _ -> MyAction in button_ [ clickHandler, class_ "add" ] [ text_ "+" ]
Arguments
| :: MisoString | DOM event name (e.g. |
| -> Decoder result | How to extract a Haskell value from the browser event object |
| -> (result -> model -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> EventHandler model action |
Like on but meant to used with the Miso.Native namespace.
view_ [ event $ static (onMain "tap" emptyDecoder ) ] [ text_ "+" ]
Arguments
| :: MisoString | DOM event name (e.g. |
| -> Decoder result | How to extract a Haskell value from the browser event object |
| -> (result -> model -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute model action |
Attach a capture-phase event handler to a VDOM node.
Convenience wrapper for .onWithOptions CAPTURE defaultOptions
Events in the capture phase propagate from the document root down to the target element, before any bubble-phase handlers run.
let captureClick = onCapture "click" emptyDecoder $ \() _ -> MyAction in button_ [ captureClick ] [ text_ "capture me" ]
Arguments
| :: Phase | |
| -> Options | Propagation options ( |
| -> MisoString | DOM event name (e.g. |
| -> Decoder result | How to extract a Haskell value from the browser event object |
| -> (result -> model -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute model action |
Attach an event handler with explicit phase and propagation options.
phase—BUBBLE(default) orCAPTURE: which DOM propagation phase the listener is registered on.options—defaultOptionsor a customOptionsvalue: controlspreventDefaultandstopPropagationbehaviour.eventName— the DOM event name, e.g."click","keydown".decoder— aDecoderthat extracts relevant fields from the JS event object.toAction— maps the decoded payload and the element'sDOMRefto anaction.
let clickHandler = onWithOptions BUBBLE defaultOptions "click" emptyDecoder $ \() _ -> Action in button_ [ clickHandler, class_ "add" ] [ text_ "+" ]
Arguments
| :: Phase | |
| -> Options | Propagation options ( |
| -> MisoString | DOM event name (e.g. |
| -> Decoder result | How to extract a Haskell value from the browser event object |
| -> (result -> model -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> EventHandler model action |
Mark an event handler to be dispatched on the Lynx main thread (MTS)
rather than the background thread. This is the analog of Lynx's
main-thread:bind prefix, and is decided per handler — so tap can be a
main-thread handler on one element and a background handler on another.
A main-thread handler runs imperatively on the MTS (no VDOM diff, no repaint);
pair it with a *With combinator to receive the target DOMRef and mutate it
via Miso.Native.MainThread. No-op on the browser/WASM runtime.
view_ [ event (static (mainThread (onTapWith Grow))) ] children
Since: 1.9.0.0
Phase during which event listener is invoked.
Since: 1.9.0.0
Lifecycle events
Arguments
| :: action | Action to dispatch after the element is inserted into the DOM |
| -> Attribute model action |
Fire an action immediately after the DOM element is inserted into the document.
Use this to trigger imperative setup (focus, measurements, third-party widget initialisation) that requires the element to be live in the page.
Since: 1.9.0.0
Arguments
| :: action | Action to dispatch just before the element is inserted into the DOM |
| -> Attribute model action |
Fire an action just before the DOM element is inserted into the document.
The element has been constructed but is not yet attached to the live DOM when this fires.
Since: 1.9.0.0
Arguments
| :: action | Action to dispatch after the element is removed from the DOM |
| -> Attribute model action |
Fire an action immediately after the DOM element is removed from the document.
The element has already been detached from the DOM when this fires.
Since: 1.9.0.0
Arguments
| :: action | Action to dispatch just before the element is removed from the DOM |
| -> Attribute model action |
Fire an action just before the DOM element is removed from the document.
The element is still present in the DOM when this fires, making it suitable for teardown logic (cancel animations, disconnect observers, etc.).
Since: 1.9.0.0
onBeforeDestroyedWith Source #
Arguments
| :: (DOMRef -> action) | Callback receiving the element's |
| -> Attribute model action |
Like onBeforeDestroyed but also receives the element's DOMRef.
Since: 1.9.0.0
Exports
module Miso.Event.Decoder
module Miso.Event.Types