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

Add visually hidden prefix and suffix text to links and buttons #465

Conversation

peteryates
Copy link
Member

@peteryates peteryates commented Nov 26, 2023

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:

<%= govuk_link_to("Delete", "#", visually_hidden_suffix: "first item")  %>

With a block:

<%= govuk_link_to("#") do %>
  Delete <%= govuk_visually_hidden("first item") %>
<% end %>

Both will output:

<a href="#" class="govuk-link">Delete<span class="govuk-visually-hidden"> first item</span></a>

Prefixes work in the same way:

<%= govuk_link_to("Mr Jones", "#", visually_hidden_prefix: "Remove")  %>
<a href="#" class="govuk-link"><span class="govuk-visually-hidden">Remove </span>Mr Jones</a>

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
@frankieroberto
Copy link
Collaborator

@peteryates I don't quite follow the changes in this PR so far. Are you suggesting adding extra arguments to govuk_link_to for visually-hidden text, like this:

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?

@peteryates
Copy link
Member Author

@frankieroberto sorry yeah, didn't quite get round to actually making the changes yet, just some minor refactoring to pave the way :)

govuk_link_to("Delete", "#", visually_hidden_suffix: "first item")

☝🏽 This first example is exactly what I had in mind, but I think there's definitely scope for a govuk_visually_hidden helper too.

@frankieroberto
Copy link
Collaborator

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… 🤔

@frankieroberto
Copy link
Collaborator

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 safe_join?

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
@peteryates peteryates force-pushed the add-visually-hidden-prefix-and-suffix-to-links-and-buttons branch from 3dc1f5e to ae8e273 Compare November 27, 2023 18:00
@peteryates
Copy link
Member Author

peteryates commented Nov 27, 2023

@frankieroberto yeah the reason i want to add these as keyword args is because remembering where to add the space gets a bit clunky. Currently it's wrong, I'll amend it so it's inside the visually hidden span next (it's currently outside). (fixed in 5722bb0)

govuk_link_to("Delete#{govuk_visually_hidden(' first item')}".html_safe, "#")

🫣

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
Comment on lines 127 to 129
prefix = (visually_hidden_prefix.present?) ? visually_hidden_prefix + " " : nil
text = (block_given?) ? block.call : original
suffix = (visually_hidden_suffix.present?) ? " " + visually_hidden_suffix : nil
Copy link
Member Author

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.

@peteryates peteryates marked this pull request as ready for review November 28, 2023 11:05
@peteryates peteryates merged commit f01217d into govuk-frontend-v5 Nov 28, 2023
7 checks passed
@peteryates peteryates deleted the add-visually-hidden-prefix-and-suffix-to-links-and-buttons branch November 28, 2023 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants