mirror of
https://github.com/NoRedInk/noredink-ui.git
synced 2024-12-28 08:04:06 +03:00
Merve EventExtras.Styled and EventExtras
This commit is contained in:
parent
7068f4463c
commit
e9ae1f8a43
@ -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
|
||||
|
@ -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 ))
|
||||
|
@ -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 ))
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -268,7 +268,7 @@ viewTabLink config isSelected tabConfig =
|
||||
SpaLink { label, href, msg } ->
|
||||
( label
|
||||
, Just href
|
||||
, [ Attributes.fromUnstyled <| EventExtras.onClickPreventDefaultForLinkWithHref msg ]
|
||||
, [ EventExtras.onClickPreventDefaultForLinkWithHref msg ]
|
||||
)
|
||||
|
||||
currentPage =
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user