-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor SelectNext into [role=combobox] to improve a11y #1330
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ import { useFocusAway } from '../../hooks/use-focus-away'; | |
import { useKeyPress } from '../../hooks/use-key-press'; | ||
import { useSyncedRef } from '../../hooks/use-synced-ref'; | ||
import type { PresentationalProps } from '../../types'; | ||
import { downcastRef } from '../../util/typing'; | ||
import { MenuCollapseIcon, MenuExpandIcon } from '../icons'; | ||
import Button from './Button'; | ||
import { inputGroupStyles } from './InputGroup'; | ||
import SelectContext from './SelectContext'; | ||
|
||
|
@@ -149,6 +149,9 @@ export type SelectProps<T> = PresentationalProps & { | |
*/ | ||
buttonId?: string; | ||
|
||
'aria-label'?: string; | ||
'aria-labelledby'?: string; | ||
|
||
/** @deprecated Use buttonContent instead */ | ||
label?: ComponentChildren; | ||
}; | ||
|
@@ -163,6 +166,8 @@ function SelectMain<T>({ | |
elementRef, | ||
classes, | ||
buttonId, | ||
'aria-label': ariaLabel, | ||
'aria-labelledby': ariaLabelledBy, | ||
}: SelectProps<T>) { | ||
const [listboxOpen, setListboxOpen] = useState(false); | ||
const closeListbox = useCallback(() => setListboxOpen(false), []); | ||
|
@@ -212,11 +217,11 @@ function SelectMain<T>({ | |
className={classnames('relative w-full border rounded', inputGroupStyles)} | ||
ref={wrapperRef} | ||
> | ||
<Button | ||
<button | ||
id={buttonId ?? defaultButtonId} | ||
variant="custom" | ||
classes={classnames( | ||
'w-full flex justify-between', | ||
className={classnames( | ||
'focus-visible-ring transition-colors whitespace-nowrap', | ||
'w-full flex items-center justify-between gap-x-2 p-2', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These classes were automatically applied by our |
||
'bg-grey-0 disabled:bg-grey-1 disabled:text-grey-6', | ||
// Add inherited rounded corners so that the toggle is consistent with | ||
// the wrapper, which is the element rendering borders. | ||
|
@@ -225,11 +230,15 @@ function SelectMain<T>({ | |
'rounded-[inherit]', | ||
classes, | ||
)} | ||
expanded={listboxOpen} | ||
type="button" | ||
role="combobox" | ||
disabled={disabled} | ||
aria-expanded={listboxOpen} | ||
aria-haspopup="listbox" | ||
aria-controls={listboxId} | ||
elementRef={buttonRef} | ||
aria-label={ariaLabel} | ||
aria-labelledby={ariaLabelledBy} | ||
ref={downcastRef(buttonRef)} | ||
onClick={() => setListboxOpen(prev => !prev)} | ||
onKeyDown={e => { | ||
if (e.key === 'ArrowDown' && !listboxOpen) { | ||
|
@@ -243,7 +252,7 @@ function SelectMain<T>({ | |
<div className="text-grey-6"> | ||
{listboxOpen ? <MenuCollapseIcon /> : <MenuExpandIcon />} | ||
</div> | ||
</Button> | ||
</button> | ||
<SelectContext.Provider value={{ selectValue, value }}> | ||
<ul | ||
className={classnames( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
An issue I noticed when reading the docs, but that I don't think we should try to address in this PR:
The documentation creates some slight confusion here because at the top of the "Component API" section it links to http://localhost:4001/using-components#presentational-components-api, which states that HTML attributes are accepted. For this particular control, those attributes are not available however. I'm not sure that accepting HTML attributes as rest props makes sense because it might be unclear which element they are applied to (the container? the button? the listbox?)
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 think this was a confusion on my part.
SelectNextProps
extendsPresentationalProps
, but that type does not mention anything about HTML attributes.I have noticed though, that all other components extending
PresentationalProps
also extend some form ofJSX.HTMLAttributes<...>
, which is not the case for SelectNext.So I don't know if
PresentationalProps
should actually implicitly extendJSX.HTMLAttributes
and we should have some other type with a different name for what is currentlyPresentationalProps
.