-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add visually hidden prefix and suffix text to links and buttons #465
Add visually hidden prefix and suffix text to links and buttons #465
Conversation
Here we're making #extract_button_link_args its own method and then composing all the attributes needed for links and buttons by simply constructing a hash from all the separate bits. Links and buttons should be treated totally separately, but #govuk_button_link_to are a combination of the two; so they need the link attributes with the button classes
These are easier to read if we do the link/button text extraction up front and then consistently call link_to, button_to or mail_to
@peteryates I don't quite follow the changes in this PR so far. Are you suggesting adding extra arguments to govuk_link_to("Delete", "#", visually_hidden_suffix: "first item") or would you do this by passing a block to the helpers, possibly with another helper for visually-hidden text? <%= govuk_link_to("#") do %>
Delete <%= govuk_visually_hidden("first item") %>
<% end %> I can see arguments for both - the second one feels like a cleaner implementation, but the first one feels easier to use maybe? |
@frankieroberto sorry yeah, didn't quite get round to actually making the changes yet, just some minor refactoring to pave the way :)
☝🏽 This first example is exactly what I had in mind, but I think there's definitely scope for a |
I think the only downside with the first example is that because of the param ordering, the visually hidden bit is separated from the rest of the text, which might make it harder to read as a single phrase? The second example with the separate helper is more flexible (you could use it with any other helper including the default Rails ones) but requires either the block syntax or nesting the helper within the first param. This was the approach we took with the tag helper in the task list helper though… 🤔 |
Here's how it might look using a nested helper... 😬 Unfortunatately due to needing to concatenate a string with the visually-hidden tag, I think you'd also need to use govuk_link_to(safe_join(["Delete, govuk_visually_hidden(' first item')]), "#") or you could concatenate anyway and mark the output as safe (so long as the string isn't user-submitted), but this feels even worse: govuk_link_to("Delete#{govuk_visually_hidden(' first item')}".html_safe, "#") ...in either case, probably the block syntax is better? |
Add a helper that makes adding visually hidden text easier. It supports the focusable keyword argument which toggles between govuk-visually-hidden and govuk-visually-hidden-focusable, as outlined in the guide: https://design-system.service.gov.uk/styles/layout/#hide-elements-and-keep-them-accessible-to-screen-readers
Often it's useful to provide a little extra context for screen reader users by adding visually hidden text to links and buttons. This is already possible using blocks but it's simpler to add them with keyword arguments
3dc1f5e
to
ae8e273
Compare
@frankieroberto yeah the reason i want to add these as keyword args is because remembering where to add the space gets a bit clunky.
🫣 |
This change prevents weird spacing issues in links and buttons when visually hidden text is inserted (but hidden) and the space between it and the regular text is visible
app/helpers/govuk_link_helper.rb
Outdated
prefix = (visually_hidden_prefix.present?) ? visually_hidden_prefix + " " : nil | ||
text = (block_given?) ? block.call : original | ||
suffix = (visually_hidden_suffix.present?) ? " " + visually_hidden_suffix : nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be made a bit more elegant.
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
#extract_button_link_args
govuk_visually_hidden
helper which makes creating visually hidden text easyvisually_hidden_prefix
andvisually_hidden_suffix
which injects the visually hidden text before or after the link or button textExample use
With keyword arguments:
With a block:
Both will output:
Prefixes work in the same way: