Skip to content

Automatically set an ID on active combobox options#99

Open
henrycatalinismith wants to merge 1 commit intogithub:mainfrom
henrycatalinismith:selected-id
Open

Automatically set an ID on active combobox options#99
henrycatalinismith wants to merge 1 commit intogithub:mainfrom
henrycatalinismith:selected-id

Conversation

@henrycatalinismith
Copy link
Copy Markdown

This is my third and final idea for a solution to #96. It also happens to be my favourite, because it's the approach in https://www.w3.org/TR/wai-aria-1.2/#example-6. In the screen recording below, we can see the ID list-id-selected being automatically applied to each option in turn as it's selected.

selected-id.mp4

It has the same upsides as #97 without the downside of the increased risk of collisions. And since it's a direct copy of the approach in one of the examples in the ARIA spec itself I'm less concerned about the possibility of an issue with JAWS than I was with #97.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements the ARIA spec pattern of dynamically applying a stable id to the currently active combobox option so aria-activedescendant always points to an element with an id, improving resilience when option markup omits ids (Issue #96).

Changes:

  • Auto-assign ${list.id}-selected as the active option’s id when the option lacks one.
  • Remove the ${list.id}-selected id from non-active options as selection moves.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

target.scrollIntoView(this.scrollIntoViewOptions)
} else {
if (el.id === `${this.list.id}-selected`) {
el.removeAttribute('id');
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

el.removeAttribute('id'); introduces a stray semicolon; the rest of this file uses a no-semicolons style. Please drop the semicolon for consistency.

Suggested change
el.removeAttribute('id');
el.removeAttribute('id')

Copilot uses AI. Check for mistakes.
Comment on lines +129 to 132
if (!target.id) {
target.id = `${this.list.id}-selected`
}
this.input.setAttribute('aria-activedescendant', target.id)
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduces new behavior for options without ids (auto-assigning and moving ${list.id}-selected). There are existing tests for aria-activedescendant behavior in test/test.js, but none cover the missing-id path; please add tests to validate id assignment, id movement across options, and cleanup when selection is cleared/stopped.

Copilot uses AI. Check for mistakes.
Comment on lines +131 to +138
}
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
fireSelectEvent(target)
target.scrollIntoView(this.scrollIntoViewOptions)
} else {
if (el.id === `${this.list.id}-selected`) {
el.removeAttribute('id');
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup logic removes the id from any non-target option whose id matches ${this.list.id}-selected. This can inadvertently delete a user-supplied id if their markup already uses that value. Consider marking ids that were auto-assigned (e.g., via a data-* flag or instance state) and only removing the id when it was generated by this library.

Suggested change
}
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
fireSelectEvent(target)
target.scrollIntoView(this.scrollIntoViewOptions)
} else {
if (el.id === `${this.list.id}-selected`) {
el.removeAttribute('id');
target.setAttribute('data-combobox-generated-id', 'true')
}
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
fireSelectEvent(target)
target.scrollIntoView(this.scrollIntoViewOptions)
} else {
if (
el.id === `${this.list.id}-selected` &&
el.getAttribute('data-combobox-generated-id') === 'true'
) {
el.removeAttribute('id')
el.removeAttribute('data-combobox-generated-id')

Copilot uses AI. Check for mistakes.
Comment on lines +129 to 133
if (!target.id) {
target.id = `${this.list.id}-selected`
}
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When selection is cleared via clearSelection() / stop() / destroy(), the auto-assigned ${this.list.id}-selected id is not removed from the last selected option, leaving a stale "selected" id in the DOM. To keep the ARIA example pattern consistent (id only on the active option) and avoid confusing consumers that query for that id, consider removing the generated id as part of clearSelection() (and only if it was generated by this library).

Copilot uses AI. Check for mistakes.
Comment on lines 136 to 140
} else {
if (el.id === `${this.list.id}-selected`) {
el.removeAttribute('id');
}
el.removeAttribute('aria-selected')
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this for loop, it's possible to remove the old ${this.list.id}-selected id (in the else branch) before reaching the new target and updating aria-activedescendant, creating a transient state where aria-activedescendant references a non-existent id. Consider setting the new aria-activedescendant / id first, then clearing the old generated id (e.g., clear-after or two-pass).

Copilot uses AI. Check for mistakes.
@henrycatalinismith
Copy link
Copy Markdown
Author

I did consider handling the kinds of things that the review bot has flagged in the first draft of this, but opted to err on the side of keeping the initial diff small so that it's more approachable for a human reviewer. More than happy to work through the automated feedback if and when we agree on the general approach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants