mikesol / purescript-deku

A PureScript web UI framework
https://purescript-deku.surge.sh/
Apache License 2.0
123 stars 12 forks source link

OnTouchstart doesn't seem to fire #95

Closed Ebmtranceboy closed 1 year ago

Ebmtranceboy commented 1 year ago

Hi, I have this silly code (changing the button text according to clicks) working well with D.OnMousedown but not with D.OnTouchstart on my Android phone, either Chrome or Samsung Internet.

module Main where

import Prelude

import Data.Tuple.Nested ((/\))

import Deku.Attribute ((<:=>), cb)
import Deku.Control (text)
import Deku.Do as Deku
import Deku.DOM as D
import Deku.Hooks (useState)
import Deku.Toplevel (runInBody)

import Effect (Effect)
import Web.Event.Event (preventDefault)

main :: Effect Unit
main = do
    runInBody Deku.do
        setParity /\ parity <- useState false
        D.div
            [ D.OnMousedown <:=> (\p -> cb \e -> do
                preventDefault e
                setParity $ not p) <$> parity
            ]
            [ D.button_ [text $ (if _ then "even" else "odd") <$> parity]
            ]

I compared both js bundles (very similar as expected) so I can't figure out where is the trouble since I have no problem when using touch events with the Web.HTML.* and Web.DOM.* libraries when I do (painfully) addEventListener (EventType "touchstart") start true (toEventTarget element)

i-am-the-slime commented 1 year ago

I tihnk this is correct. Have you tried with OnClick? That should work on both.

Ebmtranceboy commented 1 year ago

Indeed, OnClick works on both. When you say 'correct', do you mean my observations are the expected behavior ?

mikesol commented 1 year ago

Sorry, late to the party here. I can add a unit test. Does anyone know if jsdom can simulate touch events?

Ebmtranceboy commented 1 year ago

I can't say but, from this SO thread, a generic solution working for desktop, Android and MicroSoft (should I mention Apple ?)'s phones would be witchcraft :(

MonoidMusician commented 1 year ago

@mikesol this attribute name (and related ones) still have spaces in it, you need to trim it during codegen: https://github.com/mikesol/purescript-deku/blob/778a6ee1883bd353c9f51cbc27aa2ccfc1838dde/src/Deku/DOM/Attr/OnTouchstart.purs#L12

mikesol commented 1 year ago

Fixed! https://github.com/mikesol/purescript-deku/releases/tag/v0.9.24 Thanks to @Ebmtranceboy for reporting and thanks to @MonoidMusician for spotting the bug!