| 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.Decoder
Contents
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") ->Decodera -- how to extractafrom the event object -> (a -> action) -- turn the extracted value into an action ->Attributeaction
DecodeTarget
A DecodeTarget selects the sub-object of the event to decode:
— the event object itself (e.g. for keyboard events).DecodeTarget[]—DecodeTarget["target"]event.target(e.g. for input values).— triesDecodeTargets[["a"], ["b"]]event.afirst, thenevent.b; the first successful decode wins.
Built-in decoders
emptyDecoder()—click,submit, stateless eventskeycodeDecoderKeyCode—keydown/keyupkey codekeyInfoDecoderKeyInfo— key code + modifier keysvalueDecoderMisoString—input/change(event.target.value)checkedDecoderChecked— checkboxchange(event.target.checked)pointerDecoderPointerEvent— 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
- Miso.Html.Event —
onand the pre-wired event handlers (onClick,onInput, …) - Miso.Event.Types — payload types (
KeyCode,PointerEvent, …) - Miso.JSON —
Value,Parser,withObject,(.:)
Synopsis
- data Decoder a = Decoder {
- decoder :: Value -> Parser a
- decodeAt :: DecodeTarget
- data DecodeTarget
- = DecodeTarget [MisoString]
- | DecodeTargets [[MisoString]]
- at :: [MisoString] -> (Value -> Parser a) -> Decoder a
- emptyDecoder :: Decoder ()
- keycodeDecoder :: Decoder KeyCode
- keyInfoDecoder :: Decoder KeyInfo
- checkedDecoder :: Decoder Checked
- valueDecoder :: Decoder MisoString
- pointerDecoder :: Decoder PointerEvent
Types
Decoder data type for parsing events
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
| ToJSVal DecodeTarget Source # |
|
Defined in Miso.Event.Decoder | |
Combinators
Arguments
| :: [MisoString] | Path into the event object (e.g. |
| -> (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.
valueDecoder :: Decoder MisoString Source #
Retrieves "value" field in Decoder
pointerDecoder :: Decoder PointerEvent Source #
Pointer Decoder for use with events like "onpointerover"