Skip to content

Commit f320204

Browse files
wxiaoguangLoïc Dachary
authored and
Loïc Dachary
committed
Add ARIA support for Fomantic UI checkboxes (go-gitea#22599)
Replace go-gitea#22593 This is a general approach to add ARIA support for all Fomantic UI checkboxes (including radioboxes) * Pros: * General approach, it works for all Fomantic UI checkboxes / radioboxes * No need to write IDs manually everywhere * No need to tell new contributors to write IDs again and again * Cons: * Slightly affects performance, but it's really trivial, because there was already a heavy `$('.ui.checkbox').checkbox()` for Fomantic UI before. So everything is still fine. Screenshot (from the repo setting page, which has various checkboxes): <details> ![image](https://user-images.githubusercontent.com/2114189/214480937-3a54d36f-55c3-49de-9c45-c4bb21f1f4c6.png) </details> (cherry picked from commit d4610480ee610ec7d63ae17440128f057bc0d531)
1 parent 0db7a0a commit f320204

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Diff for: web_src/js/features/aria.js

+17
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ function attachOneDropdownAria($dropdown) {
9898
export function attachDropdownAria($dropdowns) {
9999
$dropdowns.each((_, e) => attachOneDropdownAria($(e)));
100100
}
101+
102+
export function attachCheckboxAria($checkboxes) {
103+
$checkboxes.checkbox();
104+
105+
// Fomantic UI checkbox needs to be something like: <div class="ui checkbox"><label /><input /></div>
106+
// It doesn't work well with <label><input />...</label>
107+
// To make it work with aria, the "id"/"for" attributes are necessary, so add them automatically if missing.
108+
// In the future, refactor to use native checkbox directly, then this patch could be removed.
109+
for (const el of $checkboxes) {
110+
const label = el.querySelector('label');
111+
const input = el.querySelector('input');
112+
if (!label || !input || input.getAttribute('id')) continue;
113+
const id = generateAriaId();
114+
input.setAttribute('id', id);
115+
label.setAttribute('for', id);
116+
}
117+
}

Diff for: web_src/js/features/common-global.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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 {attachDropdownAria} from './aria.js';
7+
import {attachCheckboxAria, attachDropdownAria} from './aria.js';
88
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
99
import {initTooltip} from '../modules/tippy.js';
1010

@@ -110,7 +110,7 @@ export function initGlobalCommon() {
110110
});
111111
attachDropdownAria($uiDropdowns);
112112

113-
$('.ui.checkbox').checkbox();
113+
attachCheckboxAria($('.ui.checkbox'));
114114

115115
$('.tabular.menu .item').tab();
116116
$('.tabable.menu .item').tab();

0 commit comments

Comments
 (0)