ToggleGroupControl: Fix accessible association of help text#76740
ToggleGroupControl: Fix accessible association of help text#76740
help text#76740Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -21 B (0%) Total Size: 7.73 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in d36bc76. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23802187611
|
ciampo
left a comment
There was a problem hiding this comment.
Pre-approving since there's no blocking feedback. Feel free to merge once remaining feedback is addressed / responded to 🚀
Unrelated to this PR, but maybe we can add it regardless — while reviewing, I realised we don't set the id to the View when ToggleGroupControl is used as a button group.
The fix is simple:
diff --git i/packages/components/src/toggle-group-control/toggle-group-control/as-button-group.tsx w/packages/components/src/toggle-group-control/toggle-group-control/as-button-group.tsx
index 051bec811a0..a78aec7d5a8 100644
--- i/packages/components/src/toggle-group-control/toggle-group-control/as-button-group.tsx
+++ w/packages/components/src/toggle-group-control/toggle-group-control/as-button-group.tsx
@@ -83,6 +83,7 @@ function UnforwardedToggleGroupControlAsButtonGroup(
{ ...otherProps }
ref={ forwardedRef }
role="group"
+ id={ baseId }
>
{ children }
</View>
| className={ classes } | ||
| isAdaptiveWidth={ isAdaptiveWidth } | ||
| // `label` is used for `aria-label` on the inner control. | ||
| // This is separate from the visual label rendered by `BaseControl`. |
There was a problem hiding this comment.
Again, not related to this PR, but noting that aria-label will take precedence over the <label>. It works fine, but maybe it's a symptom of the need for a better visual label or a future cleanup? or at least a lesson to learn for the @wordpress/ui work?
There was a problem hiding this comment.
Mm, right…. I would maybe say that this is a structural limitation of the BaseControl system, where it's not fully modular. Shouldn't be an issue with Field from Base UI, where we have easy modular access to the label element.
| return ( | ||
| <BaseControl help={ help }> | ||
| { ! hideLabelFromVision && ( | ||
| <VisualLabelWrapper> |
There was a problem hiding this comment.
Can we remove VisualLabelWrapper without causing a regression (see original reson why it was added)
| { value: 'right', label: 'Right' }, | ||
| { value: 'justify', label: 'Justify' }, | ||
| ].map( mapPropsToOptionComponent ), | ||
| help: 'Help text to describe the control.', |
There was a problem hiding this comment.
Same comment as for ComboboxControl — should help text be featured in the default story, or moved to a dedicated story?
There was a problem hiding this comment.
Matched the pattern for newer component stories where we show help text by default. (No strong opinion though, hard to say which is "better".)
# Conflicts: # packages/components/CHANGELOG.md
Good catch, fixed in a56651c |
* ToggleGroupControl: Associate help text with the control accessibly * Add help text to stories * Add changelog * ToggleGroupControl: Set id on button group View Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: ciampo <mciampini@git.wordpress.org>
* ToggleGroupControl: Associate help text with the control accessibly * Add help text to stories * Add changelog * ToggleGroupControl: Set id on button group View Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: ciampo <mciampini@git.wordpress.org>

What?
Fix the
helpprop onToggleGroupControlso that it is programmatically associated with the control viaaria-describedby. Also refactor the component to useuseBaseControlPropsmore fully, delegating label and help rendering toBaseControl.Why?
The
helptext was being rendered visually but had no accessible association with the radiogroup (or button group, in theisDeselectablecase). Screen reader users would not hear the help text when interacting with the control.The component was also hand-rolling its
hideLabelFromVisionlogic and not using theidfromuseBaseControlProps, duplicating whatBaseControlalready provides.How?
useBaseControlPropswithid,help,label, andhideLabelFromVision, and spread the returned props ontoBaseControland the inner control.idand addsaria-describedbyto the radiogroup/button group.BaseControlnow handles label rendering (including thehideLabelFromVisioncase), removing the need for the manualVisualLabelWrapperandBaseControl.VisualLabelusage.VisualLabelWrapperstyled component (and thestyledimport from Emotion).Testing Instructions
Automated
New unit tests cover:
hideLabelFromVisionis truehelptext is associated with theradiogroupviaaria-describedbyhelptext is associated with thegroupviaaria-describedbywhenisDeselectableManual
ToggleGroupControlstory in Storybook.helpprop to some text.aria-describedbypointing to the help text element.Use of AI Tools
Cursor with Claude was used to author this PR.