Skip to content

Commit

Permalink
Use a general approch to improve a11y for all checkboxes and dropdown…
Browse files Browse the repository at this point in the history
…s. (go-gitea#23542)

This PR follows go-gitea#22599 and go-gitea#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.
  • Loading branch information
wxiaoguang committed Mar 22, 2023
1 parent 43809e6 commit d4f35bd
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 94 deletions.
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ add = Add
add_all = Add All
remove = Remove
remove_all = Remove All
remove_label_str = Remove item "%s"
edit = Edit

enabled = Enabled
Expand Down
1 change: 1 addition & 0 deletions templates/base/head_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
copy_error: '{{.locale.Tr "copy_error"}}',
error_occurred: '{{.locale.Tr "error.occurred"}}',
network_error: '{{.locale.Tr "error.network_error"}}',
remove_label_str: '{{.locale.Tr "remove_label_str"}}',
},
};
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}}
Expand Down
5 changes: 1 addition & 4 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {mqBinarySearch} from '../utils.js';
import {createDropzone} from './dropzone.js';
import {initCompColorPicker} from './comp/ColorPicker.js';
import {showGlobalErrorMessage} from '../bootstrap.js';
import {attachCheckboxAria, attachDropdownAria} from './aria.js';
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
import {initTooltip} from '../modules/tippy.js';
import {svg} from '../svg.js';
Expand Down Expand Up @@ -123,9 +122,7 @@ export function initGlobalCommon() {
$uiDropdowns.filter('.slide.up').dropdown({transition: 'slide up'});
$uiDropdowns.filter('.upward').dropdown({direction: 'upward'});

attachDropdownAria($uiDropdowns);

attachCheckboxAria($('.ui.checkbox'));
$('.ui.checkbox').checkbox();

$('.tabular.menu .item').tab();
$('.tabable.menu .item').tab();
Expand Down
5 changes: 5 additions & 0 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ import {initFormattingReplacements} from './features/formatting.js';
import {initCopyContent} from './features/copycontent.js';
import {initCaptcha} from './features/captcha.js';
import {initRepositoryActionView} from './components/RepoActionView.vue';
import {initAriaCheckboxPatch} from './modules/aria/checkbox.js';
import {initAriaDropdownPatch} from './modules/aria/dropdown.js';

// Run time-critical code as soon as possible. This is safe to do because this
// script appears at the end of <body> and rendered HTML is accessible at that point.
Expand All @@ -98,6 +100,9 @@ initFormattingReplacements();
$.fn.tab.settings.silent = true;
// Disable the behavior of fomantic to toggle the checkbox when you press enter on a checkbox element.
$.fn.checkbox.settings.enableEnterKey = false;
// 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.
initAriaCheckboxPatch();
initAriaDropdownPatch();

$(document).ready(() => {
initGlobalCommon();
Expand Down
12 changes: 9 additions & 3 deletions web_src/js/features/aria.md → web_src/js/modules/aria/aria.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ To test the aria/accessibility with screen readers, developers can use the follo
* Double-finger swipe means old single-finger swipe.
* TODO: on Windows, on Linux, on iOS

# Known Problems

* Tested with Apple VoiceOver: If a dropdown menu/combobox is opened by mouse click, then arrow keys don't work.
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.
The clue: when the dropdown is only opened by mouse click, VoiceOver doesn't send 'keydown' events of arrow keys to the DOM,
VoiceOver expects to use arrow keys to navigate between some elements, but it couldn't.
Users could use Option+ArrowKeys to navigate between menu/combobox items or selection labels if the menu/combobox is opened by mouse click.

# Checkbox

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

# Dropdown

## Fomantic UI Dropdown
# Fomantic Dropdown

Fomantic Dropdown is designed to be used for many purposes:

Expand Down
5 changes: 5 additions & 0 deletions web_src/js/modules/aria/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let ariaIdCounter = 0;

export function generateAriaId() {
return `_aria_auto_id_${ariaIdCounter++}`;
}
38 changes: 38 additions & 0 deletions web_src/js/modules/aria/checkbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import $ from 'jquery';
import {generateAriaId} from './base.js';

const ariaPatchKey = '_giteaAriaPatchCheckbox';
const fomanticCheckboxFn = $.fn.checkbox;

// use our own `$.fn.checkbox` to patch Fomantic's checkbox module
export function initAriaCheckboxPatch() {
if ($.fn.checkbox === ariaCheckboxFn) throw new Error('initAriaCheckboxPatch could only be called once');
$.fn.checkbox = ariaCheckboxFn;
ariaCheckboxFn.settings = fomanticCheckboxFn.settings;
}

// the patched `$.fn.checkbox` checkbox function
// * it does the one-time attaching on the first call
function ariaCheckboxFn(...args) {
const ret = fomanticCheckboxFn.apply(this, args);
for (const el of this) {
if (el[ariaPatchKey]) continue;
attachInit(el);
}
return ret;
}

function attachInit(el) {
// Fomantic UI checkbox needs to be something like: <div class="ui checkbox"><label /><input /></div>
// It doesn't work well with <label><input />...</label>
// To make it work with aria, the "id"/"for" attributes are necessary, so add them automatically if missing.
// In the future, refactor to use native checkbox directly, then this patch could be removed.
el[ariaPatchKey] = {}; // record that this element has been patched
const label = el.querySelector('label');
const input = el.querySelector('input');
if (!label || !input || input.getAttribute('id')) return;

const id = generateAriaId();
input.setAttribute('id', id);
label.setAttribute('for', id);
}
Loading

0 comments on commit d4f35bd

Please sign in to comment.