Merve EventExtras.Styled and EventExtras

This commit is contained in:
Tessa Kelly 2020-04-24 12:51:18 -07:00
parent 7068f4463c
commit e9ae1f8a43
10 changed files with 55 additions and 96 deletions

View File

@ -15,7 +15,7 @@ module ClickableAttributes exposing
{-| -}
import AttributeExtras exposing (targetBlank)
import EventExtras.Styled as EventExtras
import EventExtras
import Html.Styled exposing (Attribute)
import Html.Styled.Attributes as Attributes
import Html.Styled.Events as Events

View File

@ -1,7 +1,7 @@
module EventExtras exposing (onClickPreventDefaultForLinkWithHref)
module EventExtras exposing (onClickForLinkWithHref, onClickPreventDefaultForLinkWithHref, onClickStopPropagation)
import Html
import Html.Events as Events
import Html.Styled as Html
import Html.Styled.Events as Events
import Json.Decode
@ -37,3 +37,49 @@ onClickPreventDefaultForLinkWithHref msg =
|> Json.Decode.andThen (succeedIfFalse msg)
|> Json.Decode.map (\a -> ( a, True ))
)
{-| This is necessary to use when intercepting the
`onClick` of `<a>` tags that trigger navigation within the app,
as a normal `onClick` will prevent the user from opening the link
in a new tab/window.
(From <https://github.com/elm-lang/html/issues/110>)
-}
onClickForLinkWithHref : msg -> Html.Attribute msg
onClickForLinkWithHref 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
Events.on "click"
(isSpecialClick
|> Json.Decode.andThen (succeedIfFalse msg)
)
{-| -}
onClickStopPropagation : msg -> Html.Attribute msg
onClickStopPropagation msg =
onWithStopPropagation "click" msg
{-| -}
onWithStopPropagation : String -> msg -> Html.Attribute msg
onWithStopPropagation name msg =
Events.stopPropagationOn name
(Json.Decode.succeed ( msg, True ))

View File

@ -1,85 +0,0 @@
module EventExtras.Styled exposing (onClickForLinkWithHref, onClickPreventDefaultForLinkWithHref, onClickStopPropagation)
import Html.Styled as Html
import Html.Styled.Events as Events
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
Events.preventDefaultOn "click"
(isSpecialClick
|> Json.Decode.andThen (succeedIfFalse msg)
|> Json.Decode.map (\a -> ( a, True ))
)
{-| This is necessary to use when intercepting the
`onClick` of `<a>` tags that trigger navigation within the app,
as a normal `onClick` will prevent the user from opening the link
in a new tab/window.
(From <https://github.com/elm-lang/html/issues/110>)
-}
onClickForLinkWithHref : msg -> Html.Attribute msg
onClickForLinkWithHref 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
Events.on "click"
(isSpecialClick
|> Json.Decode.andThen (succeedIfFalse msg)
)
{-| -}
onClickStopPropagation : msg -> Html.Attribute msg
onClickStopPropagation msg =
onWithStopPropagation "click" msg
{-| -}
onWithStopPropagation : String -> msg -> Html.Attribute msg
onWithStopPropagation name msg =
Events.stopPropagationOn name
(Json.Decode.succeed ( msg, True ))

View File

@ -54,7 +54,7 @@ import Accessibility.Styled.Role as Role
import Accessibility.Styled.Widget as Widget
import Css exposing (Style)
import Css.Global
import EventExtras.Styled as EventExtras
import EventExtras
import Html.Styled as Styled
import Html.Styled.Attributes as Attributes
import Html.Styled.Events as Events

View File

@ -60,7 +60,7 @@ import Accessibility.Styled.Role as Role
import Accessibility.Styled.Widget as Widget
import Css exposing (Style)
import Css.Global
import EventExtras.Styled as EventExtras
import EventExtras
import Html as RootHtml
import Html.Styled as Styled
import Html.Styled.Attributes as Attributes

View File

@ -312,7 +312,6 @@ linkSpa : (route -> String) -> (route -> msg) -> IconLinkSpaModel route -> Html
linkSpa toUrl toMsg config =
linkBase
[ EventExtras.onClickPreventDefaultForLinkWithHref (toMsg config.route)
|> Attributes.fromUnstyled
]
{ alt = config.alt
, url = toUrl config.route

View File

@ -279,7 +279,6 @@ linkSpa : (route -> String) -> (route -> msg) -> IconLinkSpaModel route -> Html
linkSpa toUrl toMsg config =
linkBase
[ EventExtras.onClickPreventDefaultForLinkWithHref (toMsg config.route)
|> Attributes.fromUnstyled
]
{ alt = config.alt
, url = toUrl config.route

View File

@ -16,7 +16,7 @@ import Accessibility.Styled.Aria as Aria
import Accessibility.Styled.Role as Role
import Accessibility.Styled.Widget as Widget
import Css exposing (..)
import EventExtras.Styled as EventExtras
import EventExtras
import Html.Styled
import Html.Styled.Attributes as Attr exposing (css, href)
import Html.Styled.Events as Events

View File

@ -268,7 +268,7 @@ viewTabLink config isSelected tabConfig =
SpaLink { label, href, msg } ->
( label
, Just href
, [ Attributes.fromUnstyled <| EventExtras.onClickPreventDefaultForLinkWithHref msg ]
, [ EventExtras.onClickPreventDefaultForLinkWithHref msg ]
)
currentPage =

View File

@ -61,7 +61,7 @@ import Accessibility.Styled.Role as Role
import Accessibility.Styled.Widget as Widget
import Css exposing (Color, Style)
import Css.Global as Global
import EventExtras.Styled as EventExtras
import EventExtras
import Html.Styled.Attributes as Attributes exposing (css)
import Html.Styled.Events as Events
import Json.Encode as Encode