-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visually hidden prefix and suffix text to links and buttons (#465)
It would be nice to be able to simply add visually hidden text to links and buttons without having to manually construct the HTML. It comes in really handy when working in dense admin interfaces where there might just be enough space for 'View', but we want to give screenreader users and idea of _where_ the link will take them. ## Work so far - Split out `#extract_button_link_args` - Rework and simplify the link and button helpers - Add `govuk_visually_hidden` helper which makes creating visually hidden text easy - Add keyword arguments `visually_hidden_prefix` and `visually_hidden_suffix` which injects the visually hidden text before or after the link or button text ## Example use With keyword arguments: ```erb <%= govuk_link_to("Delete", "#", visually_hidden_suffix: "first item") %> ``` With a block: ```erb <%= govuk_link_to("#") do %> Delete <%= govuk_visually_hidden("first item") %> <% end %> ``` Both will output: ```html <a href="#" class="govuk-link">Delete<span class="govuk-visually-hidden"> first item</span></a> ``` Prefixes work in the same way: ```erb <%= govuk_link_to("Mr Jones", "#", visually_hidden_prefix: "Remove") %> ``` ```html <a href="#" class="govuk-link"><span class="govuk-visually-hidden">Remove </span>Mr Jones</a> ```
- Loading branch information
Showing
10 changed files
with
281 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module GovukVisuallyHiddenHelper | ||
def govuk_visually_hidden(text = nil, focusable: false, &block) | ||
content = (block_given?) ? block.call : text | ||
|
||
return if content.blank? | ||
|
||
visually_hidden_class = focusable ? "govuk-visually-hidden-focusable" : "govuk-visually-hidden" | ||
|
||
tag.span(content, class: visually_hidden_class) | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:action_view) { include GovukSkipLinkHelper } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Visually hidden text | ||
--- | ||
|
||
markdown: | ||
Most of the time content should be both visible on the screen and availble to | ||
screen readers. However, somtimes when space is at a premium, we may want | ||
to hide text that would clutter the screen. This puts users of assistive | ||
technology like screen readers at a disadvantage. | ||
|
||
We can hide content but make it available to users of assistive technology | ||
using the `govuk-visually-hidden` class. This library provides a helper method | ||
which makes hiding content easier. | ||
|
||
== render('/partials/example.*', | ||
caption: "Setting visually hidden text with an argument", | ||
code: visually_hidden_text_via_argument) | ||
|
||
== render('/partials/example.*', | ||
caption: "Setting visually hidden text with a block", | ||
code: visually_hidden_text_via_block) | ||
|
||
== render('/partials/example.*', | ||
caption: "Focusable visually hidden text", | ||
code: focusable_visually_hidden_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Examples | ||
module VisuallyHiddenHelpers | ||
def visually_hidden_text_via_argument | ||
<<~SNIPPET | ||
p Regular text | ||
= govuk_visually_hidden("This content is visually hidden") | ||
p More regular text | ||
SNIPPET | ||
end | ||
|
||
def visually_hidden_text_via_block | ||
<<~SNIPPET | ||
p Regular text | ||
= govuk_visually_hidden do | ||
p This paragraph is visually hidden | ||
p More regular text | ||
SNIPPET | ||
end | ||
|
||
def focusable_visually_hidden_text | ||
<<~SNIPPET | ||
p Regular text | ||
p | ||
a href="#" | ||
| Some link | ||
= govuk_visually_hidden("Focus on me", focusable: true) | ||
p More regular text | ||
SNIPPET | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.