Last active
June 27, 2019 02:04
-
-
Save avh4/712d43d649b7624fab59285a70610707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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