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

Accessibility: Add focus styles #3

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion content/blog/hello.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ Welcome to my blog! It was built with `elm-pages`!
plus : number -> number -> number
plus m n =
m + n
```
```

[This is a link back home](/)
5 changes: 4 additions & 1 deletion src/Index.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Metadata exposing (Metadata)
import Pages
import Pages.PagePath as PagePath exposing (PagePath)
import Pages.Platform exposing (Page)
import Palette


view :
Expand Down Expand Up @@ -50,7 +51,9 @@ postSummary ( postPath, post ) =

linkToPost : PagePath Pages.PathKey -> Element msg -> Element msg
linkToPost postPath content =
Element.link [ Element.width Element.fill ]
Palette.link
[ Element.width Element.fill
]
{ url = PagePath.toString postPath, label = content }


Expand Down
20 changes: 15 additions & 5 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ view model siteMetadata page =
{ title = title
, body =
body
|> Element.layout
|> Element.layoutWith
{ options =
-- We set focus styles for interactable elements. See Palette.elm for more detais.
-- Note that currently these do not apply to links by default.
[ Element.focusStyle
{ borderColor = Nothing
, backgroundColor = Nothing
, shadow = Just Palette.focusStyleShadow
}
]
}
[ Element.width Element.fill
, Font.size 20
, Font.family [ Font.typeface "Roboto" ]
Expand Down Expand Up @@ -239,7 +249,7 @@ header currentPath =
, Element.Border.widthEach { bottom = 1, left = 0, right = 0, top = 0 }
, Element.Border.color (Element.rgba255 40 80 40 0.4)
]
[ Element.link []
[ Palette.link []
{ url = "/"
, label =
Element.row [ Font.size 30, Element.spacing 16 ]
Expand All @@ -266,7 +276,7 @@ highlightableLink currentPath linkDirectory displayName =
isHighlighted =
currentPath |> Directory.includes linkDirectory
in
Element.link
Palette.link
(if isHighlighted then
[ Font.underline
, Font.color Palette.color.primary
Expand Down Expand Up @@ -397,7 +407,7 @@ publishedDateView metadata =

githubRepoLink : Element msg
githubRepoLink =
Element.newTabLink []
Palette.newTabLink []
{ url = "https://github.com/dillonkearns/elm-pages"
, label =
Element.image
Expand All @@ -410,7 +420,7 @@ githubRepoLink =

elmDocsLink : Element msg
elmDocsLink =
Element.newTabLink []
Palette.newTabLink []
{ url = "https://package.elm-lang.org/packages/dillonkearns/elm-pages/latest/"
, label =
Element.image
Expand Down
69 changes: 68 additions & 1 deletion src/Palette.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Palette exposing (blogHeading, color, heading)
module Palette exposing (blogHeading, color, focusStyleShadow, heading, link, newTabLink)

import Color exposing (Color)
import Element exposing (Element)
import Element.Border
import Element.Font as Font
import Element.Region

Expand All @@ -11,6 +13,71 @@ color =
}


{-| A box-shadow record, compatible both with `Element.layoutWith` Option and `Element.focusStyle`.

For buttons, links and other interactables, we set focus styles
We use a box-shadow instead of an outline, because it follows borders.

Blue works well in this particular case.
You might want to customise this for your application.

Bear in mind that focus outlines should not be removed altogether,
because they are used by people who rely on keyboard and/or screen reader navigation.
They should also have a good color contrast with the background.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me really wants to throw in a link or two about focus styles here. I've had good results with people respecting them after they read a bit on them. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, absolutely! As a rule of thumb, I'm a big fan of self-describing code rather than comments to explain what code is doing, and, when needed, comments to explain why something unexpected is being done in code. This seems like a good start 👍

-}
focusStyleShadow =
{ color = color.primary
, offset = ( 0, 0 )
, blur = 0
, size = 2
}


{-| A composable `Element.link` that bakes in focus styles.

We need this for consistent / good-by-default styles and accessibility.
This might change in the future, if elm-ui supports setting focus globally via layoutWith.

See the notes in focusStyleShadow for more on accessibility and styling.

-}
link :
List (Element.Attribute msg)
->
{ url : String
, label : Element msg
}
-> Element msg
link attrs content =
Element.link
(Element.focused
[ Element.Border.shadow focusStyleShadow
]
:: attrs
)
content


{-| A composable `Element.newTabLink` that bakes in focus styles.
-}
newTabLink :
List (Element.Attribute msg)
->
{ url : String
, label : Element msg
}
-> Element msg
newTabLink attrs content =
Element.newTabLink
(Element.focused
[ Element.Border.shadow focusStyleShadow
]
:: attrs
)
content


heading : Int -> List (Element msg) -> Element msg
heading level content =
Element.paragraph
Expand Down