Skip to content

Commit

Permalink
Fix unclickable checkboxes (#30195) (#30199)
Browse files Browse the repository at this point in the history
Backport #30195 by @silverwind

Fix go-gitea/gitea#30185, regression from
go-gitea/gitea#30162.

The checkboxes were unclickable because the label was positioned over
the checkbox with `padding`. Now it uses `margin` so the checkbox itself
will be clickable in all cases.

Secondly, I changed the for/id linking to also add missing `for`
attributes when `id` is present. The other way around (only `for`
present) is currently not handled and I think there are likey no
occurences in the code and introducing new non-generated `id`s might
cause problems elsewhere if we do, so I skipped on that.

Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 9d38c4d60ef5bd015e1430386e38d9f32e050f8f)
  • Loading branch information
GiteaBot authored and earl-warren committed Apr 8, 2024
1 parent f4a2f43 commit d573c22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion web_src/css/modules/checkbox.css
Expand Up @@ -41,7 +41,7 @@ input[type="radio"] {

.ui.checkbox label,
.ui.radio.checkbox label {
padding-left: 1.85714em;
margin-left: 1.85714em;
}

.ui.checkbox + label {
Expand Down
17 changes: 13 additions & 4 deletions web_src/js/modules/fomantic/checkbox.js
Expand Up @@ -6,10 +6,19 @@ export function initAriaCheckboxPatch() {
if (el.hasAttribute('data-checkbox-patched')) continue;
const label = el.querySelector('label');
const input = el.querySelector('input');
if (!label || !input || input.getAttribute('id') || label.getAttribute('for')) continue;
const id = generateAriaId();
input.setAttribute('id', id);
label.setAttribute('for', id);
if (!label || !input) continue;
const inputId = input.getAttribute('id');
const labelFor = label.getAttribute('for');

if (inputId && !labelFor) { // missing "for"
label.setAttribute('for', inputId);
} else if (!inputId && !labelFor) { // missing both "id" and "for"
const id = generateAriaId();
input.setAttribute('id', id);
label.setAttribute('for', id);
} else {
continue;
}
el.setAttribute('data-checkbox-patched', 'true');
}
}

0 comments on commit d573c22

Please sign in to comment.