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.Html.Event

Description

Overview

Miso.Html.Event provides pre-wired event-handler Attribute values for the most common browser events. Each handler is built on the lower-level on / onWithOptions primitives from Miso.Event.

This module is re-exported in its entirety by Miso.Html and Miso.

Naming conventions

Handlers follow a consistent naming pattern:

onXxx action
fires action; no event data extracted
onXxxWith (a -> action)
passes extracted event data or DOMRef
onXxxWithOptions opts act
adds Options (preventDefault / stopPropagation) before firing
onXxxCapture action
registers in the capture phase instead of bubble

Quick start

import Miso

view :: Model -> View Model Action
view m =
  div_ []
    [ button_ [ onClick Increment ]        [ text "+" ]
    , input_  [ onInput SetText
                    , value_ m.text ]      []
    , form_   [ onSubmit Submit ]         []  -- preventDefault by default
    ]

Event groups

Notes

  • onSubmit enables preventDefault by default to suppress the native form submission.
  • onEnter is a convenience wrapper around onKeyDown that fires different actions depending on whether keyCode == 13.
  • The WithOptions variants require defaultEvents (or a superset) to include the relevant event name in the component's events map.

See also

Synopsis

Mouse

onClickWith :: (DOMRef -> action) -> Attribute action Source #

https://developer.mozilla.org/en-US/docs/Web/Events/click Like onClick, but passes the DOM reference along (akin to getElementById).

onClickWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch on click

-> Attribute action 

onDoubleClickWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch on double-click

-> Attribute action 

onContextMenuWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when the context menu event fires

-> Attribute action 

https://developer.mozilla.org/en-US/docs/Web/Events/contextmenu

This can be used to disable right-click context menu from appearing

div_ [ onContextMenuWithOptions NoOp defaultOptions { preventDefault = False } ] [ ]

Since: 1.9.0.0

Keyboard

onKeyDown Source #

Arguments

:: (KeyCode -> action)

Callback receiving the numeric key code of the pressed key

-> Attribute action 

onKeyDownWithInfo Source #

Arguments

:: (KeyInfo -> action)

Callback receiving the key code and modifier key state

-> Attribute action 

onKeyPress Source #

Arguments

:: (KeyCode -> action)

Callback receiving the numeric key code of the pressed key

-> Attribute action 

onKeyUp Source #

Arguments

:: (KeyCode -> action)

Callback receiving the numeric key code of the released key

-> Attribute action 

onEnter Source #

Arguments

:: action

The action to call when the keydown *is not* 13 (typically NoOp or Id)

-> action

The action to call when keydown *is* 13.

-> Attribute action 

onEnter

A convenience function for processing the Enter key.

data Action = NoOp | OnEnter

type Model = Int

view :: Model -> View model Action
view entryId = input_ [ onEnter NoOp OnEnter ]

Since: 1.9.0.0

Form

onInput Source #

Arguments

:: (MisoString -> action)

Callback receiving event.target.value

-> Attribute action 

onInputWith Source #

Arguments

:: (MisoString -> DOMRef -> action)

Callback receiving event.target.value and the element's DOMRef

-> Attribute action 

onChange Source #

Arguments

:: (MisoString -> action)

Callback receiving event.target.value

-> Attribute action 

onChangeWith Source #

Arguments

:: (MisoString -> DOMRef -> action)

Callback receiving event.target.value and the element's DOMRef

-> Attribute action 

Focus

onBlur :: action -> Attribute action Source #

blur event defined with custom options

https://developer.mozilla.org/en-US/docs/Web/Events/blur

Drag

onDragWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch while the element is being dragged

-> Attribute action 

onDragLeaveWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when a dragged element leaves this target

-> Attribute action 

onDragEnterWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when a dragged element enters this target

-> Attribute action 

onDragEndWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when the drag operation ends

-> Attribute action 

onDragStartWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when the drag starts

-> Attribute action 

onDragOverWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch while the dragged element is over this target

-> Attribute action 

Drop

onDrop Source #

Arguments

:: Options

Propagation options — typically include preventDefault to allow the drop

-> action

Action to dispatch when a dragged element is dropped on this target

-> Attribute action 

onDropWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch on drop

-> Attribute action 

Select

onSelect Source #

Arguments

:: (MisoString -> action)

Callback receiving event.target.value of the selected text

-> Attribute action 

Pointer

Media

onUnload :: action -> Attribute action Source #

onUnload event

Touch

onTouchStartWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch on touch start

-> Attribute action 

onTouchEndWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch on touch end

-> Attribute action 

onTouchMoveWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch while a touch point is moving

-> Attribute action 

onTouchCancelWithOptions Source #

Arguments

:: Options

Propagation options (preventDefault, stopPropagation)

-> action

Action to dispatch when a touch point is cancelled

-> Attribute action