Add support for aria-pressed to determine which button is clicked in toggle buttons #760
Labels
needs discussion
Whether changes are needed is still undecided and additional discussion is needed.
Describe the feature you'd like:
TL;DR
In a toggle of buttons, there's no way to determine which button is selected, other than checking CSS attributes (if background color is used as a distinction of which one is selected). If using
aria-pressed
(which should be used on its own to help browser and readers better interpret the semantic html), we could add it as an option ofgetByRole
and detect the button selected.Detailed explanation
My scenario is very simple: I have a toggle which is composed of 3 buttons - the user can only click one of these 3 at the time.
the (simplified) HTML looks like this
there's some logic to determine which one should be clicked by default. There's also some logic for making some of them disabled or enabled. The user notices which elements are selected by color (css)
From RTL POV, There are ways to determine if a button is disabled (there are properties for that).
For determine which element is selected, however, there's no current way to tell which element is selected, unless I query for having a specific css class / styling, which I don't like too much.
I read there's the aria-pressed role that fulfils exactly this functionality: adding it will help screen readers to identify which option is selected by default and which one is selected whenever I click other. That's great by its own, however, it does not help RTL to identify the element.
Quoting from the mdn docs
and about the toggle buttons
Suggested implementation:
Considering
aria-pressed
, the HTML would look like thisThat's ok on its own - it needs to be done. But for RTL, I thought an option for
getByRole
- just likeselected
andchecked
are now. That way, I could use the query like thisaria-selected
can betrue
,false
ormixed
. Not sure how would I represent the mixed. Pressed should be only used for nativebutton
or perhaps elements withrole="button"
Describe alternatives you've considered:
Keep testing by background color.
Teachability, Documentation, Adoption, Migration Strategy:
Not quite sure what to put here 😅
edit: Not sure if this should be here or in DOM testing library. I also had some doubts about opening this ticket due to #16
The text was updated successfully, but these errors were encountered: