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
Fix <Modal> component so it is possible to use the aria.labelledby prop #29020
Fix <Modal> component so it is possible to use the aria.labelledby prop #29020
Conversation
Fixes a bug where <Modal> wasn't forwarding the `aria.labelledby` prop to <ModalFrame> if the `title` prop was missing. However the purpose of `labelledby` is to allow modals to provide their own title element, in which case they won't want to use the `title` prop. The `aria.labelledby` prop is now correctly forwarded to <ModalFrame> if no title is provided. The `title` prop will take precedent over `aria.labelledby`.
Very nice to see that checklist in the PR description. We should do more of that. 🥇 |
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.
The code looks good, and thanks for adding tests 😺 , but I'd like to know more about the use case for an explicit aria-labelledby
prop. (I know it was already there and not working properly, but can't find the history on why it was added.)
For most cases, we are encouraged to use the modal title, which the dialog aria-labelledby
will point to by default.
In cases where the modal can't have a title for some reason (the welcome modal is an example), we have the contentLabel
prop, that applies an aria-label
to the dialog.
Is there any case where we would want the dialog aria-labelledby
to point to a visible description elsewhere than the modal title? Note that we'd need to be careful with where that description is located, because when the modal is active, the rest of the document is hidden with aria-hidden
, so a description located outside the modal itself will be inaccessible.
@tellthemachines I think the
For my case, I'm using I could also achieve this by using the |
I reckon you could achieve that layout just by fiddling with display, borders and padding, and absolutely positioning the close button instead 🤔 I'm not opposed to passing in |
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, @p-jackson! I agree that this could be useful, but it looks like we should update the README file, which currently says that title
is a required prop and aria.labelledby
has a default value.
I know that this is not the point of this PR, but I'd like to make a note that this API feels weird. For example, why do we have a custom object prop called aria
instead of relying on the aria-*
attributes that are standardized and already documented by W3C with plenty of references online?
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen, within } from '@testing-library/react'; |
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.
Nice! I didn't know about this within
method. 👍
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.
Neither did I! Required a bit of googling for how to test portals.
Make it clearer that while a title is required, it can be provided be either the `title`, `aria.labelledby` or `contentLabel` props.
Thanks for the reviews @tellthemachines @diegohaz! I've updated the readme to try and make it clear that you really should have a title, and that one of
Yeah I agree. Looks like this API goes pretty far back in the history; perhaps we thought providing Integrating the |
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.
Looks good, thanks!
Description
Fixes a bug where
<Modal>
wasn't forwarding thearia.labelledby
prop to<ModalFrame>
if thetitle
prop was missing. However the purpose oflabelledby
is to allow modals to provide their own title element, in which case they won't want to use thetitle
prop.The
aria.labelledby
prop is now correctly forwarded to<ModalFrame>
if no title is provided. Thetitle
prop will take precedent overaria.labelledby
.How has this been tested?
Added new unit tests that check the modal aria props are set correctly in the DOM.
I tested this in a dev environment by changing the preferences modal to render its own heading itself rather than use the
title
prop.Inspecting in the Firefox a11y tools, the modal's name is still listed as "Preferences".
Dev's will still need to test how their specific modal behaves in a screen reader as the DOM ordering could still impact the user experience. This PR just makes it possible to set the
aria-labelledby
attribute where it wasn't really possible to do so before.Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: