Skip to content

Commit

Permalink
Add aria controls and describedby
Browse files Browse the repository at this point in the history
This commit adds the aria-controls and aria-describedby
attribute options to the button presenter. This will improve the
usability of the button component for users of screenreaders.
It also adds associated tests.
  • Loading branch information
Jonathan Young committed Nov 29, 2022
1 parent 20f97fc commit 73a6c0a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Update to LUX 304 ([PR #3070](https://github.com/alphagov/govuk_publishing_components/pull/3070))
* Set attributes for single page notification button based on Account API response ([PR #3071](https://github.com/alphagov/govuk_publishing_components/pull/3071))
* Support custom text for the single page notification button component ([PR #2935](https://github.com/alphagov/govuk_publishing_components/pull/2935))
* Add `aria-controls` and `aria-describedby` attribute options to the button component


## 32.1.0

Expand Down
10 changes: 10 additions & 0 deletions app/views/govuk_publishing_components/components/docs/button.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ examples:
data:
text: Button
aria_label: Button with custom label
with_aria_controls:
description: Used to identify what element(s) the button controls, this can be useful for users with visual impairment. In the example shown, the button controls the page wrapper with ID `wrapper` but it could be another identifying attribute.
data:
text: Button
aria_controls: wrapper
with_aria_describedby:
description: Can be used to give context to a button, this will be read after aria-label by a screenreader
data:
text: Button
aria_describedby: with_aria_describedby
8 changes: 7 additions & 1 deletion lib/govuk_publishing_components/presenters/button_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class ButtonHelper
:name,
:value,
:classes,
:aria_label
:aria_label,
:aria_controls,
:aria_describedby

def initialize(local_assigns)
@href = local_assigns[:href]
Expand Down Expand Up @@ -52,6 +54,8 @@ def initialize(local_assigns)
@aria_label = local_assigns[:aria_label]
@info_text_id = "info-text-id-#{SecureRandom.hex(4)}"
@button_id = "button-id-#{SecureRandom.hex(4)}"
@aria_controls = local_assigns[:aria_controls]
@aria_describedby = local_assigns[:aria_describedby]
end

def link?
Expand Down Expand Up @@ -90,6 +94,8 @@ def html_options
options[:name] = name if name.present? && value.present?
options[:value] = value if name.present? && value.present?
options[:aria] = { label: aria_label } if aria_label
options[:aria][:controls] = aria_controls if aria_controls
options[:aria][:describedby] = aria_describedby if aria_describedby
options[:draggable] = false if link?
options
end
Expand Down
12 changes: 12 additions & 0 deletions spec/components/button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,16 @@ def component_name

assert_select '.gem-c-button[aria-label="Button with custom label"]'
end

it "renders with aria-controls" do
render_component(text: "Button", aria_controls: "Testing aria-controls")

assert_select '.gem-c-button[aria-controls="Testing aria-controls"]'
end

it "renders with aria-describedby" do
render_component(text: "Button", aria_describedby: "Testing aria-describedby")

assert_select '.gem-c-button[aria-describedby="Testing aria-describedby"]'
end
end

0 comments on commit 73a6c0a

Please sign in to comment.