This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Mouse, Touch, Pointer, Wheel and Drag events
version 2.0.2
license MPL-2.0
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 2.0.2
Committed At 2018-05-19 17:35:24 UTC
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1

README

elm-pointer-events

This package aims at handling all kinds of pointer events in elm. To be more specific, this means:

If you are looking for only one standard kind of interaction (mouse or touch), I recommend that you use the Mouse or Touch modules. If however, you are designing a multi-device (desktop/tablet/mobile/...) experience, I recommend that you use the Pointer module.

Pointer events are a unified interface for similar input devices (mouse, pen, touch, ...). Since maintaining both mouse and touch events for compatibility is really cumbersome, using a unified pointer events interface is a relief.

Beware though, that the pointer API is not well supported by all browsers. Firefox < 59 and Safari do not natively support pointer events. So I strongly recommend to use this package in pair with the elm-pep polyfill for compatibility with major browsers.

Usage

Mouse and Pointer

The Mouse and Pointer modules have very similar API so I will use Mouse as an example. Let's say you want the coordinates of a mouse down event relative to the DOM element that triggered it. In JavaScript, these are provided by the offsetX and offsetY properties of the mouse event. Using this module, these are similarly provided by the offsetPos attribute of a mouse Event:

import Mouse

-- ...

type Msg
    = MouseDownAt ( Float, Float )

view =
    div
        [Mouse.onDown (\event -> MouseDownAt event.offsetPos)]
        [text "click here"]

If you are using the Pointer module, it is recommended that you deactivate touch-action to disable browsers scroll/pinch/... touch behaviors.

Also, if you are designing some kind of drawing application, you want to be able to keep track of a pointer that leave the drawing area to know if the pointer went up. This is possible using what is called pointer capture. The example below demonstrates how to do this.

div
    [ Pointer.onDown ...
    , Pointer.onMove ...
    , Pointer.onUp ...

    -- no touch-action
    , Html.Attributes.style [ ( "touch-action", "none" ) ]

    -- pointer capture hack to continue "globally" the event anywhere on document
    , attribute "onpointerdown" "event.target.setPointerCapture(event.pointerId);"
    ]
    [ -- the drawing area
    ]

Touch

Multi-touch interactions can be managed using the Touch module. In case you only want to handle single touch interactions, you could do something like below:

import Touch

-- ...

type Msg
    = StartAt ( Float, Float )
    | MoveAt ( Float, Float )
    | EndAt ( Float, Float )

view =
    div
        [ Touch.onStart (StartAt << touchCoordinates)
        , Touch.onMove (MoveAt << touchCoordinates)
        , Touch.onEnd (EndAt << touchCoordinates)
        ]
        [text "touch here"]

touchCoordinates : Touch.Event -> ( Float, Float )
touchCoordinates touchEvent =
    List.head touchEvent.changedTouches
        |> Maybe.map .clientPos
        |> Maybe.withDefault ( 0, 0 )

Wheel

You can manage Wheel events with the corresponding module. Since it is an extension to the Mouse module all mouse inherited properties are also available in the attribute mouseEvent.

To simply check for wheel rolling up or down you could do something like below:

import Wheel

-- ...

type Msg
    = Scrolling Float

view =
    div
        [Wheel.onWheel (\event -> Scrolling event.deltaY)]
        [text "scroll here"]

Drag

Due to the limitation of not being able to call JavaScript functions directly in elm, full drag and drop API cannot be supported.

However, most of the time we just need to be able to drop some files from the file system to the web page. By providing the dragover, and drop events, this module enables such use case. Of course, the file retrieved in the form of a Json.Decode.Value would still have to be sent through ports if further processing is needed that cannot be done directly in elm using the Value.

Examples

Minimalist working examples are available for each module in the examples/ directory. To test one example, cd into one of them and compile the elm file with the command:

elm-make Main.elm --output Main.js

Then use any static http server like:

$ python3 -m http.server 8888

And open your browser at localhost:8888 to load the index.html page.

Want to contribute?

If you are interested in contributing in any way (feedback, bug report, implementation of new functionality, ...) don't hesitate to reach out on slack (user @mattpiz) and/or open an issue. Discussion is the best way to start any contribution.

Documentation

The package documentation is available on the elm package website.

License

This Source Code Form is subject to the terms of the Mozilla Public License,v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Contributors

  • Matthieu Pizenberg - @mpizenberg
  • Thomas Forgione - @tforgione (elm-pep polyfill)
  • Robert Vollmert - @robx (elm-pep polyfill)