| 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 r -> (r -> DOMRef -> action) -> Attribute action
- onCapture :: MisoString -> Decoder r -> (r -> DOMRef -> action) -> Attribute action
- onWithOptions :: Phase -> Options -> MisoString -> Decoder r -> (r -> DOMRef -> action) -> Attribute action
- data Phase
- onCreated :: action -> Attribute action
- onCreatedWith :: (DOMRef -> action) -> Attribute action
- onBeforeCreated :: action -> Attribute action
- onDestroyed :: action -> Attribute action
- onBeforeDestroyed :: action -> Attribute action
- onBeforeDestroyedWith :: (DOMRef -> action) -> Attribute action
- module Miso.Event.Decoder
- module Miso.Event.Types
Smart constructors
Arguments
| :: MisoString | DOM event name (e.g. |
| -> Decoder r | How to extract a Haskell value from the browser event object |
| -> (r -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute 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 r | How to extract a Haskell value from the browser event object |
| -> (r -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute 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 r | How to extract a Haskell value from the browser event object |
| -> (r -> DOMRef -> action) | Converts the decoded payload and the element's DOM reference to an |
| -> Attribute 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_ "+" ]
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 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 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 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 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 action |
Like onBeforeDestroyed but also receives the element's DOMRef.
Since: 1.9.0.0
Exports
module Miso.Event.Decoder
module Miso.Event.Types