Skip to content

Commit d4f35bd

Browse files
authored
Use a general approch to improve a11y for all checkboxes and dropdowns. (#23542)
This PR follows #22599 and #23450 The major improvements: 1. The `aria-*.js` are totally transparent now, no need to call `attachDropdownAria` explicitly anymore. * It hooks the `$.fn.checkbox` and `$.fn.dropdown`, then our patch works. * It makes all dynamically generated checkbox/dropdown work with a11y without any change * eg: the `conversation.find('.dropdown').dropdown();` in `repo-diff.js` 2. Since it's totally transparent now, it could be easier to modify or remove in the future. 3. It handles all selection labels as well (by onLabelCreate), so it supports "multiple selection dropdown" now. * It partially completes one of my TODOs: `TODO: multiple selection is not supported yet.` 4. The code structure is clearer, code blocks are splitted into different functions. * The old `attachOneDropdownAria` was splitted into separate functions. * It makes it easier to add more fine tunes in the future, and co-work with contributors. 6. The code logic is similar as before, only two new parts: 1. the `ariaCheckboxFn` and `ariaDropdownFn` functions 2. the `onLabelCreate` and `updateSelectionLabel` functions In `aria-dropdown.js` I had to mix jQuery and Vanilla JS somewhat, I think the code is still understandable, otherwise the code would be much more complex to read. Thanks to fsologureng for the idea about "improving the 'delete icon' with aria attributes". If there is anything unclear or incorrect, feel free to ask and discuss, or propose new PRs for it.
1 parent 43809e6 commit d4f35bd

File tree

8 files changed

+198
-94
lines changed

8 files changed

+198
-94
lines changed

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ add = Add
8484
add_all = Add All
8585
remove = Remove
8686
remove_all = Remove All
87+
remove_label_str = Remove item "%s"
8788
edit = Edit
8889

8990
enabled = Enabled

templates/base/head_script.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
4141
copy_error: '{{.locale.Tr "copy_error"}}',
4242
error_occurred: '{{.locale.Tr "error.occurred"}}',
4343
network_error: '{{.locale.Tr "error.network_error"}}',
44+
remove_label_str: '{{.locale.Tr "remove_label_str"}}',
4445
},
4546
};
4647
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}}

