Add setting for selecting first option by default #57
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.
Users often expect the first item in an autocomplete list to be inserted if they don't select anything and then hit Enter. For example, here's a video of this functionality in the GitHub emoji picker:
Screen.Recording.2022-07-15.at.12.33.17.PM.mov
This can be done with the current functionality by calling
navigate(1)
afterstart()
or by settingaria-selected
on the first item (to avoid setting theactivedescendant
and interrupting screen reader users). However, this has drawbacks. Users (particularly screen reader users) expect the first item to be selected when they press Down, regardless of what the default is (source).A better solution is to style the first item using a different style to indicate that it will be inserted without actually selecting it (the following images are stolen from our a11y docs):
Upon the first down arrow press, the first item will now be selected:
That's what this change adds support for. This new feature is disabled by default and must be enabled by passing the
defaultFirstOption
setting. Then the consumer can style the default item by selecting fordata-combobox-option-default
(I chose to use adata-*
attribute instead of a class for this because I didn't want to mess with the consumer's class list).Note that there is no way to indicate this functionality using ARIA attributes, so it has to be announced explicitly using an
aria-live
message. A demo implementation of this is available from our ownauto-complete-element
. We could build that into theCombobox
class, but I decided not to because we would have to be very opinionated about the message and implementation. Plus it would be difficult to internationalize.