-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
CustomSelectControl V2: fix labelling with a visually hidden label #63137
CustomSelectControl V2: fix labelling with a visually hidden label #63137
Conversation
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: +11 B (0%) Total Size: 1.76 MB
ℹ️ View Unchanged
|
Flaky tests detected in f606ee9. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9797481030
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the component should be wrapped in a BaseControl
(for consistency with other controls), or at the very least use BaseControl.VisualLabel
instead of Styled.SelectLabel
. Are there some considerations I'm missing?
Not really, my initial fix focused on using the components that were already being used (and that made me miss the |
f606ee9
to
c2aa264
Compare
@mirka since I don't think it would be possible to use I also had to allow The only visible difference is that the label now renders a tiny gap above itself, due to the fact that
|
@@ -55,7 +55,6 @@ WithHelpText.args = { | |||
* otherwise use if the `label` prop was passed. | |||
*/ | |||
export const WithVisualLabel: StoryFn< typeof BaseControl > = ( props ) => { | |||
// @ts-expect-error - Unclear how to fix, see also https://github.com/WordPress/gutenberg/pull/39468#discussion_r827150516 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it looks like the way to fix it was to allow ref forwarding 🤷♂️
// @ts-expect-error `children` are passed via the render prop | ||
<VisuallyHidden /> | ||
) : ( | ||
// @ts-expect-error `children` are passed via the render prop | ||
<BaseControl.VisualLabel as="div" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the label
HTML element is not correct in this case, as also confirmed by the ariakit docs:
Since it's not a native select element, we can't use the native label element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I played around and confirmed that although a native label
will work as expected in Chrome, it behaves differently in Firefox and Safari. Clicking on the label not only focuses but clicks the "select button", expanding the dropdown. So yeah, I guess it does need special handling 🫤
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's part of what Ariakit.SelectLabel
does under the hood
I extracted the Once merged, I'll rebase and remove those same changes from this PR. |
Furthermore, this visual difference won't manifest in a wp-admin context because |
ea8afa5
to
7424442
Compare
I agree that it would be risky — let's leave as-is. In the long term, I'd like to re-think I merged #63169 and rebased this PR. It should be ready for a last round of review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went through a round of testing on this and it tested well according to the test instructions.
I have nothing to add on the code, IMHO it's good to go, but I'll let @mirka approve it since she had some feedback before.
@@ -436,4 +436,14 @@ describe.each( [ | |||
} ) | |||
).toBeVisible(); | |||
} ); | |||
|
|||
it( 'Should label the component correctly even when the label is not visible', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding a test 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thank you!
…ess#63137) * Add unit tests * Use ariakit's labelling logic even when the label is visually hidden * CHANGELOG * Use BaseControl.VisualLabel
…ess#63137) * Add unit tests * Use ariakit's labelling logic even when the label is visually hidden * CHANGELOG * Use BaseControl.VisualLabel
What?
Part of #55023
Requires #63169
Fix a bug in the V2 implementation where the component wouldn't be labelled property to assistive technology when the label is hidden visually via the
hideLabelFromVision
prop.Why?
Making sure that controls are properly labelled is essential to ensure a good experience for all users.
How?
I refactored the code and made it so that the internal implementation of
CustomSelectControlV2
always relies on theAriakit.SelectLabel
component even when thehideLabelFromVision
istrue
, which takes care of setting the correct attributes necessary for the control labeling. I was able to do so by leveraging therender
prop (see docs).Testing Instructions
trunk
)hideLabelFromVision
prop:trunk
role="combobox"
is correctly labeled by the hiddenlabel