-
Notifications
You must be signed in to change notification settings - Fork 127
Remove unnecessary aria-label defaults from Details component #3534
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
Changes from 4 commits
324f3f3
b7ed975
2bfc123
b4dc8ee
e0e0e33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/view-components": patch | ||
--- | ||
|
||
Remove unnecessary aria-label from the Details component |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,9 +5,10 @@ import {controller, target} from '@github/catalyst' | |||||||||
* ensures the <details> and <summary> elements markup is properly accessible by | ||||||||||
* 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` | ||||||||||
* attributes on the summary target. | ||||||||||
* aria-label values are only set if provided via the `data-aria-label-open` and | ||||||||||
* `data-aria-label-closed` attributes on the summary target. If these attributes | ||||||||||
* are not present, no aria-label will be set, allowing screen readers to use | ||||||||||
* the visible text content. | ||||||||||
* | ||||||||||
* @example | ||||||||||
* ```html | ||||||||||
|
@@ -37,12 +38,16 @@ class DetailsToggleElement extends HTMLElement { | |||||||||
toggle() { | ||||||||||
const detailsIsOpen = this.detailsTarget.hasAttribute('open') | ||||||||||
if (detailsIsOpen) { | ||||||||||
const ariaLabelClosed = this.summaryTarget.getAttribute('data-aria-label-closed') || 'Expand' | ||||||||||
this.summaryTarget.setAttribute('aria-label', ariaLabelClosed) | ||||||||||
const ariaLabelClosed = this.summaryTarget.getAttribute('data-aria-label-closed') | ||||||||||
if (ariaLabelClosed) { | ||||||||||
this.summaryTarget.setAttribute('aria-label', ariaLabelClosed) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that when no
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't seem like this would hurt but not sure if it can get in this state 🤔 |
||||||||||
} | ||||||||||
this.summaryTarget.setAttribute('aria-expanded', 'false') | ||||||||||
} else { | ||||||||||
const ariaLabelOpen = this.summaryTarget.getAttribute('data-aria-label-open') || 'Collapse' | ||||||||||
this.summaryTarget.setAttribute('aria-label', ariaLabelOpen) | ||||||||||
const ariaLabelOpen = this.summaryTarget.getAttribute('data-aria-label-open') | ||||||||||
if (ariaLabelOpen) { | ||||||||||
this.summaryTarget.setAttribute('aria-label', ariaLabelOpen) | ||||||||||
} | ||||||||||
this.summaryTarget.setAttribute('aria-expanded', 'true') | ||||||||||
} | ||||||||||
} | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,22 @@ def test_accepts_custom_values_for_summary_aria_label | |
assert_selector("summary[data-aria-label-closed='Open me']") | ||
end | ||
|
||
def test_accepts_partial_aria_label_values | ||
render_inline(Primer::Beta::Details.new) do |component| | ||
component.with_summary(aria_label_closed: "Open me") do | ||
"Summary" | ||
end | ||
component.with_body do | ||
"Body" | ||
end | ||
end | ||
|
||
assert_selector("summary") | ||
assert_selector("summary[aria-label='Open me']") | ||
assert_selector("summary[data-aria-label-closed='Open me']") | ||
assert_selector("summary[data-aria-label-open='Collapse']") # Uses default when only one is provided | ||
end | ||
|
||
def test_prevents_rendering_without_slots | ||
render_inline(Primer::Beta::Details.new) | ||
|
||
|
@@ -159,6 +175,44 @@ def test_status | |
assert_component_state(Primer::Beta::Details, :beta) | ||
end | ||
|
||
def test_renders_no_aria_labels_when_not_provided | ||
render_inline(Primer::Beta::Details.new) do |component| | ||
component.with_summary do | ||
"Summary" | ||
end | ||
component.with_body do | ||
"Body" | ||
end | ||
end | ||
|
||
# Should not have aria-label attribute | ||
refute_selector("summary[aria-label]") | ||
# Should not have data-aria-label attributes | ||
refute_selector("summary[data-aria-label-closed]") | ||
refute_selector("summary[data-aria-label-open]") | ||
# Should still have aria-expanded | ||
assert_selector("summary[aria-expanded=false]") | ||
Comment on lines
+188
to
+194
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
def test_renders_no_aria_labels_when_not_provided_and_open | ||
render_inline(Primer::Beta::Details.new(open: true)) do |component| | ||
component.with_summary do | ||
"Summary" | ||
end | ||
component.with_body do | ||
"Body" | ||
end | ||
end | ||
|
||
# Should not have aria-label attribute | ||
refute_selector("summary[aria-label]") | ||
# Should not have data-aria-label attributes | ||
refute_selector("summary[data-aria-label-closed]") | ||
refute_selector("summary[data-aria-label-open]") | ||
# Should still have aria-expanded | ||
assert_selector("summary[aria-expanded=true]") | ||
end | ||
|
||
def test_renders_details_catalyst_element_and_data_attributes | ||
render_inline(Primer::Beta::Details.new) do |component| | ||
component.with_summary do | ||
|
@@ -171,11 +225,11 @@ def test_renders_details_catalyst_element_and_data_attributes | |
|
||
assert_selector("details-toggle") | ||
assert_selector("details[data-target='details-toggle.detailsTarget']") | ||
assert_selector("summary[aria-label='Expand']") | ||
refute_selector("summary[aria-label]") # No aria-label when not explicitly provided | ||
assert_selector("summary[aria-expanded=false]") | ||
assert_selector("summary[data-action='click:details-toggle#toggle']") | ||
assert_selector("summary[data-aria-label-closed='Expand']") | ||
assert_selector("summary[data-aria-label-open='Collapse']") | ||
refute_selector("summary[data-aria-label-closed]") # No data attributes when not explicitly provided | ||
refute_selector("summary[data-aria-label-open]") # No data attributes when not explicitly provided | ||
assert_selector("summary[data-target='details-toggle.summaryTarget']") | ||
end | ||
|
||
|
@@ -190,7 +244,7 @@ def test_renders_correct_aria_attributes_when_details_open_true | |
end | ||
|
||
assert_selector("details[open]") | ||
assert_selector("summary[aria-label='Collapse']") | ||
refute_selector("summary[aria-label]") # No aria-label when not explicitly provided | ||
assert_selector("summary[aria-expanded=true]") | ||
end | ||
end |
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.
ahh ok I was little confused at first but the test clarified -- so if you provide only one label but not the other, the state without a label will use a default label 👍