noredink-ui/src-0.18/EventExtras.elm
Richard Feldman e2907d9ba2 Prep for 0.19 dual-publish (#163)
* Use elm-css 16.0.0

* 💀 Ui.Checkbox V1 and V2

* s/Css.Foreign/Css.Global/g

* 💀 Nri.Ui.Styles.V1

* 💀 BannerAlert.V1

* 💀 Modal.V1

* 💀 Dropdown.V1

* 💀 Select.V1 and V2

* 💀 Alert.V1

* 💀 Button.V1 and V2

* 💀 Divider.V1

* 💀 Icon.V1 and V2

* 💀 Outline.V1

* 💀 SegmentedControl.V1-V5

* 💀 TextArea.V1 and V2

* 💀 TextInput.V1

* delete the rest of the modules

* actually more deletions

* InputStyles v1 is unused

* move to src-0.18

* do the 0.19 upgrade

* select options are addressable by index

* elm-css 16

* update scripts

* elm-format

* Update V2.elm

* put the nbsp back

* elm-format validation for both versions
2018-11-12 15:38:19 -08:00

42 lines
1.3 KiB
Elm

module EventExtras exposing (onClickPreventDefaultForLinkWithHref)
import Html
import Html.Events exposing (..)
import Json.Decode
{-| This is necessary to use in single-page apps (SPA) when intercepting the
`onClick` of `<a>` tags that trigger navigation within the app,
as a normal `onClickPreventDefault` will prevent the user from opening the link
in a new tab/window.
(From <https://github.com/elm-lang/html/issues/110>)
-}
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
succeedIfFalse msg preventDefault =
case preventDefault of
False ->
Json.Decode.succeed msg
True ->
Json.Decode.fail "succeedIfFalse: condition was True"
in
onWithOptions "click"
{ stopPropagation = False
, preventDefault = True
}
(isSpecialClick
|> Json.Decode.andThen (succeedIfFalse msg)
)