Skip to content

Commit b368ffc

Browse files
authored
set aria-label and aria-expanded correctly by default
1 parent 3c61e11 commit b368ffc

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

app/components/primer/beta/details.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,38 @@ class Details < Primer::Component
1414
:default => "details-overlay",
1515
:dark => "details-overlay details-overlay-dark"
1616
}.freeze
17+
ARIA_LABEL_OPEN_DEFAULT = "Collapse"
18+
ARIA_LABEL_CLOSED_DEFAULT = "Expand"
1719

1820
attr_reader :disabled
1921
alias disabled? disabled
2022

23+
# @param button [Boolean] Whether or not to render the summary element as a button.
24+
# @param aria_label_open [String] Defaults to "Collapse". Value to announce when details element is open.
25+
# @param aria_label_closed [String] Defaults to "Expand". Value to announce when details element is closed.
2126
renders_one :summary, lambda { |button: true, **system_arguments|
2227
system_arguments[:tag] = :summary
2328
system_arguments[:role] = "button"
2429

30+
aria_label_closed = system_arguments[:aria_label_closed] || ARIA_LABEL_CLOSED_DEFAULT
31+
aria_label_open = system_arguments[:aria_label_open] || ARIA_LABEL_OPEN_DEFAULT
32+
2533
system_arguments[:data] = merge_data(
2634
system_arguments, {
2735
data: {
2836
target: "detailsToggle.summaryElement",
2937
action: "click:detailsToggle#toggle",
38+
aria_label_closed: aria_label_closed,
39+
aria_label_open: aria_label_open,
40+
}
41+
}
42+
)
43+
44+
system_arguments[:aria] = merge_aria(
45+
system_arguments, {
46+
aria: {
47+
label: aria_label_open,
48+
expanded: true,
3049
}
3150
}
3251
)

app/components/primer/beta/details_toggle_element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {controller, target} from '@github/catalyst'
33
/**
44
* A companion Catalyst element for the Details view component. This element
55
* ensures the <details> and <summary> elements markup is properly accessible by
6-
* updating the aria-label and aria-expanded attributes.
6+
* updating the aria-label and aria-expanded attributes on click.
77
*
88
* aria-label values default to "Expand" and "Collapse". To override those
99
* values, use the `data-aria-label-open` and `data-aria-label-closed`

test/components/beta/details_test.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_falls_back_to_default_body_tag_when_invalid_body_tag_is_passed
9595
refute_selector("img", text: "Body", visible: false)
9696
end
9797

98-
def test_passes_props_to_button
98+
def test_passes_props_to_summary_button
9999
render_inline(Primer::Beta::Details.new) do |component|
100100
component.with_summary(size: :small, scheme: :primary) do
101101
"Summary"
@@ -108,6 +108,22 @@ def test_passes_props_to_button
108108
assert_selector("summary.btn.btn-sm.btn-primary")
109109
end
110110

111+
def test_accepts_custom_values_for_summary_aria_label
112+
render_inline(Primer::Beta::Details.new) do |component|
113+
component.with_summary(aria_label_closed: "Open me", aria_label_open: "Close me") do
114+
"Summary"
115+
end
116+
component.with_body do
117+
"Body"
118+
end
119+
end
120+
121+
assert_selector("summary")
122+
assert_selector("summary[aria-label='Close me']")
123+
assert_selector("summary[data-aria-label-open='Close me']")
124+
assert_selector("summary[data-aria-label-closed='Open me']")
125+
end
126+
111127
def test_prevents_rendering_without_slots
112128
render_inline(Primer::Beta::Details.new)
113129

@@ -155,7 +171,11 @@ def test_renders_details_catalyst_element_and_data_attributes
155171

156172
assert_selector("details-toggle")
157173
assert_selector("details[data-target='detailsToggle.detailsElement']")
158-
assert_selector("summary[data-target='detailsToggle.summaryElement']")
174+
assert_selector("summary[aria-label='Collapse']")
175+
assert_selector("summary[aria-expanded=true]")
159176
assert_selector("summary[data-action='click:detailsToggle#toggle']")
177+
assert_selector("summary[data-target='detailsToggle.summaryElement']")
178+
assert_selector("summary[data-aria-label-closed='Expand']")
179+
assert_selector("summary[data-aria-label-open='Collapse']")
160180
end
161181
end

0 commit comments

Comments
 (0)