Skip to content

Commit

Permalink
fix(ld-select): wait for popper to be visible before focusing filter
Browse files Browse the repository at this point in the history
Background: It looks like the Tether enable method returns before the actual popper element is visible. So we need to "wait" for it to become visible before setting the focus on the filter input element.

Fixes #813
  • Loading branch information
borisdiakur committed Jul 11, 2023
1 parent 845a1a5 commit ce8419d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/liquid/components/ld-select/ld-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,16 @@ export class LdSelect implements InnerFocusable {
}
}

private focusFilterAsSoonAsVisible = (filterInput: HTMLInputElement) => {
setTimeout(() => {
if (this.listboxRef.style.display === 'none') {
this.focusFilterAsSoonAsVisible(filterInput)
} else {
filterInput.focus()
}
})
}

private handleTriggerClick = (ev: Event) => {
ev.preventDefault()

Expand All @@ -960,9 +970,13 @@ export class LdSelect implements InnerFocusable {

this.togglePopper()

setTimeout(() => {
this.getFilterInput()?.focus()
})
// At this point the popper element may still have display none
// (which happens quite rarely - but it does happen!), and we need
// to "wait" for it to be visible before setting focus.
const filterInput = this.getFilterInput()
if (filterInput) {
this.focusFilterAsSoonAsVisible(filterInput)
}
}

private handleClearClick = (ev: MouseEvent) => {
Expand Down

0 comments on commit ce8419d

Please sign in to comment.