Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "disabled" links #926

Merged
merged 13 commits into from
May 18, 2022
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@
"avh4/elm-program-test": "3.3.0 <= v < 4.0.0",
"elm/html": "1.0.0 <= v < 2.0.0",
"elm-explorations/test": "1.2.2 <= v < 2.0.0",
"tesk9/accessible-html": "4.1.0 <= v < 5.0.0"
"tesk9/accessible-html": "5.0.0 <= v < 6.0.0"
}
}
55 changes: 44 additions & 11 deletions src/ClickableAttributes.elm
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
module ClickableAttributes exposing
( ClickableAttributes
, href
, init
, linkExternal
, linkExternalWithTracking
, linkSpa
, linkWithMethod
, linkWithTracking
( ClickableAttributes, init
, onClick
, toButtonAttributes
, href, linkWithMethod, linkWithTracking
, linkSpa
, linkExternal, linkExternalWithTracking
, toLinkAttributes
)

{-| -}
{-|

@docs ClickableAttributes, init


# For buttons

@docs onClick
@docs toButtonAttributes


# For links

@docs href, linkWithMethod, linkWithTracking
@docs linkSpa
@docs linkExternal, linkExternalWithTracking
@docs toLinkAttributes

-}

import Accessibility.Styled.Role as Role
import Accessibility.Styled.Widget as Widget
import EventExtras
import Html.Styled exposing (Attribute)
import Html.Styled.Attributes as Attributes
Expand Down Expand Up @@ -112,8 +128,25 @@ toButtonAttributes clickableAttributes =


{-| -}
toLinkAttributes : (route -> String) -> ClickableAttributes route msg -> ( String, List (Attribute msg) )
toLinkAttributes routeToString clickableAttributes =
toLinkAttributes : { routeToString : route -> String, isDisabled : Bool } -> ClickableAttributes route msg -> ( String, List (Attribute msg) )
toLinkAttributes { routeToString, isDisabled } clickableAttributes =
let
( linkTypeName, attributes ) =
toEnabledLinkAttributes routeToString clickableAttributes
in
( linkTypeName
, if isDisabled then
[ Role.link
, Widget.disabled True
]

else
attributes
)


toEnabledLinkAttributes : (route -> String) -> ClickableAttributes route msg -> ( String, List (Attribute msg) )
toEnabledLinkAttributes routeToString clickableAttributes =
let
stringUrl =
case ( clickableAttributes.urlString, clickableAttributes.url ) of
Expand Down
66 changes: 43 additions & 23 deletions src/Nri/Ui/Button/V10.elm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ adding a span around the text could potentially lead to regressions.
- adds `modal` helper, an alias for `large` size
- adds `notMobileCss`, `mobileCss`, `quizEngineMobileCss`
- adds `hideIconForMobile` and `hideIconFor`
- support 'disabled' links according to [Scott O'Hara's disabled links](https://www.scottohara.me/blog/2021/05/28/disabled-links.html) article


# Changes from V9:
Expand Down Expand Up @@ -463,6 +464,28 @@ type ButtonState
| Success


isDisabled : ButtonState -> Bool
isDisabled state =
case state of
Enabled ->
False

Disabled ->
True

Error ->
True

Unfulfilled ->
False

Loading ->
True

Success ->
True


{-| -}
enabled : Attribute msg
enabled =
Expand All @@ -476,7 +499,19 @@ unfulfilled =
set (\attributes -> { attributes | state = Unfulfilled })


{-| Shows inactive styling. If a button, this attribute will disable it.
{-| Shows inactive styling.

If a button, this attribute will disable it as you'd expect.

If a link, this attribute will follow the pattern laid out in [Scott O'Hara's disabled links](https://www.scottohara.me/blog/2021/05/28/disabled-links.html) article,
and essentially make the anchor a disabled placeholder.

_Caveat!_

The styleguide example will NOT work correctly because of <https://github.com/elm/browser/issues/34>, which describes a problem where "a tags without href generate a navigation event".

In most cases, if you're not using Browser.application, disabled links should work just fine.

-}
disabled : Attribute msg
disabled =
Expand Down Expand Up @@ -557,32 +592,12 @@ renderButton ((ButtonOrLink config) as button_) =
let
buttonStyle_ =
getColorPalette button_

isDisabled =
case config.state of
Enabled ->
False

Disabled ->
True

Error ->
True

Unfulfilled ->
False

Loading ->
True

Success ->
True
in
Nri.Ui.styled Html.button
(styledName "customButton")
[ buttonStyles config.size config.width buttonStyle_ config.customStyles ]
(ClickableAttributes.toButtonAttributes config.clickableAttributes
++ Attributes.disabled isDisabled
++ Attributes.disabled (isDisabled config.state)
:: Attributes.type_ "button"
:: config.customAttributes
)
Expand All @@ -596,7 +611,11 @@ renderLink ((ButtonOrLink config) as link_) =
getColorPalette link_

( linkFunctionName, attributes ) =
ClickableAttributes.toLinkAttributes identity config.clickableAttributes
ClickableAttributes.toLinkAttributes
{ routeToString = identity
, isDisabled = isDisabled config.state
}
config.clickableAttributes
in
Nri.Ui.styled Styled.a
(styledName linkFunctionName)
Expand Down Expand Up @@ -775,6 +794,7 @@ buttonStyle =
, Css.margin Css.zero
, Css.hover [ Css.textDecoration Css.none ]
, Css.disabled [ Css.cursor Css.notAllowed ]
, Css.Global.withAttribute "aria-disabled=true" [ Css.cursor Css.notAllowed ]
, Css.display Css.inlineFlex
, Css.alignItems Css.center
, Css.justifyContent Css.center
Expand Down
6 changes: 5 additions & 1 deletion src/Nri/Ui/ClickableSvg/V2.elm
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ renderLink : ButtonOrLink msg -> Html msg
renderLink ((ButtonOrLink config) as link_) =
let
( linkFunctionName, extraAttrs ) =
ClickableAttributes.toLinkAttributes identity config.clickableAttributes
ClickableAttributes.toLinkAttributes
{ routeToString = identity
, isDisabled = config.disabled
}
config.clickableAttributes

theme =
if config.disabled then
Expand Down
6 changes: 5 additions & 1 deletion src/Nri/Ui/ClickableText/V3.elm
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ link label_ attributes =
|> List.foldl (\(Attribute attribute) l -> attribute l) defaults

( name, clickableAttributes ) =
ClickableAttributes.toLinkAttributes identity config.clickableAttributes
ClickableAttributes.toLinkAttributes
{ routeToString = identity
, isDisabled = False
}
config.clickableAttributes
in
Nri.Ui.styled Html.a
(dataDescriptor name)
Expand Down
5 changes: 4 additions & 1 deletion src/Nri/Ui/SideNav/V1.elm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ viewSidebarLeaf :
viewSidebarLeaf config extraStyles entryConfig =
let
( linkFunctionName, attributes ) =
ClickableAttributes.toLinkAttributes config.routeToString
ClickableAttributes.toLinkAttributes
{ routeToString = config.routeToString
, isDisabled = False
}
entryConfig.clickableAttributes
in
Nri.Ui.styled Html.Styled.a
Expand Down
5 changes: 4 additions & 1 deletion src/Nri/Ui/SideNav/V2.elm
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ viewSidebarLeaf :
viewSidebarLeaf config extraStyles entryConfig =
let
( linkFunctionName, attributes ) =
ClickableAttributes.toLinkAttributes config.routeToString
ClickableAttributes.toLinkAttributes
{ routeToString = config.routeToString
, isDisabled = False
}
entryConfig.clickableAttributes
in
Nri.Ui.styled Html.Styled.a
Expand Down
5 changes: 4 additions & 1 deletion src/Nri/Ui/SideNav/V3.elm
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ viewSidebarLeaf :
viewSidebarLeaf config extraStyles entryConfig =
let
( linkFunctionName, attributes ) =
ClickableAttributes.toLinkAttributes config.routeToString
ClickableAttributes.toLinkAttributes
{ routeToString = config.routeToString
, isDisabled = False
}
entryConfig.clickableAttributes
in
Nri.Ui.styled Html.Styled.a
Expand Down
13 changes: 3 additions & 10 deletions styleguide-app/Examples/Button.elm
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ initDebugControls =
, ( "fillContainerWidth", Button.fillContainerWidth )
]
)
|> ControlExtra.optionalBoolListItem "disabled" ( "disabled", Button.disabled )
|> ControlExtra.optionalListItem "state (button only)"
(CommonControls.choice moduleName
[ ( "enabled", Button.enabled )
, ( "disabled", Button.disabled )
, ( "error", Button.error )
[ ( "error", Button.error )
, ( "unfulfilled", Button.unfulfilled )
, ( "loading", Button.loading )
, ( "success", Button.success )
Expand Down Expand Up @@ -276,13 +275,7 @@ buttons model =

exampleCell setStyle setSize =
buttonOrLink model.label
([ setSize
, setStyle
, Button.custom [ Html.Styled.Attributes.class "styleguide-button" ]
, Button.onClick (ShowItWorked "ButtonExample" "Button clicked!")
]
++ List.map Tuple.second model.attributes
)
(setSize :: setStyle :: List.map Tuple.second model.attributes)
|> List.singleton
|> td
[ css
Expand Down
Loading