elm / virtual-dom

The foundation of HTML and SVG in Elm.
https://package.elm-lang.org/packages/elm/virtual-dom/latest
BSD 3-Clause "New" or "Revised" License
209 stars 80 forks source link

Fixes #103 Html.map + blur event on DOM removal makes impossible states possible #166

Open decioferreira opened 4 years ago

decioferreira commented 4 years ago

Updated SSCCE: https://ellie-app.com/8cPfM8xMj7Za1

This issue happens in chrome, and not firefox (as mentioned on #103). To see the issue on the SSCCE above:

More details about the problem can be found on #103.

This should also fix elm/html#119 as mentioned on #105.

matsjoyce commented 4 years ago

@decioferreira This PR misses the case when no Html.map is used. In that case, eventNode is the sendToMap function. A version that accounts for that is:


            var oldHandler = oldCallback.__handler;
            if (oldHandler.$ === newHandler.$)
            {
                oldCallback.__handler = newHandler;
                oldCallback.__eventNode = eventNode.__tagger ? {
                    __tagger: eventNode.__tagger,
                    __parent: eventNode.__parent
                } : eventNode;
                continue;
            }
            domNode.removeEventListener(key, oldCallback);

// and

    callback.__handler = initialHandler;
    callback.__eventNode = eventNode.__tagger ? { __tagger: eventNode.__tagger, __parent: eventNode.__parent } : eventNode;

    return callback;
decioferreira commented 4 years ago

hi @matsjoyce, thank you so much for your comment. Any chance you could share an SSCCE which shows the problem that your change would allow to fix? thanks in advance

matsjoyce commented 4 years ago

With the following code, click on the input and then somewhere else (focus then unfocus).

module Main exposing (main)

import Html as H
import Html.Events as HE
import Browser

type alias Model = {}
type Msg = Blurred

main =
    Browser.sandbox
        { init = {}
        , update = update
        , view = view
        }

update msg model = {}

view model = H.input [HE.onBlur Blurred] []

In the console I get:

Test.elm:3005 Uncaught TypeError: currentEventNode is not a function
    at HTMLInputElement.callback (Test.elm:3005)
callback @ Test.elm:3005
decioferreira commented 4 years ago

@matsjoyce I've updated the code with your fix, thank you!

cmditch commented 2 years ago

Thanks for opening this @decioferreira. This has been affecting us as well. How have you addressed the problem in the meantime?

lucamug commented 2 years ago

@cmditch,

How have you addressed the problem in the meantime?

Some workaround seems available at https://github.com/elm/virtual-dom/issues/103

thomasin commented 1 year ago

Just a note: I experience this bug only in Safari now, not Chrome or Firefox (with a Mouse Leave event)