Skip to content
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

Make dialog region focusable via keyboard #2428

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-forks-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Fix an accessibility issue where the Dialog body could not be reached via keyboard navigation
4 changes: 3 additions & 1 deletion app/components/primer/alpha/dialog.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<% if content.present? %>
<%= content %>
<% else %>
<%= body %>
<scrollable-region data-labelled-by="<%= labelledby %>">
<%= body %>
</scrollable-region>
<%= footer %>
<% end %>
<% end %>
Expand Down
6 changes: 5 additions & 1 deletion app/components/primer/alpha/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def initialize(
@system_arguments, {
aria: {
disabled: true,
labelledby: "#{@id}-title",
labelledby: labelledby,
describedby: "#{@id}-description"
}
}
Expand All @@ -155,6 +155,10 @@ def before_render
with_header unless header?
with_body unless body?
end

def labelledby
"#{@id}-title"
end
end
end
end
1 change: 1 addition & 0 deletions app/components/primer/primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './alpha/action_bar_element'
import './alpha/dropdown'
import './anchored_position'
import './focus_group'
import './scrollable_region'
import './alpha/image_crop'
import './alpha/modal_dialog'
import './beta/nav_list'
Expand Down
48 changes: 48 additions & 0 deletions app/components/primer/scrollable_region.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {controller, attr} from '@github/catalyst'

@controller
export class ScrollableRegionElement extends HTMLElement {
@attr hasOverflow = false
@attr labelledBy = ''

observer: ResizeObserver

connectedCallback() {
this.style.overflow = 'auto'

this.observer = new ResizeObserver(entries => {
for (const entry of entries) {
this.hasOverflow =
entry.target.scrollHeight > entry.target.clientHeight || entry.target.scrollWidth > entry.target.clientWidth
}
})

this.observer.observe(this)
}

disconnectedCallback() {
this.observer.disconnect()
}

attributeChangedCallback(name: string) {
if (name === 'data-has-overflow') {
if (this.hasOverflow) {
this.setAttribute('aria-labelledby', this.labelledBy)
this.setAttribute('role', 'region')
this.setAttribute('tabindex', '0')
} else {
this.removeAttribute('aria-labelledby')
this.removeAttribute('role')
this.removeAttribute('tabindex')
}
}
}
}

declare global {
interface Window {
ScrollableRegionElement: typeof ScrollableRegionElement
}
}

window.ScrollableRegionElement = ScrollableRegionElement
Loading