-
Notifications
You must be signed in to change notification settings - Fork 115
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 details-toggle
Catalyst element to improve accessibility of Details component
#3292
Open
ktravers
wants to merge
10
commits into
main
Choose a base branch
from
ktravers/upstream-details-element
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6cdcca3
add details-toggle catalyst element
ktravers 7998b80
add required data attrs to component
ktravers a29601c
set aria-label and aria-expanded correctly by default
ktravers 6cf7eeb
handle details open attribute
ktravers 66761f1
add changeset
ktravers a08831a
try updating info_arch, see if that helps CI pass
ktravers 605e6bf
Generating component snapshots
ktravers aaf4a04
add reasonable default aria labels to dropdown component (depends on …
ktravers fb34443
Generating component snapshots
ktravers ba55e1d
Merge branch 'main' into ktravers/upstream-details-element
ktravers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/view-components': minor | ||
--- | ||
|
||
Improve Details component accessibility by setting aria-label and aria-expanded attributes correctly on initial render and when toggled open/closed. |
2 changes: 1 addition & 1 deletion
2
...hots.test.ts-snapshots/primer/alpha/dropdown/default/aria-snapshot--after-interaction.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...t/screenshots/snapshots.test.ts-snapshots/primer/alpha/dropdown/default/aria-snapshot.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
- group: | ||
- button "Dropdown" | ||
- button "Open": Dropdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
<% if disabled? %> | ||
<%= summary %> | ||
<% else %> | ||
<%= render(Primer::BaseComponent.new(**@system_arguments)) do %> | ||
<details-toggle> | ||
<% if disabled? %> | ||
<%= summary %> | ||
<%= body %> | ||
<% else %> | ||
<%= render(Primer::BaseComponent.new(**@system_arguments)) do %> | ||
<%= summary %> | ||
<%= body %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
</details-toggle> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 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. | ||
* | ||
* @example | ||
* ```html | ||
* <details-toggle> | ||
* <details open=true data-target="details-toggle.detailsTarget"> | ||
* <summary | ||
* aria-expanded="true" | ||
* aria-label="Collapse me" | ||
* data-target="details-toggle.summaryTarget" | ||
* data-action="click:details-toggle#toggle" | ||
* data-aria-label-closed="Expand me" | ||
* data-aria-label-open="Collapse me" | ||
* > | ||
* Click me | ||
* </summary> | ||
* <div>Contents</div> | ||
* </details> | ||
* </details-toggle> | ||
* ``` | ||
*/ | ||
|
||
@controller | ||
class DetailsToggleElement extends HTMLElement { | ||
@target detailsTarget!: HTMLDetailsElement | ||
@target summaryTarget!: 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) | ||
this.summaryTarget.setAttribute('aria-expanded', 'false') | ||
} else { | ||
const ariaLabelOpen = this.summaryTarget.getAttribute('data-aria-label-open') || 'Collapse' | ||
this.summaryTarget.setAttribute('aria-label', ariaLabelOpen) | ||
this.summaryTarget.setAttribute('aria-expanded', 'true') | ||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
DetailsToggleElement: typeof DetailsToggleElement | ||
} | ||
} | ||
|
||
window.DetailsToggleElement = DetailsToggleElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think
assert_selector
with an implicit receiver searches the entire page. Calling it on the yielded element instead should have the nesting effect you're after: