diff --git a/frontend/src/lib/components/select/MultiSelect.svelte b/frontend/src/lib/components/select/MultiSelect.svelte index 32d60bec3f..7778e15f5c 100644 --- a/frontend/src/lib/components/select/MultiSelect.svelte +++ b/frontend/src/lib/components/select/MultiSelect.svelte @@ -74,6 +74,7 @@ let filterText = $state('') let open = $state(false) let wrapperEl: HTMLDivElement | undefined = $state() + const listId = $props.id() let searchInputEl: TextInput | undefined = $state() let currentValue = $derived(value ?? []) @@ -148,15 +149,21 @@ wrapperEl?.focus() } + // Escape that closes the list must not also close the drawer or popover around the field. + // Melt popovers listen on `document`, where Svelte handles delegated events of portaled + // content too, so only stopping immediate propagation keeps the event from them. + function consumeEscape(e: KeyboardEvent) { + e.preventDefault() + e.stopImmediatePropagation() + } + function onFieldKeydown(e: KeyboardEvent) { if (e.target !== wrapperEl) return if (!open && (e.key === 'Enter' || e.key === ' ' || e.key === 'ArrowDown')) { e.preventDefault() open = true } else if (open && e.key === 'Escape') { - // Consumed here so a drawer around the field stays open. - e.preventDefault() - e.stopPropagation() + consumeEscape(e) open = false } else if (open && e.key === 'Tab') { open = false @@ -165,12 +172,11 @@ function onSearchKeydown(e: KeyboardEvent) { if (e.key === 'Escape') { - e.preventDefault() - e.stopPropagation() + consumeEscape(e) closeAndFocusField() } else if (e.key === 'Tab') { - // The list is portaled to the end of : moving focus back to the field first lets - // Tab continue from the field rather than off the end of the page. + // The list may be portaled to the end of : moving focus back to the field first + // lets Tab continue from the field rather than off the end of the page. closeAndFocusField() } } @@ -182,7 +188,8 @@
{ - open = true - // Pressing the field focused it, out of an already open list's search input. - searchInputEl?.focus() - }} + onpointerup={() => (open = true)} onkeydown={onFieldKeydown} use:clickOutside={{ onClickOutside: () => (open = false) }} {id} @@ -257,6 +260,7 @@ disabled ? 'disabled' : '' )} ulClass="options" + {listId} > {#snippet header()} {#if showSearch} @@ -265,7 +269,9 @@ bind:this={searchInputEl} bind:value={filterText} inputProps={{ - onblur: (e) => open && e.relatedTarget !== wrapperEl && searchInputEl?.focus(), + // Pressing the field or a chip focuses the field; typing must still filter. + // Not once the last option is picked, as the input is then unmounting. + onblur: () => open && showSearch && searchInputEl?.focus(), onkeydown: onSearchKeydown, placeholder: searchPlaceholder }} diff --git a/frontend/src/lib/components/select/SelectDropdown.svelte b/frontend/src/lib/components/select/SelectDropdown.svelte index 8dd952e5bf..a19b5ddd1a 100644 --- a/frontend/src/lib/components/select/SelectDropdown.svelte +++ b/frontend/src/lib/components/select/SelectDropdown.svelte @@ -26,7 +26,8 @@ startSnippet, endSnippet, bottomSnippet, - highlightFirstOnOpen = false + highlightFirstOnOpen = false, + listId }: { processedItems?: ProcessedItem[] value: T | undefined @@ -49,6 +50,8 @@ bottomSnippet?: Snippet<[{ close: () => void }]> /** When true, the first item is highlighted when the dropdown opens (even without filterText) */ highlightFirstOnOpen?: boolean + /** `id` of the options list, for a field's `aria-controls`. */ + listId?: string } = $props() let processedItems = $derived( @@ -106,63 +109,68 @@ {getInputRect} class={className} > - {@render header?.()} - {#if processedItems?.length === 0} -
{noItemsMsg}
- {/if} -
    - {#each processedItems ?? [] as item, itemIndex} - {#if (item.__select_group && itemIndex === 0) || processedItems?.[itemIndex - 1]?.__select_group !== item.__select_group} -
  • - {item.__select_group} -
  • - {/if} -
  • - -
  • - {/each} -
- {@render bottomSnippet?.({ close: () => (open = false) })} + + + {/each} + + {@render bottomSnippet?.({ close: () => (open = false) })} +