miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Event

Description

DOM event handlers and component lifecycle hooks for View.

There are two axes of event handling:

  • DOM eventson, onCapture, onWithOptions: attach JavaScript event listeners to VDOM nodes. Decoded event payloads are dispatched as action values through the MVU loop.
  • Lifecycle hooksonCreated, 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

Smart constructors

on Source #

Arguments

:: MisoString

DOM event name (e.g. "click", "input")

-> 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 action

-> 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_ "+" ]

onCapture Source #

Arguments

:: MisoString

DOM event name (e.g. "click")

-> 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 action

-> 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" ]

onWithOptions Source #

Arguments

:: Phase

Event propagation phase: BUBBLE (default) or CAPTURE

-> Options

Propagation options (preventDefault, stopPropagation)

-> MisoString

DOM event name (e.g. "click", "keydown")

-> 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 action

-> Attribute action 

Attach an event handler with explicit phase and propagation options.

  • phaseBUBBLE (default) or CAPTURE: which DOM propagation phase the listener is registered on.
  • optionsdefaultOptions or a custom Options value: controls preventDefault and stopPropagation behaviour.
  • eventName — the DOM event name, e.g. "click", "keydown".
  • decoder — a Decoder that extracts relevant fields from the JS event object.
  • toAction — maps the decoded payload and the element's DOMRef to an action.
let clickHandler = onWithOptions BUBBLE defaultOptions "click" emptyDecoder $ \() _ -> Action
in button_ [ clickHandler, class_ "add" ] [ text_ "+" ]

data Phase Source #

Phase during which event listener is invoked.

Since: 1.9.0.0

Constructors

CAPTURE 
BUBBLE 

Instances

Instances details
Show Phase Source # 
Instance details

Defined in Miso.Event.Types

Methods

showsPrec :: Int -> Phase -> ShowS #

show :: Phase -> String #

showList :: [Phase] -> ShowS #

Eq Phase Source # 
Instance details

Defined in Miso.Event.Types

Methods

(==) :: Phase -> Phase -> Bool #

(/=) :: Phase -> Phase -> Bool #

ToJSVal Phase Source # 
Instance details

Defined in Miso.Event.Types

Methods

toJSVal :: Phase -> IO JSVal Source #

Lifecycle events

onCreated Source #

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

onCreatedWith Source #

Arguments

:: (DOMRef -> action)

Callback receiving the element's DOMRef after it is inserted into the DOM

-> Attribute action 

Like onCreated but also receives the element's DOMRef.

Useful when you need to store or forward the raw DOM node to a JS library.

Since: 1.9.0.0

onBeforeCreated Source #

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

onDestroyed Source #

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

onBeforeDestroyed Source #

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 DOMRef just before it is removed from the DOM

-> Attribute action 

Like onBeforeDestroyed but also receives the element's DOMRef.

Since: 1.9.0.0

Exports