From e9ae1f8a4396c749e452f44b16fd452a829f6bdb Mon Sep 17 00:00:00 2001 From: Tessa Kelly Date: Fri, 24 Apr 2020 12:51:18 -0700 Subject: [PATCH] Merve EventExtras.Styled and EventExtras --- src/ClickableAttributes.elm | 2 +- src/EventExtras.elm | 52 ++++++++++++++++-- src/EventExtras/Styled.elm | 85 ------------------------------ src/Nri/Ui/Button/V5.elm | 2 +- src/Nri/Ui/Button/V8.elm | 2 +- src/Nri/Ui/Icon/V3.elm | 1 - src/Nri/Ui/Icon/V5.elm | 1 - src/Nri/Ui/SegmentedControl/V8.elm | 2 +- src/Nri/Ui/Tabs/V4.elm | 2 +- src/Nri/Ui/Tooltip/V1.elm | 2 +- 10 files changed, 55 insertions(+), 96 deletions(-) delete mode 100644 src/EventExtras/Styled.elm diff --git a/src/ClickableAttributes.elm b/src/ClickableAttributes.elm index f5290e23..ac036f34 100644 --- a/src/ClickableAttributes.elm +++ b/src/ClickableAttributes.elm @@ -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 diff --git a/src/EventExtras.elm b/src/EventExtras.elm index 1f5dcf9e..4f020416 100644 --- a/src/EventExtras.elm +++ b/src/EventExtras.elm @@ -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 `` 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 ) + +-} +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 )) diff --git a/src/EventExtras/Styled.elm b/src/EventExtras/Styled.elm deleted file mode 100644 index 32c697c8..00000000 --- a/src/EventExtras/Styled.elm +++ /dev/null @@ -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 `` 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 ) - --} -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 `` 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 ) - --} -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 )) diff --git a/src/Nri/Ui/Button/V5.elm b/src/Nri/Ui/Button/V5.elm index c01b8f6b..5187d821 100644 --- a/src/Nri/Ui/Button/V5.elm +++ b/src/Nri/Ui/Button/V5.elm @@ -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 diff --git a/src/Nri/Ui/Button/V8.elm b/src/Nri/Ui/Button/V8.elm index 3da720f4..53b64ce0 100644 --- a/src/Nri/Ui/Button/V8.elm +++ b/src/Nri/Ui/Button/V8.elm @@ -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 diff --git a/src/Nri/Ui/Icon/V3.elm b/src/Nri/Ui/Icon/V3.elm index 9bc60814..4e8490ae 100644 --- a/src/Nri/Ui/Icon/V3.elm +++ b/src/Nri/Ui/Icon/V3.elm @@ -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 diff --git a/src/Nri/Ui/Icon/V5.elm b/src/Nri/Ui/Icon/V5.elm index a8a2338d..372f21c3 100644 --- a/src/Nri/Ui/Icon/V5.elm +++ b/src/Nri/Ui/Icon/V5.elm @@ -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 diff --git a/src/Nri/Ui/SegmentedControl/V8.elm b/src/Nri/Ui/SegmentedControl/V8.elm index 77a5a5eb..38a44363 100644 --- a/src/Nri/Ui/SegmentedControl/V8.elm +++ b/src/Nri/Ui/SegmentedControl/V8.elm @@ -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 diff --git a/src/Nri/Ui/Tabs/V4.elm b/src/Nri/Ui/Tabs/V4.elm index 0b99ce8b..6aef879a 100644 --- a/src/Nri/Ui/Tabs/V4.elm +++ b/src/Nri/Ui/Tabs/V4.elm @@ -268,7 +268,7 @@ viewTabLink config isSelected tabConfig = SpaLink { label, href, msg } -> ( label , Just href - , [ Attributes.fromUnstyled <| EventExtras.onClickPreventDefaultForLinkWithHref msg ] + , [ EventExtras.onClickPreventDefaultForLinkWithHref msg ] ) currentPage = diff --git a/src/Nri/Ui/Tooltip/V1.elm b/src/Nri/Ui/Tooltip/V1.elm index 1731058c..5514baa5 100644 --- a/src/Nri/Ui/Tooltip/V1.elm +++ b/src/Nri/Ui/Tooltip/V1.elm @@ -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