Skip to content

Commit 7c83b16

Browse files
committed
fix: close dropdown after blur delay
Fixes #95
1 parent 06b1f24 commit 7c83b16

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/Typeahead.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,15 @@
212212
}}
213213
on:clear
214214
on:clear={open}
215-
on:blur
215+
on:blur={(e) => {
216+
// Check if focus is moving to an element within the combobox
217+
const relatedTarget = e.relatedTarget;
218+
if (relatedTarget && comboboxRef?.contains(relatedTarget)) {
219+
return;
220+
}
221+
// Close immediately for keyboard navigation (Tab, Shift+Tab)
222+
close();
223+
}}
216224
on:keydown
217225
on:keydown={(e) => {
218226
if (results.length === 0) return;
@@ -253,8 +261,10 @@
253261
class:selected={selectedIndex === index}
254262
class:disabled={result.disabled}
255263
aria-selected={selectedIndex === index}
256-
on:click={() => {
264+
on:mousedown={(e) => {
265+
// mousedown fires before blur, preventing the dropdown from closing
257266
if (result.disabled) return;
267+
e.preventDefault(); // Prevent input from losing focus
258268
selectedIndex = index;
259269
select();
260270
}}

0 commit comments

Comments
 (0)