web_src/js/features/common-global.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {mqBinarySearch} from '../utils.js';
44
import {createDropzone} from './dropzone.js';
55
import {initCompColorPicker} from './comp/ColorPicker.js';
66
import {showGlobalErrorMessage} from '../bootstrap.js';
7-
import {attachCheckboxAria, attachDropdownAria} from './aria.js';
87
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
98
import {initTooltip} from '../modules/tippy.js';
109
import {svg} from '../svg.js';
@@ -123,9 +122,7 @@ export function initGlobalCommon() {
123122
$uiDropdowns.filter('.slide.up').dropdown({transition: 'slide up'});
124123
$uiDropdowns.filter('.upward').dropdown({direction: 'upward'});
125124

126-
attachDropdownAria($uiDropdowns);
127-
128-
attachCheckboxAria($('.ui.checkbox'));
125+
$('.ui.checkbox').checkbox();
129126

130127
$('.tabular.menu .item').tab();
131128
$('.tabable.menu .item').tab();

web_src/js/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import {initFormattingReplacements} from './features/formatting.js';
8989
import {initCopyContent} from './features/copycontent.js';
9090
import {initCaptcha} from './features/captcha.js';
9191
import {initRepositoryActionView} from './components/RepoActionView.vue';
92+
import {initAriaCheckboxPatch} from './modules/aria/checkbox.js';
93+
import {initAriaDropdownPatch} from './modules/aria/dropdown.js';
9294

9395
// Run time-critical code as soon as possible. This is safe to do because this
9496
// script appears at the end of <body> and rendered HTML is accessible at that point.
@@ -98,6 +100,9 @@ initFormattingReplacements();
98100
$.fn.tab.settings.silent = true;
99101
// Disable the behavior of fomantic to toggle the checkbox when you press enter on a checkbox element.
100102
$.fn.checkbox.settings.enableEnterKey = false;
103+
// Use the patches to improve accessibility, these patches are designed to be as independent as possible, make it easy to modify or remove in the future.
104+
initAriaCheckboxPatch();
105+
initAriaDropdownPatch();
101106

102107
$(document).ready(() => {
103108
initGlobalCommon();

web_src/js/features/aria.md web_src/js/modules/aria/aria.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ To test the aria/accessibility with screen readers, developers can use the follo
2323
* Double-finger swipe means old single-finger swipe.
2424
* TODO: on Windows, on Linux, on iOS
2525

26+
# Known Problems
27+
28+
* Tested with Apple VoiceOver: If a dropdown menu/combobox is opened by mouse click, then arrow keys don't work.
29+
But if the dropdown is opened by keyboard Tab, then arrow keys work, and from then on, the keys almost work with mouse click too.
30+
The clue: when the dropdown is only opened by mouse click, VoiceOver doesn't send 'keydown' events of arrow keys to the DOM,
31+
VoiceOver expects to use arrow keys to navigate between some elements, but it couldn't.
32+
Users could use Option+ArrowKeys to navigate between menu/combobox items or selection labels if the menu/combobox is opened by mouse click.
33+
2634
# Checkbox
2735

2836
## Accessibility-friendly Checkbox
@@ -52,9 +60,7 @@ There is still a problem: Fomantic UI checkbox is not friendly to screen readers
5260
so we add IDs to all the Fomantic UI checkboxes automatically by JS.
5361
If the `label` part is empty, then the checkbox needs to get the `aria-label` attribute manually.
5462

55-
# Dropdown
56-
57-
## Fomantic UI Dropdown
63+
# Fomantic Dropdown
5864

5965
Fomantic Dropdown is designed to be used for many purposes:
6066

web_src/js/modules/aria/base.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let ariaIdCounter = 0;
2+
3+
export function generateAriaId() {
4+
return `_aria_auto_id_${ariaIdCounter++}`;
5+
}

web_src/js/modules/aria/checkbox.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import $ from 'jquery';
2+
import {generateAriaId} from './base.js';
3+
4+
const ariaPatchKey = '_giteaAriaPatchCheckbox';
5+
const fomanticCheckboxFn = $.fn.checkbox;
6+
7+
// use our own `$.fn.checkbox` to patch Fomantic's checkbox module
8+
export function initAriaCheckboxPatch() {
9+
if ($.fn.checkbox === ariaCheckboxFn) throw new Error('initAriaCheckboxPatch could only be called once');
10+
$.fn.checkbox = ariaCheckboxFn;
11+
ariaCheckboxFn.settings = fomanticCheckboxFn.settings;
12+
}
13+
14+
// the patched `$.fn.checkbox` checkbox function
15+
// * it does the one-time attaching on the first call
16+
function ariaCheckboxFn(...args) {
17+
const ret = fomanticCheckboxFn.apply(this, args);
18+
for (const el of this) {
19+
if (el[ariaPatchKey]) continue;
20+
attachInit(el);
21+
}
22+
return ret;
23+
}
24+
25+
function attachInit(el) {
26+
// Fomantic UI checkbox needs to be something like: <div class="ui checkbox"><label /><input /></div>
27+
// It doesn't work well with <label><input />...</label>
28+
// To make it work with aria, the "id"/"for" attributes are necessary, so add them automatically if missing.
29+
// In the future, refactor to use native checkbox directly, then this patch could be removed.
30+
el[ariaPatchKey] = {}; // record that this element has been patched
31+
const label = el.querySelector('label');
32+
const input = el.querySelector('input');
33+
if (!label || !input || input.getAttribute('id')) return;
34+
35+
const id = generateAriaId();
36+
input.setAttribute('id', id);
37+
label.setAttribute('for', id);
38+
}

0 commit comments

Comments
 (0)