Skip to content

Commit

Permalink
Focus selected item when select menu is open
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Feb 5, 2024
1 parent 3a3c960 commit 1291f55
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions frontend/src/lib/components/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { mdiMenuDown } from '@mdi/js';
import { popup, type PopupSettings, ListBox, ListBoxItem, focusTrap } from '@skeletonlabs/skeleton';
import type { SizeOptions } from '@floating-ui/dom';
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher, tick } from 'svelte';
import SvgIcon from '$lib/components/SVGIcon.svelte';
Expand Down Expand Up @@ -49,6 +49,19 @@
closeQuery: `.${name}-listbox-item`,
} as PopupSettings;
let selectMenu: HTMLDivElement;
$: if (comboboxOpen) {
const selectedItem = selectMenu.querySelector('[aria-selected=true].listbox-item') as HTMLDivElement | null;
const firstItem = selectMenu.querySelector('.listbox-item') as HTMLDivElement | null;
tick().then(() => {
if (selectedItem) {
selectedItem.focus();
} else {
firstItem?.focus();
}
});
}
// eslint-disable-next-line no-undef
const dispatch = createEventDispatcher<{change: T}>();
Expand Down Expand Up @@ -82,7 +95,11 @@
</button>
</div>

<div class="card w-full shadow-xl z-10 duration-0 overflow-y-auto !mt-0 {menuClass}" data-popup={name} use:focusTrap={comboboxOpen}>
<div
bind:this={selectMenu}
class="card w-full shadow-xl z-10 duration-0 overflow-y-auto !mt-0 {menuClass}"
data-popup={name}
use:focusTrap={comboboxOpen}>
<!--
Skeleton's popup close function waits for the tranistion duration...
before actually triggering the transition...
Expand Down

0 comments on commit 1291f55

Please sign in to comment.