gregziegan / elm-autocomplete

Autocomplete for Elm apps; in Elm
http://package.elm-lang.org/packages/thebritican/elm-autocomplete/latest
BSD 3-Clause "New" or "Revised" License
173 stars 43 forks source link

No closing of overlay on blur. #33

Open viktor-evdokimov opened 8 years ago

viktor-evdokimov commented 8 years ago

See screenshot

viktor-evdokimov commented 8 years ago

I don't know what is wrong with github on my machine today, but can't attach screenshot. Basically I do the following:

Have both autocomplete overlays open.

gregziegan commented 8 years ago

Good catch! Yep, those are both just examples of using the API. Will fix the business logic on the demo page. I have some more work to do on that page anyways 😃 . Am on holiday now, so any PRs are certainly welcome until I get back!

ericgj commented 8 years ago

I think this can be finessed using the event.relatedTarget -- i.e. write a custom onBlur decoder that checks if the relatedTarget id == the menu id. If it is, then don't hide the menu, else hide the menu. I'm going to try this soon.

(My guess is relatedTarget was added for exactly this kind of situation. Not sure how far back browsers support it but I think it's widely supported in modern browsers.)

ericgj commented 8 years ago

This seems to work for me (based on the Accessible example). Note adding an id to the menu div so we can check if the thing the user clicked on is the menu or not:

-- in view
let
    -- ...
    relatedTargetId = 
        Json.at ["relatedTarget", "id"] Json.string

    blurDecoder =
        Json.maybe relatedTargetId
            |> Json.map (Maybe.map \id -> if id == menuId then NoOp else OnBlur)
            |> Json.map (Maybe.withDefault NoOp)

    -- ...
in
    div [] 
        [ input
            [ on "blur" blurDecoder 
            -- ...
            ] [ ]
        , menu
        ]

-- in update
    case msg of
        -- ...
        OnBlur:
            ( resetMenu model ! [] )