Skip to content

Commit

Permalink
set aria-label and aria-expanded correctly by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ktravers authored Jan 27, 2025
1 parent 3c61e11 commit b368ffc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
19 changes: 19 additions & 0 deletions app/components/primer/beta/details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@ class Details < Primer::Component
:default => "details-overlay",
:dark => "details-overlay details-overlay-dark"
}.freeze
ARIA_LABEL_OPEN_DEFAULT = "Collapse"
ARIA_LABEL_CLOSED_DEFAULT = "Expand"

attr_reader :disabled
alias disabled? disabled

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

aria_label_closed = system_arguments[:aria_label_closed] || ARIA_LABEL_CLOSED_DEFAULT
aria_label_open = system_arguments[:aria_label_open] || ARIA_LABEL_OPEN_DEFAULT

system_arguments[:data] = merge_data(
system_arguments, {
data: {
target: "detailsToggle.summaryElement",
action: "click:detailsToggle#toggle",
aria_label_closed: aria_label_closed,
aria_label_open: aria_label_open,
}
}
)

system_arguments[:aria] = merge_aria(
system_arguments, {
aria: {
label: aria_label_open,
expanded: true,
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion app/components/primer/beta/details_toggle_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {controller, target} from '@github/catalyst'
/**
* A companion Catalyst element for the Details view component. This element
* ensures the <details> and <summary> elements markup is properly accessible by
* updating the aria-label and aria-expanded attributes.
* updating the aria-label and aria-expanded attributes on click.
*
* aria-label values default to "Expand" and "Collapse". To override those
* values, use the `data-aria-label-open` and `data-aria-label-closed`
Expand Down
24 changes: 22 additions & 2 deletions test/components/beta/details_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_falls_back_to_default_body_tag_when_invalid_body_tag_is_passed
refute_selector("img", text: "Body", visible: false)
end

def test_passes_props_to_button
def test_passes_props_to_summary_button
render_inline(Primer::Beta::Details.new) do |component|
component.with_summary(size: :small, scheme: :primary) do
"Summary"
Expand All @@ -108,6 +108,22 @@ def test_passes_props_to_button
assert_selector("summary.btn.btn-sm.btn-primary")
end

def test_accepts_custom_values_for_summary_aria_label
render_inline(Primer::Beta::Details.new) do |component|
component.with_summary(aria_label_closed: "Open me", aria_label_open: "Close me") do
"Summary"
end
component.with_body do
"Body"
end
end

assert_selector("summary")
assert_selector("summary[aria-label='Close me']")
assert_selector("summary[data-aria-label-open='Close me']")
assert_selector("summary[data-aria-label-closed='Open me']")
end

def test_prevents_rendering_without_slots
render_inline(Primer::Beta::Details.new)

Expand Down Expand Up @@ -155,7 +171,11 @@ def test_renders_details_catalyst_element_and_data_attributes

assert_selector("details-toggle")
assert_selector("details[data-target='detailsToggle.detailsElement']")
assert_selector("summary[data-target='detailsToggle.summaryElement']")
assert_selector("summary[aria-label='Collapse']")
assert_selector("summary[aria-expanded=true]")
assert_selector("summary[data-action='click:detailsToggle#toggle']")
assert_selector("summary[data-target='detailsToggle.summaryElement']")
assert_selector("summary[data-aria-label-closed='Expand']")
assert_selector("summary[data-aria-label-open='Collapse']")
end
end

0 comments on commit b368ffc

Please sign in to comment.