evancz/elm-html/4.0.2

NOTE: this package moved to elm/html

NOTE: this package is not compatible with Elm 0.19.1

Html.Events

It is often helpful to create an Union Type so you can have many different kinds of events as seen in the TodoMVC example.

Focus Helpers

onBlur : Address a -> a -> Attribute
onFocus : Address a -> a -> Attribute
onSubmit : Address a -> a -> Attribute

Keyboard Helpers

onKeyUp : Address a -> (Int -> a) -> Attribute
onKeyDown : Address a -> (Int -> a) -> Attribute
onKeyPress : Address a -> (Int -> a) -> Attribute

Mouse Helpers

onClick : Address a -> a -> Attribute
onDoubleClick : Address a -> a -> Attribute
onMouseMove : Address a -> a -> Attribute
onMouseDown : Address a -> a -> Attribute
onMouseUp : Address a -> a -> Attribute
onMouseEnter : Address a -> a -> Attribute
onMouseLeave : Address a -> a -> Attribute
onMouseOver : Address a -> a -> Attribute
onMouseOut : Address a -> a -> Attribute

Custom Event Handlers

on : String -> Decoder a -> (a -> Message) -> Attribute

Create a custom event listener.

import Json.Decode as Json


onClick : Signal.Address a -> Attribute
onClick address =
    on "click" Json.value (\_ -> Signal.message address ())

You first specify the name of the event in the same format as with JavaScript’s addEventListener. Next you give a JSON decoder, which lets you pull information out of the event object. If that decoder is successful, the resulting value is given to a function that creates a Signal.Message. So in our example, we will send () to the given address.

targetValue : Decoder String

A Json.Decoder for grabbing event.target.value from the triggered event. This is often useful for input event on text fields.

onInput : Signal.Address a -> (String -> a) -> Attribute
onInput address contentToValue =
    on "input" targetValue (\str -> Signal.message address (contentToValue str))
targetChecked : Decoder Bool

A Json.Decoder for grabbing event.target.checked from the triggered event. This is useful for input event on checkboxes.

onInput : Signal.Address a -> (Bool -> a) -> Attribute
onInput address contentToValue =
    on "input" targetChecked (\bool -> Signal.message address (contentToValue bool))
keyCode : Decoder Int

A Json.Decoder for grabbing event.keyCode from the triggered event. This is useful for key events today, though it looks like the spec is moving towards the event.key field for this someday.

onKeyUp : Signal.Address a -> (Int -> a) -> Attribute
onKeyUp address handler =
    on "keyup" keyCode (\code -> Signal.message address (handler code))
onWithOptions : String -> Options -> Decoder a -> (a -> Message) -> Attribute

Same as on but you can set a few options.

type alias Options =
{ stopPropagation : Bool
, preventDefault : Bool
}

Options for an event listener. If stopPropagation is true, it means the event stops traveling through the DOM so it will not trigger any other event listeners. If preventDefault is true, any built-in browser behavior related to the event is prevented. For example, this is used with touch events when you want to treat them as gestures of your own, not as scrolls.

defaultOptions : Options

Everything is False by default.

defaultOptions =
    { stopPropagation = False
    , preventDefault = False
    }