Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class Combobox {
tabInsertsSuggestions: boolean
firstOptionSelectionMode: FirstOptionSelectionMode
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
didAutoAssignLastSelectedId: boolean

constructor(
input: HTMLTextAreaElement | HTMLInputElement,
Expand All @@ -44,6 +45,7 @@ export default class Combobox {
this.scrollIntoViewOptions = scrollIntoViewOptions ?? {block: 'nearest', inline: 'nearest'}

this.isComposing = false
this.didAutoAssignLastSelectedId = false

if (!list.id) {
list.id = `combobox-${Math.random().toString().slice(2, 6)}`
Expand Down Expand Up @@ -126,11 +128,19 @@ export default class Combobox {
el.removeAttribute('data-combobox-option-default')

if (target === el) {
if (!target.id) {
target.id = `${this.list.id}-selected`
this.didAutoAssignLastSelectedId = true
}
this.input.setAttribute('aria-activedescendant', target.id)
Comment on lines +131 to 135
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.
target.setAttribute('aria-selected', 'true')
Comment on lines +131 to 136
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this suggestion makes sense!

fireSelectEvent(target)
target.scrollIntoView(this.scrollIntoViewOptions)
} else {
if (el.id === `${this.list.id}-selected` && this.didAutoAssignLastSelectedId) {
el.removeAttribute('id')
this.didAutoAssignLastSelectedId = false
}
el.removeAttribute('aria-selected')
Comment thread
francinelucca marked this conversation as resolved.
}
}
Expand All @@ -141,6 +151,10 @@ export default class Combobox {
for (const el of this.list.querySelectorAll('[aria-selected="true"], [data-combobox-option-default="true"]')) {
el.removeAttribute('aria-selected')
el.removeAttribute('data-combobox-option-default')
if (el.id === `${this.list.id}-selected` && this.didAutoAssignLastSelectedId) {
el.removeAttribute('id')
this.didAutoAssignLastSelectedId = false
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,55 @@ describe('combobox-nav', function () {
})
})
})

describe('with missing IDs on options', function () {
let input
let list
beforeEach(function () {
document.body.innerHTML = `
<input type="text">
<ul role="listbox" id="list-id">
<li role="option">Baymax</li>
<li id="hubot" role="option">Hubot</li>
<li role="option">R2-D2</li>
</ul>
`
input = document.querySelector('input')
list = document.querySelector('ul')
})

afterEach(function () {
document.body.innerHTML = ''
})

it('automatically adds and removes option IDs when needed for aria-activedescendant', function () {
const combobox = new Combobox(input, list)
combobox.start()
assert.equal(input.getAttribute('aria-expanded'), 'true')

press(input, 'ArrowDown')
Comment thread
henrycatalinismith marked this conversation as resolved.
assert.equal(input.getAttribute('aria-activedescendant'), 'list-id-selected')
assert.equal(list.children[0].getAttribute('id'), 'list-id-selected')
assert.equal(list.children[1].getAttribute('id'), 'hubot')
assert.equal(list.children[2].getAttribute('id'), undefined)

press(input, 'ArrowDown')
assert.equal(input.getAttribute('aria-activedescendant'), 'hubot')
assert.equal(list.children[0].getAttribute('id'), undefined)
assert.equal(list.children[1].getAttribute('id'), 'hubot')
assert.equal(list.children[2].getAttribute('id'), undefined)

press(input, 'ArrowDown')
assert.equal(input.getAttribute('aria-activedescendant'), 'list-id-selected')
assert.equal(list.children[0].getAttribute('id'), undefined)
assert.equal(list.children[1].getAttribute('id'), 'hubot')
assert.equal(list.children[2].getAttribute('id'), 'list-id-selected')

press(input, 'Escape')
assert.equal(input.getAttribute('aria-activedescendant'), undefined)
assert.equal(list.children[0].getAttribute('id'), undefined)
assert.equal(list.children[1].getAttribute('id'), 'hubot')
assert.equal(list.children[2].getAttribute('id'), undefined)
})
})
})
Loading