Skip to content

Instantly share code, notes, and snippets.

@avh4
Last active June 27, 2019 02:04
Show Gist options
  • Save avh4/712d43d649b7624fab59285a70610707 to your computer and use it in GitHub Desktop.
Save avh4/712d43d649b7624fab59285a70610707 to your computer and use it in GitHub Desktop.
import Html
import Html.Events
import Json.Decode
onClickPreventDefaultForLinkWithHref : msg -> Html.Attribute msg
onClickPreventDefaultForLinkWithHref msg =
let
isSpecialClick : Json.Decode.Decoder Bool
isSpecialClick =
Json.Decode.map2
(\isCtrl isMeta -> isCtrl || isMeta)
(Json.Decode.field "ctrlKey" Json.Decode.bool)
(Json.Decode.field "metaKey" Json.Decode.bool)
succeedIfFalse : a -> Bool -> Json.Decode.Decoder ( a, Bool )
succeedIfFalse msg_ preventDefault =
case preventDefault of
False ->
Json.Decode.succeed ( msg_, True )
True ->
Json.Decode.fail "succeedIfFalse: condition was True"
in
Html.Events.preventDefaultOn "click"
(isSpecialClick
|> Json.Decode.andThen (succeedIfFalse msg)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment