Keep selected item in view

This commit is contained in:
Andy Smith
2025-11-10 14:42:42 -06:00
parent 0b35e26fd6
commit e2ac6c1d7e
2 changed files with 16 additions and 6 deletions
@@ -80,6 +80,14 @@ export default class extends Controller {
selectedItem.setAttribute(this.selectionAttributeValue, "true")
this.currentItem = selectedItem
await nextFrame()
// Ensure the selected item is visible inside any scrollable container.
// Use 'nearest' so we avoid jumping the viewport unnecessarily.
try {
this.currentItem.scrollIntoView({ block: "nearest", inline: "nearest" })
} catch (e) {
// scrollIntoView may not be supported in older environments; ignore.
}
if (this.focusOnSelectionValue) { this.currentItem.focus() }
if (this.hasInputTarget && id) {
this.inputTarget.setAttribute("aria-activedescendant", id)