-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Use a general approch to improve a11y for all checkboxes and dropdowns. #23542
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f6216f1
improve
wxiaoguang e7ca484
use local ariaPatchKey instead of exported
wxiaoguang c62f03b
move to modules/aria
wxiaoguang 82ff90e
Merge branch 'main' into improve-dropdown-a11y
wxiaoguang a84a9ef
Merge branch 'main' into improve-dropdown-a11y
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const ariaPatchKey = '_giteaAriaPatch'; | ||
|
||
let ariaIdCounter = 0; | ||
|
||
export function generateAriaId() { | ||
return `_aria_auto_id_${ariaIdCounter++}`; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import $ from 'jquery'; | ||
import {ariaPatchKey, generateAriaId} from './aria-base.js'; | ||
|
||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.