Skip to content

Commit a023e4d

Browse files
committed
Prioritize [autofocus] items for focus
1 parent 07fe7d0 commit a023e4d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ function focusOnOpen(details: Element) {
9696
const onmousedown = () => (isMouse = true)
9797
const onkeydown = () => (isMouse = false)
9898
const ontoggle = () => {
99-
autofocus(details)
100-
if (details.hasAttribute('open') && !isMouse) {
101-
focusFirstItem(details)
102-
}
99+
if (!details.hasAttribute('open')) return
100+
if (autofocus(details)) return
101+
if (!isMouse) focusFirstItem(details)
103102
}
104103

105104
details.addEventListener('mousedown', onmousedown)
@@ -128,12 +127,14 @@ function closeCurrentMenu(event: Event) {
128127
}
129128
}
130129

131-
function autofocus(details: Element) {
132-
if (!details.hasAttribute('open')) return
133-
130+
function autofocus(details: Element): boolean {
131+
if (!details.hasAttribute('open')) return false
134132
const input = details.querySelector('[autofocus]')
135133
if (input) {
136134
input.focus()
135+
return true
136+
} else {
137+
return false
137138
}
138139
}
139140

0 commit comments

Comments
 (0)