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.Decoder

Description

Overview

Miso.Event.Decoder provides Decoder, the type that tells miso how to extract a Haskell value from a browser DOM Event object. It pairs a target path (DecodeTarget) into the event with a JSON-style parser (Value -> Parser a).

Decoders are consumed by on from Miso.Html.Event:

on :: MisoString   -- event name (e.g. "click")
   -> Decoder a      -- how to extract a from the event object
   -> (a -> action)   -- turn the extracted value into an action
   -> Attribute action

DecodeTarget

A DecodeTarget selects the sub-object of the event to decode:

  • DecodeTarget [] — the event object itself (e.g. for keyboard events).
  • DecodeTarget ["target"]event.target (e.g. for input values).
  • DecodeTargets [["a"], ["b"]] — tries event.a first, then event.b; the first successful decode wins.

Built-in decoders

emptyDecoder
()click, submit, stateless events
keycodeDecoder
KeyCodekeydown / keyup key code
keyInfoDecoder
KeyInfo — key code + modifier keys
valueDecoder
MisoStringinput / change (event.target.value)
checkedDecoder
Checked — checkbox change (event.target.checked)
pointerDecoder
PointerEvent — pointer/mouse position and metadata

Custom decoders

Build a custom decoder with at or by constructing Decoder directly:

-- Extract (offsetX, offsetY) from a click event
clickXY :: Decoder (Int, Int)
clickXY = Decoder
  { decodeAt = DecodeTarget []
  , decoder  = withObject "click" $ \o ->
      (,) <$> o .: "offsetX"
          <*> o .: "offsetY"
  }

See also

Synopsis

Types

data Decoder a Source #

Decoder data type for parsing events

Constructors

Decoder 

Fields

data DecodeTarget Source #

Data type representing path (consisting of field names) within event object where a decoder should be applied.

Constructors

DecodeTarget [MisoString]

Specify single path within Event object, where a decoder should be applied.

DecodeTargets [[MisoString]]

Specify multiple paths withing Event object, where decoding should be attempted. The first path where decoding suceeds is the one taken.

Instances

Instances details
ToJSVal DecodeTarget Source #

ToJSVal instance for DecodeTarget.

Instance details

Defined in Miso.Event.Decoder

Combinators

at Source #

Arguments

:: [MisoString]

Path into the event object (e.g. ["target"] for event.target, [] for the event itself)

-> (Value -> Parser a)

JSON-style decoder applied at the given path

-> Decoder a 

Smart constructor for building a Decoder.

Decoders

emptyDecoder :: Decoder () Source #

Empty Decoder for use with events like "click" that do not return any meaningful values

keycodeDecoder :: Decoder KeyCode Source #

Retrieves either "keyCode", "which" or "charCode" field in Decoder

keyInfoDecoder :: Decoder KeyInfo Source #

Retrieves either "keyCode", "which" or "charCode" field in Decoder, along with shift, ctrl, meta and alt.

checkedDecoder :: Decoder Checked Source #

Retrieves "checked" field in Decoder

valueDecoder :: Decoder MisoString Source #

Retrieves "value" field in Decoder

pointerDecoder :: Decoder PointerEvent Source #

Pointer Decoder for use with events like "onpointerover"