Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The toggle element provided by webform is a custom UI element, and the current implementation is inaccessible.
- Sighted keyboard users cannot focus the toggle switch to operate it (likewise screen readers).
- Even if they were focusable, the semantics are wrong. The current implementation is combination of a hidden checkbox (not passed to OS accessibility API) and cosmetic DIVs with no machine readable semantics. Screenreader users may well be unaware that the toggle elements exist at all.
Proposed resolution
- Devise a new markup/JS pattern, aim to follow practices in WAI-ARIA and ARIA Authoring Practices. Can we use a simple
<button>
witharia-pressed
, instead of managing cosmetic DIVs and a hidden checkbox in parallel? - Rework
Drupal.behaviors.webformToggle
JS andWebformToggle::preRenderCheckbox()
as appropriate. - Note 1: the webform element "title" setting should end up being the computed accessible name for the button.
- Note 2: there are single and multiple versions of this element available. Using the "toggles" version already wraps them up together with fieldset/legend elements, and uses aria-describedby to associate the "description" setting with the fieldset. In other words, this already works like radios and checkboxes, so doesn't need fixing here. Confirm it still works though...
- Note 3: The webform "description" should be associated with single-toggle elements using
aria-describedby
directly on the button element itself. - Note 4: the forthcoming ARIA 1.1 (candidate recommendation currently) introduces a new "switch" role, which aims to get around the semantic debate of whether to use a button or a checkbox role for these things. It looks ideal, however it's brand new and we should avoid using this until we know it has good support across many browser/AT combinations. For now,
<button aria-pressed="true">
is a more reliable pattern. - Question 1: how do we convey the custom toggle-off and toggle-on text (which appear inside the switch background? These are not the same as the
<label>
. Do we assume they are cosmetic, or should we include a Drupal.announce(), or something else? Discuss :-) - Question 2: do we need to update the way
WebformToggles
render element PHP class works? It seems to work by changing the render type of it's children. Fingers crossed we might not need to touch this one.
Remaining tasks
- Manual testing is essential! Keyboard-only, and a variety of screen readers.
- Functional javascript tests are possible, triggering keyboard events on the toggle button. Assert (e.g.) that the toggle-off text has gone, and the toggle-on text has appeared, or just test for expected class attribute values.
User interface changes
No visual changes are needed. This is all about keyboard operation, and conveying dynamic state semantics to assistive technology.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#11 | 2890861-11.patch | 6.33 KB | jrockowitz |
| |||
#9 | 2890861-9.patch | 5.6 KB | jrockowitz |
Comments
Comment #2
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedThis could be a good issue to look at during this weekend's accessibility sprint in Amsterdam, while we have many a11y heads in the same room.
Comment #3
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedComment #4
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedComment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@andrewmacpherson A few thoughts...
Comment #6
lomale@bluewin.ch CreditAttribution: lomale@bluewin.ch as a volunteer and commentedScreenreader users may well be unaware that the toggle elements exist at all.
This is exactly my problem. I have a lot of people that are not aware when submitting a registration that the should say YES to the first element
toggle_advanced:
'#type': toggle
'#title': 'I will participate at the next training with …..'
'#title_display': before
'#description_display': before
'#admin_title': registration
'#toggle_theme': iphone
'#toggle_size': large
'#on_text': 'YES'
'#off_text': 'NO'
because there is no required, they can let it filled with NO
could there be a way; when they accept the
termsofservice:
'#type': checkbox
'#title': 'I accept the terms of service'
'#title_display': before
'#field_suffix': 'JA / YES'
'#icheck': square-blue
'#required': true
'#required_error': 'please accept our terms of service'
'#admin_title': 'terms of service accepted‘
which is required that the
toggle_advanced:
'#type': toggle
would be set on YES
or would you suggest that I interchange the
'#on_text': 'YES'
'#off_text': 'NO'
because if i put a condition logic on the submit button
actions:
'#type': webform_actions
'#title': 'Submit button(s)'
'#submit__label': 'Submit'
lot of people claimed that the submit does not work.
Any suggestions to this problem.
Thanks
Comment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI just happened upon this code snippet.
https://www.w3schools.com/howto/howto_css_switch.asp
Is this approach more accessible? I think the
.switch input {display:none;}
might be an issue.It is definitely much much simpler.
Here is an example of CSS library on GitHub.
https://ghinda.net/css-toggle-switch/
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@andrewmacpherson Thank you for spending the time to review this element.
The accessibility and maintenance of element are not great.
I am leaning toward deprecating the Toggle element and removing it at some point in the future.
The depreciation would mean the library (https://github.com/simontabor/jquery-toggles/) is not installed by default which will disable the element. An update hook can disable the library for websites that are not using the toggle element.
I am not seeing any external libraries that implement a well-maintained and accessible switch.
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #11
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #15
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented