8000 Improve accessibility of dashboard tabs (issue #2171) by artyfakt · Pull Request #2937 · liqd/adhocracy-plus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve accessibility of dashboard tabs (issue #2171) #2937

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions adhocracy-plus/templates/a4dashboard/base_dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,31 @@
{% url 'a4dashboard:newsletter-create' organisation_slug=view.organisation.slug as newsletter_create %}
{% url 'a4dashboard:organisation-settings' organisation_slug=view.organisation.slug as organisation_settings %}

<nav class="d-none d-sm-inline-block" aria-label="{% translate 'Dashboard' %}">
<nav class="d-none d-sm-inline-block dashboard-tabs" aria-label="{% translate 'Dashboard' %}" role="tablist">
<a href="{{ project_list }}"
class="tab {% if view.menu_item == 'project' %}active{% endif %}">
class="tab {% if view.menu_item == 'project' %}active{% endif %}"
role="tab"
aria-controls="container"
aria-selected="{% if view.menu_item == 'project' %}true{% else %}false{% endif %}"
tabindex="{% if view.menu_item == 'project' %}0{% else %}-1{% endif %}">
{% translate 'Participation projects' %}
</a>
<a href="{{ newsletter_create }}"
class="tab {% if view.menu_item == 'communication' %}active{% endif %}">
class="tab {% if view.menu_item == 'communication' %}active{% endif %}"
role="tab"
aria-controls="container"
aria-selected="{% if view.menu_item == 'communication' %}true{% else %}false{% endif %}"
tabindex="{% if view.menu_item == 'communication' %}0{% else %}-1{% endif %}">
{% translate 'Communication' %}
</a>
{% has_perm 'a4_candy_organisations.change_organisation' request.user view.organisation as user_may_change_organisation %}
{% if user_may_change_organisation %}
<a href="{{ organisation_settings }}"
class="tab {% if view.menu_item == 'organisation' %}active{% endif %}">
class="tab {% if view.menu_item == 'organisation' %}active{% endif %}"
role="tab"
aria-controls="container"
aria-selected="{% if view.menu_item == 'organisation' %}true{% else %}false{% endif %}"
tabindex="{% if view.menu_item == 'organisation' %}0{% else %}-1{% endif %}">
{% translate 'Organisation settings' %}
</a>
{% endif %}
Expand All @@ -80,7 +92,7 @@
role="button"
aria-expanded="false"
data-bs-toggle="tab">
{% if request.path == project_list %}
{% if request.path == project_list %}
{% translate 'Participation projects' %}
{% elif request.path == newsletter_create %}
{% translate 'Communication' %}
Expand Down Expand Up @@ -112,16 +124,16 @@
</li>
</ul>


</div>
</div>
{% endblock %}
<div class="container">
<div class="container" role="tabpanel">
{% block dashboard_content %}{% endblock %}
</div>
{% endblock %}

{% block extra_js %}
<script src="{% static 'unload_warning.js' %}"></script>
<script src="{% static 'init_dashboard_accordion.js' %}"></script>
<script src="{% static 'dashboard_tabs.js' %}"></script>
{% endblock %}
82 changes: 82 additions & 0 deletions apps/dashboard/assets/dashboard_tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict'

/*
* This improves accessibility of tabs on dashboard pages where applying Bootstrap's
* built-in tab functionality is not possible. It ensures consistent keyboard navigability
* in both scenarios.
*
* This solution is based on the official W3C WAI example for tabs with manual activation:
* https://www.w3.org/WAI/ARIA/apg/patterns/tabs/examples/tabs-manual/
*/

class DashboardTabs {
constructor (tablist) {
this.tablist = tablist
this.firstTab = null
this.lastTab = null
this.tabs = Array.from(this.tablist.querySelectorAll('[role=tab]'))
this.tabs.forEach(tab => {
tab.addEventListener('keydown', this.onKeyDown.bind(this))
if (!this.firstTab) {
this.firstTab = tab
}
this.lastTab = tab
})
}

focusTab (tab) {
tab.focus()
}

focusPreviousTab (currentTab) {
if (currentTab === this.firstTab) {
this.focusTab(this.lastTab)
} else {
this.focusTab(this.tabs[this.tabs.indexOf(currentTab) - 1])
}
}

focusNextTab (currentTab) {
if (currentTab === this.lastTab) {
this.focusTab(this.firstTab)
} else {
this.focusTab(this.tabs[this.tabs.indexOf(currentTab) + 1])
}
}

onKeyDown (event) {
const currentTab = event.currentTarget

switch (event.key) {
case 'ArrowLeft':
this.focusPreviousTab(currentTab)
break
case 'ArrowUp':
this.focusPreviousTab(currentTab)
event.preventDefault()
break
case 'ArrowRight':
this.focusNextTab(currentTab)
break
case 'ArrowDown':
this.focusNextTab(currentTab)
event.preventDefault()
break
case 'Home':
this.focusTab(this.firstTab)
break
case 'End':
this.focusTab(this.lastTab)
break
default:
break
}
}
}

window.addEventListener('load', () => {
const tablists = document.querySelectorAll('[role=tablist].dashboard-tabs')
tablists.forEach(tablist => {
tablist = new DashboardTabs(tablist)
})
})
5 changes: 5 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ module.exports = {
'adhocracy4/adhocracy4/categories/assets/select_dropdown_init.js'
],
dependOn: 'adhocracy4'
},
dashboard_tabs: {
import: [
'./apps/dashboard/assets/dashboard_tabs.js'
]
}
},
output: {
Expand Down
0